Skip to content

Commit 36d2205

Browse files
committed
Check pattern match exhaustivity in inlined code
1 parent 177b489 commit 36d2205

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class PatternMatcher extends MiniPhase {
5656
if !inInlinedCode then
5757
// check exhaustivity and unreachability
5858
SpaceEngine.checkMatch(tree)
59+
else
60+
// only check exhaustivity, as inlining may generate unreachable code
61+
// like in i19157.scala
62+
SpaceEngine.checkMatchExhaustivityOnly(tree)
5963

6064
translated.ensureConforms(matchType)
6165
}

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,9 @@ object SpaceEngine {
901901
}
902902

903903
def checkMatch(m: Match)(using Context): Unit =
904-
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
904+
checkMatchExhaustivityOnly(m)
905905
if reachabilityCheckable(m.selector) then checkReachability(m)
906+
907+
def checkMatchExhaustivityOnly(m: Match)(using Context): Unit =
908+
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
906909
}

tests/warn/i20372.check

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i20372.scala:8:5 ----------------------------------------------
2+
8 | id(foo match { // warn
3+
| ^^^
4+
| match may not be exhaustive.
5+
|
6+
| It would fail on pattern case: Baz
7+
|
8+
| longer explanation available when compiling with `-explain`

tests/warn/i20372.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sealed trait Foo
2+
case object Bar extends Foo
3+
case object Baz extends Foo
4+
5+
inline def id[A](a: A): A = a
6+
7+
def shouldThrowAWarning(foo: Foo) =
8+
id(foo match { // warn
9+
case Bar => "Bar"
10+
})

0 commit comments

Comments
 (0)