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

Missing unchecked warning when pattern matching on a type parameter #1828

Closed
jedesah opened this issue Dec 19, 2016 · 6 comments
Closed

Missing unchecked warning when pattern matching on a type parameter #1828

jedesah opened this issue Dec 19, 2016 · 6 comments

Comments

@jedesah
Copy link
Member

jedesah commented Dec 19, 2016

def remove[S](a: S | Int, f: Int => S):S = a match {
  case a: S => a
  case a: Int => f(a)
}

val t: Int | String = 5
val t1 = remove[String](t, _.toString)

This compiles fine, but at runtime, the call to remove will throw a java.lang.ClassCastException (Integer cannot be cast to String)

@jedesah
Copy link
Member Author

jedesah commented Dec 19, 2016

It seems it has something to do with the use of a generic type because if I were to hardcode remove to String as follows:

def remove(a: String | Int):String = a match {
  case a: String => a
  case a: Int => a.toString
}

val t: Int | String = 5
val t1 = remove(t)

then no ClassCastException

@jedesah
Copy link
Member Author

jedesah commented Dec 19, 2016

I may be able to have a look at this myself if someone can give a few pointers to where the problem might lie.

@senia-psm
Copy link
Contributor

@jedesah in scala 2.12.x you'll get a warning:

scala> def unsafeCast[T](a: Any) = a match { case x: T => x; case _ => ??? }
<console>:11: warning: abstract type pattern T is unchecked since it is eliminated by erasure
       def unsafeCast[T](a: Any) = a match { case x: T => x; case _ => ??? }
                                                     ^
unsafeCast: [T](a: Any)T

scala> unsafeCast[String](1)
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

There is actually no typecheck here: case a: S => a.

@smarter
Copy link
Member

smarter commented Dec 19, 2016

Yes, pattern-matching on type parameters is unsafe and we're missing the warning, thanks for pointing that out

@smarter smarter changed the title Union type creates a ClassCastException at runtime Missing unchecked warning when pattern matching on a type parameter Dec 19, 2016
felixmulder added a commit to dotty-staging/dotty that referenced this issue Dec 19, 2016
@felixmulder felixmulder self-assigned this Dec 19, 2016
felixmulder added a commit to dotty-staging/dotty that referenced this issue Dec 19, 2016
@jedesah
Copy link
Member Author

jedesah commented Dec 19, 2016

@smarter Haha! Glad I could help!

@liufengyun
Copy link
Contributor

This is the same issue as #3324, will be fixed together.

liufengyun added a commit to dotty-staging/dotty that referenced this issue Feb 28, 2018
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 1, 2018
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 2, 2018
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 6, 2018
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 9, 2018
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 23, 2018
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 26, 2018
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants