Skip to content

Fix Tasty reflect extractor for flatten closure blocks #4728

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
Jun 27, 2018
Merged
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
15 changes: 11 additions & 4 deletions compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,19 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
case Trees.Block(stats, expr) => Some((stats, expr))
case _ => None
}
/** Normilizes non Blocks.
* i) Put `while` and `doWhile` loops in thier own blocks: `{ def while$() = ...; while$() }`
/** Normalizes non Blocks.
* i) Put `while` and `doWhile` loops in their own blocks: `{ def while$() = ...; while$() }`
* ii) Put closures in their own blocks: `{ def anon$() = ...; closure(anon$, ...) }`
*/
private def normalizedLoops(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
case block: tpd.Block if block.stats.size > 1 =>
def normalizeInnerLoops(stats: List[tpd.Tree]): List[tpd.Tree] = stats match {
case (x: tpd.DefDef) :: y :: xs if y.symbol.is(Flags.Label) =>
case (x: tpd.DefDef) :: y :: xs if needsNormalization(y) =>
tpd.Block(x :: Nil, y) :: normalizeInnerLoops(xs)
case x :: xs => x :: normalizeInnerLoops(xs)
case Nil => Nil
}
if (block.expr.symbol.is(Flags.Label)) {
if (needsNormalization(block.expr)) {
val stats1 = normalizeInnerLoops(block.stats.init)
val normalLoop = tpd.Block(block.stats.last :: Nil, block.expr)
tpd.Block(stats1, normalLoop)
Expand All @@ -438,6 +439,12 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
}
case _ => tree
}

/** If it is the second statement of a loop or a closure. See: `normalizedLoops` */
private def needsNormalization(tree: tpd.Tree)(implicit ctx: Context): Boolean = tree match {
case _: tpd.Closure => true
case _ => tree.symbol.is(Flags.Label)
}
}

object Inlined extends InlinedExtractor {
Expand Down