Skip to content

Commit d4641b5

Browse files
Adapt Inliner to the most recent refactoring on the main
1 parent 1213c1d commit d4641b5

File tree

3 files changed

+9
-1639
lines changed

3 files changed

+9
-1639
lines changed

compiler/src/dotty/tools/backend/jvm/InlinedSourceMaps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import dotc.util.{ SourcePosition, SourceFile }
88
import dotc.core.Contexts._
99
import dotc.core.Symbols.Symbol
1010
import dotc.report
11-
import dotc.typer.Inliner.InliningPosition
11+
import dotc.inlines.Inlines.InliningPosition
1212
import collection.mutable
1313

1414
/**

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

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import NameKinds.BodyRetainerName
1111
import SymDenotations.SymDenotation
1212
import config.Printers.inlining
1313
import ErrorReporting.errorTree
14-
import dotty.tools.dotc.util.{SourceFile, SourcePosition, SrcPos}
14+
import dotty.tools.dotc.util.{SourceFile, SourcePosition, SrcPos, Property}
1515
import parsing.Parsers.Parser
1616
import transform.{PostTyper, Inlining, CrossVersionChecks}
1717

@@ -28,6 +28,9 @@ object Inlines:
2828
*/
2929
private[dotc] class MissingInlineInfo extends Exception
3030

31+
object InliningPosition extends Property.StickyKey[InliningPosition]
32+
case class InliningPosition(sourcePos: SourcePosition, topLevelSymbol: Option[Symbol])
33+
3134
/** `sym` is an inline method with a known body to inline.
3235
*/
3336
def hasBodyToInline(sym: SymDenotation)(using Context): Boolean =
@@ -243,58 +246,10 @@ object Inlines:
243246

244247
/** Replace `Inlined` node by a block that contains its bindings and expansion */
245248
def dropInlined(inlined: Inlined)(using Context): Tree =
246-
val tree1 =
247-
if inlined.bindings.isEmpty then inlined.expansion
248-
else cpy.Block(inlined)(inlined.bindings, inlined.expansion)
249-
// Reposition in the outer most inlined call
250-
if (enclosingInlineds.nonEmpty) tree1 else reposition(tree1, inlined.span)
251-
252-
def reposition(tree: Tree, callSpan: Span)(using Context): Tree =
253-
// Reference test tests/run/i4947b
254-
255-
val curSource = ctx.compilationUnit.source
256-
257-
// Tree copier that changes the source of all trees to `curSource`
258-
val cpyWithNewSource = new TypedTreeCopier {
259-
override protected def sourceFile(tree: tpd.Tree): SourceFile = curSource
260-
override protected val untpdCpy: untpd.UntypedTreeCopier = new untpd.UntypedTreeCopier {
261-
override protected def sourceFile(tree: untpd.Tree): SourceFile = curSource
262-
}
263-
}
264-
265-
/** Removes all Inlined trees, replacing them with blocks.
266-
* Repositions all trees directly inside an inlined expansion of a non empty call to the position of the call.
267-
* Any tree directly inside an empty call (inlined in the inlined code) retains their position.
268-
*
269-
* Until we implement JSR-45, we cannot represent in output positions in other source files.
270-
* So, reposition inlined code from other files with the call position.
271-
*/
272-
class Reposition extends TreeMap(cpyWithNewSource) {
273-
274-
override def transform(tree: Tree)(using Context): Tree = {
275-
def fixSpan[T <: untpd.Tree](copied: T): T =
276-
copied.withSpan(if tree.source == curSource then tree.span else callSpan)
277-
def finalize(copied: untpd.Tree) =
278-
fixSpan(copied).withAttachmentsFrom(tree).withTypeUnchecked(tree.tpe)
279-
280-
inContext(ctx.withSource(curSource)) {
281-
tree match
282-
case tree: Ident => finalize(untpd.Ident(tree.name)(curSource))
283-
case tree: Literal => finalize(untpd.Literal(tree.const)(curSource))
284-
case tree: This => finalize(untpd.This(tree.qual)(curSource))
285-
case tree: JavaSeqLiteral => finalize(untpd.JavaSeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
286-
case tree: SeqLiteral => finalize(untpd.SeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
287-
case tree: Bind => finalize(untpd.Bind(tree.name, transform(tree.body))(curSource))
288-
case tree: TypeTree => finalize(tpd.TypeTree(tree.tpe))
289-
case tree: DefTree => super.transform(tree).setDefTree
290-
case EmptyTree => tree
291-
case _ => fixSpan(super.transform(tree))
292-
}
293-
}
294-
}
295-
296-
(new Reposition).transform(tree)
297-
end reposition
249+
val topLevelClass = Option.when(!inlined.call.isEmpty)(inlined.call.symbol.topLevelClass)
250+
val inliningPosition = InliningPosition(inlined.sourcePos, topLevelClass)
251+
val withPos = inlined.expansion.withAttachment(InliningPosition, inliningPosition)
252+
if inlined.bindings.isEmpty then withPos else cpy.Block(inlined)(inlined.bindings, withPos)
298253

299254
/** Leave only a call trace consisting of
300255
* - a reference to the top-level class from which the call was inlined,

0 commit comments

Comments
 (0)