Skip to content

Commit 87ffb88

Browse files
committed
Fixes to synchronize the position of trees and symbols
Various changes so that `testPickling` with `-Yprintpos-syms` now passes. It's not always obvious what position to use when new trees are created, here are the principles we used here: - When a definition tree is replaced by another definition tree (for example, in `ParamForwarding`), the new tree position is set to the old tree position. - When a reference tree is replaced by a definition tree representing an accessor and a new reference tree that calls the accessor (for example in `SuperAccessors`), the accessor position is set to the old tree *point*, and the new reference tree position is set to the old tree *position*. This makes sense because we already use zero-extent positions for compiler artifacts that the IDE should ignore and an accessor is a compilation artifact.
1 parent 1841b3d commit 87ffb88

File tree

7 files changed

+26
-19
lines changed

7 files changed

+26
-19
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
196196
case tp: MethodType =>
197197
def valueParam(name: TermName, info: Type): TermSymbol = {
198198
val maybeImplicit = if (tp.isImplicitMethod) Implicit else EmptyFlags
199-
ctx.newSymbol(sym, name, TermParam | maybeImplicit, info)
199+
ctx.newSymbol(sym, name, TermParam | maybeImplicit, info, coord = sym.coord)
200200
}
201201
val params = (tp.paramNames, tp.paramInfos).zipped.map(valueParam)
202202
val (paramss, rtp) = valueParamss(tp.instantiate(params map (_.termRef)))

compiler/src/dotty/tools/dotc/core/Symbols.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ trait Symbols { this: Context =>
288288
val tparamBuf = new mutable.ListBuffer[TypeSymbol]
289289
val trefBuf = new mutable.ListBuffer[TypeRef]
290290
for (name <- names) {
291-
val tparam = newNakedSymbol[TypeName](NoCoord)
291+
val tparam = newNakedSymbol[TypeName](owner.coord)
292292
tparamBuf += tparam
293293
trefBuf += TypeRef(owner.thisType, tparam)
294294
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ParamForwarding(thisPhase: DenotTransformer) {
7070
val superAcc =
7171
Super(This(currentClass), tpnme.EMPTY, inConstrCall = false).select(alias)
7272
typr.println(i"adding param forwarder $superAcc")
73-
DefDef(sym, superAcc.ensureConforms(sym.info.widen))
73+
DefDef(sym, superAcc.ensureConforms(sym.info.widen)).withPos(stat.pos)
7474
}
7575
return forwarder(ctx.withPhase(thisPhase.next))
7676
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,19 @@ class SuperAccessors(thisPhase: DenotTransformer) {
7676
if (clazz is Trait) superName = superName.expandedName(clazz)
7777
val superInfo = sel.tpe.widenSingleton.ensureMethodic
7878

79+
val accPos = sel.pos.focus
7980
val superAcc = clazz.info.decl(superName)
8081
.suchThat(_.signature == superInfo.signature).symbol
8182
.orElse {
8283
ctx.debuglog(s"add super acc ${sym.showLocated} to $clazz")
8384
val deferredOrPrivate = if (clazz is Trait) Deferred else Private
8485
val acc = ctx.newSymbol(
8586
clazz, superName, Artifact | Method | deferredOrPrivate,
86-
superInfo, coord = sym.coord).enteredAfter(thisPhase)
87+
superInfo, coord = accPos).enteredAfter(thisPhase)
8788
// Diagnostic for SI-7091
8889
if (!accDefs.contains(clazz))
8990
ctx.error(s"Internal error: unable to store accessor definition in ${clazz}. clazz.hasPackageFlag=${clazz is Package}. Accessor required for ${sel} (${sel.show})", sel.pos)
90-
else accDefs(clazz) += DefDef(acc, EmptyTree)
91+
else accDefs(clazz) += DefDef(acc, EmptyTree).withPos(accPos)
9192
acc
9293
}
9394

@@ -181,13 +182,14 @@ class SuperAccessors(thisPhase: DenotTransformer) {
181182
}
182183
accTypeOf(sym.info)
183184
}
185+
val accPos = sel.pos.focus
184186
val protectedAccessor = clazz.info.decl(accName).suchThat(_.signature == accType.signature).symbol orElse {
185187
val newAcc = ctx.newSymbol(
186-
clazz, accName, Artifact | Method, accType, coord = sel.pos).enteredAfter(thisPhase)
188+
clazz, accName, Artifact | Method, accType, coord = accPos).enteredAfter(thisPhase)
187189
val code = polyDefDef(newAcc, trefs => vrefss => {
188190
val (receiver :: _) :: tail = vrefss
189191
val base = receiver.select(sym).appliedToTypes(trefs)
190-
(base /: tail)(Apply(_, _))
192+
(base /: tail)(Apply(_, _)).withPos(accPos)
191193
})
192194
ctx.debuglog("created protected accessor: " + code)
193195
accDefs(clazz) += code
@@ -233,13 +235,14 @@ class SuperAccessors(thisPhase: DenotTransformer) {
233235
MethodType(receiverType :: Nil)(mt => tpe.substThis(sym.owner.asClass, mt.newParamRef(0)))
234236
}
235237
val accType = accTypeOf(sym.info)
238+
val accPos = tree.pos.focus
236239
val protectedAccessor = clazz.info.decl(accName).suchThat(_.signature == accType.signature).symbol orElse {
237240
val newAcc = ctx.newSymbol(
238-
clazz, accName, Artifact, accType, coord = tree.pos).enteredAfter(thisPhase)
241+
clazz, accName, Artifact, accType, coord = accPos).enteredAfter(thisPhase)
239242
val code = polyDefDef(newAcc, trefs => vrefss => {
240243
val (receiver :: _) :: tail = vrefss
241244
val base = receiver.select(sym).appliedToTypes(trefs)
242-
(base /: vrefss)(Apply(_, _))
245+
(base /: vrefss)(Apply(_, _)).withPos(accPos)
243246
})
244247
ctx.debuglog("created protected accessor: " + code)
245248
accDefs(clazz) += code
@@ -265,12 +268,13 @@ class SuperAccessors(thisPhase: DenotTransformer) {
265268

266269
val accName = ProtectedSetterName(field.name)
267270
val accType = MethodType(clazz.classInfo.selfType :: field.info :: Nil, defn.UnitType)
271+
val accPos = tree.pos.focus
268272
val protectedAccessor = clazz.info.decl(accName).symbol orElse {
269273
val newAcc = ctx.newSymbol(
270-
clazz, accName, Artifact, accType, coord = tree.pos).enteredAfter(thisPhase)
274+
clazz, accName, Artifact, accType, coord = accPos).enteredAfter(thisPhase)
271275
val code = DefDef(newAcc, vrefss => {
272276
val (receiver :: value :: Nil) :: Nil = vrefss
273-
Assign(receiver.select(field), value).withPos(tree.pos)
277+
Assign(receiver.select(field), value).withPos(accPos)
274278
})
275279
ctx.debuglog("created protected setter: " + code)
276280
accDefs(clazz) += code

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
9999
case nme.productElement => vrefss => productElementBody(accessors.length, vrefss.head.head)
100100
}
101101
ctx.log(s"adding $synthetic to $clazz at ${ctx.phase}")
102-
DefDef(synthetic, syntheticRHS(ctx.withOwner(synthetic)))
102+
DefDef(synthetic, syntheticRHS(ctx.withOwner(synthetic))).withPos(ctx.owner.pos.focus)
103103
}
104104

105105
/** The class

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ object EtaExpansion {
2828
val name = UniqueName.fresh(prefix)
2929
val liftedType = fullyDefinedType(expr.tpe.widen, "lifted expression", expr.pos)
3030
val sym = ctx.newSymbol(ctx.owner, name, EmptyFlags, liftedType, coord = positionCoord(expr.pos))
31-
defs += ValDef(sym, expr)
32-
ref(sym.termRef)
31+
defs += ValDef(sym, expr).withPos(expr.pos.focus)
32+
ref(sym.termRef).withPos(expr.pos)
3333
}
3434

3535
/** Lift out common part of lhs tree taking part in an operator assignment such as

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ object Inliner {
9898
val accessorType = accessedType.ensureMethodic
9999
val accessor = accessorSymbol(tree, accessorType).asTerm
100100
val accessorDef = polyDefDef(accessor, tps => argss =>
101-
rhs(refPart, tps, argss))
102-
val accessorRef = ref(accessor).appliedToTypeTrees(targs).appliedToArgss(argss)
101+
rhs(refPart, tps, argss).withPos(tree.pos.focus))
102+
val accessorRef = ref(accessor).appliedToTypeTrees(targs).appliedToArgss(argss).withPos(tree.pos)
103103
(accessorDef, accessorRef)
104104
} else {
105105
// Hard case: Reference needs to go via a dynamic prefix
@@ -135,11 +135,14 @@ object Inliner {
135135
val accessor = accessorSymbol(tree, accessorType).asTerm
136136

137137
val accessorDef = polyDefDef(accessor, tps => argss =>
138-
rhs(argss.head.head.select(refPart.symbol), tps.drop(localRefs.length), argss.tail))
138+
rhs(argss.head.head.select(refPart.symbol), tps.drop(localRefs.length), argss.tail)
139+
.withPos(tree.pos.focus)
140+
)
139141

140142
val accessorRef = ref(accessor)
141143
.appliedToTypeTrees(localRefs.map(TypeTree(_)) ++ targs)
142144
.appliedToArgss((qual :: Nil) :: argss)
145+
.withPos(tree.pos)
143146
(accessorDef, accessorRef)
144147
}
145148
accessors += accessorDef
@@ -168,8 +171,8 @@ object Inliner {
168171
// Draft code (incomplete):
169172
//
170173
// val accessor = accessorSymbol(tree, TypeAlias(tree.tpe)).asType
171-
// myAccessors += TypeDef(accessor)
172-
// ref(accessor)
174+
// myAccessors += TypeDef(accessor).withPos(tree.pos.focus)
175+
// ref(accessor).withPos(tree.pos)
173176
//
174177
tree
175178
}

0 commit comments

Comments
 (0)