Skip to content

Unsound pattern matching when using lower bounds and union types #23364

Open
@Alex1005a

Description

@Alex1005a

Compiler version

scala 3.7.1

Minimized code

case class Id1[A](val x: A)
case class Id2[A](val y: A)

type IdAll[A] = Id1[A] | Id2[A]

sealed trait Adt[A]
case class Con1[B >: Id1[A], A](x: A) extends Adt[B]
case class Con2[B >: Id2[A], A](x: A) extends Adt[B]

def test[A, T >: IdAll[A]](expr: Adt[T]): A = {
    expr match
        case Con1(x) => x
        case Con2(x) => x
}

val result = test[Int, IdAll[Int] | Id2[String]](Con2(""))
print(result)

Output

java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer

Expectation

Compiler error, because the lower bound of IdAll[A] does not guarantee that type T will not contain any Id2[B], which results in a bounds conflict.

Metadata

Metadata

Assignees

Labels

itype:bugstat:needs triageEvery issue needs to have an "area" and "itype" label

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions