@@ -67,7 +67,7 @@ trait ArgsMacros {
6767object Util {
6868 def isSynthetic (c : Reflection )(s : c.Symbol ) = isSyntheticName(getName(c)(s))
6969 def isSyntheticName (name : String ) = {
70- name == " <init>" || (name.startsWith(" <local " ) && name.endsWith(" >" )) || name == " $anonfun"
70+ name == " <init>" || (name.startsWith(" <local " ) && name.endsWith(" >" )) || name == " $anonfun" || name.startsWith( " macro$ " )
7171 }
7272 def getName (c : Reflection )(s : c.Symbol ) = {
7373 import c .given
@@ -78,16 +78,28 @@ object Util{
7878
7979object Macros {
8080
81- def actualOwner (c : Reflection )(owner : c.Symbol ): c.Symbol = {
81+ def findOwner (c : Reflection )(owner : c.Symbol , skipIf : ( c : Reflection ) => (c. Symbol ) => Boolean ): c.Symbol = {
8282 import c .given
8383 var owner0 = owner
84- // second condition is meh
85- while (Util .isSynthetic(c)(owner0) || Util .getName(c)(owner0) == " ev" ) {
86- owner0 = owner0.owner
87- }
84+ while (skipIf(c)(owner0)) owner0 = owner0.owner
8885 owner0
8986 }
9087
88+ def actualOwner (c : Reflection )(owner : c.Symbol ): c.Symbol =
89+ findOwner(c)(owner, c => owner0 => Util .isSynthetic(c)(owner0) || Util .getName(c)(owner0) == " ev" )
90+
91+ /**
92+ * In Scala 3, macro `mcr()` is expanded to:
93+ *
94+ * val macro$n = ...
95+ * macro$n
96+ *
97+ * Where n is an ordinal. This method returns the first owner that is not
98+ * such a synthetic variable.
99+ */
100+ def nonMacroOwner (c : Reflection )(owner : c.Symbol ): c.Symbol =
101+ findOwner(c)(owner, c => owner0 => Util .getName(c)(owner0).startsWith(" macro$" ))
102+
91103 def nameImpl (given ctx : QuoteContext ): Expr [Name ] = {
92104 import ctx .tasty .given
93105 val owner = actualOwner(ctx.tasty)(ctx.tasty.rootContext.owner)
@@ -104,7 +116,7 @@ object Macros {
104116
105117 def nameMachineImpl (given ctx : QuoteContext ): Expr [Name .Machine ] = {
106118 import ctx .tasty .given
107- val owner = ctx.tasty. rootContext.owner
119+ val owner = nonMacroOwner( ctx.tasty)(ctx.tasty. rootContext.owner)
108120 val simpleName = adjustName(Util .getName(ctx.tasty)(owner))
109121 ' {Name .Machine ($ {Expr (simpleName)})}
110122 }
@@ -127,7 +139,7 @@ object Macros {
127139
128140 def fullNameMachineImpl (given ctx : QuoteContext ): Expr [FullName .Machine ] = {
129141 import ctx .tasty .given
130- val owner = ctx.tasty. rootContext.owner
142+ val owner = nonMacroOwner( ctx.tasty)(ctx.tasty. rootContext.owner)
131143 val fullName = owner.fullName.trim
132144 .split(" \\ ." , - 1 )
133145 .map(_.stripPrefix(" _$" ).stripSuffix(" $" )) // meh
@@ -227,6 +239,8 @@ object Macros {
227239 var current = c.rootContext.owner
228240 if (! machine)
229241 current = actualOwner(c)(current)
242+ else
243+ current = nonMacroOwner(c)(current)
230244 var path = List .empty[Chunk ]
231245 while (current != Symbol .noSymbol && current != defn.RootPackage && current != defn.RootClass ){
232246 if (filter(current)) {
0 commit comments