Skip to content

Unreachable case warning (E121) in inline generic function #20225

@alexishorner

Description

@alexishorner

Compiler version

3.4.0

Minimized code

sealed abstract class Parent // The warning goes away if `sealed` is removed

class A extends Parent
class B extends Parent

inline def matchAs[T <: Parent](p: Parent): Unit = p match // The type bound is necessary
    case p: T =>
    case _ => println("unreachable case reached") // Warning here: "Unreachable case except for null [...]"
end matchAs

@main def main: Unit =
    matchAs[A](new B)
end main

Output

Compilation Output

[warn] -- [E121] Pattern Match Warning: [...]/inline_poc/src/main/scala/poc.scala:9:9 
[warn] 9 |    case _ => println("unreachable case reached")
[warn]   |         ^
[warn]   |Unreachable case except for null (if this is intentional, consider writing case null => instead).
[warn] one warning found

Run Output

unreachable case reached

Expectation

No warning should be issued, since the case is reachable.

Notes

The following conditions are required to trigger the warning

  • Parent must be sealed
  • T must be upper bounded

The warning stays even when p is declared inline.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions