From 921a9267da1a928e4eba733da75dc66cfb601f59 Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Tue, 14 May 2024 22:04:44 +0200 Subject: [PATCH] Correctly handle backticked macro keyword in Scala 3 --- sourcecode/src-3/sourcecode/Macros.scala | 13 ++++++++++++- sourcecode/test/src/sourcecode/SpecialName.scala | 13 +++++++++++++ sourcecode/test/src/sourcecode/Tests.scala | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 sourcecode/test/src/sourcecode/SpecialName.scala diff --git a/sourcecode/src-3/sourcecode/Macros.scala b/sourcecode/src-3/sourcecode/Macros.scala index 995f77e..8df1c84 100644 --- a/sourcecode/src-3/sourcecode/Macros.scala +++ b/sourcecode/src-3/sourcecode/Macros.scala @@ -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 == "" || (name.startsWith("")) || name == "$anonfun" || name == "macro" } diff --git a/sourcecode/test/src/sourcecode/SpecialName.scala b/sourcecode/test/src/sourcecode/SpecialName.scala new file mode 100644 index 0000000..f09362e --- /dev/null +++ b/sourcecode/test/src/sourcecode/SpecialName.scala @@ -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") + } + +} diff --git a/sourcecode/test/src/sourcecode/Tests.scala b/sourcecode/test/src/sourcecode/Tests.scala index 4bfd38b..6a8f881 100644 --- a/sourcecode/test/src/sourcecode/Tests.scala +++ b/sourcecode/test/src/sourcecode/Tests.scala @@ -18,6 +18,7 @@ object Tests{ EnumFull.run() NoSynthetic.run() Synthetic.run() + SpecialName.macroValRun() ManualImplicit() TextTests() ArgsTests()