Skip to content

Unsoundness in overriding methods with higher-order type parameters #2066

@scabug

Description

@scabug

Try to compile the following code with Scala 2.7.5 final:

trait A {
 def f[T[_]](x : T[Int]) : T[Any]
}

class B extends A {
 def f[T[+_]](x : T[Int]) : T[Any] = x
}

class P[Y](var y : Y)

It is compiled without any errors. But then you can write:

val p = new P(1)
val palias = (new B():A).f[P](p)
palias.y = "hello"

Result:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

So, this way of overriding is unsound.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions