From c56d2f840335322f0a29e8f1608f27a42d8ecb67 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 22 Sep 2023 13:55:24 +0200 Subject: [PATCH] Add reflect TypeRepr.dealiasKeepOpaques From https://github.com/lampepfl/dotty/discussions/18562 --- .../quoted/runtime/impl/QuotesImpl.scala | 1 + library/src/scala/quoted/Quotes.scala | 3 ++ project/MiMaFilters.scala | 4 +-- .../run-macros/tasty-dealiasKeepOpaques.check | 10 +++++++ .../tasty-dealiasKeepOpaques/Macro_1.scala | 8 +++++ .../tasty-dealiasKeepOpaques/Test_2.scala | 29 +++++++++++++++++++ 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/run-macros/tasty-dealiasKeepOpaques.check create mode 100644 tests/run-macros/tasty-dealiasKeepOpaques/Macro_1.scala create mode 100644 tests/run-macros/tasty-dealiasKeepOpaques/Test_2.scala diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index bc4f84c147c8..ca4d7b7bd7de 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -1790,6 +1790,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler def widenTermRefByName: TypeRepr = self.widenTermRefExpr def widenByName: TypeRepr = self.widenExpr def dealias: TypeRepr = self.dealias + def dealiasKeepOpaques: TypeRepr = self.dealiasKeepOpaques def simplified: TypeRepr = self.simplified def classSymbol: Option[Symbol] = if self.classSymbol.exists then Some(self.classSymbol.asClass) diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index 15df4a08e1f0..377fbd51fb23 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -2652,6 +2652,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => /** Follow aliases, annotated types until type is no longer alias type, annotated type. */ def dealias: TypeRepr + /** Follow non-opaque aliases, annotated types until type is no longer alias type, annotated type. */ + def dealiasKeepOpaques: TypeRepr + /** A simplified version of this type which is equivalent wrt =:= to this type. * Reduces typerefs, applied match types, and and or types. */ diff --git a/project/MiMaFilters.scala b/project/MiMaFilters.scala index 8937b30084ef..2566a6da1119 100644 --- a/project/MiMaFilters.scala +++ b/project/MiMaFilters.scala @@ -6,8 +6,8 @@ object MiMaFilters { // New API in 3.4.X ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule.ValOrDefDefTypeTest"), ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule.ValOrDefDefMethods"), - ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#defnModule.FunctionClass") - // New API in 3.4.X + ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#defnModule.FunctionClass"), + ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.dealiasKeepOpaques"), ) val TastyCore: Seq[ProblemFilter] = Seq( ProblemFilters.exclude[DirectMissingMethodProblem]("dotty.tools.tasty.TastyFormat.EXPLICITtpt"), diff --git a/tests/run-macros/tasty-dealiasKeepOpaques.check b/tests/run-macros/tasty-dealiasKeepOpaques.check new file mode 100644 index 000000000000..11835b6febbd --- /dev/null +++ b/tests/run-macros/tasty-dealiasKeepOpaques.check @@ -0,0 +1,10 @@ +java.lang.String +java.lang.String +scala.collection.immutable.List[Test_2$package.A] +scala.collection.immutable.List[scala.Int] +Test_2$package.OA +Test_2$package.OB +Test_2$package.OC[scala.Int] +Test_2$package.OA +Test_2$package.OB +Test_2$package.OC[scala.Int] diff --git a/tests/run-macros/tasty-dealiasKeepOpaques/Macro_1.scala b/tests/run-macros/tasty-dealiasKeepOpaques/Macro_1.scala new file mode 100644 index 000000000000..d1c4a482235d --- /dev/null +++ b/tests/run-macros/tasty-dealiasKeepOpaques/Macro_1.scala @@ -0,0 +1,8 @@ +import scala.quoted.* + +inline def dealiasKeepOpaques[T]: String = ${ impl[T] } + +def impl[T: Type](using Quotes) : Expr[String] = { + import quotes.reflect.* + Expr(TypeRepr.of[T].dealiasKeepOpaques.show) +} diff --git a/tests/run-macros/tasty-dealiasKeepOpaques/Test_2.scala b/tests/run-macros/tasty-dealiasKeepOpaques/Test_2.scala new file mode 100644 index 000000000000..1612816ccbc5 --- /dev/null +++ b/tests/run-macros/tasty-dealiasKeepOpaques/Test_2.scala @@ -0,0 +1,29 @@ +type A = String +type B = List[A] +type C[X] = List[X] + +opaque type OA = String +object OA: + def test = println(dealiasKeepOpaques[OA]) + +opaque type OB = List[A] +object OB: + def test = println(dealiasKeepOpaques[OB]) + +opaque type OC[X] = List[X] +object OC: + def test = println(dealiasKeepOpaques[OC[Int]]) + + +@main def Test: Unit = { + println(dealiasKeepOpaques[String]) + println(dealiasKeepOpaques[A]) + println(dealiasKeepOpaques[B]) + println(dealiasKeepOpaques[C[Int]]) + println(dealiasKeepOpaques[OA]) + println(dealiasKeepOpaques[OB]) + println(dealiasKeepOpaques[OC[Int]]) + OA.test + OB.test + OC.test +}