Skip to content

Commit f9cddcf

Browse files
Merge pull request #8291 from dotty-staging/add-reflect-symbol-new-val
Add Reflection.Symbol.newVal
2 parents 1681e2d + 2a62424 commit f9cddcf

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
6767
def Context_requiredMethod(self: Context)(path: String): Symbol = self.requiredMethod(path)
6868
def Context_isJavaCompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.JavaCompilationUnit]
6969
def Context_isScala2CompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.Scala2CompilationUnit]
70-
def Context_compilationUnitClassname(self: Context): String =
70+
def Context_compilationUnitClassname(self: Context): String =
7171
self.compilationUnit match {
7272
case cu: fromtasty.JavaCompilationUnit => cu.className
7373
case cu: fromtasty.Scala2CompilationUnit => cu.className
@@ -1758,10 +1758,11 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
17581758
def Symbol_of(fullName: String)(using ctx: Context): Symbol =
17591759
ctx.requiredClass(fullName)
17601760

1761-
def Symbol_newMethod(parent: Symbol, name: String, flags: Flags, tpe: Type, privateWithin: Symbol)(using ctx: Context): Symbol = {
1762-
val computedFlags = flags | Flags.Method
1763-
ctx.newSymbol(parent, name.toTermName, computedFlags, tpe, privateWithin)
1764-
}
1761+
def Symbol_newMethod(parent: Symbol, name: String, flags: Flags, tpe: Type, privateWithin: Symbol)(using ctx: Context): Symbol =
1762+
ctx.newSymbol(parent, name.toTermName, flags | Flags.Method, tpe, privateWithin)
1763+
1764+
def Symbol_newVal(parent: Symbol, name: String, flags: Flags, tpe: Type, privateWithin: Symbol)(using ctx: Context): Symbol =
1765+
ctx.newSymbol(parent, name.toTermName, flags, tpe, privateWithin)
17651766

17661767
def Symbol_isTypeParam(self: Symbol)(using ctx: Context): Boolean =
17671768
self.isTypeParam

library/src/scala/tasty/Reflection.scala

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,22 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
21092109
def newMethod(parent: Symbol, name: String, tpe: Type, flags: Flags, privateWithin: Symbol)(using ctx: Context): Symbol =
21102110
internal.Symbol_newMethod(parent, name, flags, tpe, privateWithin)
21112111

2112+
/** Generates a new val/var/lazy val symbol with the given parent, name and type.
2113+
*
2114+
* This symbol starts without an accompanying definition.
2115+
* It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
2116+
* this symbol to the ValDef constructor.
2117+
*
2118+
* Note: Also see Reflection.let
2119+
*
2120+
* @param flags extra flags to with which the symbol should be constructed
2121+
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
2122+
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
2123+
* direct or indirect children of the reflection context's owner.
2124+
*/
2125+
def newVal(parent: Symbol, name: String, tpe: Type, flags: Flags, privateWithin: Symbol)(using ctx: Context): Symbol =
2126+
internal.Symbol_newVal(parent, name, flags, tpe, privateWithin)
2127+
21122128
/** Definition not available */
21132129
def noSymbol(using ctx: Context): Symbol =
21142130
internal.Symbol_noSymbol
@@ -2772,19 +2788,8 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
27722788

27732789
/** Bind the `rhs` to a `val` and use it in `body` */
27742790
def let(rhs: Term)(body: Ident => Term)(using ctx: Context): Term = {
2775-
import scala.quoted.QuoteContext
2776-
given QuoteContext = new QuoteContext(this)
2777-
val expr = (rhs.seal: @unchecked) match {
2778-
case '{ $rhsExpr: $t } =>
2779-
'{
2780-
val x = $rhsExpr
2781-
${
2782-
val id = ('x).unseal.asInstanceOf[Ident]
2783-
body(id).seal
2784-
}
2785-
}
2786-
}
2787-
expr.unseal
2791+
val sym = Symbol.newVal(ctx.owner, "x", rhs.tpe.widen, Flags.EmptyFlags, Symbol.noSymbol)
2792+
Block(List(ValDef(sym, Some(rhs))), body(Ref(sym).asInstanceOf[Ident]))
27882793
}
27892794

27902795
/** Bind the given `terms` to names and use them in the `body` */

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,8 @@ trait CompilerInterface {
13091309

13101310
def Symbol_newMethod(parent: Symbol, name: String, flags: Flags, tpe: Type, privateWithin: Symbol)(using ctx: Context): Symbol
13111311

1312+
def Symbol_newVal(parent: Symbol, name: String, flags: Flags, tpe: Type, privateWithin: Symbol)(using ctx: Context): Symbol
1313+
13121314
def Symbol_isTypeParam(self: Symbol)(using ctx: Context): Boolean
13131315

13141316
def Symbol_isPackageDef(symbol: Symbol)(using ctx: Context): Boolean

0 commit comments

Comments
 (0)