Skip to content

Commit

Permalink
Correctly handle backticked macro keyword in Scala 3 (#163)
Browse files Browse the repository at this point in the history
Change the synthetic check to use flags and exposed methods instead of
names.

#120
  • Loading branch information
KacperFKorban authored May 21, 2024
1 parent bbe8e3f commit b1c3d74
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion sourcecode/src-3/sourcecode/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,18 @@ trait ArgsMacros {
}

object Util{
def isSynthetic(using Quotes)(s: quotes.reflect.Symbol) = isSyntheticName(getName(s))
def isSynthetic(using Quotes)(s: quotes.reflect.Symbol) =
isSyntheticAlt(s)

def isSyntheticAlt(using Quotes)(s: quotes.reflect.Symbol) = {
import quotes.reflect._
s.flags.is(Flags.Synthetic) || s.isClassConstructor || s.isLocalDummy || isScala2Macro(s)
}
def isScala2Macro(using Quotes)(s: quotes.reflect.Symbol) = {
import quotes.reflect._
(s.flags.is(Flags.Macro) && s.owner.flags.is(Flags.Scala2x)) ||
(s.flags.is(Flags.Macro) && !s.flags.is(Flags.Inline))
}
def isSyntheticName(name: String) = {
name == "<init>" || (name.startsWith("<local ") && name.endsWith(">")) || name == "$anonfun" || name == "macro"
}
Expand Down
13 changes: 13 additions & 0 deletions sourcecode/test/src/sourcecode/SpecialName.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sourcecode

object SpecialName {

def macroValRun() = {
def keyword(implicit name: sourcecode.Name): String = name.value

val `macro` = keyword

assert(`macro` == "macro")
}

}
1 change: 1 addition & 0 deletions sourcecode/test/src/sourcecode/Tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object Tests{
EnumFull.run()
NoSynthetic.run()
Synthetic.run()
SpecialName.macroValRun()
ManualImplicit()
TextTests()
ArgsTests()
Expand Down

0 comments on commit b1c3d74

Please sign in to comment.