Skip to content

An opaque type should not be covariant when the underlying type is invariant #13997

Closed
@Atry

Description

@Atry

Compiler version

3.1.0

Minimized code

opaque type CovariantArray[+A] = Array[A]

object CovariantArray:
  def crash() =
    val stringArray: CovariantArray[String] = Array("foo", "bar")
    val anyArray: CovariantArray[Any] = stringArray
    anyArray(0) = 42
    stringArray(0).length

CovariantArray.crash()

Output

java.lang.ArrayStoreException: java.lang.Integer
	at Playground$CovariantArray$.crash(main.scala:9)
	at Playground$.<clinit>(main.scala:12)
	... 13 more

Expectation

Should not compile

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:opaque-typesitype:soundnessSoundness bug (it lets us compile code that crashes at runtime with a ClassCastException)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions