Skip to content

Commit 46cea60

Browse files
Fix owner of bodies to their rightful symbols
When inlining the RHS of definitions, their owner was set to be the class extending the inline trait This problem, however, only raised an error when using macros in the inline trait (for some reason...?) This fix sets the owner of the RHSs to the symbol of their definition
1 parent 9e02a81 commit 46cea60

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,11 @@ class Inliner(val call: tpd.Tree)(using Context):
565565
protected val inlinerTypeMap: InlinerTypeMap = InlinerTypeMap()
566566
protected val inlinerTreeMap: InlinerTreeMap = InlinerTreeMap()
567567

568+
protected def inlineCtx(inlineTyper: InlineTyper)(using Context): Context =
569+
inlineContext(call).fresh.setTyper(inlineTyper).setNewScope
570+
568571
/** The Inlined node representing the inlined call */
569-
def inlined(rhsToInline: tpd.Tree): (List[MemberDef], Tree) =
572+
def inlined(rhsToInline: tpd.Tree)(using Context): (List[MemberDef], Tree) =
570573
inlining.println(i"-----------------------\nInlining $call\nWith RHS $rhsToInline")
571574

572575
def paramTypess(call: Tree, acc: List[List[Type]]): List[List[Type]] = call match
@@ -608,7 +611,7 @@ class Inliner(val call: tpd.Tree)(using Context):
608611

609612
val inlineTyper = new InlineTyper(ctx.reporter.errorCount)
610613

611-
val inlineCtx = inlineContext(call).fresh.setTyper(inlineTyper).setNewScope
614+
val inlineCtx = this.inlineCtx(inlineTyper)
612615

613616
// A tree type map to prepare the inlined body for typechecked.
614617
// The translation maps references to `this` and parameters to

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,9 @@ object Inlines:
550550
override protected val inlinerTypeMap: InlinerTypeMap = InlineTraitTypeMap()
551551
override protected val inlinerTreeMap: InlinerTreeMap = InlineTraitTreeMap()
552552

553+
override protected def inlineCtx(inlineTyper: InlineTyper)(using Context): Context =
554+
ctx.fresh.setTyper(inlineTyper).setNewScope
555+
553556
private val paramAccessorsValueOf: Map[Name, Tree] =
554557
def allArgs(tree: Tree, acc: Vector[List[Tree]]): List[List[Tree]] = tree match
555558
case Apply(fun, args) => allArgs(fun, acc :+ args)
@@ -620,7 +623,7 @@ object Inlines:
620623
if vdef.symbol.isTermParamAccessor then
621624
paramAccessorsValueOf(vdef.symbol.name)
622625
else
623-
inlinedRhs(vdef.rhs.changeOwner(vdef.symbol, inlinedSym))
626+
inlinedRhs(vdef, inlinedSym)
624627
tpd.ValDef(inlinedSym.asTerm, rhs).withSpan(parent.span)
625628

626629
private def inlinedDefDef(ddef: DefDef, inlinedSym: Symbol)(using Context): DefDef =
@@ -631,7 +634,8 @@ object Inlines:
631634
paramss =>
632635
val oldParamSyms = ddef.paramss.flatten.map(_.symbol)
633636
val newParamSyms = paramss.flatten.map(_.symbol)
634-
inlinedRhs(ddef.rhs.subst(oldParamSyms, newParamSyms).changeOwner(ddef.symbol, inlinedSym))
637+
val ddef1 = cpy.DefDef(ddef)(rhs = ddef.rhs.subst(oldParamSyms, newParamSyms))
638+
inlinedRhs(ddef1, inlinedSym)
635639
tpd.DefDef(inlinedSym.asTerm, rhsFun).withSpan(parent.span)
636640

637641
private def inlinedPrimaryConstructorDefDef(ddef: DefDef)(using Context): DefDef =
@@ -652,11 +656,12 @@ object Inlines:
652656
private def inlinedTypeDef(tdef: TypeDef, inlinedSym: Symbol)(using Context): TypeDef =
653657
tpd.TypeDef(inlinedSym.asType).withSpan(parent.span)
654658

655-
private def inlinedRhs(rhs: Tree): Tree =
659+
private def inlinedRhs(vddef: ValOrDefDef, inlinedSym: Symbol)(using Context): Tree =
660+
val rhs = vddef.rhs.changeOwner(vddef.symbol, inlinedSym)
656661
if rhs.isEmpty then
657662
rhs
658663
else
659-
val inlinedRhs = inlined(rhs)._2
664+
val inlinedRhs = inContext(ctx.withOwner(inlinedSym)) { inlined(rhs)._2 }
660665
Inlined(tpd.ref(parentSym), Nil, inlinedRhs).withSpan(parent.span)
661666

662667
end InlineParentTrait

0 commit comments

Comments
 (0)