Skip to content

Pattern Matching inconsistency #22064

Open
@CyrilFMoser

Description

@CyrilFMoser

Compiler version

3.6.1

Minimized code

This does not issue a pattern match exhaustivity warning:

sealed trait T_A[B]
case class CC_C(a: Int, b: T_A[Int]) extends T_A[Char]

val v_a: T_A[Char] = null
val v_b: Int = v_a match{
  case CC_C(0, _) => 0 
}

But this does:

sealed trait T_A
sealed trait T_B
case class CC_A(a: Int, b: T_B) extends T_A

val v_a: T_A = null
val v_b: Int = v_a match{
  case CC_A(0,_) => 0 
}

Expectation

They should behave the same. The issue generally appears when matching against traits that don't have any case classes extending them, given certain generics. (Here T[Int]). But it does not appear when a trait simply has no case classes extending them, regardless of generics (T_B).

My guess is that this is related to the fact that the pattern matcher does not enforce covering nulls explicitly, so if the only way to create a case class is with a null it thinks that this case class could never be constructed (first program) and therefore also does not need to match against it

I believe my previous bug reports had this same issue at the root, and the fix in #21000 (which to my knowledge should be included in 3.6.1?) did not resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions