Skip to content

Commit

Permalink
Check pattern match exhaustivity in inlined code
Browse files Browse the repository at this point in the history
  • Loading branch information
jchyb committed May 15, 2024
1 parent 177b489 commit 36d2205
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class PatternMatcher extends MiniPhase {
if !inInlinedCode then
// check exhaustivity and unreachability
SpaceEngine.checkMatch(tree)
else
// only check exhaustivity, as inlining may generate unreachable code
// like in i19157.scala
SpaceEngine.checkMatchExhaustivityOnly(tree)

translated.ensureConforms(matchType)
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,9 @@ object SpaceEngine {
}

def checkMatch(m: Match)(using Context): Unit =
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
checkMatchExhaustivityOnly(m)
if reachabilityCheckable(m.selector) then checkReachability(m)

def checkMatchExhaustivityOnly(m: Match)(using Context): Unit =
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
}
8 changes: 8 additions & 0 deletions tests/warn/i20372.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i20372.scala:8:5 ----------------------------------------------
8 | id(foo match { // warn
| ^^^
| match may not be exhaustive.
|
| It would fail on pattern case: Baz
|
| longer explanation available when compiling with `-explain`
10 changes: 10 additions & 0 deletions tests/warn/i20372.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sealed trait Foo
case object Bar extends Foo
case object Baz extends Foo

inline def id[A](a: A): A = a

def shouldThrowAWarning(foo: Foo) =
id(foo match { // warn
case Bar => "Bar"
})

0 comments on commit 36d2205

Please sign in to comment.