Skip to content

Missing variance checks in rhs of type aliases leads to unsoudness #1252

Closed
@smarter

Description

@smarter

This shouldn't compile because X appears covariantly in the rhs of type Id[-X] = X:

abstract class A {
  type Id[-X]
  def a: Id[Any]
  def b: Id[String] = a
}
class B extends A {
  type Id[-X] = X // error in scalac, compiles in dotty
  override def a = 1
}
object Test {
  def main(args: Array[String]): Unit = {
    val b = new B
    println(b.a)
    println(b.b)
    val x: String = b.b // ClassCastException at runtime
  }
}

scalac doesn't allow type Id[-X] = X and type Id[-X] <: X but allows type Id[-X] >: X, I haven't thought enough about it to know if that makes sense.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions