From b59b901a8ab74635c43d9ef3bb1d9a3cce392c83 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 | 1 + .../run-macros/tasty-dealiasKeepOpaques.check | 10 +++++++ .../tasty-dealiasKeepOpaques/Macro_1.scala | 8 +++++ .../tasty-dealiasKeepOpaques/Test_2.scala | 29 +++++++++++++++++++ 6 files changed, 52 insertions(+) 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 e27140459a49..908e5643fc71 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 3d5896e74f66..174f1913220a 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 954b18a5841b..ab8cf0785aa6 100644 --- a/project/MiMaFilters.scala +++ b/project/MiMaFilters.scala @@ -9,6 +9,7 @@ object MiMaFilters { ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule.ValOrDefDefMethods"), ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#defnModule.FunctionClass"), ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#defnModule.PolyFunctionClass"), + 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 +}