From ad518b574be0ce1add1343a1b7b2d3be575ea250 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 24 Aug 2020 17:48:32 +0200 Subject: [PATCH 1/2] Move currentOwner to Symbol --- .../community-projects/dotty-cps-async | 2 +- community-build/community-projects/sourcecode | 2 +- .../ReflectionCompilerInterface.scala | 5 ++--- .../scala/internal/tasty/CompilerInterface.scala | 10 +++------- library/src/scala/tasty/Reflection.scala | 14 ++++---------- tests/run-macros/i6988/FirstArg_1.scala | 4 ++-- tests/run-macros/i7025/Macros_1.scala | 2 +- .../tasty-create-method-symbol/Macro_1.scala | 16 ++++++++-------- tests/run-macros/tasty-location/quoted_1.scala | 2 +- 9 files changed, 23 insertions(+), 34 deletions(-) diff --git a/community-build/community-projects/dotty-cps-async b/community-build/community-projects/dotty-cps-async index b7ffc2981d90..fdd0b7503e6e 160000 --- a/community-build/community-projects/dotty-cps-async +++ b/community-build/community-projects/dotty-cps-async @@ -1 +1 @@ -Subproject commit b7ffc2981d90ee854bb0d51f0dda423b6f0f5c2a +Subproject commit fdd0b7503e6efdf99a361d9a5d1394a6009abf33 diff --git a/community-build/community-projects/sourcecode b/community-build/community-projects/sourcecode index 15eaf0144d9c..54f6a0a1105e 160000 --- a/community-build/community-projects/sourcecode +++ b/community-build/community-projects/sourcecode @@ -1 +1 @@ -Subproject commit 15eaf0144d9cc4c45b2a44735a6366396c5181c7 +Subproject commit 54f6a0a1105e7a063b92306cb5d70f3f489ac805 diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala index aef55f72ee7d..f15e35ab1a0e 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala @@ -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 // ///////////////// @@ -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 diff --git a/library/src/scala/internal/tasty/CompilerInterface.scala b/library/src/scala/internal/tasty/CompilerInterface.scala index 9347d331d69f..4d37d7db3748 100644 --- a/library/src/scala/internal/tasty/CompilerInterface.scala +++ b/library/src/scala/internal/tasty/CompilerInterface.scala @@ -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 // ///////////////// @@ -853,6 +846,9 @@ trait CompilerInterface extends scala.tasty.reflect.Types { // SYMBOLS // ///////////// + /** Returns the symbol of the current enclosing definition */ + 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 diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index 8e6cf205b0bd..77760cc36dcb 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -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 // @@ -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) @@ -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])) } diff --git a/tests/run-macros/i6988/FirstArg_1.scala b/tests/run-macros/i6988/FirstArg_1.scala index 2d837d4ca096..0c32159803ab 100644 --- a/tests/run-macros/i6988/FirstArg_1.scala +++ b/tests/run-macros/i6988/FirstArg_1.scala @@ -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) @@ -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)}) } diff --git a/tests/run-macros/i7025/Macros_1.scala b/tests/run-macros/i7025/Macros_1.scala index 7b6c16474c94..9a8744c8d6a9 100644 --- a/tests/run-macros/i7025/Macros_1.scala +++ b/tests/run-macros/i7025/Macros_1.scala @@ -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)}) } diff --git a/tests/run-macros/tasty-create-method-symbol/Macro_1.scala b/tests/run-macros/tasty-create-method-symbol/Macro_1.scala index 028c9aeefcc8..9bdc2f804982 100644 --- a/tests/run-macros/tasty-create-method-symbol/Macro_1.scala +++ b/tests/run-macros/tasty-create-method-symbol/Macro_1.scala @@ -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]), @@ -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) @@ -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]), @@ -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]), @@ -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]), @@ -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]), @@ -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])), diff --git a/tests/run-macros/tasty-location/quoted_1.scala b/tests/run-macros/tasty-location/quoted_1.scala index c340eb53d0ed..16bb7ca69fe6 100644 --- a/tests/run-macros/tasty-location/quoted_1.scala +++ b/tests/run-macros/tasty-location/quoted_1.scala @@ -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)})} } From 8085c8261bcf54969616614460f07521a4e0897d Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 26 Aug 2020 11:57:31 +0200 Subject: [PATCH 2/2] Update library/src/scala/internal/tasty/CompilerInterface.scala Co-authored-by: Fengyun Liu --- library/src/scala/internal/tasty/CompilerInterface.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/scala/internal/tasty/CompilerInterface.scala b/library/src/scala/internal/tasty/CompilerInterface.scala index 4d37d7db3748..7420c18cdea4 100644 --- a/library/src/scala/internal/tasty/CompilerInterface.scala +++ b/library/src/scala/internal/tasty/CompilerInterface.scala @@ -846,7 +846,7 @@ trait CompilerInterface extends scala.tasty.reflect.Types { // SYMBOLS // ///////////// - /** Returns the symbol of the current enclosing definition */ + /** 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. */