From c8a7306639ee59fa103a324bee853022d5c55afc Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 4 Dec 2020 10:08:03 +0100 Subject: [PATCH] Update reflect Symbol declarations API --- community-build/community-projects/verify | 2 +- .../dotty/communitybuild/FieldsImpl.scala | 4 +-- .../quoted/runtime/impl/QuotesImpl.scala | 36 ++++++++++++------- library/src/scala/quoted/Quotes.scala | 30 +++++++++------- .../dotty/dokka/tasty/ClassLikeSupport.scala | 4 +-- scala3doc/src/dotty/dokka/tasty/SymOps.scala | 2 +- .../tasty/comments/CommentExpanderTests.scala | 4 +-- .../tasty/comments/MemberLookupTests.scala | 4 +-- .../interpreter/jvm/Interpreter.scala | 2 +- .../Macro_1.scala | 8 ++--- tests/run-macros/i6518/Macro_1.scala | 8 ++--- tests/run-macros/i8520/Macro_1.scala | 2 +- .../inferred-repeated-result/test_1.scala | 2 +- 13 files changed, 62 insertions(+), 46 deletions(-) diff --git a/community-build/community-projects/verify b/community-build/community-projects/verify index 95fb4082f072..03ce1f8a0aa8 160000 --- a/community-build/community-projects/verify +++ b/community-build/community-projects/verify @@ -1 +1 @@ -Subproject commit 95fb4082f0729208ff465d81a043f2e77baa900d +Subproject commit 03ce1f8a0aa8cc15536540c508c6088a8426c4e4 diff --git a/community-build/src/scala/dotty/communitybuild/FieldsImpl.scala b/community-build/src/scala/dotty/communitybuild/FieldsImpl.scala index 2d7124727af2..045f34271ea4 100644 --- a/community-build/src/scala/dotty/communitybuild/FieldsImpl.scala +++ b/community-build/src/scala/dotty/communitybuild/FieldsImpl.scala @@ -9,9 +9,9 @@ object FieldsImpl: def fieldsImpl[V: Type, T: Type](from: Expr[V])(using Quotes): Expr[Seq[T]] = import quotes.reflect._ val retType = TypeTree.of[T].tpe - def isProjectField(s: Symbol) = + def isProjectField(s: Symbol) = s.isValDef && s.tree.asInstanceOf[ValDef].tpt.tpe <:< retType val projectsTree = Term.of(from) - val symbols = TypeTree.of[V].symbol.members.filter(isProjectField) + val symbols = TypeTree.of[V].symbol.memberMethods.filter(isProjectField) val selects = symbols.map(Select(projectsTree, _).asExprOf[T]) '{ println(${Expr(retType.show)}); ${Varargs(selects)} } diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index 8c942f0216d1..dd6ef0055cb7 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -2344,35 +2344,45 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler val sym = self.unforcedDecls.find(sym => sym.name == name.toTermName) if (isField(sym)) sym else dotc.core.Symbols.NoSymbol - def classMethod(name: String): List[Symbol] = + def declaredMethod(name: String): List[Symbol] = self.typeRef.decls.iterator.collect { case sym if isMethod(sym) && sym.name.toString == name => sym.asTerm }.toList - def classMethods: List[Symbol] = + def declaredMethods: List[Symbol] = self.typeRef.decls.iterator.collect { case sym if isMethod(sym) => sym.asTerm }.toList - def members: List[Symbol] = - self.typeRef.info.decls.toList - - def typeMembers: List[Symbol] = - self.unforcedDecls.filter(_.isType) - - def typeMember(name: String): Symbol = - self.unforcedDecls.find(sym => sym.name == name.toTypeName) - - def method(name: String): List[Symbol] = + def memberMethod(name: String): List[Symbol] = appliedTypeRef(self).allMembers.iterator.map(_.symbol).collect { case sym if isMethod(sym) && sym.name.toString == name => sym.asTerm }.toList - def methods: List[Symbol] = + def memberMethods: List[Symbol] = appliedTypeRef(self).allMembers.iterator.map(_.symbol).collect { case sym if isMethod(sym) => sym.asTerm }.toList + def declaredType(name: String): List[Symbol] = + self.typeRef.decls.iterator.collect { + case sym if sym.isType && sym.name.toString == name => sym.asType + }.toList + + def declaredTypes: List[Symbol] = + self.typeRef.decls.iterator.collect { + case sym if sym.isType => sym.asType + }.toList + + def memberType(name: String): Symbol = + self.unforcedDecls.find(sym => sym.name == name.toTypeName) + + def memberTypes: List[Symbol] = + self.unforcedDecls.filter(_.isType) + + def declarations: List[Symbol] = + self.typeRef.info.decls.toList + def paramSymss: List[List[Symbol]] = self.denot.paramSymss def primaryConstructor: Symbol = self.denot.primaryConstructor def allOverriddenSymbols: Iterator[Symbol] = self.denot.allOverriddenSymbols diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index db59e0017742..e399f0d4a369 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -3155,25 +3155,31 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => def field(name: String): Symbol /** Get non-private named methods defined directly inside the class */ - def classMethod(name: String): List[Symbol] + def declaredMethod(name: String): List[Symbol] /** Get all non-private methods defined directly inside the class, exluding constructors */ - def classMethods: List[Symbol] + def declaredMethods: List[Symbol] - /** Type member directly declared in the class */ - def typeMembers: List[Symbol] + /** Get named non-private methods declared or inherited */ + def memberMethod(name: String): List[Symbol] - /** Type member with the given name directly declared in the class */ - def typeMember(name: String): Symbol + /** Get all non-private methods declared or inherited */ + def memberMethods: List[Symbol] - /** All members directly declared in the class */ - def members: List[Symbol] + /** Get non-private named methods defined directly inside the class */ + def declaredType(name: String): List[Symbol] - /** Get named non-private methods declared or inherited */ - def method(name: String): List[Symbol] + /** Get all non-private methods defined directly inside the class, exluding constructors */ + def declaredTypes: List[Symbol] - /** Get all non-private methods declared or inherited */ - def methods: List[Symbol] + /** Type member with the given name directly declared in the class */ + def memberType(name: String): Symbol + + /** Type member directly declared in the class */ + def memberTypes: List[Symbol] + + /** All members directly declared in the class */ + def declarations: List[Symbol] /** The symbols of each type parameter list and value parameter list of this * method, or Nil if this isn't a method. diff --git a/scala3doc/src/dotty/dokka/tasty/ClassLikeSupport.scala b/scala3doc/src/dotty/dokka/tasty/ClassLikeSupport.scala index c60c1c5426aa..037fd0138f9e 100644 --- a/scala3doc/src/dotty/dokka/tasty/ClassLikeSupport.scala +++ b/scala3doc/src/dotty/dokka/tasty/ClassLikeSupport.scala @@ -120,7 +120,7 @@ trait ClassLikeSupport: } // TODO check given methods? case dd: DefDef if !dd.symbol.isHiddenByVisibility && dd.symbol.isGiven => - Some(dd.symbol.owner.typeMember(dd.name)) + Some(dd.symbol.owner.memberType(dd.name)) .filterNot(_.exists) .map { _ => parseMethod(dd.symbol, kind = Kind.Given(getGivenInstance(dd).map(_.asSignature), None)) @@ -156,7 +156,7 @@ trait ClassLikeSupport: case vd: ValDef if !isSyntheticField(vd.symbol) && (!vd.symbol.flags.is(Flags.Case) || !vd.symbol.flags.is(Flags.Enum)) => Some(parseValDef(vd)) - case c: ClassDef if c.symbol.owner.method(c.name).exists(_.flags.is(Flags.Given)) => + case c: ClassDef if c.symbol.owner.memberMethod(c.name).exists(_.flags.is(Flags.Given)) => Some(parseGivenClasslike(c)) case c: ClassDef if c.symbol.shouldDocumentClasslike && !c.symbol.isGiven => diff --git a/scala3doc/src/dotty/dokka/tasty/SymOps.scala b/scala3doc/src/dotty/dokka/tasty/SymOps.scala index dbf025288bd6..23ffc93c7ca9 100644 --- a/scala3doc/src/dotty/dokka/tasty/SymOps.scala +++ b/scala3doc/src/dotty/dokka/tasty/SymOps.scala @@ -101,7 +101,7 @@ class SymOps[Q <: Quotes](val q: Q): else val pointsTo = if (!sym.isTypeDef) PointingToDeclaration.INSTANCE - else PointingToGenericParameters(sym.owner.typeMembers.indexOf(sym)) + else PointingToGenericParameters(sym.owner.memberTypes.indexOf(sym)) val method = if (sym.isDefDef) Some(sym) diff --git a/scala3doc/test/dotty/dokka/tasty/comments/CommentExpanderTests.scala b/scala3doc/test/dotty/dokka/tasty/comments/CommentExpanderTests.scala index 0feeff3f0eb4..d86b3e743c1f 100644 --- a/scala3doc/test/dotty/dokka/tasty/comments/CommentExpanderTests.scala +++ b/scala3doc/test/dotty/dokka/tasty/comments/CommentExpanderTests.scala @@ -10,7 +10,7 @@ import dotty.dokka.tasty.TastyParser class CommentExpanderTests { def check(using quoted.Quotes)(): Unit = assertCommentEquals( - qr.Symbol.requiredClass("tests.B").method("otherMethod").head, + qr.Symbol.requiredClass("tests.B").memberMethod("otherMethod").head, "/** This is my foo: Bar, actually. */", ) assertCommentEquals( @@ -18,7 +18,7 @@ class CommentExpanderTests { "/** This is foo: Foo expanded. */", ) assertCommentEquals( - qr.Symbol.requiredModule("tests.O").method("method").head, + qr.Symbol.requiredModule("tests.O").memberMethod("method").head, "/** This is foo: O's foo. */", ) diff --git a/scala3doc/test/dotty/dokka/tasty/comments/MemberLookupTests.scala b/scala3doc/test/dotty/dokka/tasty/comments/MemberLookupTests.scala index 310ae4a04ca4..828b2cd35f67 100644 --- a/scala3doc/test/dotty/dokka/tasty/comments/MemberLookupTests.scala +++ b/scala3doc/test/dotty/dokka/tasty/comments/MemberLookupTests.scala @@ -89,9 +89,9 @@ class LookupTestCases[Q <: Quotes](val q: Quotes) { if s.flags.is(q.reflect.Flags.Module) then s.moduleClass else s Sym(hackResolveModule(symbol.field(name))) def fun(name: String) = - val List(sym) = symbol.method(name) + val List(sym) = symbol.memberMethod(name) Sym(sym) - def tpe(name: String) = Sym(symbol.typeMember(name)) + def tpe(name: String) = Sym(symbol.memberType(name)) } def cls(fqn: String) = Sym(q.reflect.Symbol.classSymbol(fqn)) diff --git a/tests/run-custom-args/tasty-interpreter/interpreter/jvm/Interpreter.scala b/tests/run-custom-args/tasty-interpreter/interpreter/jvm/Interpreter.scala index 35271be86f67..a01c3396c7e1 100644 --- a/tests/run-custom-args/tasty-interpreter/interpreter/jvm/Interpreter.scala +++ b/tests/run-custom-args/tasty-interpreter/interpreter/jvm/Interpreter.scala @@ -29,7 +29,7 @@ class Interpreter[Q <: Quotes & Singleton](using q0: Q) extends TreeInterpreter[ } // println(method) - val symbol = sym.methods.find(_.name == method.getName).get + val symbol = sym.memberMethods.find(_.name == method.getName).get if (symbol.isDefinedInCurrentRun) { val argsList = if (args == null) Nil else args.toList diff --git a/tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala b/tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala index 726e2d82c8a3..2b7bf43fafa9 100644 --- a/tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala +++ b/tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala @@ -58,25 +58,25 @@ object TypeToolbox { inline def methodIn[T](inline mem: String): Seq[String] = ${methodInImpl[T]('mem)} private def methodInImpl[T: Type](mem: Expr[String])(using Quotes) : Expr[Seq[String]] = { import quotes.reflect._ - Expr(TypeTree.of[T].symbol.classMethod(mem.valueOrError).map(_.name)) + Expr(TypeTree.of[T].symbol.declaredMethod(mem.valueOrError).map(_.name)) } inline def methodsIn[T]: Seq[String] = ${methodsInImpl[T]} private def methodsInImpl[T: Type](using Quotes) : Expr[Seq[String]] = { import quotes.reflect._ - Expr(TypeTree.of[T].symbol.classMethods.map(_.name)) + Expr(TypeTree.of[T].symbol.declaredMethods.map(_.name)) } inline def method[T](inline mem: String): Seq[String] = ${methodImpl[T]('mem)} private def methodImpl[T: Type](mem: Expr[String])(using Quotes) : Expr[Seq[String]] = { import quotes.reflect._ - Expr(TypeTree.of[T].symbol.method(mem.valueOrError).map(_.name)) + Expr(TypeTree.of[T].symbol.memberMethod(mem.valueOrError).map(_.name)) } inline def methods[T]: Seq[String] = ${methodsImpl[T]} private def methodsImpl[T: Type](using Quotes) : Expr[Seq[String]] = { import quotes.reflect._ - Expr(TypeTree.of[T].symbol.methods.map(_.name)) + Expr(TypeTree.of[T].symbol.memberMethods.map(_.name)) } inline def typeTag[T](x: T): String = ${typeTagImpl[T]} diff --git a/tests/run-macros/i6518/Macro_1.scala b/tests/run-macros/i6518/Macro_1.scala index 0a1b626d8804..0185b311d332 100644 --- a/tests/run-macros/i6518/Macro_1.scala +++ b/tests/run-macros/i6518/Macro_1.scala @@ -7,10 +7,10 @@ object Macros { private def testImpl(using Quotes) : Expr[String] = { import quotes.reflect._ val classSym = TypeRepr.of[Function1].classSymbol.get - classSym.classMethod("apply") - classSym.classMethods - classSym.method("apply") - Expr(classSym.methods.map(_.name).sorted.mkString("\n")) + classSym.declaredMethod("apply") + classSym.declaredMethods + classSym.memberMethod("apply") + Expr(classSym.memberMethods.map(_.name).sorted.mkString("\n")) } } diff --git a/tests/run-macros/i8520/Macro_1.scala b/tests/run-macros/i8520/Macro_1.scala index c6e50f90f6c7..67bf962e69f9 100644 --- a/tests/run-macros/i8520/Macro_1.scala +++ b/tests/run-macros/i8520/Macro_1.scala @@ -8,6 +8,6 @@ def testExpr[T[_]: Type](using Quotes): Expr[Unit] = { if f.is(Flags.Covariant) then "+" else if f.is(Flags.Contravariant) then "-" else " " - val t = TypeRepr.of[T].typeSymbol.typeMembers.map(x => (x.name, variance(x.flags))) + val t = TypeRepr.of[T].typeSymbol.memberTypes.map(x => (x.name, variance(x.flags))) '{ println(${Expr(t.toString)}) } } diff --git a/tests/run-macros/inferred-repeated-result/test_1.scala b/tests/run-macros/inferred-repeated-result/test_1.scala index 63270c6eb1a6..d27d2f9360d0 100644 --- a/tests/run-macros/inferred-repeated-result/test_1.scala +++ b/tests/run-macros/inferred-repeated-result/test_1.scala @@ -8,7 +8,7 @@ object Macros { val tree = Term.of(expr) val methods = - tree.tpe.classSymbol.get.classMethods.map { m => + tree.tpe.classSymbol.get.declaredMethods.map { m => val name = m.show m.tree match case ddef: DefDef =>