Skip to content

Unsoundness from not-yet-initialized path without involving divergence #11572

Open
@mario-bucev

Description

@mario-bucev

Compiler version

Scala compiler version 3.0.0-RC2-bin-SNAPSHOT-nonbootstrapped-git-55da939 -- Copyright 2002-2021, LAMP/EPFL

Minimized code

@main def test: Unit = {
  trait Cov[+X] {
    def get: X
  }
  trait Bounded {
    type T >: Cov[Int] <: Cov[String]
  }
  val t: Bounded = new Bounded {
    // Note: using this instead of t produces an error (as expected)
    override type T >: t.T <: t.T
  }
  
  val covInt = new Cov[Int] {
    override def get: Int = 3
  }
  val str: String = ((covInt: t.T): Cov[String]).get // ClassCastException: class Integer cannot be cast to class String
}  

Output

Click to expand
Exception in thread "main" 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')
	at Main$package$.test(Main.scala:16)
	at test.main(Main.scala:1)

Expectation

The above instantiation of t should be rejected, as it is using a not-yet-initialized t to construct non-sensical bounds using the assumption that Cov[Int] <: Cov[String].

This issue is similar to #5854 but does not involve divergence.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions