Skip to content

Commit

Permalink
Add reflect TypeLambda.declaredVariances
Browse files Browse the repository at this point in the history
Fixes #16734
  • Loading branch information
nicolasstucki committed May 24, 2023
1 parent ca7c29f commit 0f4609b
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2212,6 +2212,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
extension (self: TypeLambda)
def param(idx: Int): TypeRepr = self.newParamRef(idx)
def paramBounds: List[TypeBounds] = self.paramInfos
def declaredVariances: List[Flags] =
if self.isDeclaredVarianceLambda then self.declaredVariances
else self.paramNames.map(_ => Flags.EmptyFlags)
end extension
end TypeLambdaMethods

Expand Down
8 changes: 8 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3235,8 +3235,16 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Extension methods of `TypeLambda` */
trait TypeLambdaMethods:
extension (self: TypeLambda)
/** Reference to the i-th parameter */
/** Type bounds of the i-th parameter */
def param(idx: Int) : TypeRepr
def paramBounds: List[TypeBounds]
/** Variance flags for the i-th parameter
*
* Variance flags can be one of `Flags.{Covariant, Contravariant, EmptyFlags}`.
*/
@experimental
def declaredVariances: List[Flags]
end extension
end TypeLambdaMethods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ val experimentalDefinitionInLibrary = Set(
// Should be stabilized in 3.4.0
"scala.quoted.Quotes.reflectModule.defnModule.FunctionClass",
"scala.quoted.Quotes.reflectModule.FlagsModule.AbsOverride",
"scala.quoted.Quotes.reflectModule.TypeLambdaMethods.declaredVariances",
// Can be stabilized in 3.4.0 (unsure) or later
"scala.quoted.Quotes.reflectModule.CompilationInfoModule.XmacroSettings",
"scala.quoted.Quotes.reflectModule.FlagsModule.JavaAnnotation",
Expand Down
67 changes: 67 additions & 0 deletions tests/run-macros/i16734.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any] => scala.Any] => K1Inv[F]
F
A

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any] => scala.Any] => K1Cov[F]
F
+A

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any] => scala.Any] => K1Con[F]
F
-A

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any] => scala.Any] => K2InvInv[F]
F
A, B

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any] => scala.Any] => K2InvCov[F]
F
A, +B

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any] => scala.Any] => K2InvCon[F]
F
A, -B

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any] => scala.Any] => K2CovInv[F]
F
+A, B

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any] => scala.Any] => K2CovCov[F]
F
+A, +B

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any] => scala.Any] => K2CovCon[F]
F
+A, -B

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any] => scala.Any] => K2ConInv[F]
F
-A, B

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any] => scala.Any] => K2ConCov[F]
F
-A, +B

[F >: scala.Nothing <: [A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any] => scala.Any] => K2ConCon[F]
F
-A, -B

[G >: scala.Nothing <: [A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any, C >: scala.Nothing <: scala.Any, D >: scala.Nothing <: [X1 >: scala.Nothing <: scala.Any, Y1 >: scala.Nothing <: scala.Any, Z1 >: scala.Nothing <: scala.Any] => scala.Any, E >: scala.Nothing <: [X2 >: scala.Nothing <: scala.Any, Y2 >: scala.Nothing <: scala.Any, Z2 >: scala.Nothing <: scala.Any] => scala.Any, F >: scala.Nothing <: [X3 >: scala.Nothing <: scala.Any, Y3 >: scala.Nothing <: scala.Any, Z3 >: scala.Nothing <: scala.Any] => scala.Any] => scala.Any] => KFunky[G]
G
A, +B, -C, D, +E, -F
X1, +Y1, -Z1
X2, +Y2, -Z2
X3, +Y3, -Z3

[A >: scala.Nothing <: scala.Any, F >: scala.Nothing <: [B >: scala.Nothing <: scala.Any] => scala.Any] => F[A]
A, F
B

[A >: scala.Nothing <: scala.Any, F >: scala.Nothing <: [B >: scala.Nothing <: scala.Any] => scala.Any] => F[A]
A, F
+B

[A >: scala.Nothing <: scala.Any, F >: scala.Nothing <: [B >: scala.Nothing <: scala.Any] => scala.Any] => F[A]
A, F
-B

21 changes: 21 additions & 0 deletions tests/run-macros/i16734/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import scala.quoted.*

inline def variances[A <: AnyKind]: String =
${variancesImpl[A]}

def variancesImpl[A <: AnyKind: Type](using Quotes): Expr[String] =
import quotes.reflect.*
def loop(tpe: TypeRepr): List[String] =
tpe match
case tpe: TypeLambda =>
tpe.paramNames.zip(tpe.declaredVariances).map { (name, variance) =>
if variance == Flags.Covariant then "+" + name
else if variance == Flags.Contravariant then "-" + name
else name
}.mkString(", ") :: tpe.paramTypes.flatMap(loop)
case tpe: TypeBounds =>
loop(tpe.low) ++ loop(tpe.hi)
case _ =>
Nil
val res = (Type.show[A] :: loop(TypeRepr.of[A])).mkString("", "\n", "\n")
Expr(res)
35 changes: 35 additions & 0 deletions tests/run-macros/i16734/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
trait K1Inv[F[A]]
trait K1Cov[F[+A]]
trait K1Con[F[-A]]

trait K2InvInv[F[A, B]]
trait K2InvCov[F[A, +B]]
trait K2InvCon[F[A, -B]]
trait K2CovInv[F[+A, B]]
trait K2CovCov[F[+A, +B]]
trait K2CovCon[F[+A, -B]]
trait K2ConInv[F[-A, B]]
trait K2ConCov[F[-A, +B]]
trait K2ConCon[F[-A, -B]]


trait KFunky[G[A, +B, -C, D[X1, +Y1, -Z1], +E[X2, +Y2, -Z2], -F[X3, +Y3, -Z3]]]


@main def Test =
println(variances[K1Inv])
println(variances[K1Cov])
println(variances[K1Con])
println(variances[K2InvInv])
println(variances[K2InvCov])
println(variances[K2InvCon])
println(variances[K2CovInv])
println(variances[K2CovCov])
println(variances[K2CovCon])
println(variances[K2ConInv])
println(variances[K2ConCov])
println(variances[K2ConCon])
println(variances[KFunky])
println(variances[[A, F[B]] =>> F[A]])
println(variances[[A, F[+B]] =>> F[A]])
println(variances[[A, F[-B]] =>> F[A]])

0 comments on commit 0f4609b

Please sign in to comment.