@@ -78,7 +78,6 @@ abstract class UnCurry extends InfoTransform
78
78
private val forceExpandFunction = settings.Ydelambdafy .value == " inline"
79
79
private var needTryLift = false
80
80
private var inConstructorFlag = 0L
81
- private val byNameArgs = mutable.HashSet .empty[Tree ]
82
81
private val noApply = mutable.HashSet .empty[Tree ]
83
82
private val newMembers = mutable.Map .empty[Symbol , Buffer [Tree ]]
84
83
@@ -124,11 +123,14 @@ abstract class UnCurry extends InfoTransform
124
123
EmptyTree
125
124
}
126
125
127
- /* Is tree a reference `x` to a call by name parameter that needs to be converted to
128
- * x.apply()? Note that this is not the case if `x` is used as an argument to another
129
- * call by name parameter.
126
+ /* Is `tree` a reference `x` to a call by name parameter and eligible to be converted to x.apply()?
127
+ *
128
+ * Normally, such a reference evaluates the argument.
129
+ *
130
+ * This is not the case if `x` is used as an argument to a method that takes it as a call by name parameter.
131
+ * Additionally, an expression `() => x` is "unwrapped" to `x` and must not be applied.
130
132
*/
131
- def isByNameRef (tree : Tree ) = {
133
+ def isByNameRefToApply (tree : Tree ) = {
132
134
val sym = tree.symbol
133
135
val maybe = (
134
136
(sym ne null )
@@ -138,7 +140,7 @@ abstract class UnCurry extends InfoTransform
138
140
tree match {
139
141
case _ if ! maybe => false
140
142
case _ : This | _ : Super => false
141
- case tree => isByName(sym) && ! byNameArgs (tree)
143
+ case tree => isByName(sym) && ! noApply (tree)
142
144
}
143
145
}
144
146
@@ -227,7 +229,7 @@ abstract class UnCurry extends InfoTransform
227
229
// Normally, we can unwrap `() => cbn` to `cbn` where `cbn` refers to a CBN argument (typically `cbn` is an Ident)
228
230
// because we know `cbn` will already be a `Function0` thunk. When we're targeting a SAM,
229
231
// the types don't align and we must preserve the function wrapper.
230
- if (fun.vparams.isEmpty && isByNameRef (fun.body) && ! fun.attachments.contains[SAMFunction ]) {
232
+ if (fun.vparams.isEmpty && isByNameRefToApply (fun.body) && ! fun.attachments.contains[SAMFunction ]) {
231
233
noApply += fun.body
232
234
fun.body
233
235
}
@@ -357,12 +359,12 @@ abstract class UnCurry extends InfoTransform
357
359
val param0 = if (params0.hasNext) params0.next() else NoSymbol
358
360
if (! isByNameParamType(param.info)) {
359
361
if (param0 != NoSymbol && isByNameParamType(param0.info)) // pass 2, sig is uncurried in expansion
360
- byNameArgs += arg
362
+ noApply += arg
361
363
arg
362
364
}
363
- else if (isByNameRef (arg)) {
365
+ else if (isByNameRefToApply (arg)) {
364
366
// thunk does not need to be forced because it's a reference to a by-name arg passed to a by-name param
365
- byNameArgs += arg
367
+ noApply += arg
366
368
arg.setType(functionType(Nil , arg.tpe))
367
369
} else {
368
370
log(s " Argument ' $arg' at line ${arg.pos.line} is ${param.info} from ${fun.fullName}" )
@@ -563,12 +565,10 @@ abstract class UnCurry extends InfoTransform
563
565
564
566
case _ =>
565
567
val tree1 = super .transform(tree)
566
- if (isByNameRef(tree1)) {
567
- val tree2 = tree1 setType functionType(Nil , tree1.tpe)
568
- val tree3 =
569
- if (noApply(tree2)) tree2
570
- else localTyper.typedPos(tree1.pos)(Apply (Select (tree2, nme.apply), Nil ))
571
- return tree3
568
+ if (isByNameRefToApply(tree1)) {
569
+ val tree2 = tree1.setType(functionType(Nil , tree1.tpe))
570
+ val tree3 = localTyper.typedPos(tree1.pos)(Apply (Select (tree2, nme.apply), Nil ))
571
+ return tree3 // result type already set
572
572
}
573
573
tree1
574
574
}
0 commit comments