Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SI-7249 Reign in overzealous Function0 optimization.

The fix for SI-1247 went too far, and could result in
premature evaluation of the expression that yields the
Function0.

This commit checks that said expression is safe to inline.
If not, a wrapper `() => ....` is still required.

The optimization is still enabled in sitations like the
original test case, run/t1247.scala.
  • Loading branch information...
commit 552b6234dbbb4ff1e971e2c0f87ecd4090dd36a5 1 parent b7b4f87
@retronym retronym authored
View
6 src/compiler/scala/tools/nsc/transform/UnCurry.scala
@@ -492,10 +492,14 @@ abstract class UnCurry extends InfoTransform
}
else {
log(s"Argument '$arg' at line ${arg.pos.safeLine} is $formal from ${fun.fullName}")
+ def canUseDirectly(recv: Tree) = (
+ recv.tpe.typeSymbol.isSubClass(FunctionClass(0))
+ && treeInfo.isExprSafeToInline(recv)
+ )
arg match {
// don't add a thunk for by-name argument if argument already is an application of
// a Function0. We can then remove the application and use the existing Function0.
- case Apply(Select(recv, nme.apply), Nil) if recv.tpe.typeSymbol isSubClass FunctionClass(0) =>
+ case Apply(Select(recv, nme.apply), Nil) if canUseDirectly(recv) =>
recv
case _ =>
newFunction0(arg)
View
1  test/files/run/t7249.check
@@ -0,0 +1 @@
+Yup!
View
7 test/files/run/t7249.scala
@@ -0,0 +1,7 @@
+object Test extends App {
+ def bnToLambda(s: => String): () => String = () => s
+ var x: () => String = () => sys.error("Nope")
+ val y = bnToLambda { x() }
+ x = () => "Yup!"
+ println(y())
+}
Please sign in to comment.
Something went wrong with that request. Please try again.