Skip to content

Commit f3b58cf

Browse files
committed
Bring back pattern match exhaustivity checking for macros
Also move the -Werror reporting until after the suspended warning reporting - this was needed for the added test to work (as previously the compilation would not error out even if -Werror was used).
1 parent ed71954 commit f3b58cf

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,10 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
385385
runPhases(allPhases = fusedPhases)(using runCtx)
386386
cancelAsyncTasty()
387387

388-
ctx.reporter.finalizeReporting()
389388
if (!ctx.reporter.hasErrors)
390389
Rewrites.writeBack()
391390
suppressions.runFinished(hasErrors = ctx.reporter.hasErrors)
391+
ctx.reporter.finalizeReporting()
392392
while (finalizeActions.nonEmpty && canProgress()) {
393393
val action = finalizeActions.remove(0)
394394
action()

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ 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)
49+
// Skip unreachability analysis on inlined code (eg pos/i19157)
5050
if !tpd.enclosingInlineds.nonEmpty then
5151
// check exhaustivity and unreachability
5252
SpaceEngine.checkMatch(tree)
53+
else
54+
// only check exhaustivity
55+
SpaceEngine.checkMatchExhaustivityOnly(tree)
5356

5457
translated.ensureConforms(matchType)
5558
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,10 @@ object SpaceEngine {
972972
end checkReachability
973973

974974
def checkMatch(m: Match)(using Context): Unit =
975-
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
975+
checkMatchExhaustivityOnly(m)
976976
if reachabilityCheckable(m.selector) then checkReachability(m)
977+
978+
def checkMatchExhaustivityOnly(m: Match)(using Context): Unit =
979+
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
980+
977981
}

tests/neg-macros/i22212.check

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
-- [E029] Pattern Match Exhaustivity Warning: tests/neg-macros/i22212/Test_2.scala:2:34 --------------------------------
3+
2 |@main def main() = Macro.makeMatch()
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:11
12+
11 | (_: Foo) match
13+
| ^^^^^^
14+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15+
|This location contains code that was inlined from Macro_1.scala:11
16+
11 | (_: Foo) match
17+
| ^
18+
12 | case Bar => ()
19+
--------------------------------------------------------------------------------------------------------------------
20+
|
21+
| longer explanation available when compiling with `-explain`
22+
No warnings can be incurred under -Werror (or -Xfatal-warnings)

tests/neg-macros/i22212/Macro_1.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
sealed trait Foo
4+
case object Bar extends Foo
5+
case object Baz extends Foo
6+
7+
object Macro {
8+
inline def makeMatch() = ${makeMatchImpl}
9+
def makeMatchImpl(using Quotes) = {
10+
'{
11+
(_: Foo) match
12+
case Bar => ()
13+
}
14+
}
15+
}

tests/neg-macros/i22212/Test_2.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//> using options -Xfatal-warnings
2+
@main def main() = Macro.makeMatch()
3+
// nopos-error: No warnings can be incurred under -Werror (or -Xfatal-warnings)

0 commit comments

Comments
 (0)