Skip to content

Commit 78b1cfc

Browse files
committed
Avoid using call in inlinedContext
1 parent 767e24e commit 78b1cfc

File tree

8 files changed

+34
-22
lines changed

8 files changed

+34
-22
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,9 @@ object Trees {
12461246
* innermost enclosing call for which the inlined version is currently
12471247
* processed.
12481248
*/
1249-
protected def inlineContext(call: Tree)(using Context): Context = ctx
1249+
protected def inlineContext(pos: SourcePosition)(using Context): Context = ctx
1250+
1251+
protected def outlineContext(using Context): Context = ctx
12501252

12511253
abstract class TreeMap(val cpy: TreeCopier = inst.cpy) { self =>
12521254
def transform(tree: Tree)(using Context): Tree = {
@@ -1304,7 +1306,8 @@ object Trees {
13041306
case SeqLiteral(elems, elemtpt) =>
13051307
cpy.SeqLiteral(tree)(transform(elems), transform(elemtpt))
13061308
case Inlined(call, bindings, expansion) =>
1307-
cpy.Inlined(tree)(call, transformSub(bindings), transform(expansion)(using inlineContext(call)))
1309+
val ctx1 = if call.isEmpty then outlineContext else inlineContext(tree.sourcePos)
1310+
cpy.Inlined(tree)(call, transformSub(bindings), transform(expansion)(using ctx1))
13081311
case TypeTree() =>
13091312
tree
13101313
case SingletonTypeTree(ref) =>
@@ -1442,7 +1445,8 @@ object Trees {
14421445
case SeqLiteral(elems, elemtpt) =>
14431446
this(this(x, elems), elemtpt)
14441447
case Inlined(call, bindings, expansion) =>
1445-
this(this(x, bindings), expansion)(using inlineContext(call))
1448+
val ctx1 = if call.isEmpty then outlineContext else inlineContext(tree.sourcePos)
1449+
this(this(x, bindings), expansion)(using ctx1)
14461450
case TypeTree() =>
14471451
x
14481452
case SingletonTypeTree(ref) =>

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,21 +1240,25 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
12401240
var count: Int = 0
12411241
}
12421242

1243-
/** Record an enclosing inlined call.
1244-
* EmptyTree calls (for parameters) cancel the next-enclosing call in the list instead of being added to it.
1245-
* We assume parameters are never nested inside parameters.
1246-
*/
1247-
override def inlineContext(call: Tree)(using Context): Context = {
1243+
/** Record an enclosing inlined call position. */
1244+
override def inlineContext(pos: SourcePosition)(using Context): Context = {
1245+
// We assume enclosingInlineds is already normalized, and only process the new call with the head.
1246+
val oldIC = enclosingInlineds
1247+
val newIC = pos :: oldIC
1248+
1249+
val ctx1 = ctx.fresh.setProperty(InlinedCalls, newIC)
1250+
if oldIC.isEmpty then ctx1.setProperty(InlinedTrees, new Counter) else ctx1
1251+
}
1252+
1253+
/** Record an remove the last inlined call position. */
1254+
override def outlineContext(using Context): Context = {
12481255
// We assume enclosingInlineds is already normalized, and only process the new call with the head.
12491256
val oldIC = enclosingInlineds
12501257

12511258
val newIC =
1252-
if call.isEmpty then
1253-
oldIC match
1254-
case t1 :: ts2 => ts2
1255-
case _ => oldIC
1256-
else
1257-
call.sourcePos :: oldIC
1259+
oldIC match
1260+
case t1 :: ts2 => ts2
1261+
case _ => oldIC
12581262

12591263
val ctx1 = ctx.fresh.setProperty(InlinedCalls, newIC)
12601264
if oldIC.isEmpty then ctx1.setProperty(InlinedTrees, new Counter) else ctx1

compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object PickledQuotes {
5757
val unpickled = withMode(Mode.ReadPositions)(
5858
unpickle(tastyBytes, splices, isType = false))
5959
val Inlined(call, Nil, expnasion) = unpickled
60-
val inlineCtx = inlineContext(call)
60+
val inlineCtx = inlineContext(unpickled.sourcePos)
6161
val expansion1 = spliceTypes(expnasion, splices)(using inlineCtx)
6262
val expansion2 = spliceTerms(expansion1, splices)(using inlineCtx)
6363
cpy.Inlined(unpickled)(call, Nil, expansion2)

compiler/src/dotty/tools/dotc/transform/MegaPhase.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ class MegaPhase(val miniPhases: Array[MiniPhase]) extends Phase {
394394
case tree: Inlined =>
395395
inContext(prepInlined(tree, start)(using outerCtx)) {
396396
val bindings = transformSpecificTrees(tree.bindings, start)
397-
val expansion = transformTree(tree.expansion, start)(using inlineContext(tree.call))
397+
val ctx1 = if tree.call.isEmpty then outlineContext else inlineContext(tree.sourcePos)
398+
val expansion = transformTree(tree.expansion, start)(using ctx1)
398399
goInlined(cpy.Inlined(tree)(tree.call, bindings, expansion), start)
399400
}
400401
case tree: Return =>

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
289289
super.transform(tree1)
290290
}
291291
case Inlined(call, bindings, expansion) if !call.isEmpty =>
292-
val pos = call.sourcePos
292+
val pos = tree.sourcePos
293293
val callTrace = Inliner.inlineCallTrace(call.symbol, pos)(using ctx.withSource(pos.source))
294-
cpy.Inlined(tree)(callTrace, transformSub(bindings), transform(expansion)(using inlineContext(call)))
294+
cpy.Inlined(tree)(callTrace, transformSub(bindings), transform(expansion)(using inlineContext(tree.sourcePos)))
295295
case templ: Template =>
296296
withNoCheckNews(templ.parents.flatMap(newPart)) {
297297
forwardParamAccessors(templ)

compiler/src/dotty/tools/dotc/transform/YCheckPositions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ class YCheckPositions extends Phase {
3939
assert(bindings.isEmpty)
4040
val old = sources
4141
sources = old.tail
42-
traverse(expansion)(using inlineContext(EmptyTree).withSource(sources.head))
42+
traverse(expansion)(using outlineContext.withSource(sources.head))
4343
sources = old
4444
case Inlined(call, bindings, expansion) =>
4545
bindings.foreach(traverse(_))
4646
sources = call.symbol.topLevelClass.source :: sources
4747
if (!isMacro(call)) // FIXME macro implementations can drop Inlined nodes. We should reinsert them after macro expansion based on the positions of the trees
48-
traverse(expansion)(using inlineContext(call).withSource(sources.head))
48+
traverse(expansion)(using inlineContext(tree.sourcePos).withSource(sources.head))
4949
sources = sources.tail
5050
case _ => traverseChildren(tree)
5151
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
652652

653653
val inlineTyper = new InlineTyper(ctx.reporter.errorCount)
654654

655-
val inlineCtx = inlineContext(call).fresh.setTyper(inlineTyper).setNewScope
655+
val inlineCtx = inlineContext(call.sourcePos).fresh.setTyper(inlineTyper).setNewScope
656656

657657
def inlinedFromOutside(tree: Tree)(span: Span): Tree =
658658
Inlined(EmptyTree, Nil, tree)(using ctx.withSource(inlinedMethod.topLevelClass.source)).withSpan(span)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,10 @@ class Typer extends Namer
16271627

16281628
def typedInlined(tree: untpd.Inlined, pt: Type)(using Context): Tree = {
16291629
val (bindings1, exprCtx) = typedBlockStats(tree.bindings)
1630-
val expansion1 = typed(tree.expansion, pt)(using inlineContext(tree.call)(using exprCtx))
1630+
val ctx1 =
1631+
if tree.call.isEmpty then outlineContext(using exprCtx)
1632+
else inlineContext(tree.sourcePos)(using exprCtx)
1633+
val expansion1 = typed(tree.expansion, pt)(using ctx1)
16311634
assignType(cpy.Inlined(tree)(tree.call, bindings1.asInstanceOf[List[MemberDef]], expansion1),
16321635
bindings1, expansion1)
16331636
}

0 commit comments

Comments
 (0)