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" due to compiler doesn't generate error "Covariant type T occurs in contravariant position in type T of value element" for private members" #18676

Closed
unkarjedy opened this issue Oct 11, 2023 · 2 comments
Labels
area:refchecks area:typer itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)

Comments

@unkarjedy
Copy link
Contributor

Compiler version

Scala 3.3.1

Minimized code

abstract class Animal { def name: String }
case class Dog(name: String) extends Animal
case class Cat(name: String) extends Animal

class Wrapper[+T](private var element: T)

object Wrapper {
  def main(args: Array[String]): Unit = {
    val dog: Wrapper[Dog] = new Wrapper(Dog("Rex"))
    val animal: Wrapper[Animal] = dog
    animal.element = Cat("Kitty")
    println(dog.element.name)
  }
}

Output

The code compiles, but when you run the program it crashes with error

Exception in thread "main" java.lang.ClassCastException: class Cat cannot be cast to class Dog (Cat and Dog are in unnamed module of loader 'app')
	at Wrapper$.main(Animal.scala:12)
	at Wrapper.main(Animal.scala)

Expectation

The code compiles in Scala 3 but didn't compile in 2.13.
It should produce error
Covariant type T occurs in contravariant position in type T of value element

The compiler shouldn't produce the error only when the visibility is private[this].
It looks like there is a typo somewhere in the compiler which checks for private, not private[this]


Related issue in The Scala Plugin, which actually shows the error:
https://youtrack.jetbrains.com/issue/SCL-21676

image
@unkarjedy unkarjedy added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 11, 2023
@sjrd sjrd added area:typer area:refchecks itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) labels Oct 11, 2023
@sjrd
Copy link
Member

sjrd commented Oct 11, 2023

Uh oh. That's a soundness issue. Thanks for the report.

What's worrying about it is that it casts doubt on the idea that private[this] can be inferred as an internal optimization of private. This issue highlights the fact that it holds dedicated semantics.

@bishabosha bishabosha removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Oct 13, 2023
@sjrd
Copy link
Member

sjrd commented Nov 3, 2023

This was fixed in #18693

@sjrd sjrd closed this as completed Nov 3, 2023
@sjrd sjrd removed their assignment Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:refchecks area:typer itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)
Projects
None yet
Development

No branches or pull requests

3 participants