Skip to content

Move currentOwner to Symbol #9629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte

type Context = core.Contexts.Context

def Context_owner(self: Context): Symbol = self.owner


/////////////////
// Constraints //
/////////////////
Expand Down Expand Up @@ -1674,6 +1671,8 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte

type Symbol = core.Symbols.Symbol

def Symbol_currentOwner(using ctx: Context): Symbol = ctx.owner

def Symbol_owner(self: Symbol)(using Context): Symbol = self.owner
def Symbol_maybeOwner(self: Symbol)(using Context): Symbol = self.maybeOwner

Expand Down
10 changes: 3 additions & 7 deletions library/src/scala/internal/tasty/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ trait CompilerInterface extends scala.tasty.reflect.Types {
def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): TypeTree


/////////////
// CONTEXT //
/////////////

/** Returns the owner of the context */
def Context_owner(self: Context): Symbol

/////////////////
// Constraints //
/////////////////
Expand Down Expand Up @@ -853,6 +846,9 @@ trait CompilerInterface extends scala.tasty.reflect.Types {
// SYMBOLS //
/////////////

/** Returns the symbol of the enclosing definition of the given context */
def Symbol_currentOwner(using ctx: Context): Symbol

/** Owner of this symbol. The owner is the symbol in which this symbol is defined. Throws if this symbol does not have an owner. */
def Symbol_owner(self: Symbol)(using ctx: Context): Symbol

Expand Down
14 changes: 4 additions & 10 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
def rootContext: Context // TODO: Should this be moved to QuoteContext?
given Context = rootContext // TODO: Should be an implicit converion from QuoteContext to Context

given ContextOps as Context.type = Context

object Context:
extension (self: Context):
/** Returns the owner of the context */
def owner: Symbol = reflectSelf.Context_owner(self)
end extension
end Context


///////////////
// Source //
Expand Down Expand Up @@ -1903,6 +1894,9 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>

object Symbol:

/** Returns the symbol of the current enclosing definition */
def currentOwner(using ctx: Context): Symbol = reflectSelf.Symbol_currentOwner

/** Get package symbol if package is either defined in current compilation run or present on classpath. */
def requiredPackage(path: String)(using ctx: Context): Symbol = reflectSelf.Symbol_requiredPackage(path)

Expand Down Expand Up @@ -2680,7 +2674,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>

/** Bind the `rhs` to a `val` and use it in `body` */
def let(rhs: Term)(body: Ident => Term)(using ctx: Context): Term = {
val sym = Symbol.newVal(ctx.owner, "x", rhs.tpe.widen, Flags.EmptyFlags, Symbol.noSymbol)
val sym = Symbol.newVal(Symbol.currentOwner, "x", rhs.tpe.widen, Flags.EmptyFlags, Symbol.noSymbol)
Block(List(ValDef(sym, Some(rhs))), body(Ref(sym).asInstanceOf[Ident]))
}

Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/i6988/FirstArg_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Macros {
def argsImpl(using qctx: QuoteContext) : Expr[FirstArg] = {
import qctx.tasty._

def enclosingClass(cur: Symbol = rootContext.owner): Symbol =
def enclosingClass(cur: Symbol = Symbol.currentOwner): Symbol =
if (cur.isClassDef) cur
else enclosingClass(cur.owner)

Expand All @@ -24,7 +24,7 @@ object Macros {

def literal(value: String): Expr[String] =
Literal(Constant(value)).seal.asInstanceOf[Expr[String]]
val paramss = enclosingParamList(rootContext.owner)
val paramss = enclosingParamList(Symbol.currentOwner)
val firstArg = paramss.flatten.head
val ref = Select.unique(This(enclosingClass()), firstArg.name)
'{ FirstArg(${ref.seal}, ${Expr(firstArg.name)}) }
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/i7025/Macros_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Macros {
if owner.isClassDef then owner
else nearestEnclosingDef(owner.owner)

val x = nearestEnclosingDef(rootContext.owner)
val x = nearestEnclosingDef(Symbol.currentOwner)
if x.isDefDef then
val code = x.signature.toString
'{ println(${Expr(code)}) }
Expand Down
16 changes: 8 additions & 8 deletions tests/run-macros/tasty-create-method-symbol/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Macros {

// simple smoke test
val sym1 : Symbol = Symbol.newMethod(
rootContext.owner,
Symbol.currentOwner,
"sym1",
MethodType(List("a","b"))(
_ => List(typeOf[Int], typeOf[Int]),
Expand All @@ -27,7 +27,7 @@ object Macros {

// test for no argument list (no Apply node)
val sym2 : Symbol = Symbol.newMethod(
rootContext.owner,
Symbol.currentOwner,
"sym2",
ByNameType(typeOf[Int]))
assert(sym2.isDefDef)
Expand All @@ -43,7 +43,7 @@ object Macros {

// test for multiple argument lists
val sym3 : Symbol = Symbol.newMethod(
rootContext.owner,
Symbol.currentOwner,
"sym3",
MethodType(List("a"))(
_ => List(typeOf[Int]),
Expand All @@ -63,7 +63,7 @@ object Macros {

// test for recursive references
val sym4 : Symbol = Symbol.newMethod(
rootContext.owner,
Symbol.currentOwner,
"sym4",
MethodType(List("x"))(
_ => List(typeOf[Int]),
Expand All @@ -85,7 +85,7 @@ object Macros {

// test for nested functions (one symbol is the other's parent, and we use a Closure)
val sym5 : Symbol = Symbol.newMethod(
rootContext.owner,
Symbol.currentOwner,
"sym5",
MethodType(List("x"))(
_ => List(typeOf[Int]),
Expand Down Expand Up @@ -119,13 +119,13 @@ object Macros {

// test mutually recursive definitions
val sym6_1 : Symbol = Symbol.newMethod(
rootContext.owner,
Symbol.currentOwner,
"sym6_1",
MethodType(List("x"))(
_ => List(typeOf[Int]),
_ => typeOf[Int]))
val sym6_2 : Symbol = Symbol.newMethod(
rootContext.owner,
Symbol.currentOwner,
"sym6_2",
MethodType(List("x"))(
_ => List(typeOf[Int]),
Expand Down Expand Up @@ -166,7 +166,7 @@ object Macros {

// test polymorphic methods by synthesizing an identity method
val sym7 : Symbol = Symbol.newMethod(
rootContext.owner,
Symbol.currentOwner,
"sym7",
PolyType(List("T"))(
tp => List(TypeBounds(typeOf[Nothing], typeOf[Any])),
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/tasty-location/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object Location {
if (sym == defn.RootClass || sym == defn.EmptyPackageClass) acc
else listOwnerNames(sym.owner, sym.name :: acc)

val list = listOwnerNames(rootContext.owner, Nil)
val list = listOwnerNames(Symbol.currentOwner, Nil)
'{new Location(${Expr(list)})}
}

Expand Down