scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.84k stars 1.05k forks source link

Unsound reduction of match types with refined trait member as scrutinee #20515

Open EugeneFlesselle opened 4 months ago

EugeneFlesselle commented 4 months ago

Compiler version

3.5.0-RC1

Minimized code

This is another instance of #19746 except the prefix is a trait value instead of a function parameter.

trait V:
  type X = this.type match
    case W[x] => x

trait W[+Y] extends V

trait T:
  val w: W[Any]
  val x: w.X = 0

object U extends T:
  val w: W[Boolean] = new W[Boolean] {}

@main def Test =
  val b: Boolean = U.x : U.w.X // ClassCastException
smarter commented 4 months ago

Another variation I came up with before I realized this issue already existed:

class W[+Y]
object W:
  type Extract[A] = A match
    case W[x] => x

trait Base[+T]:
  val w: T
  def take(x: W.Extract[w.type]): Int

class SubInt extends Base[W[Int]]:
  val w: W[Int] = W[Int]()
  def take(x: W.Extract[w.type]): Int = x

@main def run =
  val b: Base[W[Any]] = SubInt()
  val i: Int = b.take("") // ClassCastException