Skip to content

Commit a14b547

Browse files
committed
Add reflect Symbol.paramVariance
Related to #16734
1 parent e66d790 commit a14b547

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

+1
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
27222722
}
27232723

27242724
def isTypeParam: Boolean = self.isTypeParam
2725+
def paramVariance: Flags = self.paramVariance
27252726
def signature: Signature = self.signature
27262727
def moduleClass: Symbol = self.denot.moduleClass
27272728
def companionClass: Symbol = self.denot.companionClass

library/src/scala/quoted/Quotes.scala

+9
Original file line numberDiff line numberDiff line change
@@ -4063,8 +4063,17 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
40634063
/** Fields of a case class type -- only the ones declared in primary constructor */
40644064
def caseFields: List[Symbol]
40654065

4066+
/** Is this the symbol of a type parameter */
40664067
def isTypeParam: Boolean
40674068

4069+
/** Variance flags for of this type parameter.
4070+
*
4071+
* Variance flags can be one of `Flags.{Covariant, Contravariant, EmptyFlags}`.
4072+
* If this is not the symbol of a type parameter the result is `Flags.EmptyFlags`.
4073+
*/
4074+
@experimental
4075+
def paramVariance: Flags
4076+
40684077
/** Signature of this definition */
40694078
def signature: Signature
40704079

tests/run-custom-args/tasty-inspector/stdlibExperimentalDefinitions.scala

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ val experimentalDefinitionInLibrary = Set(
6767
"scala.quoted.Quotes.reflectModule.defnModule.FunctionClass",
6868
"scala.quoted.Quotes.reflectModule.FlagsModule.AbsOverride",
6969
"scala.quoted.Quotes.reflectModule.TypeLambdaMethods.paramVariances",
70+
"scala.quoted.Quotes.reflectModule.SymbolMethods.paramVariance",
7071
// Can be stabilized in 3.4.0 (unsure) or later
7172
"scala.quoted.Quotes.reflectModule.CompilationInfoModule.XmacroSettings",
7273
"scala.quoted.Quotes.reflectModule.FlagsModule.JavaAnnotation",

tests/run-macros/i16734c.check

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class C1Inv
2+
A
3+
4+
class C1Cov
5+
+A
6+
7+
class C1Con
8+
-A
9+
10+
class C2InvInv
11+
A, B
12+
13+
class C2InvCov
14+
A, +B
15+
16+
class C2InvCon
17+
A, -B
18+
19+
class C2CovInv
20+
+A, B
21+
22+
class C2CovCov
23+
+A, +B
24+
25+
class C2CovCon
26+
+A, -B
27+
28+
class C2ConInv
29+
-A, B
30+
31+
class C2ConCov
32+
-A, +B
33+
34+
class C2ConCon
35+
-A, -B
36+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.quoted.*
2+
3+
inline def classVariances[A <: AnyKind]: String =
4+
${variancesImpl[A]}
5+
6+
def variancesImpl[A <: AnyKind: Type](using Quotes): Expr[String] =
7+
import quotes.reflect.*
8+
val variances = TypeRepr.of[A].typeSymbol.typeMembers.filter(_.isTypeParam).map { sym =>
9+
if sym.paramVariance == Flags.Covariant then "+" + sym.name
10+
else if sym.paramVariance == Flags.Contravariant then "-" + sym.name
11+
else sym.name
12+
}
13+
val res = variances.mkString(TypeRepr.of[A].typeSymbol.toString + "\n", ", ", "\n")
14+
Expr(res)

tests/run-macros/i16734c/Test_2.scala

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class C1Inv[A] { type T }
2+
class C1Cov[+A] { type T }
3+
class C1Con[-A] { type T }
4+
5+
class C2InvInv[A, B] { type T }
6+
class C2InvCov[A, +B] { type T }
7+
class C2InvCon[A, -B] { type T }
8+
class C2CovInv[+A, B] { type T }
9+
class C2CovCov[+A, +B] { type T }
10+
class C2CovCon[+A, -B] { type T }
11+
class C2ConInv[-A, B] { type T }
12+
class C2ConCov[-A, +B] { type T }
13+
class C2ConCon[-A, -B] { type T }
14+
15+
@main def Test =
16+
println(classVariances[C1Inv])
17+
println(classVariances[C1Cov])
18+
println(classVariances[C1Con])
19+
println(classVariances[C2InvInv])
20+
println(classVariances[C2InvCov])
21+
println(classVariances[C2InvCon])
22+
println(classVariances[C2CovInv])
23+
println(classVariances[C2CovCov])
24+
println(classVariances[C2CovCon])
25+
println(classVariances[C2ConInv])
26+
println(classVariances[C2ConCov])
27+
println(classVariances[C2ConCon])

0 commit comments

Comments
 (0)