Skip to content

'case' with 'given' error has precedence over unreachable case #22191

@sjbiaga

Description

@sjbiaga

I've run into this weird behavior by chance, between "old" code I had and its refactoring.

Compiler version

3.6.2-RC3

Minimized example

First, the "old" code was pattern matching an Option[(List[Int], Map[Int, Double])], but I dropped the Option, leaving just the (pair) tuple. Yet, Some in pattern matching remained:

object X:
  def m(using List[Int])(using Map[Int, Double]) = {}
  def apply(using t2: (List[Int], Map[Int, Double])) =
  t2 match
    case Some((given List[Int], given Map[Int, Double])) => m
    case _ =>

So I got this:

Output Error message

5 |      case Some((given List[Int], given Map[Int, Double])) => m
  |                                                               ^
  |No given instance of type List[Int] was found for parameter x$1 of method m in object X
  |
  |Note: given instance given_List_Int was not considered because it was not imported with `import given`.
1 error found

Why this Error was not helpful

Second, if I now removed the call to method m...

object X:
  def m(using List[Int])(using Map[Int, Double]) = {}
  def apply(using t2: (List[Int], Map[Int, Double])) =
  t2 match
    case Some((given List[Int], given Map[Int, Double])) =>
    case _ =>

.. the "correct" error is now:

-- [E030] Match case Unreachable Warning: --------------------------------------
5 |      case Some((given List[Int], given Map[Int, Double])) =>
  |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |           Unreachable case
-- Error: ----------------------------------------------------------------------
5 |      case Some((given List[Int], given Map[Int, Double])) =>
  |           ^
  |this case is unreachable since type (List[Int], Map[Int, Double]) is not a subclass of class Some
1 warning found
1 error found

The first message was misleading because it actually discovered the Some-wrapped given List[Int], whereas by the second message, the case was unreachable.

I know that Scala compiler reports from "right to left", so I'm not exactly sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions