From 4f9ef984ac3085c17a91c035d9bcc3346d064a97 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 13 Apr 2022 14:23:00 +0200 Subject: [PATCH] Add reflect AppliedType constructor Fixes #14740 --- .../src/scala/quoted/runtime/impl/QuotesImpl.scala | 2 ++ library/src/scala/quoted/Quotes.scala | 3 +++ project/MiMaFilters.scala | 1 + tests/pos-macros/i14740/Macro_1.scala | 11 +++++++++++ tests/pos-macros/i14740/Test_2.scala | 3 +++ 5 files changed, 20 insertions(+) create mode 100644 tests/pos-macros/i14740/Macro_1.scala create mode 100644 tests/pos-macros/i14740/Test_2.scala diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index 7641b9f0489b..37da7dafd25a 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -1893,6 +1893,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler end AppliedTypeTypeTest object AppliedType extends AppliedTypeModule: + def apply(tycon: TypeRepr, args: List[TypeRepr]): AppliedType = + Types.AppliedType(tycon, args) def unapply(x: AppliedType): (TypeRepr, List[TypeRepr]) = (AppliedTypeMethods.tycon(x), AppliedTypeMethods.args(x)) end AppliedType diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index 862a9def8dcb..013a99559fbb 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -2812,6 +2812,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => /** Methods of the module object `val AppliedType` */ trait AppliedTypeModule { this: AppliedType.type => + /** Applied the type constructor `T` to a list of type arguments `T_1,..,T_n` to create `T[T_1,..,T_n]` */ + @experimental + def apply(tycon: TypeRepr, args: List[TypeRepr]): AppliedType def unapply(x: AppliedType): (TypeRepr, List[TypeRepr]) } diff --git a/project/MiMaFilters.scala b/project/MiMaFilters.scala index 8bd16f134f57..2d41aca4fed1 100644 --- a/project/MiMaFilters.scala +++ b/project/MiMaFilters.scala @@ -11,6 +11,7 @@ object MiMaFilters { ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.typeRef"), ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.termRef"), ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeTreeModule.ref"), + ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#AppliedTypeModule.apply"), // Experimental `MainAnnotation` APIs. Can be added in 3.3.0 or later. ProblemFilters.exclude[MissingClassProblem]("scala.annotation.MainAnnotation"), diff --git a/tests/pos-macros/i14740/Macro_1.scala b/tests/pos-macros/i14740/Macro_1.scala new file mode 100644 index 000000000000..49b869e2b307 --- /dev/null +++ b/tests/pos-macros/i14740/Macro_1.scala @@ -0,0 +1,11 @@ +import scala.quoted.* + +type Foo[T] + +transparent inline def emptyList[T]: Any = ${ impl[T] } + +private def impl[T: Type](using Quotes): Expr[Any] = { + import quotes.reflect._ + val tpe = AppliedType(TypeRepr.of[Foo], List(TypeRepr.of[T])) // test AppliedType constructor + Typed('{???}.asTerm, Inferred(tpe)).asExpr // '{ ??? : Foo[T] } +} diff --git a/tests/pos-macros/i14740/Test_2.scala b/tests/pos-macros/i14740/Test_2.scala new file mode 100644 index 000000000000..a59d01526f4f --- /dev/null +++ b/tests/pos-macros/i14740/Test_2.scala @@ -0,0 +1,3 @@ +def test1: Foo[Any] = emptyList[Any] +def test2: Foo[Int] = emptyList[Int] +def test3: Foo[String] = emptyList[String]