Skip to content

Commit e14979d

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 3783220 commit e14979d

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
@@ -1062,15 +1062,23 @@ class Inliner(val call: tpd.Tree)(using Context):
10621062
* This corresponds to the symbols that will need to be interpreted.
10631063
*/
10641064
private def macroDependencies(tree: Tree)(using Context) =
1065+
val typeAccumulator = new TypeAccumulator[List[Symbol]] {
1066+
def apply(x: List[Symbol], tp: Type): List[Symbol] = tp match
1067+
case tp: TermRef =>
1068+
if tp.symbol.isDefinedInCurrentRun then foldOver(tp.symbol :: x, tp)
1069+
else foldOver(x, tp)
1070+
case tp: ThisType if tp.typeSymbol.isDefinedInCurrentRun =>
1071+
foldOver(tp.typeSymbol :: x, tp)
1072+
case _ =>
1073+
x
1074+
}
10651075
new TreeAccumulator[List[Symbol]] {
10661076
override def apply(syms: List[Symbol], tree: tpd.Tree)(using Context): List[Symbol] =
1067-
tree match {
1068-
case tree: RefTree if tree.isTerm && level == -1 && tree.symbol.isDefinedInCurrentRun && !tree.symbol.isLocal =>
1069-
foldOver(tree.symbol :: syms, tree)
1070-
case _: This if level == -1 && tree.symbol.isDefinedInCurrentRun =>
1071-
tree.symbol :: syms
1077+
tree match
1078+
case tree: RefTree if tree.isTerm && level == -1 && !tree.symbol.isLocal =>
1079+
typeAccumulator(syms, tree.tpe)
10721080
case _: TypTree => syms
10731081
case _ => foldOver(syms, tree)
1074-
}
10751082
}.apply(Nil, tree)
1083+
10761084
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)