Skip to content

Unsoundness due to missing variance checks of private members #7567

Closed
@joroKr21

Description

@joroKr21

minimized code

class A
class B extends A
class C extends A

object Foo {
  private[this] class Bar[+T](var x: T)
  def foo: B = {
    val barB: Bar[B] = new Bar(new B)
    val barA: Bar[A] = barB
    barA.x = new C
    barB.x
  }
}

Foo.foo

result

java.lang.ClassCastException

expectation

Variance error is reported.

variations

  • private is also broken in Dotty
  • private[this] is also broken in Scalac
  • private is detected in Scalac

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions