Skip to content

Commit a096e75

Browse files
committed
Refactor split quote symbol mapping
Now, the mapping is a `SeqMap[Symbol, Symbol]` to convey its ordering and the `Bind` is created when we are creating the tree of the quote pattern.
1 parent 3fb0f98 commit a096e75

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import dotty.tools.dotc.util.Spans._
2525
import dotty.tools.dotc.util.Stats.record
2626
import dotty.tools.dotc.reporting.IllegalVariableInPatternAlternative
2727
import scala.collection.mutable
28+
import scala.collection.collection.SeqMap
2829

2930
/** Type quotes `'{ ... }` and splices `${ ... }` */
3031
trait QuotesAndSplices {
@@ -202,7 +203,7 @@ trait QuotesAndSplices {
202203
* will return
203204
* ```
204205
* (
205-
* Map(<t$giveni>: Symbol -> <t @ _>: Bind),
206+
* Map(<t$giveni>: Symbol -> <t>: Symbol),
206207
* <'{
207208
* @scala.internal.Quoted.patternType type t
208209
* scala.internal.Quoted.patternHole[List[t]]
@@ -211,10 +212,10 @@ trait QuotesAndSplices {
211212
* )
212213
* ```
213214
*/
214-
private def splitQuotePattern(quoted: Tree)(using Context): (collection.Map[Symbol, Bind], Tree, List[Tree]) = {
215+
private def splitQuotePattern(quoted: Tree)(using Context): (SeqMap[Symbol, Symbol], Tree, List[Tree]) = {
215216
val ctx0 = ctx
216217

217-
val bindSymMapping: collection.Map[Symbol, Bind] = unapplyBindingsMapping(quoted)
218+
val bindSymMapping: SeqMap[Symbol, Symbol] = unapplyBindingsMapping(quoted)
218219

219220
object splitter extends tpd.TreeMap {
220221
private var variance: Int = 1
@@ -294,7 +295,7 @@ trait QuotesAndSplices {
294295
report.error(IllegalVariableInPatternAlternative(tdef.symbol.name), tdef.srcPos)
295296
if variance == -1 then
296297
tdef.symbol.addAnnotation(Annotation(New(ref(defn.QuotedRuntimePatterns_fromAboveAnnot.typeRef)).withSpan(tdef.span)))
297-
val bindingType = bindSymMapping(tdef.symbol).symbol.typeRef
298+
val bindingType = bindSymMapping(tdef.symbol).typeRef
298299
val bindingTypeTpe = AppliedType(defn.QuotedTypeClass.typeRef, bindingType :: Nil)
299300
val sym = newPatternBoundSymbol(nameOfSyntheticGiven, bindingTypeTpe, tdef.span, flags = ImplicitVal)(using ctx0)
300301
buff += Bind(sym, untpd.Ident(nme.WILDCARD).withType(bindingTypeTpe)).withSpan(tdef.span)
@@ -338,10 +339,10 @@ trait QuotesAndSplices {
338339
* binding that will be as type variable in the encoded `unapply` of the quote pattern.
339340
*
340341
* @return Mapping from type variable symbols defined in the quote pattern into
341-
* type variable `Bind` definitions for the `unapply` of the quote pattern.
342+
* type variable definitions for the `unapply` of the quote pattern.
342343
* This mapping retains the original type variable definition order.
343344
*/
344-
private def unapplyBindingsMapping(quoted: Tree)(using Context): collection.Map[Symbol, Bind] = {
345+
private def unapplyBindingsMapping(quoted: Tree)(using Context): SeqMap[Symbol, Symbol] = {
345346
// Collect all existing type variable bindings and create new symbols for them.
346347
// The old info is used, it may contain references to the old symbols.
347348
val (oldBindings, newBindings) = {
@@ -379,8 +380,7 @@ trait QuotesAndSplices {
379380
ctx.gadtState.addToConstraint(newBindings) // This must be performed after the info has been updated
380381

381382
// Map into Bind nodes retaining the original order
382-
val newBindingBinds = newBindings.map(newSym => Bind(newSym, untpd.Ident(nme.WILDCARD).withType(newSym.info)).withSpan(quoted.span))
383-
mutable.LinkedHashMap.from(oldBindings.lazyZip(newBindingBinds))
383+
mutable.LinkedHashMap.from(oldBindings.lazyZip(newBindings))
384384
}
385385

386386
/** Type a quote pattern `case '{ <quoted> } =>` qiven the a current prototype. Typing the pattern
@@ -470,20 +470,23 @@ trait QuotesAndSplices {
470470
else tpd.Block(typeTypeVariables, pattern)
471471
}
472472

473-
val (typeBindings, shape, splices) = splitQuotePattern(quoted1)
473+
val (bindSymMapping, shape, splices) = splitQuotePattern(quoted1)
474474

475475
class ReplaceBindings extends TypeMap() {
476476
override def apply(tp: Type): Type = tp match {
477477
case tp: TypeRef =>
478478
val tp1 = if (tp.symbol.isTypeSplice) tp.dealias else tp
479-
mapOver(typeBindings.get(tp1.typeSymbol).fold(tp)(_.symbol.typeRef))
479+
mapOver(bindSymMapping.get(tp1.typeSymbol).fold(tp)(_.typeRef))
480480
case tp => mapOver(tp)
481481
}
482482
}
483483
val replaceBindings = new ReplaceBindings
484484
val patType = defn.tupleType(splices.tpes.map(tpe => replaceBindings(tpe.widen)))
485485

486-
val typeBindingsTuple = tpd.hkNestedPairsTypeTree(typeBindings.values.toList)
486+
val typeBinds = bindSymMapping.values.toList.map(sym =>
487+
Bind(sym, untpd.Ident(nme.WILDCARD).withType(sym.info)).withSpan(quoted.span)
488+
)
489+
val typeBindingsTuple = tpd.hkNestedPairsTypeTree(typeBinds)
487490

488491
val replaceBindingsInTree = new TreeMap {
489492
private var bindMap = Map.empty[Symbol, Symbol]

0 commit comments

Comments
 (0)