Skip to content

Commit 71f9a9f

Browse files
committed
Fix macro dependency tracking for this
The symbols of trees do not have enough information to determine the full dependencies. For example, if we have an `Ident` we need to look at its type to figure out which is the `this` prefix on which it is defined. Fix #18434
1 parent f1bad54 commit 71f9a9f

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,15 +1065,23 @@ class Inliner(val call: tpd.Tree)(using Context):
10651065
* This corresponds to the symbols that will need to be interpreted.
10661066
*/
10671067
private def macroDependencies(tree: Tree)(using Context) =
1068+
val typeAccumulator = new TypeAccumulator[List[Symbol]] {
1069+
def apply(x: List[Symbol], tp: Type): List[Symbol] = tp match
1070+
case tp: TermRef =>
1071+
if tp.symbol.isDefinedInCurrentRun then foldOver(tp.symbol :: x, tp)
1072+
else foldOver(x, tp)
1073+
case tp: ThisType if tp.typeSymbol.isDefinedInCurrentRun =>
1074+
foldOver(tp.typeSymbol :: x, tp)
1075+
case _ =>
1076+
x
1077+
}
10681078
new TreeAccumulator[List[Symbol]] {
10691079
override def apply(syms: List[Symbol], tree: tpd.Tree)(using Context): List[Symbol] =
1070-
tree match {
1071-
case tree: RefTree if tree.isTerm && level == -1 && tree.symbol.isDefinedInCurrentRun && !tree.symbol.isLocal =>
1072-
foldOver(tree.symbol :: syms, tree)
1073-
case _: This if level == -1 && tree.symbol.isDefinedInCurrentRun =>
1074-
tree.symbol :: syms
1080+
tree match
1081+
case tree: RefTree if tree.isTerm && level == -1 && !tree.symbol.isLocal =>
1082+
typeAccumulator(syms, tree.tpe)
10751083
case _: TypTree => syms
10761084
case _ => foldOver(syms, tree)
1077-
}
10781085
}.apply(Nil, tree)
1086+
10791087
end Inliner
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package user
2+
3+
import defn.Macro
4+
5+
object Inline extends Macro {
6+
inline def callMacro(): Int =
7+
${ impl() }
8+
}

tests/pos-macros/i18434/Macro_1.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package defn
2+
3+
import scala.quoted.*
4+
5+
abstract class Macro {
6+
def impl()(using Quotes): Expr[Int] = '{1}
7+
}

tests/pos-macros/i18434/Test_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package user
2+
3+
object Test {
4+
Inline.callMacro()
5+
}

0 commit comments

Comments
 (0)