Skip to content

Commit

Permalink
LamdaLift tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Jan 3, 2022
1 parent d8bf672 commit 0c14898
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/compiler/scala/tools/nsc/transform/LambdaLift.scala
Expand Up @@ -488,28 +488,23 @@ abstract class LambdaLift extends InfoTransform {
if (sym.isLocalToBlock) liftDef(withFreeParams)
else withFreeParams

case ValDef(mods, name, tpt, rhs) =>
if (sym.isCapturedVariable) {
val tpt1 = TypeTree(sym.tpe) setPos tpt.pos

val refTypeSym = sym.tpe.typeSymbol

val factoryCall = typer.typedPos(rhs.pos) {
rhs match {
case EmptyTree =>
val zeroMSym = refZeroMethod(refTypeSym)
gen.mkMethodCall(zeroMSym, Nil)
case arg =>
val createMSym = refCreateMethod(refTypeSym)
gen.mkMethodCall(createMSym, arg :: Nil)
}
case ValDef(mods, name, tpt, rhs) if sym.isCapturedVariable =>
val tpt1 = TypeTree(sym.tpe) setPos tpt.pos
val refTypeSym = sym.tpe.typeSymbol
val factoryCall = typer.typedPos(rhs.pos) {
rhs match {
case EmptyTree =>
val zeroMSym = refZeroMethod(refTypeSym)
gen.mkMethodCall(zeroMSym, Nil)
case arg =>
val createMSym = refCreateMethod(refTypeSym)
gen.mkMethodCall(createMSym, arg :: Nil)
}

if (settings.warnCaptured)
reporter.warning(tree.pos, s"Modification of variable $name within a closure causes it to be boxed.")

treeCopy.ValDef(tree, mods, name, tpt1, factoryCall)
} else tree
}
if (settings.warnCaptured)
reporter.warning(tree.pos, s"Modification of variable $name within a closure causes it to be boxed.")
treeCopy.ValDef(tree, mods, name, tpt1, factoryCall)
case ValDef(_, _, _, _) => tree
case Return(Block(stats, value)) =>
Block(stats, treeCopy.Return(tree, value)) setType tree.tpe setPos tree.pos
case Return(expr) =>
Expand Down

0 comments on commit 0c14898

Please sign in to comment.