-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Unsoundness in GADT casting when using a variant type constructor #14983
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
Labels
area:gadt
itype:soundness
Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)
Milestone
Comments
Further minimization: sealed trait Tree[+T]
case class Node[+T](innerTree: List[Tree[List[T]]]) extends Tree[List[T]]
case class Leaf[+T](v: T) extends Tree[T]
def recurse[Ti](tree: Tree[Ti]): Ti = tree match
case Node(innerTree) =>
List(List(List(innerTree.map(recurse)))) // We should have got compilation error here...
case Leaf(v) => v
@main def run: Unit =
val res6: List[Int] = recurse(Node(List(Leaf(List(1)))))
val res7: Int = res6.head // ... instead of runtime error here |
This issue was picked for the Issue Spree number 17 of June 14th which takes place in a week from now. @dwijnand @gagandeepkalra will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here. |
Further minimised: sealed trait Tree[+A]
final case class Node[+B](x: Tuple1[B]) extends Tree[Tuple1[B]]
def meth[X](tree: Tree[X]): X = tree match
case Node(x) =>
Tuple1("boom") // error
object Test:
def main(args: Array[String]): Unit = assert(meth(Node(Tuple1(42)))._1 == 42) |
This was
linked to
pull requests
Jun 23, 2022
Ti = List[Ti]
leading to runtime class cast exceptions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area:gadt
itype:soundness
Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)
Compiler version
Seems to affect at least 3.1.2, 3.0.2, 2.13.8, and 2.12.15
Minimized code
convenient scastie: https://scastie.scala-lang.org/w1GEk4z8TxO9oF9lGgHzoQ
Output
java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to java.lang.Integer
Expectation
expected a compiler erorr
List[Ti]
is notTi
.The text was updated successfully, but these errors were encountered: