Skip to content

Commit 29de68c

Browse files
Move code to constructors while preserving its source information
1 parent fd70613 commit 29de68c

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,10 @@ class Inliner(val call: tpd.Tree)(using Context):
583583
val inlinedCtx = ctx.withSource(inlinedMethod.topLevelClass.source)
584584
paramProxy.get(tree.tpe) match {
585585
case Some(t) if tree.isTerm && t.isSingleton =>
586-
val inlinedSingleton = singleton(t).withSpan(tree.span)
586+
val inlinedSingleton = singleton(t).withSpan(argSpan)
587587
inlinedFromOutside(inlinedSingleton)(tree.span)
588588
case Some(t) if tree.isType =>
589-
inlinedFromOutside(TypeTree(t).withSpan(tree.span))(tree.span)
589+
inlinedFromOutside(TypeTree(t).withSpan(argSpan))(tree.span)
590590
case _ => tree
591591
}
592592
case tree @ Select(qual: This, name) if tree.symbol.is(Private) && tree.symbol.isInlineMethod =>

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -150,32 +150,34 @@ class Constructors extends MiniPhase with IdentityDenotTransformer { thisPhase =
150150
// drop the () when replacing by the parameter.
151151
object intoConstr extends TreeMap {
152152
private var inSuperCall = false
153-
override def transform(tree: Tree)(using Context): Tree = tree match {
154-
case Ident(_) | Select(This(_), _) =>
155-
var sym = tree.symbol
156-
def isOverridableSelect = tree.isInstanceOf[Select] && !sym.isEffectivelyFinal
157-
def switchOutsideSupercall = !sym.is(Mutable) && !isOverridableSelect
158-
// If true, switch to constructor parameters also in the constructor body
159-
// that follows the super call.
160-
// Variables need to go through the getter since they might have been updated.
161-
// References via this need to use the getter as well as long as that getter
162-
// can be overridden. This is needed to handle overrides correctly. See run/i15723.scala.
163-
// Note that in a supercall we need to switch to parameters in any case since then
164-
// calling the virtual getter call would be illegal.
165-
//
166-
// Note: We intentionally treat references via this and identifiers differently
167-
// here. Identifiers in a constructor always bind to the parameter. This is
168-
// done for backwards compatbility.
169-
if sym.is(ParamAccessor) && (switchOutsideSupercall || inSuperCall) then
170-
sym = sym.subst(accessors, paramSyms)
171-
if sym.maybeOwner.isConstructor then ref(sym).withSpan(tree.span) else tree
172-
case Apply(fn, Nil) =>
173-
val fn1 = transform(fn)
174-
if ((fn1 ne fn) && fn1.symbol.is(Param) && fn1.symbol.owner.isPrimaryConstructor)
175-
fn1 // in this case, fn1.symbol was an alias for a parameter in a superclass
176-
else cpy.Apply(tree)(fn1, Nil)
177-
case _ =>
178-
if (noDirectRefsFrom(tree)) tree else super.transform(tree)
153+
override def transform(tree: Tree)(using Context): Tree = inContext(transformCtx(tree)) {
154+
tree match {
155+
case Ident(_) | Select(This(_), _) =>
156+
var sym = tree.symbol
157+
def isOverridableSelect = tree.isInstanceOf[Select] && !sym.isEffectivelyFinal
158+
def switchOutsideSupercall = !sym.is(Mutable) && !isOverridableSelect
159+
// If true, switch to constructor parameters also in the constructor body
160+
// that follows the super call.
161+
// Variables need to go through the getter since they might have been updated.
162+
// References via this need to use the getter as well as long as that getter
163+
// can be overridden. This is needed to handle overrides correctly. See run/i15723.scala.
164+
// Note that in a supercall we need to switch to parameters in any case since then
165+
// calling the virtual getter call would be illegal.
166+
//
167+
// Note: We intentionally treat references via this and identifiers differently
168+
// here. Identifiers in a constructor always bind to the parameter. This is
169+
// done for backwards compatbility.
170+
if sym.is(ParamAccessor) && (switchOutsideSupercall || inSuperCall) then
171+
sym = sym.subst(accessors, paramSyms)
172+
if sym.maybeOwner.isConstructor then ref(sym).withSpan(tree.span) else tree
173+
case Apply(fn, Nil) =>
174+
val fn1 = transform(fn)
175+
if ((fn1 ne fn) && fn1.symbol.is(Param) && fn1.symbol.owner.isPrimaryConstructor)
176+
fn1 // in this case, fn1.symbol was an alias for a parameter in a superclass
177+
else cpy.Apply(tree)(fn1, Nil)
178+
case _ =>
179+
if (noDirectRefsFrom(tree)) tree else super.transform(tree)
180+
}
179181
}
180182

181183
def apply(tree: Tree, prevOwner: Symbol)(using Context): Tree =

0 commit comments

Comments
 (0)