Skip to content

Commit 829f9eb

Browse files
Call super before matching in Tree-/TypeMap
Override some properties of Inliner that hinder the work of the inline trait inliner. Some of the work done in the Inliner works only for method calls, not for bodies
1 parent 5570b25 commit 829f9eb

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

compiler/src/dotty/tools/dotc/inlines/Inliner.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ object Inliner:
115115
oldOwners: List[Symbol],
116116
newOwners: List[Symbol],
117117
substFrom: List[Symbol],
118-
substTo: List[Symbol])(using Context)
118+
substTo: List[Symbol],
119+
val inlineCopier: TreeCopier)(using Context)
119120
extends TreeTypeMap(
120-
typeMap, treeMap, oldOwners, newOwners, substFrom, substTo, InlineCopier()):
121+
typeMap, treeMap, oldOwners, newOwners, substFrom, substTo, inlineCopier):
121122

122123
override def copy(
123124
typeMap: Type => Type,
@@ -126,7 +127,7 @@ object Inliner:
126127
newOwners: List[Symbol],
127128
substFrom: List[Symbol],
128129
substTo: List[Symbol])(using Context) =
129-
new InlinerMap(typeMap, treeMap, oldOwners, newOwners, substFrom, substTo)
130+
new InlinerMap(typeMap, treeMap, oldOwners, newOwners, substFrom, substTo, inlineCopier)
130131

131132
override def transformInlined(tree: Inlined)(using Context) =
132133
if tree.call.isEmpty then
@@ -565,6 +566,7 @@ class Inliner(val call: tpd.Tree)(using Context):
565566

566567
protected def substFrom: List[Symbol] = Nil
567568
protected def substTo: List[Symbol] = Nil
569+
protected def inlineCopier: TreeCopier = InlineCopier()
568570

569571
protected def inlineCtx(inlineTyper: InlineTyper)(using Context): Context =
570572
inlineContext(call).fresh.setTyper(inlineTyper).setNewScope
@@ -624,7 +626,8 @@ class Inliner(val call: tpd.Tree)(using Context):
624626
oldOwners = inlinedMethod :: Nil,
625627
newOwners = ctx.owner :: Nil,
626628
substFrom = substFrom,
627-
substTo = substTo
629+
substTo = substTo,
630+
inlineCopier = inlineCopier
628631
)(using inlineCtx)
629632

630633
inlining.println(

compiler/src/dotty/tools/dotc/inlines/Inlines.scala

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -533,29 +533,32 @@ object Inlines:
533533
end expandDefs
534534

535535
protected class InlineTraitTypeMap extends InlinerTypeMap {
536-
override def apply(t: Type) = t match {
537-
case t: ThisType if t.cls == parentSym => ctx.owner.thisType
538-
case t => super.apply(t)
536+
override def apply(t: Type) = super.apply(t) match {
537+
case t: ThisType if t.cls == parentSym =>
538+
ctx.owner.thisType
539+
case t =>
540+
mapOver(t)
539541
}
540542
}
541543

542544
protected class InlineTraitTreeMap extends InlinerTreeMap {
543-
override def apply(tree: Tree) = tree match {
544-
case tree: This if tree.qual.name == parentSym.name =>
545-
Inlined(EmptyTree, Nil, This(ctx.owner.asClass).withSpan(parent.span)).withSpan(tree.span)
545+
override def apply(tree: Tree) = super.apply(tree) match {
546+
case tree: This if tree.symbol == parentSym =>
547+
Inlined(EmptyTree, Nil, This(ctx.owner.asClass).withSpan(parent.span)).withSpan(parent.span)
546548
case tree: This =>
547549
tree.tpe match {
548550
case thisTpe: ThisType if thisTpe.cls.isInlineTrait =>
549-
integrate(This(ctx.owner.asClass).withSpan(call.span), thisTpe.cls)
550-
case _ => super.apply(tree)
551+
integrate(This(ctx.owner.asClass).withSpan(parent.span), thisTpe.cls)
552+
case _ =>
553+
tree
551554
}
552555
case Select(qual, name) =>
553556
paramAccessorsMapper.getParamAccessorName(qual.symbol, name) match {
554-
case Some(newName) => Select(apply(qual), newName).withSpan(tree.span)
555-
case None => super.apply(tree)
557+
case Some(newName) => Select(this(qual), newName).withSpan(parent.span)
558+
case None => Select(this(qual), name)
556559
}
557560
case tree =>
558-
super.apply(tree)
561+
tree
559562
}
560563
}
561564

@@ -564,6 +567,11 @@ object Inlines:
564567

565568
override protected def substFrom: List[Symbol] = innerClassNewSyms.keys.toList
566569
override protected def substTo: List[Symbol] = innerClassNewSyms.values.toList
570+
override protected def inlineCopier: tpd.TreeCopier = new TypedTreeCopier() {
571+
// FIXME it feels weird... Is this correct?
572+
override def Apply(tree: Tree)(fun: Tree, args: List[Tree])(using Context): Apply =
573+
untpd.cpy.Apply(tree)(fun, args).withTypeUnchecked(tree.tpe)
574+
}
567575

568576
override protected def computeThisBindings(): Unit = ()
569577
override protected def canElideThis(tpe: ThisType): Boolean = true

0 commit comments

Comments
 (0)