Match type upper bound affect type inference?

Code without upper bound on match type compiles without warning

enum A {
  case AA
}
enum B {
  case BA
  case BB
}
type P[X <: A] = X match {
  case A.AA.type => B.BA.type
}
def apply[X <: A](x: X, y: P[X]): Unit = x match {
  case x: A.AA.type => y match {
    case y: B.BA.type => ???
  }
}

while the code with upper bound warns “match may not be exhaustive.”:

enum A {
  case AA
}
enum B {
  case BA
  case BB
}
type P[X <: A] <: B = X match { // Upper bound added
  case A.AA.type => B.BA.type
}
def apply[X <: A](x: X, y: P[X]): Unit = x match {
  case x: A.AA.type => y match {
    case y: B.BA.type => ??? // match may not be exhaustive. It would fail on pattern case: BB
  }
}
1 Like

I suspect it’s because for the first case, the y: P[X] isn’t reduced at all and is remained as P[A] while the second case, the y: P[X] is somehow treated as B which is its upper bound?