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

java.lang.ClassCastException on a trivial match #15662

Closed
strelec opened this issue Jul 13, 2022 · 1 comment · Fixed by #15669
Closed

java.lang.ClassCastException on a trivial match #15662

strelec opened this issue Jul 13, 2022 · 1 comment · Fixed by #15669
Assignees
Labels
area:pattern-matching itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)
Milestone

Comments

@strelec
Copy link

strelec commented Jul 13, 2022

Compiler version

3.2.0-RC1
On 3.1.3 it is a compiler error instead (it should not be)

Minimized code

case class Composite[T](v: T)

def m(composite: Composite[_]): Unit =
  composite match {
    case Composite[Int](v) => println(v)
    case _ => println("OTHER")
  }

m(Composite("This is String"))

Output

java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer

Expectation

Print "OTHER"

@strelec strelec added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 13, 2022
@som-snytt
Copy link
Contributor

som-snytt commented Jul 13, 2022

#15356 explains the revised translation.

When written as an extractor, it correctly warns, so "constructor" pattern style should also warn:

-- Unchecked Warning: -------------------------------------------------------------------------------------------------------
3 |    case X[Int](v) => println(v)
  |         ^
  |         the type test for C[Int] cannot be checked at runtime

Edit: actually, I don't understand the new meaning, where the X is a non-case class with an unapply (Baz in the test).

@mbovel mbovel added area:pattern-matching and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 13, 2022
@KacperFKorban KacperFKorban self-assigned this Jul 13, 2022
@dwijnand dwijnand added the itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) label Jul 13, 2022
@Kordyjan Kordyjan added this to the 3.2.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:pattern-matching itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants