Skip to content

Commit

Permalink
Add reflect TypeRepr.dealiasKeepOpaques
Browse files Browse the repository at this point in the history
From #18562
  • Loading branch information
nicolasstucki committed Oct 17, 2023
1 parent 89fa247 commit b59b901
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
1 change: 1 addition & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
10 changes: 10 additions & 0 deletions tests/run-macros/tasty-dealiasKeepOpaques.check
Original file line number Diff line number Diff line change
@@ -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]
8 changes: 8 additions & 0 deletions tests/run-macros/tasty-dealiasKeepOpaques/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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)
}
29 changes: 29 additions & 0 deletions tests/run-macros/tasty-dealiasKeepOpaques/Test_2.scala
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b59b901

Please sign in to comment.