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 is unsound in the presence of type unions #5826

Closed
LPTK opened this issue Jan 31, 2019 · 1 comment

Comments

@LPTK
Copy link
Contributor

commented Jan 31, 2019

scala> def test[A]: List[Int] | A => Int = { case ls: List[Int] => ls.head  case _ => 0 }
def test[A] => List[Int] | A => Int

scala> test(List("oops"))
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
	at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:103)

The definition of test should at least yield a warning like "erased type argument Int in type pattern List[Int] is unchecked".

@LPTK

This comment has been minimized.

Copy link
Contributor Author

commented Jan 31, 2019

Note that even the following passes the type checker without a warning (at least in the REPL):

scala> def test: List[Int]|List[String] => Int = { case ls: List[Int] => ls.head  case _ => 0 }
def test List[Int] | List[String] => Int

scala> test(List("oops"))
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
	at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:103)

@liufengyun liufengyun self-assigned this Feb 6, 2019

liufengyun added a commit to dotty-staging/dotty that referenced this issue Feb 6, 2019

Blaisorblade added a commit that referenced this issue Feb 7, 2019

Merge pull request #5858 from dotty-staging/fix-5826
Fix #5826: specialize union types to avoid constraint cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.