Skip to content

Commit

Permalink
Backport "Fix #20372: Check pattern match exhaustivity in inlined cod…
Browse files Browse the repository at this point in the history
…e" to LTS (#21115)

Backports #20403 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jul 8, 2024
2 parents 7c1bb85 + 72a9014 commit 2e5a8e8
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 @@ -903,6 +903,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 2e5a8e8

Please sign in to comment.