Skip to content

Commit

Permalink
Add reflect Symbol.paramVariance
Browse files Browse the repository at this point in the history
Related to #16734
  • Loading branch information
nicolasstucki committed Sep 19, 2023
1 parent 6acd923 commit c0880ad
Show file tree
Hide file tree
Showing 6 changed files with 88 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 @@ -2756,6 +2756,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
}

def isTypeParam: Boolean = self.isTypeParam
def paramVariance: Flags = self.paramVariance
def signature: Signature = self.signature
def moduleClass: Symbol = self.denot.moduleClass
def companionClass: Symbol = self.denot.companionClass
Expand Down
9 changes: 9 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4086,8 +4086,17 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Fields of a case class type -- only the ones declared in primary constructor */
def caseFields: List[Symbol]

/** Is this the symbol of a type parameter */
def isTypeParam: Boolean

/** Variance flags for of this type parameter.
*
* Variance flags can be one of `Flags.{Covariant, Contravariant, EmptyFlags}`.
* If this is not the symbol of a type parameter the result is `Flags.EmptyFlags`.
*/
@experimental
def paramVariance: Flags

/** Signature of this definition */
def signature: Signature

Expand Down
36 changes: 36 additions & 0 deletions tests/run-macros/i16734c.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class C1Inv
A

class C1Cov
+A

class C1Con
-A

class C2InvInv
A, B

class C2InvCov
A, +B

class C2InvCon
A, -B

class C2CovInv
+A, B

class C2CovCov
+A, +B

class C2CovCon
+A, -B

class C2ConInv
-A, B

class C2ConCov
-A, +B

class C2ConCon
-A, -B

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

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

def variancesImpl[A <: AnyKind: Type](using Quotes): Expr[String] =
import quotes.reflect.*
val variances = TypeRepr.of[A].typeSymbol.typeMembers.filter(_.isTypeParam).map { sym =>
if sym.paramVariance == Flags.Covariant then "+" + sym.name
else if sym.paramVariance == Flags.Contravariant then "-" + sym.name
else sym.name
}
val res = variances.mkString(TypeRepr.of[A].typeSymbol.toString + "\n", ", ", "\n")
Expr(res)
27 changes: 27 additions & 0 deletions tests/run-macros/i16734c/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class C1Inv[A] { type T }
class C1Cov[+A] { type T }
class C1Con[-A] { type T }

class C2InvInv[A, B] { type T }
class C2InvCov[A, +B] { type T }
class C2InvCon[A, -B] { type T }
class C2CovInv[+A, B] { type T }
class C2CovCov[+A, +B] { type T }
class C2CovCon[+A, -B] { type T }
class C2ConInv[-A, B] { type T }
class C2ConCov[-A, +B] { type T }
class C2ConCon[-A, -B] { type T }

@main def Test =
println(classVariances[C1Inv])
println(classVariances[C1Cov])
println(classVariances[C1Con])
println(classVariances[C2InvInv])
println(classVariances[C2InvCov])
println(classVariances[C2InvCon])
println(classVariances[C2CovInv])
println(classVariances[C2CovCov])
println(classVariances[C2CovCon])
println(classVariances[C2ConInv])
println(classVariances[C2ConCov])
println(classVariances[C2ConCon])
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ val experimentalDefinitionInLibrary = Set(
//// New APIs: Quotes
"scala.quoted.Quotes.reflectModule.FlagsModule.AbsOverride",
"scala.quoted.Quotes.reflectModule.TypeLambdaMethods.paramVariances",
"scala.quoted.Quotes.reflectModule.SymbolMethods.paramVariance",
// Can be stabilized in 3.4.0 (unsure) or later
"scala.quoted.Quotes.reflectModule.CompilationInfoModule.XmacroSettings",
"scala.quoted.Quotes.reflectModule.FlagsModule.JavaAnnotation",
Expand Down

0 comments on commit c0880ad

Please sign in to comment.