Skip to content

Unexpected pattern match compile error for generic case class #11132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
grindvoll opened this issue Sep 4, 2018 · 1 comment
Open

Unexpected pattern match compile error for generic case class #11132

grindvoll opened this issue Sep 4, 2018 · 1 comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) patmat should compile typer
Milestone

Comments

@grindvoll
Copy link

grindvoll commented Sep 4, 2018

The following code fails to compile for scala 2.12.6 and 2.13.0-M5:

trait Is[A]
case object IsInt extends Is[Int]
case object IsString extends Is[String]
case class C[A](is: Is[A], value: A)

val c: Any = C(IsString, "name")  // Note: val c: C[_] works!
c match {
  case C(IsInt, i) if i < 10 => println(s"An Int less than 10")
  case C(IsString, s) => println(s"A String with length ${s.length}")
  case _ => println("No match")
}

The compiler reports:

[error] C:\dev\scala\213test\src\main\scala\Hello.scala:11:12: pattern type is incompatible with expected type;
[error]  found   : IsInt.type
[error]  required: Is[Any]
[error] Note: Int <: Any (and IsInt.type <: Is[Int]), but trait Is is invariant in type A.
[error] You may wish to define A as +A instead. (SLS 4.5)
[error]     case C(IsInt, i) if i < 10 => println(s"An Int less than 10")
[error]            ^
[error] C:\dev\scala\213test\src\main\scala\Hello.scala:11:27: value < is not a member of Any
[error]     case C(IsInt, i) if i < 10 => println(s"An Int less than 10")
[error]                           ^
[error] C:\dev\scala\213test\src\main\scala\Hello.scala:12:12: pattern type is incompatible with expected type;
[error]  found   : IsString.type
[error]  required: Is[Any]
[error] Note: String <: Any (and IsString.type <: Is[String]), but trait Is is invariant in type A.
[error] You may wish to define A as +A instead. (SLS 4.5)
[error]     case C(IsString, s) => println(s"A String with length ${s.length}")
[error]            ^
[error] C:\dev\scala\213test\src\main\scala\Hello.scala:12:63: value length is not a member of Any
[error]     case C(IsString, s) => println(s"A String with length ${s.length}")
[error]                                                               ^
[error] four errors found

The code compiles by refining the type c from val c: Any to val c: C[_].

(In Dotty, the result is exactly the opposite. Matching on c: Any works, while matching on c: C[_] fails.)

@SethTisue
Copy link
Member

In Dotty, the result is exactly the opposite. Matching on c: Any works, while matching on c: C[_] fails

3.2.1-RC1-bin-20220823-3ad97df-NIGHTLY accepts the c: C[_] version.

@SethTisue SethTisue added this to the Backlog milestone Aug 25, 2022
@SethTisue SethTisue added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) patmat should compile typer
Projects
None yet
Development

No branches or pull requests

4 participants