Skip to content

Unsoundness due to initialization-produced null #4042

Closed
@sir-wabbit

Description

@sir-wabbit

Similar to #4031. Derived from this gist, which works in Scala2.

object App {
  def coerce[U, V](u: U): V = {
    trait X { type R >: U }
    trait Y { type R = V }

    class T[A <: X](val a: A)(val value: a.R)

    object O { val x : Y & X = x }

    val a = new T[Y & X](O.x)(u)
    a.value
  }

  def main(args: Array[String]): Unit = {
    val x: Int = coerce[String, Int]("a")
    println(x + 1)
  }
}

/////////////////////////////////

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
	at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:101)
	at App$.main(HelloWorld.scala:15)
	at App.main(HelloWorld.scala)

Tested on the most recent master branch.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions