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

Missing variance check on private members #18588

Closed
alexandru opened this issue Sep 24, 2023 · 2 comments
Closed

Missing variance check on private members #18588

alexandru opened this issue Sep 24, 2023 · 2 comments
Labels
area:typer itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)

Comments

@alexandru
Copy link

alexandru commented Sep 24, 2023

Compiler version

From 3.0.0 to 3.3.1.

Minimized code

This code compiles fine, but fails at runtime with a ClassCastException. Note that it contains no @uncheckedVariance annotations or forced down-casting:

class Box[+A](value: A) {
  private var cached: A = value
  def get: A = cached

  def put[AA >: A](value: AA): Unit = {
    val box: Box[AA] = this
    box.cached = value
  }
}

trait Animal
object Dog extends Animal
object Cat extends Animal

val dogBox: Box[Dog.type] = new Box(Dog)
dogBox.put(Cat)
val dog: Dog.type = dogBox.get

Note: I actually noticed this in the lampepfl/gears repository (see sample).

Expectation

The compiler should issue an error like:

Covariant type A occurs in contravariant position in type A of value cached
@alexandru alexandru added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 24, 2023
@SethTisue
Copy link
Member

SethTisue commented Sep 24, 2023

🙀

seems similar to #7567 — maybe the fix for that one just needs adjustment? @bishabosha

@SethTisue SethTisue added the itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) label Sep 24, 2023
@Kordyjan Kordyjan added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 25, 2023
@dwijnand
Copy link
Member

Fixed in #18693

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

4 participants