diff options
author | Denys Shabalin <denys.shabalin@typesafe.com> | 2014-02-28 14:04:31 +0100 |
---|---|---|
committer | Denys Shabalin <denys.shabalin@typesafe.com> | 2014-02-28 21:27:27 +0100 |
commit | fae2912a60b33f6866a87d30a4e698e433939dc7 (patch) | |
tree | 76ec65450632f773fa3790d83bcb01fdb67c0b5b /src/reflect | |
parent | e17c055df049c6f8b42d31629e70df5bb44e2bfb (diff) | |
download | scala-fae2912a60b33f6866a87d30a4e698e433939dc7.tar.gz scala-fae2912a60b33f6866a87d30a4e698e433939dc7.tar.bz2 scala-fae2912a60b33f6866a87d30a4e698e433939dc7.zip |
Fix block construction/deconstruction asymmetry
Deconstruction of blocks in case clauses uncovered asymmetry between
construction and deconstruction of blocks:
tree match { case cq"$pat => ..$cases" => cq"$pat => ..$cases" }
Such an identity-like transformation used to produce an incorrect tree due
to the fact that zero-element block was mistakingly associated with
empty tree. Such association was used as a solution to block flatenning:
val ab = q"a; b"
q"..$ab; c" // ==> q"a; b; c"
val a = q"a"
q"..$a; c" // ==> q"a; c"
val empty = q""
q"..$empty; c" // ==> q"c"
This commit changes meaning of zero-element block to a be a synthetic unit
instead. This is consistent with parsing of `{}`, cases, ifs and
non-abstract empty-bodied methods. A local tweak to block flattenning is
used to flatten empty tree as empty list instead.
Diffstat (limited to 'src/reflect')
-rw-r--r-- | src/reflect/scala/reflect/internal/ReificationSupport.scala | 29 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/TreeGen.scala | 2 |
2 files changed, 16 insertions, 15 deletions
diff --git a/src/reflect/scala/reflect/internal/ReificationSupport.scala b/src/reflect/scala/reflect/internal/ReificationSupport.scala index ea230a215b..7f345845fe 100644 --- a/src/reflect/scala/reflect/internal/ReificationSupport.scala +++ b/src/reflect/scala/reflect/internal/ReificationSupport.scala @@ -94,7 +94,11 @@ trait ReificationSupport { self: SymbolTable => def setSymbol[T <: Tree](tree: T, sym: Symbol): T = { tree.setSymbol(sym); tree } - def toStats(tree: Tree): List[Tree] = SyntacticBlock.unapply(tree).get + def toStats(tree: Tree): List[Tree] = tree match { + case EmptyTree => Nil + case SyntacticBlock(stats) => stats + case _ => throw new IllegalArgumentException(s"can't flatten $tree") + } def mkAnnotation(tree: Tree): Tree = tree match { case SyntacticNew(Nil, SyntacticApplied(SyntacticTypeApplied(_, _), _) :: Nil, noSelfType, Nil) => @@ -239,7 +243,7 @@ trait ReificationSupport { self: SymbolTable => def unapply(templ: Template): Option[(List[Tree], ValDef, Modifiers, List[List[ValDef]], List[Tree], List[Tree])] = { val Template(parents, selfType, _) = templ val tbody = treeInfo.untypecheckedTemplBody(templ) - + def result(ctorMods: Modifiers, vparamss: List[List[ValDef]], edefs: List[Tree], body: List[Tree]) = Some((parents, selfType, ctorMods, vparamss, edefs, body)) def indexOfCtor(trees: List[Tree]) = @@ -448,28 +452,25 @@ trait ReificationSupport { self: SymbolTable => * block as a list of elements rather than (stats, expr) pair * it also: * - * 1. Treats of q"" (empty tree) as zero-element block. - * - * 2. Strips trailing synthetic units which are inserted by the + * 1. Strips trailing synthetic units which are inserted by the * compiler if the block ends with a definition rather - * than an expression. + * than an expression or is empty. * - * 3. Matches non-block term trees and recognizes them as + * 2. Matches non-block term trees and recognizes them as * single-element blocks for sake of consistency with * compiler's default to treat single-element blocks with - * expressions as just expressions. + * expressions as just expressions. The only exception is q"" + * which is not considered to be a block. */ object SyntacticBlock extends SyntacticBlockExtractor { - def apply(stats: List[Tree]): Tree = - if (stats.isEmpty) EmptyTree - else gen.mkBlock(stats) + def apply(stats: List[Tree]): Tree = gen.mkBlock(stats) def unapply(tree: Tree): Option[List[Tree]] = tree match { case bl @ self.Block(stats, SyntheticUnit()) => Some(treeInfo.untypecheckedBlockBody(bl)) case bl @ self.Block(stats, expr) => Some(treeInfo.untypecheckedBlockBody(bl) :+ expr) - case EmptyTree => Some(Nil) - case _ if tree.isTerm => Some(tree :: Nil) - case _ => None + case SyntheticUnit() => Some(Nil) + case _ if tree.isTerm && tree.nonEmpty => Some(tree :: Nil) + case _ => None } } diff --git a/src/reflect/scala/reflect/internal/TreeGen.scala b/src/reflect/scala/reflect/internal/TreeGen.scala index 6011289baf..9066c73393 100644 --- a/src/reflect/scala/reflect/internal/TreeGen.scala +++ b/src/reflect/scala/reflect/internal/TreeGen.scala @@ -452,7 +452,7 @@ abstract class TreeGen { /** Create block of statements `stats` */ def mkBlock(stats: List[Tree]): Tree = - if (stats.isEmpty) Literal(Constant(())) + if (stats.isEmpty) mkSyntheticUnit() else if (!stats.last.isTerm) Block(stats, mkSyntheticUnit()) else if (stats.length == 1) stats.head else Block(stats.init, stats.last) |