Skip to content

Commit b1c3d74

Browse files
Correctly handle backticked macro keyword in Scala 3 (#163)
Change the synthetic check to use flags and exposed methods instead of names. #120
1 parent bbe8e3f commit b1c3d74

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

sourcecode/src-3/sourcecode/Macros.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,18 @@ trait ArgsMacros {
6464
}
6565

6666
object Util{
67-
def isSynthetic(using Quotes)(s: quotes.reflect.Symbol) = isSyntheticName(getName(s))
67+
def isSynthetic(using Quotes)(s: quotes.reflect.Symbol) =
68+
isSyntheticAlt(s)
69+
70+
def isSyntheticAlt(using Quotes)(s: quotes.reflect.Symbol) = {
71+
import quotes.reflect._
72+
s.flags.is(Flags.Synthetic) || s.isClassConstructor || s.isLocalDummy || isScala2Macro(s)
73+
}
74+
def isScala2Macro(using Quotes)(s: quotes.reflect.Symbol) = {
75+
import quotes.reflect._
76+
(s.flags.is(Flags.Macro) && s.owner.flags.is(Flags.Scala2x)) ||
77+
(s.flags.is(Flags.Macro) && !s.flags.is(Flags.Inline))
78+
}
6879
def isSyntheticName(name: String) = {
6980
name == "<init>" || (name.startsWith("<local ") && name.endsWith(">")) || name == "$anonfun" || name == "macro"
7081
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package sourcecode
2+
3+
object SpecialName {
4+
5+
def macroValRun() = {
6+
def keyword(implicit name: sourcecode.Name): String = name.value
7+
8+
val `macro` = keyword
9+
10+
assert(`macro` == "macro")
11+
}
12+
13+
}

sourcecode/test/src/sourcecode/Tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ object Tests{
1818
EnumFull.run()
1919
NoSynthetic.run()
2020
Synthetic.run()
21+
SpecialName.macroValRun()
2122
ManualImplicit()
2223
TextTests()
2324
ArgsTests()

0 commit comments

Comments
 (0)