Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
  • Loading branch information
nicolasstucki committed Feb 7, 2024
commit 71f9a9fded4be902221ab73437dd6678d2287ef7
20 changes: 14 additions & 6 deletions compiler/src/dotty/tools/dotc/inlines/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1065,15 +1065,23 @@ class Inliner(val call: tpd.Tree)(using Context):
* This corresponds to the symbols that will need to be interpreted.
*/
private def macroDependencies(tree: Tree)(using Context) =
val typeAccumulator = new TypeAccumulator[List[Symbol]] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to figure out if this is good enough because it's hard to know which trees are valid here. I think it'd be much clearer if we combined this logic and the checks from checkIfValidStaticCall in the same traversal, so we'd know exactly which tree shape we need to handle.

By the way, checkIfValidStaticCall is flawed because of local imports:

object Inline extends Macro { val x: Macro = ??? import x._ inline def callMacro(): Int = ${ impl() } }
def apply(x: List[Symbol], tp: Type): List[Symbol] = tp match
case tp: TermRef =>
if tp.symbol.isDefinedInCurrentRun then foldOver(tp.symbol :: x, tp)
else foldOver(x, tp)
case tp: ThisType if tp.typeSymbol.isDefinedInCurrentRun =>
foldOver(tp.typeSymbol :: x, tp)
case _ =>
x
}
new TreeAccumulator[List[Symbol]] {
override def apply(syms: List[Symbol], tree: tpd.Tree)(using Context): List[Symbol] =
tree match {
case tree: RefTree if tree.isTerm && level == -1 && tree.symbol.isDefinedInCurrentRun && !tree.symbol.isLocal =>
foldOver(tree.symbol :: syms, tree)
case _: This if level == -1 && tree.symbol.isDefinedInCurrentRun =>
tree.symbol :: syms
tree match
case tree: RefTree if tree.isTerm && level == -1 && !tree.symbol.isLocal =>
typeAccumulator(syms, tree.tpe)
case _: TypTree => syms
case _ => foldOver(syms, tree)
}
}.apply(Nil, tree)

end Inliner
8 changes: 8 additions & 0 deletions tests/pos-macros/i18434/Inline_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package user

import defn.Macro

object Inline extends Macro {
inline def callMacro(): Int =
${ impl() }
}
7 changes: 7 additions & 0 deletions tests/pos-macros/i18434/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package defn

import scala.quoted.*

abstract class Macro {
def impl()(using Quotes): Expr[Int] = '{1}
}
5 changes: 5 additions & 0 deletions tests/pos-macros/i18434/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package user

object Test {
Inline.callMacro()
}