Skip to content

Commit 664b8b6

Browse files
Don't generate members if they are overridden by user
1 parent a67ee07 commit 664b8b6

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ object Inlines:
170170
tree2
171171
end inlineCall
172172

173-
def inlineParentTrait(parent: tpd.Tree)(using Context): List[Tree] =
173+
def inlineParentTrait(parent: tpd.Tree, childOverrideDecls: Set[Symbol])(using Context): List[Tree] =
174174
val traitSym = if parent.symbol.isConstructor then parent.symbol.owner else parent.symbol
175-
if traitSym.isInlineTrait then InlineParentTrait(parent, traitSym).expandDefs()
175+
if traitSym.isInlineTrait then InlineParentTrait(parent, traitSym, childOverrideDecls).expandDefs()
176176
else Nil
177177

178178
/** Try to inline a pattern with an inline unapply method. Fail with error if the maximal
@@ -466,14 +466,15 @@ object Inlines:
466466
end expand
467467
end InlineCall
468468

469-
private class InlineParentTrait(parent: tpd.Tree, parentSym: Symbol)(using Context) extends Inliner(parent):
469+
private class InlineParentTrait(parent: tpd.Tree, parentSym: Symbol, overriddenDecls: Set[Symbol])(using Context) extends Inliner(parent):
470470
import tpd._
471471
import Inlines.*
472472

473473
def expandDefs(): List[Tree] =
474474
val tpd.Block(stats, _) = Inlines.bodyToInline(parentSym): @unchecked
475-
val inlinedSymbols = stats.map(stat => inlinedMember(stat.symbol))
476-
stats.zip(inlinedSymbols).map(expandStat)//.map(inlined(_)._2)
475+
val stats1 = stats.filterNot(isMemberOverridden)
476+
val inlinedSymbols = stats1.map(stat => inlinedMember(stat.symbol))
477+
stats1.zip(inlinedSymbols).map(expandStat)//.map(inlined(_)._2)
477478
end expandDefs
478479

479480
private val argsMap: Map[Name, Tree] =
@@ -498,6 +499,9 @@ object Inlines:
498499
case t => mapOver(t)
499500
}
500501

502+
private def isMemberOverridden(stat: Tree): Boolean =
503+
overriddenDecls.flatMap(_.allOverriddenSymbols).toSet.contains(stat.symbol)
504+
501505
private def expandStat(stat: tpd.Tree, inlinedSym: Symbol): tpd.Tree = stat match
502506
case stat: ValDef if stat.symbol.is(ParamAccessor) =>
503507
tpd.ValDef(inlinedSym.asTerm, argsMap(inlinedSym.name.asTermName)).withSpan(parent.span)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,12 +2654,16 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
26542654
else {
26552655
val dummy = localDummy(cls, impl)
26562656
val inlineTraitDefs =
2657-
if ctx.isAfterTyper then Nil
2658-
else parents1.flatMap(Inlines.inlineParentTrait)
2657+
if ctx.isAfterTyper then
2658+
Nil
2659+
else
2660+
val clsDecls = cls.info.decls.toList.toSet
2661+
parents1.flatMap(parent => Inlines.inlineParentTrait(parent, clsDecls))
26592662
val body1 = addAccessorDefs(cls, typedStats(impl.body, dummy)(using ctx.inClassContext(self1.symbol))._1) ::: inlineTraitDefs
26602663

26612664
if !ctx.isAfterTyper && cls.isInlineTrait then
26622665
def isConstructorType(t: Tree) = t.isInstanceOf[TypeDef] && cls.typeParams.contains(t.symbol)
2666+
// If the following is changed, remember to adapt TreeUnpickler.scala as well
26632667
val membersToInline = body1.filter { t =>
26642668
!isConstructorType(t)
26652669
&& !t.symbol.is(Deferred)

0 commit comments

Comments
 (0)