@@ -368,7 +368,7 @@ object Types {
368
368
}
369
369
370
370
/** Is this a higher-kinded type lambda with given parameter variances? */
371
- def isVariantLambda : Boolean = false
371
+ def isDeclaredVarianceLambda : Boolean = false
372
372
373
373
// ----- Higher-order combinators -----------------------------------
374
374
@@ -3433,15 +3433,19 @@ object Types {
3433
3433
}
3434
3434
3435
3435
/** A type lambda of the form `[X_0 B_0, ..., X_n B_n] => T`
3436
- * Variances are encoded in parameter names. A name starting with `+`
3437
- * designates a covariant parameter, a name starting with `-` designates
3438
- * a contravariant parameter, and every other name designates a non-variant parameter.
3436
+ * Variances are encoded in parameter names. A
3439
3437
*
3440
3438
* @param paramNames The names `X_0`, ..., `X_n`
3441
3439
* @param paramInfosExp A function that, given the polytype itself, returns the
3442
3440
* parameter bounds `B_1`, ..., `B_n`
3443
3441
* @param resultTypeExp A function that, given the polytype itself, returns the
3444
3442
* result type `T`.
3443
+ * @param variances The variances of the type parameters, if the type lambda
3444
+ * carries variances, i.e. it is a bound of an abstract type
3445
+ * or the rhs of a match alias or opaque alias. The parameter
3446
+ * is Nil for all other lambdas.
3447
+ *
3448
+ * Variances are stored in the `typeParams` list of the lambda.
3445
3449
*/
3446
3450
class HKTypeLambda (val paramNames : List [TypeName ], @ constructorOnly variances : List [Variance ])(
3447
3451
paramInfosExp : HKTypeLambda => List [TypeBounds ], resultTypeExp : HKTypeLambda => Type )
@@ -3457,28 +3461,28 @@ object Types {
3457
3461
3458
3462
private def setVariances (tparams : List [LambdaParam ], vs : List [Variance ]): Unit =
3459
3463
if tparams.nonEmpty then
3460
- tparams.head.givenVariance = vs.head
3464
+ tparams.head.declaredVariance = vs.head
3461
3465
setVariances(tparams.tail, vs.tail)
3462
3466
3463
- override val isVariantLambda = variances.nonEmpty
3464
- if isVariantLambda then setVariances(typeParams, variances)
3467
+ override val isDeclaredVarianceLambda = variances.nonEmpty
3468
+ if isDeclaredVarianceLambda then setVariances(typeParams, variances)
3465
3469
3466
- def givenVariances =
3467
- if isVariantLambda then typeParams.map(_.givenVariance )
3470
+ def declaredVariances =
3471
+ if isDeclaredVarianceLambda then typeParams.map(_.declaredVariance )
3468
3472
else Nil
3469
3473
3470
3474
override def computeHash (bs : Binders ): Int =
3471
- doHash(new Binders (this , bs), givenVariances ::: paramNames, resType, paramInfos)
3475
+ doHash(new Binders (this , bs), declaredVariances ::: paramNames, resType, paramInfos)
3472
3476
3473
3477
// No definition of `eql` --> fall back on equals, which calls iso
3474
3478
3475
3479
final override def iso (that : Any , bs : BinderPairs ): Boolean = that match {
3476
3480
case that : HKTypeLambda =>
3477
3481
paramNames.eqElements(that.paramNames)
3478
- && isVariantLambda == that.isVariantLambda
3479
- && (! isVariantLambda
3482
+ && isDeclaredVarianceLambda == that.isDeclaredVarianceLambda
3483
+ && (! isDeclaredVarianceLambda
3480
3484
|| typeParams.corresponds(that.typeParams)((x, y) =>
3481
- x.givenVariance == y.givenVariance ))
3485
+ x.declaredVariance == y.declaredVariance ))
3482
3486
&& {
3483
3487
val bs1 = new BinderPairs (this , that, bs)
3484
3488
paramInfos.equalElements(that.paramInfos, bs1) &&
@@ -3489,7 +3493,7 @@ object Types {
3489
3493
}
3490
3494
3491
3495
override def newLikeThis (paramNames : List [ThisName ], paramInfos : List [PInfo ], resType : Type )(implicit ctx : Context ): This =
3492
- newLikeThis(paramNames, givenVariances , paramInfos, resType)
3496
+ newLikeThis(paramNames, declaredVariances , paramInfos, resType)
3493
3497
3494
3498
def newLikeThis (paramNames : List [ThisName ], variances : List [Variance ], paramInfos : List [PInfo ], resType : Type )(implicit ctx : Context ): This =
3495
3499
HKTypeLambda (paramNames, variances)(
@@ -3501,8 +3505,8 @@ object Types {
3501
3505
3502
3506
protected def prefixString : String = " HKTypeLambda"
3503
3507
final override def toString : String =
3504
- if isVariantLambda then
3505
- s " HKTypeLambda( $paramNames, $paramInfos, $resType, ${givenVariances .map(_.flagsString)}) "
3508
+ if isDeclaredVarianceLambda then
3509
+ s " HKTypeLambda( $paramNames, $paramInfos, $resType, ${declaredVariances .map(_.flagsString)}) "
3506
3510
else super .toString
3507
3511
}
3508
3512
@@ -3612,7 +3616,7 @@ object Types {
3612
3616
bounds.derivedAlias(expand(bounds.alias, true ))
3613
3617
case bounds : TypeAlias =>
3614
3618
bounds.derivedAlias(expand(bounds.alias,
3615
- isOpaqueAlias | params.exists(! _.paramVariance.isEmpty)))
3619
+ isOpaqueAlias || params.exists(! _.paramVariance.isEmpty)))
3616
3620
case TypeBounds (lo, hi) =>
3617
3621
bounds.derivedTypeBounds(
3618
3622
if lo.isRef(defn.NothingClass ) then lo else expand(lo, true ),
@@ -3659,18 +3663,32 @@ object Types {
3659
3663
def paramRef (implicit ctx : Context ): Type = tl.paramRefs(n)
3660
3664
3661
3665
private var myVariance : FlagSet = UndefinedFlags
3666
+
3667
+ /** Low level setter, only called from Variances.setStructuralVariances */
3662
3668
def storedVariance_= (v : Variance ): Unit =
3663
3669
myVariance = v
3670
+
3671
+ /** Low level getter, only called from Variances.setStructuralVariances */
3664
3672
def storedVariance : Variance =
3665
3673
myVariance
3666
3674
3667
- def givenVariance_= (v : Variance ): Unit =
3675
+ /** Set the declared variance of this parameter.
3676
+ * @pre the containing lambda is a isDeclaredVarianceLambda
3677
+ */
3678
+ def declaredVariance_= (v : Variance ): Unit =
3679
+ assert(tl.isDeclaredVarianceLambda)
3668
3680
assert(myVariance == UndefinedFlags )
3669
3681
myVariance = v
3670
- def givenVariance : Variance =
3682
+
3683
+ /** The declared variance of this parameter.
3684
+ * @pre the containing lambda is a isDeclaredVarianceLambda
3685
+ */
3686
+ def declaredVariance : Variance =
3687
+ assert(tl.isDeclaredVarianceLambda)
3671
3688
assert(myVariance != UndefinedFlags )
3672
3689
myVariance
3673
3690
3691
+ /** The declared or structural variance of this parameter. */
3674
3692
def paramVariance (implicit ctx : Context ): Variance =
3675
3693
if myVariance == UndefinedFlags then
3676
3694
tl match
0 commit comments