Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Alex1005a opened this issue Jun 13, 2025 · 1 comment
Open

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

Alex1005a opened this issue Jun 13, 2025 · 1 comment
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label

Comments

@Alex1005a
Copy link
Contributor

Alex1005a commented Jun 13, 2025

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.

@Alex1005a Alex1005a added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 13, 2025
@Alex1005a
Copy link
Contributor Author

Not surprisingly, replacing union types with intersection types, and lower bounds with upper bounds, the issue remains.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label
Projects
None yet
Development

No branches or pull requests

1 participant