Skip to content

Commit 1f451ec

Browse files
committed
Add newUniqueVal/newUniqueMethod
1 parent c715a47 commit 1f451ec

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -2485,8 +2485,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
24852485
newMethod(owner, name, tpe, Flags.EmptyFlags, noSymbol)
24862486
def newMethod(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
24872487
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)
24882491
def newVal(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
24892492
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)
24902496
def newBind(owner: Symbol, name: String, flags: Flags, tpe: TypeRepr): Symbol =
24912497
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | Case, tpe)
24922498
def noSymbol: Symbol = dotc.core.Symbols.NoSymbol

library/src/scala/quoted/Quotes.scala

+39-1
Original file line numberDiff line numberDiff line change
@@ -3669,13 +3669,32 @@ 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+
36723691
/** Generates a new val/var/lazy val symbol with the given parent, name and type.
36733692
*
36743693
* This symbol starts without an accompanying definition.
36753694
* It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
36763695
* this symbol to the ValDef constructor.
36773696
*
3678-
* Note: Also see reflect.let
3697+
* Note: Also see ValDef.let
36793698
*
36803699
* @param parent The owner of the val/var/lazy val
36813700
* @param name The name of the val/var/lazy val
@@ -3687,6 +3706,25 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
36873706
*/
36883707
def newVal(parent: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol
36893708

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+
36903728
/** Generates a pattern bind symbol with the given parent, name and type.
36913729
*
36923730
* This symbol starts without an accompanying definition.

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

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ val experimentalDefinitionInLibrary = Set(
7171
// Need experimental annotation macros to check that design works.
7272
"scala.quoted.Quotes.reflectModule.ClassDefModule.apply",
7373
"scala.quoted.Quotes.reflectModule.SymbolModule.newClass",
74+
"scala.quoted.Quotes.reflectModule.SymbolModule.newUniqueMethod",
75+
"scala.quoted.Quotes.reflectModule.SymbolModule.newUniqueVal",
7476

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

0 commit comments

Comments
 (0)