Skip to content

Commit 83e28e3

Browse files
committed
Cleanup implementation of Reposition
Trees now have sources that can be used directly without the need of `enclosingInlineds` to track the current source. This new implemetation simply uses the tree positions directly and does not need to keep the context in sync with the `enclosingInlineds`.
1 parent bf02a04 commit 83e28e3

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ object Inliner {
155155
if (enclosingInlineds.nonEmpty) inlined // Remove in the outer most inlined call
156156
else reposition(inlined, inlined.call.span)
157157

158-
def reposition(tree: Tree, span: Span)(implicit ctx: Context): Tree = {
158+
def reposition(tree: Tree, callSpan: Span)(implicit ctx: Context): Tree = {
159159
val curSource = ctx.compilationUnit.source
160160

161161
// Tree copier that changes the source of all trees to `curSource`
@@ -169,39 +169,33 @@ object Inliner {
169169
/** Removes all Inlined trees, replacing them with blocks.
170170
* Repositions all trees directly inside an inlined expansion of a non empty call to the position of the call.
171171
* Any tree directly inside an empty call (inlined in the inlined code) retains their position.
172+
*
173+
* Until we implement JSR-45, we cannot represent in output positions in other source files.
174+
* So, reposition inlined code from other files with the call position.
172175
*/
173176
class Reposition extends TreeMap(cpyWithNewSource) {
174-
def finalize(tree: Tree, copied: untpd.Tree) =
175-
copied.withSpan(tree.span).withAttachmentsFrom(tree).withTypeUnchecked(tree.tpe)
176-
177-
def reposition(tree: Tree)(implicit ctx: Context): Tree = enclosingInlineds match {
178-
case call :: _ if call.symbol.source != curSource =>
179-
tree match {
180-
case _: EmptyTree[?] | _: EmptyValDef[?] => tree
181-
case _ =>
182-
// Until we implement JSR-45, we cannot represent in output positions in other source files.
183-
// So, reposition inlined code from other files with the call position:
184-
tree.withSpan(span)
185-
}
186-
case _ => tree
187-
}
188177

189178
override def transform(tree: Tree)(implicit ctx: Context): Tree = {
190-
val transformed = reposition(tree match {
179+
def finalize(copied: untpd.Tree) =
180+
val span = if tree.source == curSource then tree.span else callSpan
181+
copied.withSpan(span).withAttachmentsFrom(tree).withTypeUnchecked(tree.tpe)
182+
183+
given as Context = ctx.withSource(curSource)
184+
185+
tree match {
191186
case tree: Inlined =>
192-
tpd.seq(transformSub(tree.bindings), transform(tree.expansion)(inlineContext(tree.call)))(ctx.withSource(curSource)) : Tree
193-
case tree: Ident => finalize(tree, untpd.Ident(tree.name)(curSource))
194-
case tree: Literal => finalize(tree, untpd.Literal(tree.const)(curSource))
195-
case tree: This => finalize(tree, untpd.This(tree.qual)(curSource))
196-
case tree: JavaSeqLiteral => finalize(tree, untpd.JavaSeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
197-
case tree: SeqLiteral => finalize(tree, untpd.SeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
198-
case tree: TypeTree => tpd.TypeTree(tree.tpe)(ctx.withSource(curSource)).withSpan(tree.span)
199-
case tree: Bind => finalize(tree, untpd.Bind(tree.name, transform(tree.body))(curSource))
187+
if tree.bindings.isEmpty then transform(tree.expansion)
188+
else transform(cpy.Block(tree)(tree.bindings, tree.expansion))
189+
case tree: Ident => finalize(untpd.Ident(tree.name)(curSource))
190+
case tree: Literal => finalize(untpd.Literal(tree.const)(curSource))
191+
case tree: This => finalize(untpd.This(tree.qual)(curSource))
192+
case tree: JavaSeqLiteral => finalize(untpd.JavaSeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
193+
case tree: SeqLiteral => finalize(untpd.SeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
194+
case tree: Bind => finalize(untpd.Bind(tree.name, transform(tree.body))(curSource))
195+
case tree: TypeTree => finalize(tpd.TypeTree(tree.tpe))
200196
case tree: DefTree => super.transform(tree).setDefTree
201197
case _ => super.transform(tree)
202-
})
203-
assert(transformed.isInstanceOf[EmptyTree[?]] || transformed.isInstanceOf[EmptyValDef[?]] || transformed.source == curSource)
204-
transformed
198+
}
205199
}
206200
}
207201

0 commit comments

Comments
 (0)