scala / scala3

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

Wrong unreachable code reported for inline def pattern match #20395

Open enif-lee opened 1 month ago

enif-lee commented 1 month ago

Compiler version

3.3.3

Minimized code

sealed trait NodeId
case object Hi extends NodeId
case object Hello extends NodeId

extension(value: NodeId)
  inline def parse[T <: NodeId] = value match
    case t: T => print("match")
    case _ => print("not match")

@main
def main(): Unit = {
  Hi.parse[Hello.type]
}

Output

not match
[IJ]compile
[info] compiling 2 Scala sources to /Users/js_lee/git/personal/scala3-test/target/scala-3.3.3/classes ...
[warn] -- [E121] Pattern Match Warning: /Users/js_lee/git/personal/scala3-test/src/main/scala/main.scala:12:9 
[warn] 12 |    case _ => print("not match")
[warn]    |         ^
[warn]    |Unreachable case except for null (if this is intentional, consider writing case null => instead).
[warn] -- [E030] Match case Unreachable Warning: /Users/js_lee/git/personal/scala3-test/src/main/scala/main.scala:17:10 
[warn] 17 |  Hi.parse[Hello.type]
[warn]    |  ^^^^^^^^^^^^^^^^^^^^
[warn]    |  Unreachable case
[warn]    |----------------------------------------------------------------------------
[warn]    |Inline stack trace
[warn]    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[warn]    |This location contains code that was inlined from main.scala:11
[warn] 11 |    case t: T => print("match")
[warn]    |         ^^^^
[warn]     ----------------------------------------------------------------------------
[warn] two warnings found

![image](https://github.com/scala/scala3/assets/9916002/dba8e346-3f3b-4a39-a1dd-c1472683407

Expectation

The case _ is reachable case but The compiler doesn't think so

Gedochao commented 1 month ago

BTW. E030 seems to be gone on Next (both 3.4.1 and 3.5.0-RC1-bin-20240512-99c4c00-NIGHTLY) Although the problem remains, otherwise.

Output on 3.4.1:

-- [E121] Pattern Match Warning: /Users/pchabelski/IdeaProjects/scala-cli-tests/compiler-repro/repro.scala:8:9 
8 |    case _ => print("not match")
  |         ^
  |Unreachable case except for null (if this is intentional, consider writing case null => instead).
1 warning found
mbovel commented 1 month ago

Is this issue similar to #20225?