Skip to content

Commit 61764dd

Browse files
hamzaremmaljchyb
authored andcommitted
Add the possibility to create a typeSymbol in the Quotes API
1 parent ac28899 commit 61764dd

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2649,6 +2649,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
26492649
def newBind(owner: Symbol, name: String, flags: Flags, tpe: TypeRepr): Symbol =
26502650
checkValidFlags(flags.toTermFlags, Flags.validBindFlags)
26512651
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | dotc.core.Flags.Case, tpe)
2652+
2653+
def newType(owner: Symbol, name: String, flags: Flags, tpe: TypeRepr, privateWithin: Symbol): Symbol =
2654+
checkValidFlags(flags.toTypeFlags, Flags.validTypeFlags)
2655+
dotc.core.Symbols.newSymbol(owner, name.toTypeName, flags | dotc.core.Flags.Deferred, tpe, privateWithin)
2656+
26522657
def noSymbol: Symbol = dotc.core.Symbols.NoSymbol
26532658

26542659
private inline def checkValidFlags(inline flags: Flags, inline valid: Flags): Unit =
@@ -2989,6 +2994,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
29892994

29902995
// Keep: aligned with Quotes's `newBind` doc
29912996
private[QuotesImpl] def validBindFlags: Flags = Case // Flags that could be allowed: Implicit | Given | Erased
2997+
2998+
private[QuotesImpl] def validTypeFlags: Flags = Private | Protected | Override | Deferred | Final | Infix | Local
2999+
29923000
end Flags
29933001

29943002
given FlagsMethods: FlagsMethods with

library/src/scala/quoted/Quotes.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3963,6 +3963,24 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
39633963
// Keep: `flags` doc aligned with QuotesImpl's `validBindFlags`
39643964
def newBind(parent: Symbol, name: String, flags: Flags, tpe: TypeRepr): Symbol
39653965

3966+
/** Generate a new type symbol with the given parent, name and type
3967+
*
3968+
* This symbol starts without an accompanying definition.
3969+
* It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3970+
* this symbol to the TypeDef constructor.
3971+
*
3972+
* @param parent The owner of the type
3973+
* @param name The name of the type
3974+
* @param flags extra flags to with which symbol can be constructed. `Deferred` flag will be added. Can be `Private` | `Protected` | `Override` | `Deferred` | `Final` | `Infix` | `Local`
3975+
* @param tpe The rhs or bounds of the type
3976+
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3977+
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3978+
* direct or indirect children of the reflection context's owner.
3979+
*/
3980+
@experimental
3981+
// Keep: `flags` doc aligned with QuotesImpl's `validTypeFlags`
3982+
def newType(parent: Symbol, name: String, flags: Flags, tpe: TypeRepr, privateWithin: Symbol): Symbol
3983+
39663984
/** Definition not available */
39673985
def noSymbol: Symbol
39683986

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.quoted.*
2+
3+
inline def testMacro = ${ testImpl }
4+
5+
def testImpl(using Quotes): Expr[Unit] = {
6+
import quotes.reflect.*
7+
val sym = Symbol.newType(Symbol.spliceOwner, "mytype", Flags.EmptyFlags, TypeRepr.of[String], Symbol.noSymbol)
8+
assert(TypeDef(sym).show == "type mytype = java.lang.String")
9+
'{ () }
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
def test = testMacro

0 commit comments

Comments
 (0)