Skip to content

isInstanceOf is statically optimized away based on compiler knowledge about types #10379

@valenterry

Description

@valenterry

The isInstaceOf check are optimized away in the following situation:

    class A
    trait T
    
    val casted: A with T = (new A).asInstanceOf[A with T]
    casted.isInstanceOf[A with T] // = true

However:

    casted.isInstanceOf[A] // = true
    casted.isInstanceOf[T] // = false

(fiddle: https://scalafiddle.io/sf/rjfApD5/0)

The check is optimized away and replaced with true. I am not sure if that is a bug or maybe even desired (optimization) behaviour. However, when using isInstanceOf I would intuitively have expected a runtime-check no matter what the compiler statically knows.

In any case I would expect the test for T and for A with T to both give the same result, be it true or false.

I could reproduce this in both scala 2.11.8 and 2.12.1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions