@@ -91,8 +91,10 @@ private[async] trait AnfTransform extends TransformUtils {
91
91
val simpleApply = treeCopy.Apply (tree, simpleFun, argExprss)
92
92
simpleApply.attachments.remove[ContainsAwait .type ]
93
93
if (isAwait(fun)) {
94
- val valDef = defineVal(transformState.name.await(), treeCopy.Apply (tree, fun, argExprss), tree.pos)
95
- val ref = gen.mkAttributedStableRef(valDef.symbol).setType(tree.tpe)
94
+ val valDef = defineVal(transformState.name.await(), treeCopy.Apply (tree, fun, argExprss), tree.pos, isLocalVar = true )
95
+ val ref = gen.mkAttributedStableRef(valDef.symbol)
96
+ .setType(tree.tpe)
97
+ .updateAttachment(ResRef )
96
98
currentStats += valDef
97
99
atPos(tree.pos)(ref)
98
100
} else {
@@ -101,13 +103,15 @@ private[async] trait AnfTransform extends TransformUtils {
101
103
102
104
case Block (stats, expr) =>
103
105
// First, transform the block contents into a separate List[Tree]
104
- val (trees, _) = withNewControlFlowBlock {
105
- stats.foreach(stat => {
106
- val expr = transform(stat);
107
- if (! isLiteralUnit(expr)) currentStats += expr
108
- })
106
+ val (trees, _) = withNewControlFlowBlock[Unit ] {
107
+ for (stat <- stats)
108
+ transform(stat) match {
109
+ case t0 if t0.hasAttachment[ResRef .type ] =>
110
+ currentStats += typedPos(t0.pos)(Assign (t0, gen.mkZero(t0.symbol.info)))
111
+ case t0 if ! isLiteralUnit(t0) => currentStats += t0
112
+ case _ =>
113
+ }
109
114
currentStats += transform(expr)
110
- ()
111
115
}
112
116
113
117
// Identify groups of statements compiled from pattern matches and process them separately to
@@ -125,7 +129,7 @@ private[async] trait AnfTransform extends TransformUtils {
125
129
126
130
// However, we let `onTail` add the expr to `currentStats` (that was more efficient than using `ts.dropRight(1).foreach(addToStats)`)
127
131
// Compensate by removing it from the buffer and returning the expr.
128
- // If the expr it itself a unit-typed LabelDef, move it to the stats and leave a Unit expression in its place
132
+ // If the expr is itself a unit-typed LabelDef, move it to the stats and leave a Unit expression in its place
129
133
// to make life easier for transformMatchOrIf
130
134
currentStats.remove(currentStats.size - 1 ) match {
131
135
case ld : LabelDef if ld.tpe.typeSymbol == definitions.BoxedUnitClass =>
@@ -222,7 +226,7 @@ private[async] trait AnfTransform extends TransformUtils {
222
226
// after the preceding sibling, but rather will be the target of a control flow jump.
223
227
private def transformNewControlFlowBlock (tree : Tree ): Tree = {
224
228
val savedStats = currentStats
225
- this .currentStats = new ListBuffer [Tree ]
229
+ this .currentStats = ListBuffer .empty [Tree ]
226
230
try transform(tree) match {
227
231
case b@ Block (stats, expr) =>
228
232
treeCopy.Block (b, currentStats.prependToList(stats), expr)
@@ -237,7 +241,7 @@ private[async] trait AnfTransform extends TransformUtils {
237
241
238
242
private def withNewControlFlowBlock [T ](f : => T ): (List [Tree ], T ) = {
239
243
val savedStats = currentStats
240
- this .currentStats = new ListBuffer [Tree ]
244
+ this .currentStats = ListBuffer .empty [Tree ]
241
245
try {
242
246
val result = f
243
247
(currentStats.toList, result)
@@ -355,15 +359,18 @@ private[async] trait AnfTransform extends TransformUtils {
355
359
} else t
356
360
}
357
361
358
- def defineVal (name : global.TermName , rhs : global.Tree , pos : Position ): ValDef = {
359
- val sym = currentOwner.newTermSymbol(name, pos, Flags .SYNTHETIC ).setInfo(rhs.tpe)
362
+ def defineVal (name : global.TermName , rhs : global.Tree , pos : Position , isLocalVar : Boolean = false ): ValDef = {
363
+ val flags = if (isLocalVar) Flags .MUTABLE | Flags .SYNTHETIC else Flags .SYNTHETIC
364
+ val sym = currentOwner.newTermSymbol(name, pos, flags).setInfo(rhs.tpe)
360
365
ValDef (sym, rhs.changeOwner((currentOwner, sym))).setType(NoType )
361
366
}
362
367
363
368
def defineVar (name : TermName , tp : Type , pos : Position ): ValDef = {
364
369
val sym = currentOwner.newTermSymbol(name, pos, Flags .MUTABLE | Flags .SYNTHETIC ).setInfo(tp)
365
370
ValDef (sym, gen.mkZero(tp).setPos(pos)).setType(NoType )
366
371
}
372
+
373
+ private object ResRef extends PlainAttachment
367
374
}
368
375
369
376
private def typedAssign (lhs : Tree , varSym : Symbol ) =
0 commit comments