Skip to content

Commit 304df79

Browse files
committed
Make sure captured types are listed before terms
1 parent 6e5be23 commit 304df79

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

compiler/src/dotty/tools/dotc/transform/Splicing.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,19 @@ class Splicing extends MacroTransform:
189189
* ```
190190
*/
191191
private class SpliceTransformer(spliceOwner: Symbol, isCaptured: Symbol => Boolean) extends Transformer:
192-
private var refBindingMap = mutable.Map.empty[Symbol, (Tree, Symbol)]
192+
private val typeBindingMap = mutable.Map.empty[Symbol, (Tree, Symbol)]
193+
private val termBindingMap = mutable.Map.empty[Symbol, (Tree, Symbol)]
193194
/** Reference to the `Quotes` instance of the current level 1 splice */
194195
private var quotes: Tree | Null = null // TODO: add to the context
195196
private var healedTypes: QuoteTypeTags | Null = null // TODO: add to the context
196197

197198
def transformSplice(tree: tpd.Tree, tpe: Type, holeIdx: Int)(using Context): tpd.Tree =
198199
assert(level == 0)
199200
val newTree = transform(tree)
200-
val (refs, bindings) = refBindingMap.values.toList.unzip
201+
val (typeRefs, typeBindings) = typeBindingMap.values.toList.unzip
202+
val (termRefs, termBindings) = termBindingMap.values.toList.unzip
203+
val refs = typeRefs ::: termRefs
204+
val bindings = typeBindings ::: termBindings
201205
val bindingsTypes = bindings.map(_.termRef.widenTermRefExpr)
202206
val methType = MethodType(bindingsTypes, newTree.tpe)
203207
val meth = newSymbol(spliceOwner, nme.ANON_FUN, Synthetic | Method, methType)
@@ -341,7 +345,7 @@ class Splicing extends MacroTransform:
341345
Param,
342346
defn.QuotedExprClass.typeRef.appliedTo(tpe),
343347
)
344-
val bindingSym = refBindingMap.getOrElseUpdate(tree.symbol, (tree, newBinding))._2
348+
val bindingSym = termBindingMap.getOrElseUpdate(tree.symbol, (tree, newBinding))._2
345349
ref(bindingSym)
346350

347351
private def newQuotedTypeClassBinding(tpe: Type)(using Context) =
@@ -354,7 +358,7 @@ class Splicing extends MacroTransform:
354358

355359
private def capturedType(tree: Tree)(using Context): Symbol =
356360
val tpe = tree.tpe.widenTermRefExpr
357-
val bindingSym = refBindingMap
361+
val bindingSym = typeBindingMap
358362
.getOrElseUpdate(tree.symbol, (TypeTree(tree.tpe), newQuotedTypeClassBinding(tpe)))._2
359363
bindingSym
360364

@@ -364,7 +368,7 @@ class Splicing extends MacroTransform:
364368
val capturePartTypes = new TypeMap {
365369
def apply(tp: Type) = tp match {
366370
case typeRef: TypeRef if containsCapturedType(typeRef) =>
367-
val termRef = refBindingMap
371+
val termRef = typeBindingMap
368372
.getOrElseUpdate(typeRef.symbol, (TypeTree(typeRef), newQuotedTypeClassBinding(typeRef)))._2.termRef
369373
val tagRef = healedTypes.nn.getTagRef(termRef)
370374
tagRef

0 commit comments

Comments
 (0)