Skip to content

Commit 7305713

Browse files
committed
enclosingInlineds: Clarify invariants
1 parent b5abba6 commit 7305713

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,9 @@ object Trees {
574574

575575
/** A tree representing inlined code.
576576
*
577-
* @param call Info about the original call that was inlined
578-
* Until PostTyper, this is the full call, afterwards only
577+
* @param call Tree representing the original call that was inlined.
578+
* If EmptyTree: this represents an argument actual tree.
579+
* Otherwise: Until PostTyper, this is the full call, afterwards only
579580
* a reference to the toplevel class from which the call was inlined.
580581
* @param bindings Bindings for proxies to be used in the inlined code
581582
* @param expansion The inlined tree, minus bindings.
@@ -587,6 +588,8 @@ object Trees {
587588
* The reason to keep `bindings` separate is because they are typed in a
588589
* different context: `bindings` represent the arguments to the inlined
589590
* call, whereas `expansion` represents the body of the inlined function.
591+
* Call argument trees (whether kept in `bindings` or inlined in the body)
592+
* are typed in the caller's context.
590593
*/
591594
case class Inlined[-T >: Untyped] private[ast] (call: tpd.Tree, bindings: List[MemberDef[T]], expansion: Tree[T])
592595
extends Tree[T] {

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,15 +1079,24 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
10791079
}
10801080
}
10811081

1082+
//inlineContext, enclosingInlineds, sourceFile and Decorators.sourcePos work closely together.
1083+
10821084
/** A key to be used in a context property that tracks enclosing inlined calls */
10831085
private val InlinedCalls = new Property.Key[List[Tree]]
10841086

1085-
/** Record an enclosing inlined call.
1086-
* EmptyTree calls (for parameters) cancel the next-enclosing call in the list instead of being added to it.
1087-
* We assume parameters are never nested inside parameters.
1087+
/** Record an enclosing inlined call in enclosingInlineds, and produces the context for visiting
1088+
* Inlined(call, ...).
1089+
*
1090+
* Invariants:
1091+
* - enclosingInlineds never contains EmptyTree nodes.
1092+
* - if enclosingInlineds.nonEmpty, the current tree comes from the file *defining* enclosingInlineds.head.symbol;
1093+
* this is exploited in e.g. Decorators.sourcePos.
1094+
*
1095+
* Arguments of inlined calls (enclosed in Inlined(EmptyTree, ...)) come instead from the call-site, so
1096+
* inlineContext(EmptyTree) pops the enclosing call.
10881097
*/
10891098
override def inlineContext(call: Tree)(implicit ctx: Context): Context = {
1090-
// We assume enclosingInlineds is already normalized, and only process the new call with the head.
1099+
// enclosingInlineds is already normalized, so we only process the new call with the head.
10911100
val oldIC = enclosingInlineds
10921101
val newIC = (call, oldIC) match {
10931102
case (t, t1 :: ts2) if t.isEmpty =>
@@ -1106,7 +1115,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11061115
/** The source file where the symbol of the `inline` method referred to by `call`
11071116
* is defined
11081117
*/
1109-
def sourceFile(call: Tree)(implicit ctx: Context) = {
1118+
def sourceFile(call: Tree)(implicit ctx: Context): SourceFile = {
11101119
val file = call.symbol.sourceFile
11111120
val encoding = ctx.settings.encoding.value
11121121
if (file != null && file.exists) new SourceFile(file, Codec(encoding)) else NoSource

0 commit comments

Comments
 (0)