Skip to content

Commit dbb9d5e

Browse files
nicolasstuckiKordyjan
authored andcommitted
Make quote pattern type variable order deterministic
This changes the order in which the type variables are listed in the encoded quote pattern. The order of the variables is the same as the order as they appear in the source. [Cherry-picked 2db2c11]
1 parent 82efa61 commit dbb9d5e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ trait QuotesAndSplices {
199199
* )
200200
* ```
201201
*/
202-
private def splitQuotePattern(quoted: Tree)(using Context): (Map[Symbol, Bind], Tree, List[Tree]) = {
202+
private def splitQuotePattern(quoted: Tree)(using Context): (collection.Map[Symbol, Bind], Tree, List[Tree]) = {
203203
val ctx0 = ctx
204204

205-
val typeBindings: collection.mutable.Map[Symbol, Bind] = collection.mutable.Map.empty
205+
val typeBindings: mutable.Map[Symbol, Bind] = mutable.LinkedHashMap.empty
206206
def getBinding(sym: Symbol): Bind =
207207
typeBindings.getOrElseUpdate(sym, {
208208
val bindingBounds = sym.info
@@ -296,13 +296,19 @@ trait QuotesAndSplices {
296296
}
297297
}
298298
val shape0 = splitter.transform(quoted)
299-
val patterns = (splitter.freshTypePatBuf.iterator ++ splitter.typePatBuf.iterator ++ splitter.patBuf.iterator).toList
299+
val patterns = (splitter.typePatBuf.iterator ++ splitter.freshTypePatBuf.iterator ++ splitter.patBuf.iterator).toList
300300
val freshTypeBindings = splitter.freshTypeBindingsBuff.result()
301301

302-
val shape1 = seq(
303-
freshTypeBindings,
304-
shape0
305-
)
302+
val shape1 = shape0 match
303+
case Block(stats @ ((tdef: TypeDef) :: rest), expr) if tdef.symbol.hasAnnotation(defn.QuotedRuntimePatterns_patternTypeAnnot) =>
304+
val (bindings, otherStats) = stats.span {
305+
case tdef: TypeDef => tdef.symbol.hasAnnotation(defn.QuotedRuntimePatterns_patternTypeAnnot)
306+
case _ => true
307+
}
308+
cpy.Block(shape0)(bindings ::: freshTypeBindings ::: otherStats, expr)
309+
case _ =>
310+
seq(freshTypeBindings, shape0)
311+
306312
val shape2 =
307313
if (freshTypeBindings.isEmpty) shape1
308314
else {
@@ -319,7 +325,7 @@ trait QuotesAndSplices {
319325
new TreeTypeMap(typeMap = typeMap).transform(shape1)
320326
}
321327

322-
(typeBindings.toMap, shape2, patterns)
328+
(typeBindings, shape2, patterns)
323329
}
324330

325331
/** Type a quote pattern `case '{ <quoted> } =>` qiven the a current prototype. Typing the pattern

0 commit comments

Comments
 (0)