From 2105653fbf689ec398a108ecc8abfce1c2c82455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Chantepie?= Date: Sun, 14 Nov 2021 22:08:50 +0100 Subject: [PATCH] Add Reflect TypeRepr.typeArgs Closes #13947 --- .../src/scala/quoted/runtime/impl/QuotesImpl.scala | 5 +++++ library/src/scala/quoted/Quotes.scala | 3 +++ project/MiMaFilters.scala | 2 ++ tests/run-macros/i13947.check | 3 +++ tests/run-macros/i13947/Macro_1.scala | 12 ++++++++++++ tests/run-macros/i13947/Test_2.scala | 4 ++++ 6 files changed, 29 insertions(+) create mode 100644 tests/run-macros/i13947.check create mode 100644 tests/run-macros/i13947/Macro_1.scala create mode 100644 tests/run-macros/i13947/Test_2.scala diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index d6e13fbbd168..3de7f0bfc670 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -1749,6 +1749,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler dotc.core.Types.decorateTypeApplications(self).appliedTo(targs) def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr = self.subst(from, to) + + def typeArgs: List[TypeRepr] = self match + case AppliedType(_, args) => args + case _ => List.empty end extension end TypeReprMethods @@ -2480,6 +2484,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler def name: String = self.denot.name.toString def fullName: String = self.denot.fullName.toString + def pos: Option[Position] = if self.exists then Some(self.sourcePos) else None diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index 397f9d3b6aae..a375709844db 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -2571,6 +2571,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => @experimental def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr + /** The applied type arguments (empty if there is no such arguments) */ + @experimental + def typeArgs: List[TypeRepr] end extension } diff --git a/project/MiMaFilters.scala b/project/MiMaFilters.scala index 33ab5d2613e7..338826343885 100644 --- a/project/MiMaFilters.scala +++ b/project/MiMaFilters.scala @@ -9,6 +9,8 @@ object MiMaFilters { ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.append"), ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"), ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"), + ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.typeArgs"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.typeArgs"), ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.double"), ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.double$"), ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.float"), diff --git a/tests/run-macros/i13947.check b/tests/run-macros/i13947.check new file mode 100644 index 000000000000..674afdfa11eb --- /dev/null +++ b/tests/run-macros/i13947.check @@ -0,0 +1,3 @@ +[] +[scala.Predef.String] +[scala.Int, scala.Float, scala.Long] diff --git a/tests/run-macros/i13947/Macro_1.scala b/tests/run-macros/i13947/Macro_1.scala new file mode 100644 index 000000000000..49a4971e553c --- /dev/null +++ b/tests/run-macros/i13947/Macro_1.scala @@ -0,0 +1,12 @@ +import scala.quoted.* + +inline def printTypeParams[A]: Unit = ${ printTypeParamsImpl[A] } + +def printTypeParamsImpl[A: Type](using Quotes): Expr[Unit] = { + import quotes.reflect.* + + val targs: List[TypeRepr] = TypeRepr.of[A].typeArgs + val debug = targs.map(_.show).mkString("[", ", ", "]") + + '{ println(${Expr(debug)}) } +} diff --git a/tests/run-macros/i13947/Test_2.scala b/tests/run-macros/i13947/Test_2.scala new file mode 100644 index 000000000000..009193cf12b8 --- /dev/null +++ b/tests/run-macros/i13947/Test_2.scala @@ -0,0 +1,4 @@ +@main def Test: Unit = + printTypeParams[scala.util.Random] + printTypeParams[Option[String]] + printTypeParams[Function2[Int, Float, Long]]