Skip to content

Pattern match over a parametrised ADT with type-constrained children no longer compiles #12226

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

Closed
adamw opened this issue Apr 26, 2021 · 5 comments · Fixed by #12258
Closed
Assignees
Milestone

Comments

@adamw
Copy link
Contributor

adamw commented Apr 26, 2021

Compiler version

3.0.0-RC3

Minimized code

object Test extends App {
  sealed trait P[T]
  case class C1[T <: String](c1: T) extends P[T]
  case class C2[T](c2: T) extends P[T]

  def test[T](p: P[T]): Unit = p match {
    case C1(_) => println(1)
    case C2(_) => println(2)
  }

  test(C1("x"))
  test(C2(10))
}

Output

[error] -- [E057] Type Mismatch Error: Test.scala:9:9
[error] 9 |    case C1(_) => println(1)
[error]   |         ^
[error]   |         Type argument T does not conform to upper bound String

Expectation

Used to compile just fine with 2.11/2.12/2.13.

@smarter
Copy link
Member

smarter commented Apr 26, 2021

@abgruszecki Is there a good reason for emitting an error here?

@abgruszecki
Copy link
Contributor

No, I don't see a reason, much less a good one. I think this a problem with inferring type arguments to .unapply methods.

@adamw
Copy link
Contributor Author

adamw commented Apr 26, 2021

It seems that a work-around is:

  def test[T](p: P[T]): Unit = p match {
    case c1: C1[T] => println(s"1 ${c1.c1}")
    case c2: C2[T] => println(s"2 ${c2.c2}")
  }

which only emits a non-exhaustivity warning using 3-RC3 (no warnings or errors on 2.11/2.12/2.13), but at least compiles on all Scala versions :)

[warn] 8 |  def test[T](p: P[T]): Unit = p match {
[warn]   |                               ^
[warn]   |                               match may not be exhaustive.
[warn]   |
[warn]   |                               It would fail on pattern case: C1(_)

@abgruszecki
Copy link
Contributor

Great, thanks for letting us know there's a workaround @adamw!

@abgruszecki
Copy link
Contributor

BTW this doesn't seem to be about type arguments to .unapply, I thought that P was covariant but it is in fact invariant. I'm looking into it right now, but it seems that the problem here is instead that the context no longer has GADT constraints when it checks that .unapply has correct type arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants