From 72a9014f711a62115c12de827226b2a8f072239b Mon Sep 17 00:00:00 2001 From: Jan Chyb Date: Tue, 14 May 2024 12:25:27 +0200 Subject: [PATCH] Check pattern match exhaustivity in inlined code [Cherry-picked 36d2205284b9042304204da0b4a16f13392c4a04] --- .../dotty/tools/dotc/transform/PatternMatcher.scala | 4 ++++ .../src/dotty/tools/dotc/transform/patmat/Space.scala | 5 ++++- tests/warn/i20372.check | 8 ++++++++ tests/warn/i20372.scala | 10 ++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/warn/i20372.check create mode 100644 tests/warn/i20372.scala diff --git a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala index 3778a263c14e..316acf02d453 100644 --- a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -56,6 +56,10 @@ class PatternMatcher extends MiniPhase { if !inInlinedCode then // check exhaustivity and unreachability SpaceEngine.checkMatch(tree) + else + // only check exhaustivity, as inlining may generate unreachable code + // like in i19157.scala + SpaceEngine.checkMatchExhaustivityOnly(tree) translated.ensureConforms(matchType) } diff --git a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala index 560f1d6e92e9..118b7fe70ce3 100644 --- a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala +++ b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala @@ -903,6 +903,9 @@ object SpaceEngine { } def checkMatch(m: Match)(using Context): Unit = - if exhaustivityCheckable(m.selector) then checkExhaustivity(m) + checkMatchExhaustivityOnly(m) if reachabilityCheckable(m.selector) then checkReachability(m) + + def checkMatchExhaustivityOnly(m: Match)(using Context): Unit = + if exhaustivityCheckable(m.selector) then checkExhaustivity(m) } diff --git a/tests/warn/i20372.check b/tests/warn/i20372.check new file mode 100644 index 000000000000..7946c424df0c --- /dev/null +++ b/tests/warn/i20372.check @@ -0,0 +1,8 @@ +-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i20372.scala:8:5 ---------------------------------------------- +8 | id(foo match { // warn + | ^^^ + | match may not be exhaustive. + | + | It would fail on pattern case: Baz + | + | longer explanation available when compiling with `-explain` diff --git a/tests/warn/i20372.scala b/tests/warn/i20372.scala new file mode 100644 index 000000000000..2886bd11b09f --- /dev/null +++ b/tests/warn/i20372.scala @@ -0,0 +1,10 @@ +sealed trait Foo +case object Bar extends Foo +case object Baz extends Foo + +inline def id[A](a: A): A = a + +def shouldThrowAWarning(foo: Foo) = + id(foo match { // warn + case Bar => "Bar" + })