Skip to content

Don't reduce inline matches, when a guard cannot be checked at compile time #13601

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 1 commit into from
Oct 1, 2021
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
28 changes: 19 additions & 9 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,11 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
*/
type MatchRedux = Option[(List[MemberDef], Tree)]

/** Same as MatchRedux, but also includes a boolean
* that is true if the guard can be checked at compile time.
*/
type MatchReduxWithGuard = Option[(List[MemberDef], Tree, Boolean)]

/** Reduce an inline match
* @param mtch the match tree
* @param scrutinee the scrutinee expression, assumed to be pure, or
Expand Down Expand Up @@ -1423,7 +1428,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
val scrutineeSym = newSym(InlineScrutineeName.fresh(), Synthetic, scrutType).asTerm
val scrutineeBinding = normalizeBinding(ValDef(scrutineeSym, scrutinee))

def reduceCase(cdef: CaseDef): MatchRedux = {
def reduceCase(cdef: CaseDef): MatchReduxWithGuard = {
val caseBindingMap = new mutable.ListBuffer[(Symbol, MemberDef)]()

def substBindings(
Expand All @@ -1442,21 +1447,26 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
val gadtCtx = ctx.fresh.setFreshGADTBounds.addMode(Mode.GadtConstraintInference)
if (reducePattern(caseBindingMap, scrutineeSym.termRef, cdef.pat)(using gadtCtx)) {
val (caseBindings, from, to) = substBindings(caseBindingMap.toList, mutable.ListBuffer(), Nil, Nil)
val guardOK = cdef.guard.isEmpty || {
typer.typed(cdef.guard.subst(from, to), defn.BooleanType) match {
case ConstantValue(true) => true
case _ => false
val (guardOK, canReduceGuard) =
if cdef.guard.isEmpty then (true, true)
else typer.typed(cdef.guard.subst(from, to), defn.BooleanType) match {
case ConstantValue(v: Boolean) => (v, true)
case _ => (false, false)
}
}
if (guardOK) Some((caseBindings.map(_.subst(from, to)), cdef.body.subst(from, to)))
else None
if guardOK then Some((caseBindings.map(_.subst(from, to)), cdef.body.subst(from, to), canReduceGuard))
else if canReduceGuard then None
else Some((caseBindings.map(_.subst(from, to)), cdef.body.subst(from, to), canReduceGuard))
}
else None
}

def recur(cases: List[CaseDef]): MatchRedux = cases match {
case Nil => None
case cdef :: cases1 => reduceCase(cdef) `orElse` recur(cases1)
case cdef :: cases1 =>
reduceCase(cdef) match
case None => recur(cases1)
case r @ Some((caseBindings, rhs, canReduceGuard)) if canReduceGuard => Some((caseBindings, rhs))
case _ => None
}

recur(cases)
Expand Down
9 changes: 9 additions & 0 deletions tests/neg/i13570.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Error: tests/neg/i13570.scala:8:5 -----------------------------------------------------------------------------------
8 | jim(Seq(1,2)) // error
| ^^^^^^^^^^^^^
| cannot reduce inline match with
| scrutinee: seq$proxy1 : (seq$proxy1 : Seq[Int])
| patterns : case s @ _:Seq[Int] if s.isEmpty
| case s @ _:Seq[Int]
| case _
| This location contains code that was inlined from i13570.scala:3
8 changes: 8 additions & 0 deletions tests/neg/i13570.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object Test:
inline def jim(seq: Seq[Int]) =
inline seq match
case s: Seq[Int] if s.isEmpty => println("seq is empty")
case s: Seq[Int] => println("seq is not empty")
case _ => println("somthing hinky happened")

jim(Seq(1,2)) // error