@@ -110,13 +110,19 @@ private[async] trait AnfTransform {
110110 statsExprThrow
111111 } else {
112112 val varDef = defineVar(name.ifRes, expr.tpe, tree.pos)
113- def branchWithAssign (orig : Tree ) = api.typecheck(atPos(orig.pos) {
114- def cast (t : Tree ) = mkAttributedCastPreservingAnnotations(t, tpe(varDef.symbol))
115- orig match {
116- case Block (thenStats, thenExpr) => newBlock(thenStats, Assign (Ident (varDef.symbol), cast(thenExpr)))
117- case _ => Assign (Ident (varDef.symbol), cast(orig))
113+ def typedAssign (lhs : Tree ) =
114+ api.typecheck(atPos(lhs.pos)(Assign (Ident (varDef.symbol), mkAttributedCastPreservingAnnotations(lhs, tpe(varDef.symbol)))))
115+
116+ def branchWithAssign (t : Tree ): Tree = {
117+ t match {
118+ case MatchEnd (ld) =>
119+ deriveLabelDef(ld, branchWithAssign)
120+ case blk @ Block (thenStats, thenExpr) =>
121+ treeCopy.Block (blk, thenStats, typedAssign(thenExpr)).setType(definitions.UnitTpe )
122+ case _ =>
123+ typedAssign(t)
118124 }
119- })
125+ }
120126 val ifWithAssign = treeCopy.If (tree, cond, branchWithAssign(thenp), branchWithAssign(elsep)).setType(definitions.UnitTpe )
121127 stats :+ varDef :+ ifWithAssign :+ atPos(tree.pos)(gen.mkAttributedStableRef(varDef.symbol)).setType(tree.tpe)
122128 }
@@ -139,11 +145,14 @@ private[async] trait AnfTransform {
139145 api.typecheck(atPos(lhs.pos)(Assign (Ident (varDef.symbol), mkAttributedCastPreservingAnnotations(lhs, tpe(varDef.symbol)))))
140146 val casesWithAssign = cases map {
141147 case cd@ CaseDef (pat, guard, body) =>
142- val newBody = body match {
143- case b@ Block (caseStats, caseExpr) => treeCopy.Block (b, caseStats, typedAssign(caseExpr)).setType(definitions.UnitTpe )
144- case _ => typedAssign(body)
148+ def bodyWithAssign (t : Tree ): Tree = {
149+ t match {
150+ case MatchEnd (ld) => deriveLabelDef(ld, bodyWithAssign)
151+ case b@ Block (caseStats, caseExpr) => treeCopy.Block (b, caseStats, bodyWithAssign(caseExpr)).setType(definitions.UnitTpe )
152+ case _ => typedAssign(t)
153+ }
145154 }
146- treeCopy.CaseDef (cd, pat, guard, newBody ).setType(definitions.UnitTpe )
155+ treeCopy.CaseDef (cd, pat, guard, bodyWithAssign(body) ).setType(definitions.UnitTpe )
147156 }
148157 val matchWithAssign = treeCopy.Match (tree, scrut, casesWithAssign).setType(definitions.UnitTpe )
149158 require(matchWithAssign.tpe != null , matchWithAssign)
@@ -228,11 +237,6 @@ private[async] trait AnfTransform {
228237 val stats1 = stats.flatMap(linearize.transformToList).filterNot(isLiteralUnit)
229238 val exprs1 = linearize.transformToList(expr)
230239 val trees = stats1 ::: exprs1
231- def isMatchEndLabel (t : Tree ): Boolean = t match {
232- case ValDef (_, _, _, t) if isMatchEndLabel(t) => true
233- case ld : LabelDef if ld.name.toString.startsWith(" matchEnd" ) => true
234- case _ => false
235- }
236240 def groupsEndingWith [T ](ts : List [T ])(f : T => Boolean ): List [List [T ]] = if (ts.isEmpty) Nil else {
237241 ts.indexWhere(f) match {
238242 case - 1 => List (ts)
@@ -241,7 +245,7 @@ private[async] trait AnfTransform {
241245 ts1 :: groupsEndingWith(ts2)(f)
242246 }
243247 }
244- val matchGroups = groupsEndingWith(trees)(isMatchEndLabel)
248+ val matchGroups = groupsEndingWith(trees){ case MatchEnd (_) => true ; case _ => false }
245249 val trees1 = matchGroups.flatMap(eliminateMatchEndLabelParameter)
246250 val result = trees1 flatMap {
247251 case Block (stats, expr) => stats :+ expr
0 commit comments