Skip to content

Commit 58e0f7e

Browse files
authored
Backport "Add Inlined.inlinedFromOuterScope and refactor inline context" to LTS (#19123)
Backports #18236 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents ad5153c + 2e189d2 commit 58e0f7e

File tree

16 files changed

+46
-40
lines changed

16 files changed

+46
-40
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@ object Trees {
673673
*/
674674
case class Inlined[+T <: Untyped] private[ast] (call: tpd.Tree, bindings: List[MemberDef[T]], expansion: Tree[T])(implicit @constructorOnly src: SourceFile)
675675
extends Tree[T] {
676+
677+
def inlinedFromOuterScope: Boolean = call.isEmpty
678+
676679
type ThisTree[+T <: Untyped] = Inlined[T]
677680
override def isTerm = expansion.isTerm
678681
override def isType = expansion.isType
@@ -1464,7 +1467,7 @@ object Trees {
14641467
* innermost enclosing call for which the inlined version is currently
14651468
* processed.
14661469
*/
1467-
protected def inlineContext(call: tpd.Tree)(using Context): Context = ctx
1470+
protected def inlineContext(tree: Inlined)(using Context): Context = ctx
14681471

14691472
/** The context to use when mapping or accumulating over a tree */
14701473
def localCtx(tree: Tree)(using Context): Context
@@ -1534,8 +1537,8 @@ object Trees {
15341537
cpy.Try(tree)(transform(block), transformSub(cases), transform(finalizer))
15351538
case SeqLiteral(elems, elemtpt) =>
15361539
cpy.SeqLiteral(tree)(transform(elems), transform(elemtpt))
1537-
case Inlined(call, bindings, expansion) =>
1538-
cpy.Inlined(tree)(call, transformSub(bindings), transform(expansion)(using inlineContext(call)))
1540+
case tree @ Inlined(call, bindings, expansion) =>
1541+
cpy.Inlined(tree)(call, transformSub(bindings), transform(expansion)(using inlineContext(tree)))
15391542
case TypeTree() =>
15401543
tree
15411544
case SingletonTypeTree(ref) =>
@@ -1676,8 +1679,8 @@ object Trees {
16761679
this(this(this(x, block), handler), finalizer)
16771680
case SeqLiteral(elems, elemtpt) =>
16781681
this(this(x, elems), elemtpt)
1679-
case Inlined(call, bindings, expansion) =>
1680-
this(this(x, bindings), expansion)(using inlineContext(call))
1682+
case tree @ Inlined(call, bindings, expansion) =>
1683+
this(this(x, bindings), expansion)(using inlineContext(tree))
16811684
case TypeTree() =>
16821685
x
16831686
case SingletonTypeTree(ref) =>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,17 +1389,17 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
13891389
* EmptyTree calls (for parameters) cancel the next-enclosing call in the list instead of being added to it.
13901390
* We assume parameters are never nested inside parameters.
13911391
*/
1392-
override def inlineContext(call: Tree)(using Context): Context = {
1392+
override def inlineContext(tree: Inlined)(using Context): Context = {
13931393
// We assume enclosingInlineds is already normalized, and only process the new call with the head.
13941394
val oldIC = enclosingInlineds
13951395

13961396
val newIC =
1397-
if call.isEmpty then
1397+
if tree.inlinedFromOuterScope then
13981398
oldIC match
13991399
case t1 :: ts2 => ts2
14001400
case _ => oldIC
14011401
else
1402-
call :: oldIC
1402+
tree.call :: oldIC
14031403

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

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,12 +527,12 @@ class TreePickler(pickler: TastyPickler) {
527527
case SeqLiteral(elems, elemtpt) =>
528528
writeByte(REPEATED)
529529
withLength { pickleTree(elemtpt); elems.foreach(pickleTree) }
530-
case Inlined(call, bindings, expansion) =>
530+
case tree @ Inlined(call, bindings, expansion) =>
531531
writeByte(INLINED)
532532
bindings.foreach(preRegister)
533533
withLength {
534534
pickleTree(expansion)
535-
if (!call.isEmpty) pickleTree(call)
535+
if (!tree.inlinedFromOuterScope) pickleTree(call)
536536
bindings.foreach { b =>
537537
assert(b.isInstanceOf[DefDef] || b.isInstanceOf[ValDef])
538538
pickleTree(b)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ class InlineReducer(inliner: Inliner)(using Context):
342342
}
343343
case Alternative(pats) =>
344344
pats.exists(reducePattern(caseBindingMap, scrut, _))
345-
case Inlined(EmptyTree, Nil, ipat) =>
346-
reducePattern(caseBindingMap, scrut, ipat)
345+
case tree: Inlined if tree.inlinedFromOuterScope =>
346+
reducePattern(caseBindingMap, scrut, tree.expansion)
347347
case _ => false
348348
}
349349
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ object Inliner:
129129
new InlinerMap(typeMap, treeMap, oldOwners, newOwners, substFrom, substTo)
130130

131131
override def transformInlined(tree: Inlined)(using Context) =
132-
if tree.call.isEmpty then
132+
if tree.inlinedFromOuterScope then
133133
tree.expansion match
134134
case expansion: TypeTree => expansion
135135
case _ => tree
@@ -549,7 +549,7 @@ class Inliner(val call: tpd.Tree)(using Context):
549549

550550
val inlineTyper = new InlineTyper(ctx.reporter.errorCount)
551551

552-
val inlineCtx = inlineContext(call).fresh.setTyper(inlineTyper).setNewScope
552+
val inlineCtx = inlineContext(Inlined(call, Nil, ref(defn.Predef_undefined))).fresh.setTyper(inlineTyper).setNewScope
553553

554554
def inlinedFromOutside(tree: Tree)(span: Span): Tree =
555555
Inlined(EmptyTree, Nil, tree)(using ctx.withSource(inlinedMethod.topLevelClass.source)).withSpan(span)
@@ -1049,7 +1049,7 @@ class Inliner(val call: tpd.Tree)(using Context):
10491049
}
10501050
val inlinedNormalizer = new TreeMap {
10511051
override def transform(tree: tpd.Tree)(using Context): tpd.Tree = tree match {
1052-
case Inlined(EmptyTree, Nil, expr) if enclosingInlineds.isEmpty => transform(expr)
1052+
case tree @ Inlined(_, Nil, expr) if tree.inlinedFromOuterScope && enclosingInlineds.isEmpty => transform(expr)
10531053
case _ => super.transform(tree)
10541054
}
10551055
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ object Inlines:
101101
override def transform(t: Tree)(using Context) =
102102
if call.span.exists then
103103
t match
104-
case Inlined(t, Nil, expr) if t.isEmpty => expr
104+
case t @ Inlined(_, Nil, expr) if t.inlinedFromOuterScope => expr
105105
case _ if t.isEmpty => t
106106
case _ => super.transform(t.withSpan(call.span))
107107
else t

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
535535
case tree @ Inlined(call, bindings, body) =>
536536
val bodyText = if bindings.isEmpty then toText(body) else blockText(bindings :+ body)
537537
if homogenizedView || !ctx.settings.XprintInline.value then bodyText
538-
else if call.isEmpty then stringText("{{") ~ stringText("/* inlined from outside */") ~ bodyText ~ stringText("}}")
538+
else if tree.inlinedFromOuterScope then stringText("{{") ~ stringText("/* inlined from outside */") ~ bodyText ~ stringText("}}")
539539
else keywordText("{{") ~ keywordText("/* inlined from ") ~ toText(call) ~ keywordText(" */") ~ bodyText ~ keywordText("}}")
540540
case tpt: untpd.DerivedTypeTree =>
541541
"<derived typetree watching " ~ tpt.watched.showSummary() ~ ">"

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ object PickledQuotes {
8181

8282
/** Unpickle the tree contained in the TastyExpr */
8383
def unpickleTerm(pickled: String | List[String], typeHole: TypeHole, termHole: ExprHole)(using Context): Tree = {
84-
val unpickled = withMode(Mode.ReadPositions)(unpickle(pickled, isType = false))
85-
val Inlined(call, Nil, expansion) = unpickled: @unchecked
86-
val inlineCtx = inlineContext(call)
87-
val expansion1 = spliceTypes(expansion, typeHole)(using inlineCtx)
88-
val expansion2 = spliceTerms(expansion1, typeHole, termHole)(using inlineCtx)
89-
cpy.Inlined(unpickled)(call, Nil, expansion2)
84+
withMode(Mode.ReadPositions)(unpickle(pickled, isType = false)) match
85+
case tree @ Inlined(call, Nil, expansion) =>
86+
val inlineCtx = inlineContext(tree)
87+
val expansion1 = spliceTypes(expansion, typeHole)(using inlineCtx)
88+
val expansion2 = spliceTerms(expansion1, typeHole, termHole)(using inlineCtx)
89+
cpy.Inlined(tree)(call, Nil, expansion2)
9090
}
9191

9292

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,10 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
384384
}
385385

386386
tree match {
387-
case Inlined(call, _, _) if !call.isEmpty =>
387+
case tree: Inlined if !tree.inlinedFromOuterScope =>
388388
// The inlined call is normally ignored by TreeTraverser but we need to
389389
// record it as a dependency
390-
traverse(call)
390+
traverse(tree.call)
391391
case vd: ValDef if vd.symbol.is(ModuleVal) =>
392392
// Don't visit module val
393393
case t: Template if t.symbol.owner.is(ModuleClass) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ class MegaPhase(val miniPhases: Array[MiniPhase]) extends Phase {
396396
case tree: Inlined =>
397397
inContext(prepInlined(tree, start)(using outerCtx)) {
398398
val bindings = transformSpecificTrees(tree.bindings, start)
399-
val expansion = transformTree(tree.expansion, start)(using inlineContext(tree.call))
399+
val expansion = transformTree(tree.expansion, start)(using inlineContext(tree))
400400
goInlined(cpy.Inlined(tree)(tree.call, bindings, expansion), start)
401401
}
402402
case tree: Quote =>

0 commit comments

Comments
 (0)