Skip to content

Commit a0690e1

Browse files
Merge pull request #8648 from dotty-staging/clenup-inlined-positions-implememtations
Cleanup inlined positions implementation
2 parents 7ae44d4 + e1b34c7 commit a0690e1

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ final class SearchRoot extends SearchHistory {
19571957
// Substitute dictionary references into dictionary entry RHSs
19581958
val rhsMap = new TreeTypeMap(treeMap = {
19591959
case id: Ident if vsymMap.contains(id.symbol) =>
1960-
tpd.ref(vsymMap(id.symbol))
1960+
tpd.ref(vsymMap(id.symbol))(ctx.withSource(id.source)).withSpan(id.span)
19611961
case tree => tree
19621962
})
19631963
val nrhss = rhss.map(rhsMap(_))
@@ -1981,7 +1981,7 @@ final class SearchRoot extends SearchHistory {
19811981

19821982
val res = resMap(tree)
19831983

1984-
val blk = Inliner.reposition(Block(classDef :: inst :: Nil, res), span)
1984+
val blk = Block(classDef :: inst :: Nil, res).withSpan(span)
19851985

19861986
success.copy(tree = blk)(success.tstate, success.gstate)
19871987
}

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

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,15 @@ object Inliner {
194194

195195
/** Replace `Inlined` node by a block that contains its bindings and expansion */
196196
def dropInlined(inlined: Inlined)(implicit ctx: Context): Tree =
197-
if (enclosingInlineds.nonEmpty) inlined // Remove in the outer most inlined call
198-
else reposition(inlined, inlined.call.span)
197+
val tree1 =
198+
if inlined.bindings.isEmpty then inlined.expansion
199+
else cpy.Block(inlined)(inlined.bindings, inlined.expansion)
200+
// Reposition in the outer most inlined call
201+
if (enclosingInlineds.nonEmpty) tree1 else reposition(tree1, inlined.span)
202+
203+
def reposition(tree: Tree, callSpan: Span)(implicit ctx: Context): Tree = {
204+
// Reference test tests/run/i4947b
199205

200-
def reposition(tree: Tree, span: Span)(implicit ctx: Context): Tree = {
201206
val curSource = ctx.compilationUnit.source
202207

203208
// Tree copier that changes the source of all trees to `curSource`
@@ -211,39 +216,30 @@ object Inliner {
211216
/** Removes all Inlined trees, replacing them with blocks.
212217
* Repositions all trees directly inside an inlined expansion of a non empty call to the position of the call.
213218
* Any tree directly inside an empty call (inlined in the inlined code) retains their position.
219+
*
220+
* Until we implement JSR-45, we cannot represent in output positions in other source files.
221+
* So, reposition inlined code from other files with the call position.
214222
*/
215223
class Reposition extends TreeMap(cpyWithNewSource) {
216-
def finalize(tree: Tree, copied: untpd.Tree) =
217-
copied.withSpan(tree.span).withAttachmentsFrom(tree).withTypeUnchecked(tree.tpe)
218-
219-
def reposition(tree: Tree)(implicit ctx: Context): Tree = enclosingInlineds match {
220-
case call :: _ if call.symbol.source != curSource =>
221-
tree match {
222-
case _: EmptyTree[?] | _: EmptyValDef[?] => tree
223-
case _ =>
224-
// Until we implement JSR-45, we cannot represent in output positions in other source files.
225-
// So, reposition inlined code from other files with the call position:
226-
tree.withSpan(span)
227-
}
228-
case _ => tree
229-
}
230224

231225
override def transform(tree: Tree)(implicit ctx: Context): Tree = {
232-
val transformed = reposition(tree match {
233-
case tree: Inlined =>
234-
tpd.seq(transformSub(tree.bindings), transform(tree.expansion)(inlineContext(tree.call)))(ctx.withSource(curSource)) : Tree
235-
case tree: Ident => finalize(tree, untpd.Ident(tree.name)(curSource))
236-
case tree: Literal => finalize(tree, untpd.Literal(tree.const)(curSource))
237-
case tree: This => finalize(tree, untpd.This(tree.qual)(curSource))
238-
case tree: JavaSeqLiteral => finalize(tree, untpd.JavaSeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
239-
case tree: SeqLiteral => finalize(tree, untpd.SeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
240-
case tree: TypeTree => tpd.TypeTree(tree.tpe)(ctx.withSource(curSource)).withSpan(tree.span)
241-
case tree: Bind => finalize(tree, untpd.Bind(tree.name, transform(tree.body))(curSource))
226+
def finalize(copied: untpd.Tree) =
227+
val span = if tree.source == curSource then tree.span else callSpan
228+
copied.withSpan(span).withAttachmentsFrom(tree).withTypeUnchecked(tree.tpe)
229+
230+
given as Context = ctx.withSource(curSource)
231+
232+
tree match {
233+
case tree: Ident => finalize(untpd.Ident(tree.name)(curSource))
234+
case tree: Literal => finalize(untpd.Literal(tree.const)(curSource))
235+
case tree: This => finalize(untpd.This(tree.qual)(curSource))
236+
case tree: JavaSeqLiteral => finalize(untpd.JavaSeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
237+
case tree: SeqLiteral => finalize(untpd.SeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
238+
case tree: Bind => finalize(untpd.Bind(tree.name, transform(tree.body))(curSource))
239+
case tree: TypeTree => finalize(tpd.TypeTree(tree.tpe))
242240
case tree: DefTree => super.transform(tree).setDefTree
243241
case _ => super.transform(tree)
244-
})
245-
assert(transformed.isInstanceOf[EmptyTree[?]] || transformed.isInstanceOf[EmptyValDef[?]] || transformed.source == curSource)
246-
transformed
242+
}
247243
}
248244
}
249245

library/src/scala/internal/quoted/Matcher.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private[quoted] object Matcher {
2424
*/
2525
private type Env = Map[Symbol, Symbol]
2626

27-
inline private def withEnv[T](env: Env)(body: => Env ?=> T): T = body(using env)
27+
inline private def withEnv[T](env: Env)(inline body: Env ?=> T): T = body(using env)
2828

2929
class SymBinding(val sym: Symbol, val fromAbove: Boolean)
3030

0 commit comments

Comments
 (0)