Skip to content

Commit 1599314

Browse files
committed
Add reflect fresh names API
* Add `Name.fresh` * Remove `newUniqueMethod` * Remove `newUniqueVal` This API is necessary to be able to create new class members without having name clashes. It should also be usable to create new top level definitions that have names that do not clash. This version of unique name can be used for `val`, `def` and `class` symbols.
1 parent beadb88 commit 1599314

File tree

31 files changed

+98
-103
lines changed

31 files changed

+98
-103
lines changed

compiler/src/dotty/tools/dotc/core/NameKinds.scala

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ object NameKinds {
300300
val UniqueInlineName: UniqueNameKind = new UniqueNameKind("$i")
301301
val InlineScrutineeName: UniqueNameKind = new UniqueNameKind("$scrutinee")
302302
val InlineBinderName: UniqueNameKind = new UniqueNameKind("$proxy")
303+
val MacroNames: UniqueNameKind = new UniqueNameKind("$macro$")
303304

304305
/** A kind of unique extension methods; Unlike other unique names, these can be
305306
* unmangled.

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

+7-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import dotty.tools.dotc.quoted.{MacroExpansion, PickledQuotes}
2121
import scala.quoted.runtime.{QuoteUnpickler, QuoteMatching}
2222
import scala.quoted.runtime.impl.printers._
2323

24+
import scala.annotation.experimental
2425
import scala.reflect.TypeTest
2526

2627
object QuotesImpl {
@@ -2485,14 +2486,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
24852486
newMethod(owner, name, tpe, Flags.EmptyFlags, noSymbol)
24862487
def newMethod(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
24872488
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | dotc.core.Flags.Method, tpe, privateWithin)
2488-
def newUniqueMethod(owner: Symbol, namePrefix: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
2489-
val name = NameKinds.UniqueName.fresh(namePrefix.toTermName)
2490-
dotc.core.Symbols.newSymbol(owner, name, dotc.core.Flags.PrivateMethod | flags, tpe, privateWithin)
24912489
def newVal(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
24922490
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags, tpe, privateWithin)
2493-
def newUniqueVal(owner: Symbol, namePrefix: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
2494-
val name = NameKinds.UniqueName.fresh(namePrefix.toTermName)
2495-
dotc.core.Symbols.newSymbol(owner, name, flags, tpe, privateWithin)
24962491
def newBind(owner: Symbol, name: String, flags: Flags, tpe: TypeRepr): Symbol =
24972492
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | Case, tpe)
24982493
def noSymbol: Symbol = dotc.core.Symbols.NoSymbol
@@ -2810,6 +2805,12 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
28102805
end extension
28112806
end FlagsMethods
28122807

2808+
@experimental
2809+
object Name extends NameModule:
2810+
def fresh(name: String): String =
2811+
NameKinds.MacroNames.fresh(name.toTermName).toString
2812+
end Name
2813+
28132814
type Position = dotc.util.SourcePosition
28142815

28152816
object Position extends PositionModule:

library/src/scala/annotation/MacroAnnotation.scala

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ trait MacroAnnotation extends StaticAnnotation:
3535
* case DefDef(name, TermParamClause(param :: Nil) :: Nil, tpt, Some(rhsTree)) =>
3636
* (Ref(param.symbol).asExpr, rhsTree.asExpr) match
3737
* case ('{ $paramRefExpr: t }, '{ $rhsExpr: u }) =>
38-
* val cacheSymbol = Symbol.newUniqueVal(tree.symbol.owner, name + "Cache", TypeRepr.of[mutable.Map[t, u]], Flags.Private, Symbol.noSymbol)
38+
* val cacheName = Name.fresh(name + "Cache")
39+
* val cacheSymbol = Symbol.newVal(tree.symbol.owner, cacheName, TypeRepr.of[mutable.Map[t, u]], Flags.Private, Symbol.noSymbol)
3940
* val cacheRhs = '{ mutable.Map.empty[t, u] }.asTerm
4041
* val cacheVal = ValDef(cacheSymbol, Some(cacheRhs))
4142
* val cacheRefExpr = Ref(cacheSymbol).asExprOf[mutable.Map[t, u]]
@@ -55,10 +56,10 @@ trait MacroAnnotation extends StaticAnnotation:
5556
* ```
5657
* and the macro will modify the definition to create
5758
* ```scala
58-
* val fibCache =
59+
* val macro$fibCache$1 =
5960
* scala.collection.mutable.Map.empty[Int, Int]
6061
* def fib(n: Int): Int =
61-
* fibCache.getOrElseUpdate(
62+
* macro$fibCache$1.getOrElseUpdate(
6263
* n,
6364
* {
6465
* println(s"compute fib of $n")
@@ -104,7 +105,7 @@ trait MacroAnnotation extends StaticAnnotation:
104105
* Some(equalsExpr[c](argss.head.head.asExpr, fields).asTerm)
105106
* val equalsOverrideDef = DefDef(equalsOverrideSym, equalsOverrideDefBody)
106107
*
107-
* val hashSym = Symbol.newUniqueVal(cls, "hash", TypeRepr.of[Int], Flags.Private | Flags.Lazy, Symbol.noSymbol)
108+
* val hashSym = Symbol.newVal(cls, Name.fresh("hash"), TypeRepr.of[Int], Flags.Private | Flags.Lazy, Symbol.noSymbol)
108109
* val hashVal = ValDef(hashSym, Some(hashCodeExpr(className, fields)(using hashSym.asQuotes).asTerm))
109110
*
110111
* val hashCodeSym = Symbol.requiredMethod("java.lang.Object.hashCode")
@@ -167,12 +168,12 @@ trait MacroAnnotation extends StaticAnnotation:
167168
* that match
168169
* case that: User => this.name == that.name && this.id == that.id
169170
* case _ => false
170-
* private lazy val hash$1: Int =
171+
* private lazy val macro$hash$1: Int =
171172
* var acc = 515782504 // scala.runtime.Statics.mix(-889275714, "User".hashCode)
172173
* acc = scala.runtime.Statics.mix(acc, scala.runtime.Statics.anyHash(name))
173174
* acc = scala.runtime.Statics.mix(acc, id)
174175
* scala.runtime.Statics.finalizeHash(acc, 2)
175-
* override def hashCode(): Int = hash$1
176+
* override def hashCode(): Int = macro$hash$1
176177
* ```
177178
*
178179
* @param Quotes Implicit instance of Quotes used for tree reflection

library/src/scala/quoted/Quotes.scala

+15-38
Original file line numberDiff line numberDiff line change
@@ -3669,25 +3669,6 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
36693669
*/
36703670
def newMethod(parent: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol
36713671

3672-
/** Generates a new method symbol with the given parent, name and type.
3673-
*
3674-
* To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
3675-
*
3676-
* @param parent The owner of the method
3677-
* @param name The name of the method
3678-
* @param tpe The type of the method (MethodType, PolyType, ByNameType)
3679-
* @param flags extra flags to with which the symbol should be constructed
3680-
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3681-
*
3682-
* This symbol starts without an accompanying definition.
3683-
* It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3684-
* this symbol to the DefDef constructor.
3685-
*
3686-
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3687-
* direct or indirect children of the reflection context's owner.
3688-
*/
3689-
@experimental def newUniqueMethod(parent: Symbol, namePrefix: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol
3690-
36913672
/** Generates a new val/var/lazy val symbol with the given parent, name and type.
36923673
*
36933674
* This symbol starts without an accompanying definition.
@@ -3706,25 +3687,6 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
37063687
*/
37073688
def newVal(parent: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol
37083689

3709-
/** Generates a new val/var/lazy val symbol with the given parent, name prefix and type.
3710-
*
3711-
* This symbol starts without an accompanying definition.
3712-
* It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3713-
* this symbol to the ValDef constructor.
3714-
*
3715-
* Note: Also see newVal
3716-
* Note: Also see ValDef.let
3717-
*
3718-
* @param parent The owner of the val/var/lazy val
3719-
* @param name The name of the val/var/lazy val
3720-
* @param tpe The type of the val/var/lazy val
3721-
* @param flags extra flags to with which the symbol should be constructed
3722-
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3723-
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3724-
* direct or indirect children of the reflection context's owner.
3725-
*/
3726-
@experimental def newUniqueVal(parent: Symbol, namePrefix: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol
3727-
37283690
/** Generates a pattern bind symbol with the given parent, name and type.
37293691
*
37303692
* This symbol starts without an accompanying definition.
@@ -4415,6 +4377,21 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
44154377
end extension
44164378
}
44174379

4380+
///////////////
4381+
// Names //
4382+
///////////////
4383+
4384+
/** Module object with utilities on definition names */
4385+
@experimental
4386+
val Name: NameModule
4387+
4388+
/** Methods of the module object `val Name` */
4389+
@experimental
4390+
trait NameModule { this: Name.type =>
4391+
/** TODO */
4392+
def fresh(name: String): String
4393+
}
4394+
44184395
///////////////
44194396
// POSITIONS //
44204397
///////////////

project/MiMaFilters.scala

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ object MiMaFilters {
2323
ProblemFilters.exclude[MissingClassProblem]("scala.caps"),
2424
ProblemFilters.exclude[MissingClassProblem]("scala.caps$Pure"),
2525
ProblemFilters.exclude[MissingClassProblem]("scala.caps$unsafe$"),
26+
27+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule.Name"),
2628
)
2729
val TastyCore: Seq[ProblemFilter] = Seq(
2830
ProblemFilters.exclude[MissingMethodProblem]("dotty.tools.tasty.TastyFormat.APPLYsigpoly"),

tests/neg-macros/annot-accessIndirect/Macro_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import scala.quoted._
55
class hello extends MacroAnnotation {
66
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
77
import quotes.reflect._
8-
val helloSymbol = Symbol.newUniqueVal(tree.symbol.owner, "hello", TypeRepr.of[String], Flags.EmptyFlags, Symbol.noSymbol)
8+
val helloSymbol = Symbol.newVal(tree.symbol.owner, Name.fresh("hello"), TypeRepr.of[String], Flags.EmptyFlags, Symbol.noSymbol)
99
val helloVal = ValDef(helloSymbol, Some(Literal(StringConstant("Hello, World!"))))
1010
List(helloVal, tree)
1111
}

tests/neg-macros/annot-mod-class-add-top-method.check

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
-- Error: tests/neg-macros/annot-mod-class-add-top-method/Test_2.scala:1:0 ---------------------------------------------
33
1 |@addTopLevelMethod // error
44
|^^^^^^^^^^^^^^^^^^
5-
|macro annotation can not add top-level method. @addTopLevelMethod tried to add method toLevelMethod$1.
5+
|macro annotation can not add top-level method. @addTopLevelMethod tried to add method toLevelMethod$macro$1.
66
-- Error: tests/neg-macros/annot-mod-class-add-top-method/Test_2.scala:4:0 ---------------------------------------------
77
4 |@addTopLevelMethod // error
88
|^^^^^^^^^^^^^^^^^^
9-
|macro annotation can not add top-level method. @addTopLevelMethod tried to add method toLevelMethod$2.
9+
|macro annotation can not add top-level method. @addTopLevelMethod tried to add method toLevelMethod$macro$2.

tests/neg-macros/annot-mod-class-add-top-method/Macro_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class addTopLevelMethod extends MacroAnnotation:
1010
case ClassDef(name, ctr, parents, self, body) =>
1111
val cls = tree.symbol
1212
val methType = MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Int])
13-
val methSym = Symbol.newUniqueMethod(cls.owner, "toLevelMethod", methType, Flags.EmptyFlags, Symbol.noSymbol)
13+
val methSym = Symbol.newMethod(cls.owner, Name.fresh("toLevelMethod"), methType, Flags.EmptyFlags, Symbol.noSymbol)
1414
val methDef = DefDef(methSym, _ => Some(Literal(IntConstant(1))))
1515
List(methDef, tree)
1616
case _ =>

tests/neg-macros/annot-mod-class-add-top-val.check

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
-- Error: tests/neg-macros/annot-mod-class-add-top-val/Test_2.scala:1:0 ------------------------------------------------
33
1 |@addTopLevelVal // error
44
|^^^^^^^^^^^^^^^
5-
|macro annotation can not add top-level value. @addTopLevelVal tried to add value toLevelvalod$1.
5+
|macro annotation can not add top-level value. @addTopLevelVal tried to add value toLevelVal$macro$1.
66
-- Error: tests/neg-macros/annot-mod-class-add-top-val/Test_2.scala:4:0 ------------------------------------------------
77
4 |@addTopLevelVal // error
88
|^^^^^^^^^^^^^^^
9-
|macro annotation can not add top-level value. @addTopLevelVal tried to add value toLevelvalod$2.
9+
|macro annotation can not add top-level value. @addTopLevelVal tried to add value toLevelVal$macro$2.

tests/neg-macros/annot-mod-class-add-top-val/Macro_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class addTopLevelVal extends MacroAnnotation:
99
tree match
1010
case ClassDef(name, ctr, parents, self, body) =>
1111
val cls = tree.symbol
12-
val valSym = Symbol.newUniqueVal(cls.owner, "toLevelvalod", TypeRepr.of[Int], Flags.EmptyFlags, Symbol.noSymbol)
12+
val valSym = Symbol.newVal(cls.owner, Name.fresh("toLevelVal"), TypeRepr.of[Int], Flags.EmptyFlags, Symbol.noSymbol)
1313
val valDef = DefDef(valSym, _ => Some(Literal(IntConstant(1))))
1414
List(valDef, tree)
1515
case _ =>

tests/neg-macros/annot-result-owner.check

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
-- Error: tests/neg-macros/annot-result-owner/Test_2.scala:1:0 ---------------------------------------------------------
33
1 |@insertVal // error
44
|^^^^^^^^^^
5-
|macro annotation @insertVal added value definitionWithWrongOwner$1 with an inconsistent owner. Expected it to be owned by package object Test_2$package but was owned by method foo.
5+
|macro annotation @insertVal added value definitionWithWrongOwner$macro$1 with an inconsistent owner. Expected it to be owned by package object Test_2$package but was owned by method foo.
66
-- Error: tests/neg-macros/annot-result-owner/Test_2.scala:5:2 ---------------------------------------------------------
77
5 | @insertVal // error
88
| ^^^^^^^^^^
9-
|macro annotation @insertVal added value definitionWithWrongOwner$2 with an inconsistent owner. Expected it to be owned by method bar but was owned by method foo.
9+
|macro annotation @insertVal added value definitionWithWrongOwner$macro$2 with an inconsistent owner. Expected it to be owned by method bar but was owned by method foo.

tests/neg-macros/annot-result-owner/Macro_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class insertVal extends MacroAnnotation:
66
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
77
import quotes.reflect._
88
// Use of wrong owner
9-
val valSym = Symbol.newUniqueVal(tree.symbol, "definitionWithWrongOwner", TypeRepr.of[Unit], Flags.Private, Symbol.noSymbol)
9+
val valSym = Symbol.newVal(tree.symbol, Name.fresh("definitionWithWrongOwner"), TypeRepr.of[Unit], Flags.Private, Symbol.noSymbol)
1010
val valDef = ValDef(valSym, Some('{}.asTerm))
1111
List(valDef, tree)

tests/run-custom-args/tasty-inspector/stdlibExperimentalDefinitions.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ val experimentalDefinitionInLibrary = Set(
7474
// Need experimental annotation macros to check that design works.
7575
"scala.quoted.Quotes.reflectModule.ClassDefModule.apply",
7676
"scala.quoted.Quotes.reflectModule.SymbolModule.newClass",
77-
"scala.quoted.Quotes.reflectModule.SymbolModule.newUniqueMethod",
78-
"scala.quoted.Quotes.reflectModule.SymbolModule.newUniqueVal",
7977
"scala.quoted.Quotes.reflectModule.SymbolMethods.info",
78+
"scala.quoted.Quotes.reflectModule.Name",
79+
"scala.quoted.Quotes.reflectModule.NameModule",
8080

8181
// New APIs: Lightweight lazy vals. Can be stabilized in 3.3.0
8282
"scala.runtime.LazyVals$.Evaluating",

tests/run-macros/annot-bind/Macro_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class bind(str: String) extends MacroAnnotation:
77
import quotes.reflect._
88
tree match
99
case ValDef(name, tpt, Some(rhsTree)) =>
10-
val valSym = Symbol.newUniqueVal(tree.symbol.owner, str, tpt.tpe, Flags.Private, Symbol.noSymbol)
10+
val valSym = Symbol.newVal(tree.symbol.owner, Name.fresh(str), tpt.tpe, Flags.Private, Symbol.noSymbol)
1111
val valDef = ValDef(valSym, Some(rhsTree))
1212
val newRhs = Ref(valSym)
1313
val newTree = ValDef.copy(tree)(name, tpt, Some(newRhs))

tests/run-macros/annot-generate/Macro_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import scala.quoted._
55
class hello extends MacroAnnotation {
66
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
77
import quotes.reflect._
8-
val helloSymbol = Symbol.newUniqueVal(tree.symbol.owner, "hello", TypeRepr.of[String], Flags.EmptyFlags, Symbol.noSymbol)
8+
val helloSymbol = Symbol.newVal(tree.symbol.owner, Name.fresh("hello"), TypeRepr.of[String], Flags.EmptyFlags, Symbol.noSymbol)
99
val helloVal = ValDef(helloSymbol, Some(Literal(StringConstant("Hello, World!"))))
1010
List(helloVal, tree)
1111
}

tests/run-macros/annot-memo/Macro_1.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class memoize extends MacroAnnotation:
1010
case DefDef(name, TermParamClause(param :: Nil) :: Nil, tpt, Some(rhsTree)) =>
1111
(Ref(param.symbol).asExpr, rhsTree.asExpr) match
1212
case ('{ $paramRefExpr: t }, '{ $rhsExpr: u }) =>
13-
val cacheSymbol = Symbol.newUniqueVal(tree.symbol.owner, name + "Cache", TypeRepr.of[mutable.Map[t, u]], Flags.Private, Symbol.noSymbol)
13+
val cacheName = Name.fresh(name + "Cache")
14+
val cacheSymbol = Symbol.newVal(tree.symbol.owner, cacheName, TypeRepr.of[mutable.Map[t, u]], Flags.Private, Symbol.noSymbol)
1415
val cacheRhs = '{ mutable.Map.empty[t, u] }.asTerm
1516
val cacheVal = ValDef(cacheSymbol, Some(cacheRhs))
1617
val cacheRefExpr = Ref(cacheSymbol).asExprOf[mutable.Map[t, u]]

tests/run-macros/annot-memo/Test_2.scala

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ class Bar:
55
if n <= 1 then n
66
else fib(n - 1) + fib(n - 2)
77

8+
//> private val fibCache$macro$1: mutable.Map[(n : Int), Int] = mutable.Map.empty[(n : Int), Int] // FIXME: leaks (n : Int)
9+
//> @memoize def fib(n: Int): Int =
10+
//> fibCache$macro$1.getOrElseUpdate(n, {
11+
//> println(s"compute fib of $n")
12+
//> if n <= 1 then n
13+
//> else fib(n - 1) + fib(n - 2)
14+
//> })
15+
816
@memoize
917
def fib(n: Long): Long =
1018
println(s"compute fib of $n")

tests/run-macros/annot-mod-class-add-def/Macro_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class addIndirectToString(msg: String) extends MacroAnnotation:
1010
case ClassDef(name, ctr, parents, self, body) =>
1111
val cls = tree.symbol
1212
val stringMethType = ByNameType.apply(TypeRepr.of[String])
13-
val stringSym = Symbol.newUniqueMethod(cls, "string", stringMethType, Flags.Private, Symbol.noSymbol)
13+
val stringSym = Symbol.newMethod(cls, Name.fresh("string"), stringMethType, Flags.Private, Symbol.noSymbol)
1414
val stringDef = DefDef(stringSym, _ => Some(Literal(StringConstant(msg))))
1515

1616
val toStringMethType = Symbol.requiredMethod("java.lang.Object.toString").info

tests/run-macros/annot-mod-class-add-def/Test_2.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
@addIndirectToString("This is Foo")
22
class Foo
3-
//> private def string$1: String = "This is Foo"
4-
//> def toString(): String = toString$1
3+
//> private def string$macro$1: String = "This is Foo"
4+
//> def toString(): String = string$macro$1
55

66
@addIndirectToString("This is Foo object")
77
object Foo
8-
//> private def string$2: String = "This is Foo object"
9-
//> def toString(): String = string$2
8+
//> private def string$macro$2: String = "This is Foo object"
9+
//> def toString(): String = string$macro$2
1010

1111
@main def Test(): Unit =
1212
val foo = new Foo

tests/run-macros/annot-mod-class-add-inner-class/Macro_1.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class addInnerClass extends MacroAnnotation:
1313
def showClassDecls(showCls: Symbol): List[Symbol] =
1414
List(Symbol.newMethod(showCls, "showMe", MethodType(List("x"))(_ => List(cls.typeRef), _ => TypeRepr.of[String])))
1515
val parents = List(TypeTree.of[Object])
16-
// FIXME use unique mane instead of "Show" to avoid conflicts
17-
val showClassSym = Symbol.newClass(cls, "Show", parents.map(_.tpe), showClassDecls, None)
16+
val showClassSym = Symbol.newClass(cls, Name.fresh("Show"), parents.map(_.tpe), showClassDecls, None)
1817
val showMeSym = showClassSym.declaredMethod("showMe").head
1918

2019
val showMeDef = DefDef(showMeSym, argss => Some('{ "showMe: " + ${argss.head.head.asExpr}.getClass }.asTerm))

tests/run-macros/annot-mod-class-add-lazy-val/Macro_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class addMemoToString(msg: String) extends MacroAnnotation:
99
tree match
1010
case ClassDef(name, ctr, parents, self, body) =>
1111
val cls = tree.symbol
12-
val stringLazyValSym = Symbol.newUniqueVal(cls, "toString", TypeRepr.of[String], Flags.Lazy | Flags.Private, Symbol.noSymbol)
12+
val stringLazyValSym = Symbol.newVal(cls, Name.fresh("string"), TypeRepr.of[String], Flags.Lazy | Flags.Private, Symbol.noSymbol)
1313

1414
val toStringMethType = Symbol.requiredMethod("java.lang.Object.toString").info
1515
val toStringOverrideSym = Symbol.newMethod(cls, "toString", toStringMethType, Flags.Override, Symbol.noSymbol)

0 commit comments

Comments
 (0)