Skip to content
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

Fix #20372: Check pattern match exhaustivity in inlined code #20403

Merged
merged 1 commit into from
May 20, 2024
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
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"
})
Loading