Skip to content

Commit 8563571

Browse files
authored
Fix #20372: Check pattern match exhaustivity in inlined code (#20403)
Fixes #20372 c0e93f1 previously disabled warnings for pattern matches in Inlined code, as things like: ```scala inline def count(inline x: Boolean) = x match case true => 1 case false => 0 count(true) // inlined to true match {case true => 1; case false => 0 } ``` would throw warnings about unreachable cases, which could have been confusing to the users. However, in those cases it should be enough to disallow checks for unreachable cases specifically, and leave exhaustivity checks intact.
2 parents 8ef2c5c + 36d2205 commit 8563571

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

Diff for: 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
}

Diff for: 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
}

Diff for: 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`

Diff for: 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)