Skip to content
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

Pattern matching and case classes with type parameters #5900

Open
scabug opened this issue Jun 12, 2012 · 18 comments
Open

Pattern matching and case classes with type parameters #5900

scabug opened this issue Jun 12, 2012 · 18 comments
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) gadt infer patmat typelevel typer
Milestone

Comments

@scabug
Copy link

scabug commented Jun 12, 2012

trait C[T]
case class D[T](c: C[T])

object IntC extends C[Int]

object Test {
 (null: AnyRef) match { case D(IntC) => }
}

fails to compile with

Test.scala:10: error: pattern type is incompatible with expected type;
 found   : IntC.type
 required: C[Any]
Note: Int <: Any (and IntC.type <: C[Int]), but trait C is invariant in type T.
You may wish to define T as +T instead. (SLS 4.5)
  (null: AnyRef) match { case D(IntC) => }
                                ^
one error found

The error goes away using a hand-crafted unapply method:

object E {
  def unapply[T](x: D[T]): Option[C[T]] = None
}

// ...

(null: AnyRef) match { case E(IntC) => }
@scabug
Copy link
Author

scabug commented Jun 12, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5900?orig=1
Reporter: @jrudolph

@scabug
Copy link
Author

scabug commented Jun 13, 2012

@adriaanm said (edited on Jun 13, 2012 8:53:00 AM UTC):
as Johannes says, this is due to adapt's (5.1)

(5) Convert constructors in a pattern as follows: 
(5.1) If constructor refers to a case class factory, set tree's type to the unique 
      instance of its primary constructor that is a subtype of  the expected type. 
(5.2) If constructor refers to an extractor, convert to application of 
      unapply or unapplySeq method. 

@scabug
Copy link
Author

scabug commented Jun 13, 2012

@scabug
Copy link
Author

scabug commented Jun 13, 2012

@adriaanm said:
note to self:

The unapply and case class scenarios should behave in the same way.
Technically, I think we should retract "failed" inference results (such as Any) using adjustTypeArgs,
and replace them by skolems, as we do for unapply's.

@scabug
Copy link
Author

scabug commented Nov 22, 2013

@adriaanm said:
another workaround is to add a type ascription to the scrutinee (??? : C[_] ) match ...

@scabug
Copy link
Author

scabug commented Nov 22, 2013

@adriaanm said (edited on Nov 22, 2013 10:14:00 PM UTC):
a variation:

  scala> case class Transition[S](x: S) ;  object C ; (??? : Any)   match  { case Transition(C) => }
    <console>:11: error: pattern type is incompatible with expected type;
     found   : C.type
     required: S
            (??? : Any) match  { case Transition(C) => }
                                                 ^

@scabug
Copy link
Author

scabug commented Nov 22, 2013

@adriaanm said:
Jason bisected it:

scala-hash is missing a build or two around the critical area, but is in:
*   acd7780 Merge pull request #3014 from ceedubs/pr/implicitNotFound-scaladoc
|\
| * 9835d33 Describe type parameter interpolation in @implicitNotFound documentation
* |   90a3126 Merge pull request #3005 from paulp/pr/7886
|\ \
| * | 5708e9d SI-6680 unsoundness in gadt typing.
| * | 95d5554 SI-7886 unsoundness in pattern matcher.

@scabug
Copy link
Author

scabug commented Nov 23, 2013

@retronym said:
Here's my take on it (the variation in Adriaan's comment which is a regression).

https://github.com/retronym/scala/compare/ticket/5900?expand=1

@scabug
Copy link
Author

scabug commented Nov 26, 2013

@retronym said:
I haven't really figured out the right way to fix this one. My branch works, but I feel like its not the most direct fix.

@adriaan: once you've delivered M7 to the world, lets put our heads together and seek out something better.

@scabug
Copy link
Author

scabug commented Dec 1, 2013

@retronym said:
My notes on the difference in typechecking / soundness between case classes and extractors: https://gist.github.com/retronym/7704153

@scabug
Copy link
Author

scabug commented Feb 10, 2014

@adriaanm said:
Sorry, I don't know how to fix this in time for 2.11

@scabug
Copy link
Author

scabug commented Feb 10, 2014

@retronym said:
I think we can revert just enough of Paul's soundness fix to avoid the regression.

I'd rather not ship a change to pattern inference only to change it again when we fix this for real. I'm marking as 2.11.0 again and will followup tomorrow with a patch.

@scabug
Copy link
Author

scabug commented Feb 10, 2014

@adriaanm said:
Great!

@scabug
Copy link
Author

scabug commented Feb 11, 2014

@retronym said (edited on Feb 11, 2014 10:35:32 PM UTC):
Still working on this one, will try again for the elusive tomorrow.

@scabug
Copy link
Author

scabug commented Feb 12, 2014

@retronym said:
The regression from 2.10 is addressed in scala/scala#3514 by reverting part of #7886.

@scabug
Copy link
Author

scabug commented Feb 13, 2014

@adriaanm said:
rebased/tests fixed at: scala/scala#3523

@scabug
Copy link
Author

scabug commented Feb 15, 2014

@adriaanm said:
Regression fixed but this ticket is about the real fix, which can't happen in 2.11.x

@scabug
Copy link
Author

scabug commented May 13, 2016

@milessabin said:
It'd be great to get this updated for 2.12.x and merged.

@SethTisue SethTisue added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Oct 26, 2023
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/) gadt infer patmat typelevel typer
Projects
None yet
Development

No branches or pull requests

4 participants