Skip to content

Commit 788016a

Browse files
authored
Bring back pattern match exhaustivity checking for macros (#22622)
Fixes #22212
2 parents ab1bf38 + f28b9c1 commit 788016a

File tree

6 files changed

+41
-4
lines changed

6 files changed

+41
-4
lines changed

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ class PatternMatcher extends MiniPhase {
4646
case rt => tree.tpe
4747
val translated = new Translator(matchType, this).translateMatch(tree)
4848

49-
// Skip analysis on inlined code (eg pos/i19157)
50-
if !tpd.enclosingInlineds.nonEmpty then
51-
// check exhaustivity and unreachability
52-
SpaceEngine.checkMatch(tree)
49+
// check exhaustivity and unreachability
50+
SpaceEngine.checkMatch(tree)
5351

5452
translated.ensureConforms(matchType)
5553
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ object SpaceEngine {
917917
!sel.tpe.hasAnnotation(defn.UncheckedAnnot)
918918
&& !sel.tpe.widen.isRef(defn.QuotedExprClass)
919919
&& !sel.tpe.widen.isRef(defn.QuotedTypeClass)
920+
&& tpd.enclosingInlineds.isEmpty // Skip reachability on inlined code (eg i19157/i22212)
920921

921922
def checkReachability(m: Match)(using Context): Unit = trace(i"checkReachability($m)"):
922923
val selTyp = toUnderlying(m.selector.tpe).dealias

tests/warn/i22212.check

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i22212/Test_2.scala:3:19 --------------------------------------
3+
3 | Macro.makeMatch() // warn: match may not be exhaustive.
4+
| ^^^^^^^^^^^^^^^^^
5+
| match may not be exhaustive.
6+
|
7+
| It would fail on pattern case: Baz
8+
|---------------------------------------------------------------------------------------------------------------------
9+
|Inline stack trace
10+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
11+
|This location contains code that was inlined from Macro_1.scala:7
12+
7 | (_: Foo) match
13+
| ^^^^^^
14+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15+
|This location contains code that was inlined from Macro_1.scala:7
16+
7 | (_: Foo) match
17+
| ^
18+
8 | case Bar => ()
19+
---------------------------------------------------------------------------------------------------------------------
20+
|
21+
| longer explanation available when compiling with `-explain`

tests/warn/i22212/Data_1.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sealed trait Foo
2+
case object Bar extends Foo
3+
case object Baz extends Foo

tests/warn/i22212/Macro_1.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.quoted._
2+
3+
object Macro {
4+
inline def makeMatch() = ${makeMatchImpl}
5+
def makeMatchImpl(using Quotes) = {
6+
'{
7+
(_: Foo) match
8+
case Bar => ()
9+
}
10+
}
11+
}

tests/warn/i22212/Test_2.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test:
2+
def main(args: Array[String]): Unit =
3+
Macro.makeMatch() // warn: match may not be exhaustive.

0 commit comments

Comments
 (0)