@@ -368,7 +368,7 @@ object Types {
368368 }
369369
370370 /** Is this a higher-kinded type lambda with given parameter variances? */
371- def isVariantLambda : Boolean = false
371+ def isDeclaredVarianceLambda : Boolean = false
372372
373373// ----- Higher-order combinators -----------------------------------
374374
@@ -3433,15 +3433,19 @@ object Types {
34333433 }
34343434
34353435 /** 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
34393437 *
34403438 * @param paramNames The names `X_0`, ..., `X_n`
34413439 * @param paramInfosExp A function that, given the polytype itself, returns the
34423440 * parameter bounds `B_1`, ..., `B_n`
34433441 * @param resultTypeExp A function that, given the polytype itself, returns the
34443442 * 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.
34453449 */
34463450 class HKTypeLambda (val paramNames : List [TypeName ], @ constructorOnly variances : List [Variance ])(
34473451 paramInfosExp : HKTypeLambda => List [TypeBounds ], resultTypeExp : HKTypeLambda => Type )
@@ -3457,28 +3461,28 @@ object Types {
34573461
34583462 private def setVariances (tparams : List [LambdaParam ], vs : List [Variance ]): Unit =
34593463 if tparams.nonEmpty then
3460- tparams.head.givenVariance = vs.head
3464+ tparams.head.declaredVariance = vs.head
34613465 setVariances(tparams.tail, vs.tail)
34623466
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)
34653469
3466- def givenVariances =
3467- if isVariantLambda then typeParams.map(_.givenVariance )
3470+ def declaredVariances =
3471+ if isDeclaredVarianceLambda then typeParams.map(_.declaredVariance )
34683472 else Nil
34693473
34703474 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)
34723476
34733477 // No definition of `eql` --> fall back on equals, which calls iso
34743478
34753479 final override def iso (that : Any , bs : BinderPairs ): Boolean = that match {
34763480 case that : HKTypeLambda =>
34773481 paramNames.eqElements(that.paramNames)
3478- && isVariantLambda == that.isVariantLambda
3479- && (! isVariantLambda
3482+ && isDeclaredVarianceLambda == that.isDeclaredVarianceLambda
3483+ && (! isDeclaredVarianceLambda
34803484 || typeParams.corresponds(that.typeParams)((x, y) =>
3481- x.givenVariance == y.givenVariance ))
3485+ x.declaredVariance == y.declaredVariance ))
34823486 && {
34833487 val bs1 = new BinderPairs (this , that, bs)
34843488 paramInfos.equalElements(that.paramInfos, bs1) &&
@@ -3489,7 +3493,7 @@ object Types {
34893493 }
34903494
34913495 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)
34933497
34943498 def newLikeThis (paramNames : List [ThisName ], variances : List [Variance ], paramInfos : List [PInfo ], resType : Type )(implicit ctx : Context ): This =
34953499 HKTypeLambda (paramNames, variances)(
@@ -3501,8 +3505,8 @@ object Types {
35013505
35023506 protected def prefixString : String = " HKTypeLambda"
35033507 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)}) "
35063510 else super .toString
35073511 }
35083512
@@ -3612,7 +3616,7 @@ object Types {
36123616 bounds.derivedAlias(expand(bounds.alias, true ))
36133617 case bounds : TypeAlias =>
36143618 bounds.derivedAlias(expand(bounds.alias,
3615- isOpaqueAlias | params.exists(! _.paramVariance.isEmpty)))
3619+ isOpaqueAlias || params.exists(! _.paramVariance.isEmpty)))
36163620 case TypeBounds (lo, hi) =>
36173621 bounds.derivedTypeBounds(
36183622 if lo.isRef(defn.NothingClass ) then lo else expand(lo, true ),
@@ -3659,18 +3663,32 @@ object Types {
36593663 def paramRef (implicit ctx : Context ): Type = tl.paramRefs(n)
36603664
36613665 private var myVariance : FlagSet = UndefinedFlags
3666+
3667+ /** Low level setter, only called from Variances.setStructuralVariances */
36623668 def storedVariance_= (v : Variance ): Unit =
36633669 myVariance = v
3670+
3671+ /** Low level getter, only called from Variances.setStructuralVariances */
36643672 def storedVariance : Variance =
36653673 myVariance
36663674
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)
36683680 assert(myVariance == UndefinedFlags )
36693681 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)
36713688 assert(myVariance != UndefinedFlags )
36723689 myVariance
36733690
3691+ /** The declared or structural variance of this parameter. */
36743692 def paramVariance (implicit ctx : Context ): Variance =
36753693 if myVariance == UndefinedFlags then
36763694 tl match
0 commit comments