Skip to content

Variance checking ignores structural types #18035

Closed
@s5bug

Description

@s5bug

Compiler version

3.2.2, 3.3.0

Minimized code

import reflect.Selectable.reflectiveSelectable

class A[+Cov](f: Cov => Unit) {
  def foo: { def apply(c: Cov): Unit } =
    f
}

val aForString = new A[String](_.length)
// => val aForString: A[String]

val aForStringIsAForAny: A[Any] = aForString
// => val aForStringIsAForAny: A[Any]

aForStringIsAForAny.foo(123)
// => java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')

Expectation

An error should be thrown at compile time at the definition of foo as it places Cov in a contravariant position in the structural type

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions