Skip to content

Commit e7d4857

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 82022b8 commit e7d4857

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
@@ -24,6 +24,7 @@ import dotty.tools.dotc.util.Spans._
2424
import dotty.tools.dotc.util.Stats.record
2525
import dotty.tools.dotc.reporting.IllegalVariableInPatternAlternative
2626
import scala.collection.mutable
27+
import scala.collection.collection.SeqMap
2728

2829

2930
/** Type quotes `'{ ... }` and splices `${ ... }` */
@@ -190,7 +191,7 @@ trait QuotesAndSplices {
190191
* will return
191192
* ```
192193
* (
193-
* Map(<t$giveni>: Symbol -> <t @ _>: Bind),
194+
* Map(<t$giveni>: Symbol -> <t>: Symbol),
194195
* <'{
195196
* @scala.internal.Quoted.patternType type t
196197
* scala.internal.Quoted.patternHole[List[t]]
@@ -199,10 +200,10 @@ trait QuotesAndSplices {
199200
* )
200201
* ```
201202
*/
202-
private def splitQuotePattern(quoted: Tree)(using Context): (collection.Map[Symbol, Bind], Tree, List[Tree]) = {
203+
private def splitQuotePattern(quoted: Tree)(using Context): (SeqMap[Symbol, Symbol], Tree, List[Tree]) = {
203204
val ctx0 = ctx
204205

205-
val bindSymMapping: collection.Map[Symbol, Bind] = unapplyBindingsMapping(quoted)
206+
val bindSymMapping: SeqMap[Symbol, Symbol] = unapplyBindingsMapping(quoted)
206207

207208
object splitter extends tpd.TreeMap {
208209
private var variance: Int = 1
@@ -282,7 +283,7 @@ trait QuotesAndSplices {
282283
report.error(IllegalVariableInPatternAlternative(tdef.symbol.name), tdef.srcPos)
283284
if variance == -1 then
284285
tdef.symbol.addAnnotation(Annotation(New(ref(defn.QuotedRuntimePatterns_fromAboveAnnot.typeRef)).withSpan(tdef.span)))
285-
val bindingType = bindSymMapping(tdef.symbol).symbol.typeRef
286+
val bindingType = bindSymMapping(tdef.symbol).typeRef
286287
val bindingTypeTpe = AppliedType(defn.QuotedTypeClass.typeRef, bindingType :: Nil)
287288
val sym = newPatternBoundSymbol(nameOfSyntheticGiven, bindingTypeTpe, tdef.span, flags = ImplicitVal)(using ctx0)
288289
buff += Bind(sym, untpd.Ident(nme.WILDCARD).withType(bindingTypeTpe)).withSpan(tdef.span)
@@ -326,10 +327,10 @@ trait QuotesAndSplices {
326327
* binding that will be as type variable in the encoded `unapply` of the quote pattern.
327328
*
328329
* @return Mapping from type variable symbols defined in the quote pattern into
329-
* type variable `Bind` definitions for the `unapply` of the quote pattern.
330+
* type variable definitions for the `unapply` of the quote pattern.
330331
* This mapping retains the original type variable definition order.
331332
*/
332-
private def unapplyBindingsMapping(quoted: Tree)(using Context): collection.Map[Symbol, Bind] = {
333+
private def unapplyBindingsMapping(quoted: Tree)(using Context): SeqMap[Symbol, Symbol] = {
333334
// Collect all existing type variable bindings and create new symbols for them.
334335
// The old info is used, it may contain references to the old symbols.
335336
val (oldBindings, newBindings) = {
@@ -367,8 +368,7 @@ trait QuotesAndSplices {
367368
ctx.gadtState.addToConstraint(newBindings) // This must be performed after the info has been updated
368369

369370
// Map into Bind nodes retaining the original order
370-
val newBindingBinds = newBindings.map(newSym => Bind(newSym, untpd.Ident(nme.WILDCARD).withType(newSym.info)).withSpan(quoted.span))
371-
mutable.LinkedHashMap.from(oldBindings.lazyZip(newBindingBinds))
371+
mutable.LinkedHashMap.from(oldBindings.lazyZip(newBindings))
372372
}
373373

374374
/** Type a quote pattern `case '{ <quoted> } =>` qiven the a current prototype. Typing the pattern
@@ -434,20 +434,23 @@ trait QuotesAndSplices {
434434
if quoted.isType then typedType(quoted0, WildcardType)(using quoteCtx)
435435
else typedExpr(quoted0, WildcardType)(using quoteCtx)
436436

437-
val (typeBindings, shape, splices) = splitQuotePattern(quoted1)
437+
val (bindSymMapping, shape, splices) = splitQuotePattern(quoted1)
438438

439439
class ReplaceBindings extends TypeMap() {
440440
override def apply(tp: Type): Type = tp match {
441441
case tp: TypeRef =>
442442
val tp1 = if (tp.symbol.isTypeSplice) tp.dealias else tp
443-
mapOver(typeBindings.get(tp1.typeSymbol).fold(tp)(_.symbol.typeRef))
443+
mapOver(bindSymMapping.get(tp1.typeSymbol).fold(tp)(_.typeRef))
444444
case tp => mapOver(tp)
445445
}
446446
}
447447
val replaceBindings = new ReplaceBindings
448448
val patType = defn.tupleType(splices.tpes.map(tpe => replaceBindings(tpe.widen)))
449449

450-
val typeBindingsTuple = tpd.hkNestedPairsTypeTree(typeBindings.values.toList)
450+
val typeBinds = bindSymMapping.values.toList.map(sym =>
451+
Bind(sym, untpd.Ident(nme.WILDCARD).withType(sym.info)).withSpan(quoted.span)
452+
)
453+
val typeBindingsTuple = tpd.hkNestedPairsTypeTree(typeBinds)
451454

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

0 commit comments

Comments
 (0)