Skip to content

ClassCastException during pattern match on inner classes defined in base traits #3548

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

Closed
grindvoll opened this issue Nov 24, 2017 · 1 comment

Comments

@grindvoll
Copy link

grindvoll commented Nov 24, 2017

The following code crashes with a ClassCastException:

    trait Common {
      case class Data(a: String)
    }
    object O1 extends Common
    object O2 extends Common

    val data = O2.Data("test")

    // Runtime error: java.lang.ClassCastException: O2$ cannot be cast to O1$
    data match {
        case O1.Data(s) => println("O1 data")
        case O2.Data(s) => println("O2 data")
        case _ => println("Unknown")
    }

(Observed on 0.4.0-RC1)

@allanrenucci
Copy link
Contributor

After pattern matcher

Dotty:

 if (x1.$isInstanceOf$[Common.this.Data].&&(x1.asInstanceOf[Common.this.Data].1_<outer>.eq(Common.this))

Scalac:

if (x1.isInstanceOf[O1.Data].&&((x1.asInstanceOf[O1.Data]: O1.Data).<outer>.eq(O1)))

Bytecode

Here Dotty emits an invalid cast that throws at runtime:

if (data2 instanceof Common.Data && (O1$)data2.Common$Data$$$outer() == O1$.MODULE$)

Scalac:

if (data2 instanceof Common.Data && data2.Common$Data$$$outer() == O1$.MODULE$)

@liufengyun liufengyun self-assigned this Nov 24, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Nov 24, 2017
…pected type

Otherwise, ElimOuterSelect will insert `ensureConforms(tp)` in outer test,
which causes run-time exeception.
liufengyun added a commit to dotty-staging/dotty that referenced this issue Dec 19, 2017
…pected type

Otherwise, ElimOuterSelect will insert `ensureConforms(tp)` in outer test,
which causes run-time exeception.
odersky added a commit that referenced this issue Dec 23, 2017
Fix #3548: set outer select type to be the type enclosing the expected type
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

3 participants