Skip to content

Commit e373171

Browse files
Kordyjananatoliykmetyuk
authored andcommitted
Sort inlining requests before creating SMAP
Fixes SMAP generation breaking bytecode idempotency assertions
1 parent 2c5b93a commit e373171

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,16 @@ object InlinedSourceMaps:
8282
end Stratum
8383

8484
def sourceMapFor(cunit: CompilationUnit)(internalNameProvider: Symbol => String)(using Context): InlinedSourceMap =
85-
val requests = mutable.ListBuffer.empty[Request]
86-
var lastLine = cunit.tpdTree.sourcePos.endLine
85+
val requests = mutable.ListBuffer.empty[(SourcePosition, SourcePosition)]
8786
var internalNames = Map.empty[SourceFile, String]
8887

8988
class RequestCollector(enclosingFile: SourceFile) extends TreeTraverser:
9089
override def traverse(tree: Tree)(using Context): Unit =
9190
if tree.source != enclosingFile && tree.source != cunit.source then
9291
tree.getAttachment(InliningPosition) match
9392
case Some(InliningPosition(targetPos, cls)) =>
94-
val firstFakeLine = allocate(tree.sourcePos)
95-
requests += Request(targetPos, tree.sourcePos, firstFakeLine)
93+
requests += (targetPos -> tree.sourcePos)
94+
9695
cls match
9796
case Some(symbol) if !internalNames.isDefinedAt(tree.source) =>
9897
internalNames += (tree.source -> internalNameProvider(symbol))
@@ -108,13 +107,15 @@ object InlinedSourceMaps:
108107
else traverseChildren(tree)
109108
end RequestCollector
110109

110+
var lastLine = cunit.tpdTree.sourcePos.endLine
111111
def allocate(origPos: SourcePosition): Int =
112112
val line = lastLine + 1
113113
lastLine += origPos.lines.length
114114
line
115115

116116
RequestCollector(cunit.source).traverse(cunit.tpdTree)
117-
InlinedSourceMap(cunit, requests.toList, internalNames)
117+
val allocated = requests.sortBy(_._1.start).map(r => Request(r._1, r._2, allocate(r._2)))
118+
InlinedSourceMap(cunit, allocated.toList, internalNames)
118119
end sourceMapFor
119120

120121
class InlinedSourceMap private[InlinedSourceMaps] (

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,8 @@ object Inliner {
230230
val topLevelClass = Some(inlined.call.symbol.topLevelClass).filter(_.exists)
231231
val inliningPosition = InliningPosition(inlined.sourcePos, topLevelClass)
232232

233-
// In case that bindings are present we are adding InliningPosition attachement both to the resulting block and to the expansion
234-
// as in some cases the block is removed in one of later phases and attachment is lost.
235-
val tree1 =
236-
if inlined.bindings.isEmpty then inlined.expansion
237-
else cpy.Block(inlined)(inlined.bindings, inlined.expansion.withAttachment(InliningPosition, inliningPosition))
238-
tree1.withAttachment(InliningPosition, inliningPosition)
233+
val withPos = inlined.expansion.withAttachment(InliningPosition, inliningPosition)
234+
if inlined.bindings.isEmpty then withPos else cpy.Block(inlined)(inlined.bindings, withPos)
239235

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

0 commit comments

Comments
 (0)