diff --git a/src/compiler/scala/tools/reflect/quasiquotes/Placeholders.scala b/src/compiler/scala/tools/reflect/quasiquotes/Placeholders.scala index 5669ec731f07..825d0c04f3d3 100644 --- a/src/compiler/scala/tools/reflect/quasiquotes/Placeholders.scala +++ b/src/compiler/scala/tools/reflect/quasiquotes/Placeholders.scala @@ -17,7 +17,6 @@ trait Placeholders { self: Quasiquotes => // Step 1: Transform Scala source with holes into vanilla Scala source - lazy val holeMap = new HoleMap() lazy val posMap = mutable.ListMap[Position, (Int, Int)]() lazy val code = { val sb = new StringBuilder() @@ -58,25 +57,27 @@ trait Placeholders { self: Quasiquotes => sb.toString } - class HoleMap { - private var underlying = immutable.SortedMap[String, Hole]() - private val accessed = mutable.Set[String]() + object holeMap { + private val underlying = mutable.LinkedHashMap.empty[String, Hole] + private val accessed = mutable.Set.empty[String] def unused: Set[Name] = (underlying.keys.toSet -- accessed).map(TermName(_)) - def contains(key: Name) = underlying.contains(key.toString) - def apply(key: Name) = { - val s = key.toString - accessed += s - underlying(s) - } - def update(key: Name, hole: Hole) = { + def contains(key: Name): Boolean = underlying.contains(key.toString) + def apply(key: Name): Hole = { + val skey = key.toString + val value = underlying(skey) + accessed += skey + value + } + def update(key: Name, hole: Hole) = underlying += key.toString -> hole + def get(key: Name): Option[Hole] = { + val skey = key.toString + underlying.get(skey).map { v => + accessed += skey + v + } } - def get(key: Name) = { - val s = key.toString - accessed += s - underlying.get(s) - } - def toList = underlying.toList + def keysIterator: Iterator[TermName] = underlying.keysIterator.map(TermName(_)) } // Step 2: Transform vanilla Scala AST into an AST with holes @@ -179,4 +180,4 @@ trait Placeholders { self: Quasiquotes => case _ => None } } -} \ No newline at end of file +} diff --git a/src/compiler/scala/tools/reflect/quasiquotes/Reifiers.scala b/src/compiler/scala/tools/reflect/quasiquotes/Reifiers.scala index 273245f7bd4f..aefd6132e0f4 100644 --- a/src/compiler/scala/tools/reflect/quasiquotes/Reifiers.scala +++ b/src/compiler/scala/tools/reflect/quasiquotes/Reifiers.scala @@ -29,7 +29,7 @@ trait Reifiers { self: Quasiquotes => /** Map that stores freshly generated names linked to the corresponding names in the reified tree. * This information is used to reify names created by calls to freshTermName and freshTypeName. */ - var nameMap = collection.mutable.HashMap.empty[Name, Set[TermName]].withDefault { _ => Set() } + val nameMap = collection.mutable.HashMap.empty[Name, Set[TermName]].withDefault { _ => Set() } /** Wraps expressions into: * a block which starts with a sequence of vals that correspond @@ -71,7 +71,7 @@ trait Reifiers { self: Quasiquotes => // q"..$freshdefs; $tree" SyntacticBlock(freshdefs :+ tree) } else { - val freevars = holeMap.toList.map { case (name, _) => Ident(name) } + val freevars = holeMap.keysIterator.map(Ident(_)).toList val isVarPattern = tree match { case Bind(name, Ident(nme.WILDCARD)) => true case _ => false } val cases = if(isVarPattern) { @@ -162,7 +162,7 @@ trait Reifiers { self: Quasiquotes => reifyBuildCall(nme.SyntacticNew, earlyDefs, parents, selfdef, body) case SyntacticDefDef(mods, name, tparams, build.ImplicitParams(vparamss, implparams), tpt, rhs) => if (implparams.nonEmpty) - mirrorBuildCall(nme.SyntacticDefDef, reify(mods), reify(name), reify(tparams), + mirrorBuildCall(nme.SyntacticDefDef, reify(mods), reify(name), reify(tparams), reifyBuildCall(nme.ImplicitParams, vparamss, implparams), reify(tpt), reify(rhs)) else reifyBuildCall(nme.SyntacticDefDef, mods, name, tparams, vparamss, tpt, rhs) diff --git a/test/files/scalacheck/quasiquotes/TermConstructionProps.scala b/test/files/scalacheck/quasiquotes/TermConstructionProps.scala index 54187d68c216..145e51ab6883 100644 --- a/test/files/scalacheck/quasiquotes/TermConstructionProps.scala +++ b/test/files/scalacheck/quasiquotes/TermConstructionProps.scala @@ -291,4 +291,9 @@ object TermConstructionProps extends QuasiquoteProperties("term construction") { val stats2 = List.empty[Tree] assert(q"{ ..$stats2 }" ≈ q"") } + + property("consistent variable order") = test { + val q"$a = $b = $c = $d = $e = $f = $g = $h = $k = $l" = q"a = b = c = d = e = f = g = h = k = l" + assert(a ≈ q"a" && b ≈ q"b" && c ≈ q"c" && d ≈ q"d" && e ≈ q"e" && g ≈ q"g" && h ≈ q"h" && k ≈ q"k" && l ≈ q"l") + } }