Skip to content

Bring back pattern match exhaustivity checking for macros #22622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ class PatternMatcher extends MiniPhase {
case rt => tree.tpe
val translated = new Translator(matchType, this).translateMatch(tree)

// Skip analysis on inlined code (eg pos/i19157)
if !tpd.enclosingInlineds.nonEmpty then
// check exhaustivity and unreachability
SpaceEngine.checkMatch(tree)
// check exhaustivity and unreachability
SpaceEngine.checkMatch(tree)

translated.ensureConforms(matchType)
}
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ object SpaceEngine {
!sel.tpe.hasAnnotation(defn.UncheckedAnnot)
&& !sel.tpe.widen.isRef(defn.QuotedExprClass)
&& !sel.tpe.widen.isRef(defn.QuotedTypeClass)
&& tpd.enclosingInlineds.isEmpty // Skip reachability on inlined code (eg i19157/i22212)

def checkReachability(m: Match)(using Context): Unit = trace(i"checkReachability($m)"):
val selTyp = toUnderlying(m.selector.tpe).dealias
Expand Down
21 changes: 21 additions & 0 deletions tests/warn/i22212.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i22212/Test_2.scala:3:19 --------------------------------------
3 | Macro.makeMatch() // warn: match may not be exhaustive.
| ^^^^^^^^^^^^^^^^^
| match may not be exhaustive.
|
| It would fail on pattern case: Baz
|---------------------------------------------------------------------------------------------------------------------
|Inline stack trace
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|This location contains code that was inlined from Macro_1.scala:7
7 | (_: Foo) match
| ^^^^^^
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|This location contains code that was inlined from Macro_1.scala:7
7 | (_: Foo) match
| ^
8 | case Bar => ()
---------------------------------------------------------------------------------------------------------------------
|
| longer explanation available when compiling with `-explain`
3 changes: 3 additions & 0 deletions tests/warn/i22212/Data_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sealed trait Foo
case object Bar extends Foo
case object Baz extends Foo
11 changes: 11 additions & 0 deletions tests/warn/i22212/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted._

object Macro {
inline def makeMatch() = ${makeMatchImpl}
def makeMatchImpl(using Quotes) = {
'{
(_: Foo) match
case Bar => ()
}
}
}
3 changes: 3 additions & 0 deletions tests/warn/i22212/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Test:
def main(args: Array[String]): Unit =
Macro.makeMatch() // warn: match may not be exhaustive.
Loading