Skip to content

Commit a8993cc

Browse files
committed
Add TreeOps.setDefTree
1 parent 8c79d34 commit a8993cc

File tree

5 files changed

+14
-19
lines changed

5 files changed

+14
-19
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,13 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
10011001
foreachSubTree { tree => if (f(tree)) buf += tree }
10021002
buf.toList
10031003
}
1004+
1005+
/** Set this tree as the `defTree` of its symbol and return this tree */
1006+
def setDefTree(implicit ctx: Context): ThisTree = {
1007+
val sym = tree.symbol
1008+
if (sym.exists) sym.defTree = tree
1009+
tree
1010+
}
10041011
}
10051012

10061013
/** Map Inlined nodes, NamedArgs, Blocks with no statements and local references to underlying arguments.

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

+1-3
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,6 @@ class TreeUnpickler(reader: TastyReader,
851851
sym.info = ta.avoidPrivateLeaks(sym, tree.sourcePos)
852852
}
853853

854-
sym.defTree = tree
855-
856854
if (ctx.mode.is(Mode.ReadComments)) {
857855
assert(ctx.docCtx.isDefined, "Mode is `ReadComments`, but no `docCtx` is set.")
858856
commentUnpicklerOpt.foreach { commentUnpickler =>
@@ -862,7 +860,7 @@ class TreeUnpickler(reader: TastyReader,
862860
}
863861
}
864862

865-
tree
863+
tree.setDefTree
866864
}
867865

868866
private def readTemplate(implicit ctx: Context): Template = {

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ abstract class Lifter {
4747
var liftedType = expr.tpe.widen
4848
if (liftedFlags.is(Method)) liftedType = ExprType(liftedType)
4949
val lifted = ctx.newSymbol(ctx.owner, name, liftedFlags | Synthetic, liftedType, coord = spanCoord(expr.span))
50-
val ddef = liftedDef(lifted, expr).withSpan(expr.span)
51-
lifted.defTree = ddef
52-
defs += ddef
50+
defs += liftedDef(lifted, expr).withSpan(expr.span).setDefTree
5351
ref(lifted.termRef).withSpan(expr.span.focus)
5452
}
5553

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
260260
if (isByName) DefDef(boundSym, arg.changeOwner(ctx.owner, boundSym))
261261
else ValDef(boundSym, arg)
262262
}.withSpan(boundSym.span)
263-
boundSym.defTree = binding
264-
bindingsBuf += binding
263+
bindingsBuf += binding.setDefTree
265264
binding
266265
}
267266

@@ -313,9 +312,8 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
313312
ref(rhsClsSym.sourceModule)
314313
else
315314
inlineCallPrefix
316-
val binding = ValDef(selfSym.asTerm, rhs).withSpan(selfSym.span)
315+
val binding = ValDef(selfSym.asTerm, rhs).withSpan(selfSym.span).setDefTree
317316
bindingsBuf += binding
318-
selfSym.defTree = binding
319317
inlining.println(i"proxy at $level: $selfSym = ${bindingsBuf.last}")
320318
lastSelf = selfSym
321319
lastLevel = level
@@ -490,11 +488,9 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
490488

491489
// The normalized bindings collected in `bindingsBuf`
492490
bindingsBuf.transform { binding =>
493-
val transformedBinding = reducer.normalizeBinding(binding)(inlineCtx)
494491
// Set trees to symbols allow macros to see the definition tree.
495492
// This is used by `underlyingArgument`.
496-
transformedBinding.symbol.defTree = transformedBinding
497-
transformedBinding
493+
reducer.normalizeBinding(binding)(inlineCtx).setDefTree
498494
}
499495

500496
// Run a typing pass over the inlined tree. See InlineTyper for details.

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -1489,11 +1489,9 @@ class Typer extends Namer
14891489
val vdef1 = assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
14901490
if (sym.is(Inline, butNot = DeferredOrTermParamOrAccessor))
14911491
checkInlineConformant(rhs1, isFinal = sym.is(Final), em"right-hand side of inline $sym")
1492-
if (sym.exists)
1493-
sym.defTree = vdef1
14941492
patchIfLazy(vdef1)
14951493
patchFinalVals(vdef1)
1496-
vdef1
1494+
vdef1.setDefTree
14971495
}
14981496

14991497
/** Add a @volitile to lazy vals when rewriting from Scala2 */
@@ -1564,9 +1562,7 @@ class Typer extends Namer
15641562
for (param <- tparams1 ::: vparamss1.flatten)
15651563
checkRefsLegal(param, sym.owner, (name, sym) => sym.is(TypeParam), "secondary constructor")
15661564

1567-
val ddef1 = assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym)
1568-
sym.defTree = ddef1
1569-
ddef1
1565+
assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym).setDefTree
15701566
//todo: make sure dependent method types do not depend on implicits or by-name params
15711567
}
15721568

0 commit comments

Comments
 (0)