Skip to content

Commit d983f4f

Browse files
committed
Drop VariantNames
1 parent 39657ec commit d983f4f

File tree

12 files changed

+31
-59
lines changed

12 files changed

+31
-59
lines changed

compiler/src/dotty/tools/dotc/core/NameKinds.scala

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,6 @@ object NameKinds {
338338
}
339339
}
340340

341-
/** The kind of names that also encode a variance: 0 for contravariance, 1 for covariance. */
342-
val VariantName: NumberedNameKind = new NumberedNameKind(VARIANT, "Variant") {
343-
def mkString(underlying: TermName, info: ThisInfo) = "-+"(info.num).toString + underlying
344-
}
345-
346341
/** Names of the form N_<outer>. Emitted by inliner, replaced by outer path
347342
* in ExplicitOuter.
348343
*/

compiler/src/dotty/tools/dotc/core/NameOps.scala

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -139,32 +139,8 @@ object NameOps {
139139
name.replace { case ExpandedName(_, unexp) => unexp }
140140
}
141141

142-
/** Remove the variance from the name. */
143-
def invariantName: N = likeSpacedN {
144-
name.replace { case VariantName(invariant, _) => invariant }
145-
}
146-
147142
def errorName: N = likeSpacedN(name ++ nme.ERROR)
148143

149-
/** Map variance value -1, +1 to 0, 1 */
150-
private def varianceToNat(v: Int) = (v + 1) / 2
151-
152-
/** Map 0, 1 to variance value -1, +1 */
153-
private def natToVariance(n: Int) = n * 2 - 1
154-
155-
/** Name with variance prefix: `+` for covariant, `-` for contravariant */
156-
def withVariance(v: Int): N = {
157-
val underlying = name.exclude(VariantName)
158-
likeSpacedN(
159-
if (v == 0) underlying
160-
else VariantName(underlying.toTermName, varianceToNat(v)))
161-
}
162-
163-
/** The variance as implied by the variance prefix, or 0 if there is
164-
* no variance prefix.
165-
*/
166-
def variance: Int = name.collect { case VariantName(_, n) => natToVariance(n) }.getOrElse(0)
167-
168144
def freshened(implicit ctx: Context): N = likeSpacedN {
169145
name.toTermName match {
170146
case ModuleClassName(original) => ModuleClassName(original.freshened)

compiler/src/dotty/tools/dotc/core/NameTags.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ object NameTags extends TastyFormat.NameTags {
4545
case TRAITSETTER => "TRAITSETTER"
4646
case UNIQUE => "UNIQUE"
4747
case DEFAULTGETTER => "DEFAULTGETTER"
48-
case VARIANT => "VARIANT"
4948
case OUTERSELECT => "OUTERSELECT"
5049

5150
case SUPERACCESSOR => "SUPERACCESSOR"

compiler/src/dotty/tools/dotc/core/TypeApplications.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class TypeApplications(val self: Type) extends AnyVal {
302302
* GenericCompanion only expects a non-variant CC, the fact that GenTraversable
303303
* is covariant is irrelevant, so can be ignored.
304304
*/
305-
def adaptHkVariances(bound: Type)(implicit ctx: Context): Type = {
305+
def adaptHkVariances(bound: Type)(implicit ctx: Context): Type = self/*{
306306
val hkParams = bound.hkTypeParams
307307
if (hkParams.isEmpty) self
308308
else {
@@ -325,7 +325,7 @@ class TypeApplications(val self: Type) extends AnyVal {
325325
}
326326
adaptArg(self)
327327
}
328-
}
328+
}*/
329329

330330
/** The type representing
331331
*

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import config.Config
1111
import config.Printers.{constr, subtyping, gadts, noPrinter}
1212
import TypeErasure.{erasedLub, erasedGlb}
1313
import TypeApplications._
14-
import Variances.variancesConform
14+
import Variances.{Variance, variancesConform}
1515
import Constants.Constant
1616
import transform.TypeUtils._
1717
import transform.SymUtils._
@@ -1952,7 +1952,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
19521952
val t2 = distributeAnd(tp2, tp1)
19531953
if (t2.exists) t2
19541954
else if (isErased) erasedGlb(tp1, tp2, isJava = false)
1955-
else liftIfHK(tp1, tp2, op, original)
1955+
else liftIfHK(tp1, tp2, op, original, _ | _)
19561956
}
19571957
}
19581958

@@ -2000,7 +2000,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
20002000
val t2 = distributeOr(tp2, tp1)
20012001
if (t2.exists) t2
20022002
else if (isErased) erasedLub(tp1, tp2)
2003-
else liftIfHK(tp1, tp2, OrType(_, _), _ | _)
2003+
else liftIfHK(tp1, tp2, OrType(_, _), _ | _, _ & _)
20042004
}
20052005
}
20062006

@@ -2009,7 +2009,8 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
20092009
*
20102010
* [X1, ..., Xn] -> op(tp1[X1, ..., Xn], tp2[X1, ..., Xn])
20112011
*/
2012-
private def liftIfHK(tp1: Type, tp2: Type, op: (Type, Type) => Type, original: (Type, Type) => Type) = {
2012+
private def liftIfHK(tp1: Type, tp2: Type,
2013+
op: (Type, Type) => Type, original: (Type, Type) => Type, combineVariance: (Variance, Variance) => Variance) = {
20132014
val tparams1 = tp1.typeParams
20142015
val tparams2 = tp2.typeParams
20152016
def applied(tp: Type) = tp.appliedTo(tp.typeParams.map(_.paramInfoAsSeenFrom(tp)))
@@ -2020,9 +2021,12 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
20202021
original(applied(tp1), tp2)
20212022
else if (tparams1.hasSameLengthAs(tparams2))
20222023
HKTypeLambda(
2023-
paramNames = HKTypeLambda.syntheticParamNames(tparams1.length).lazyZip(tparams1).lazyZip(tparams2)
2024-
.map((pname, tparam1, tparam2) =>
2025-
pname.withVariance((tparam1.paramVarianceSign + tparam2.paramVarianceSign) / 2)))(
2024+
paramNames = HKTypeLambda.syntheticParamNames(tparams1.length),
2025+
variances =
2026+
if tp1.isVariantLambda && tp2.isVariantLambda then
2027+
tparams1.lazyZip(tparams2).map((p1, p2) => combineVariance(p1.paramVariance, p2.paramVariance))
2028+
else Nil
2029+
)(
20262030
paramInfosExp = tl => tparams1.lazyZip(tparams2).map((tparam1, tparam2) =>
20272031
tl.integrate(tparams1, tparam1.paramInfoAsSeenFrom(tp1)).bounds &
20282032
tl.integrate(tparams2, tparam2.paramInfoAsSeenFrom(tp2)).bounds),

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ object Types {
363363
case _ => false
364364
}
365365

366+
/** Is this a higher-kinded type lambda with given parameter variances? */
367+
def isVariantLambda: Boolean = false
368+
366369
// ----- Higher-order combinators -----------------------------------
367370

368371
/** Returns true if there is a part of this type that satisfies predicate `p`.
@@ -3453,11 +3456,11 @@ object Types {
34533456
tparams.head.givenVariance = vs.head
34543457
setVariances(tparams.tail, vs.tail)
34553458

3456-
val isVariant = variances.nonEmpty
3457-
if isVariant then setVariances(typeParams, variances)
3459+
override val isVariantLambda = variances.nonEmpty
3460+
if isVariantLambda then setVariances(typeParams, variances)
34583461

34593462
def givenVariances =
3460-
if isVariant then typeParams.map(_.givenVariance)
3463+
if isVariantLambda then typeParams.map(_.givenVariance)
34613464
else Nil
34623465

34633466
override def computeHash(bs: Binders): Int =
@@ -3468,8 +3471,8 @@ object Types {
34683471
final override def iso(that: Any, bs: BinderPairs): Boolean = that match {
34693472
case that: HKTypeLambda =>
34703473
paramNames.eqElements(that.paramNames)
3471-
&& isVariant == that.isVariant
3472-
&& (!isVariant
3474+
&& isVariantLambda == that.isVariantLambda
3475+
&& (!isVariantLambda
34733476
|| typeParams.corresponds(that.typeParams)((x, y) =>
34743477
x.givenVariance == y.givenVariance))
34753478
&& {
@@ -3494,7 +3497,7 @@ object Types {
34943497

34953498
protected def prefixString: String = "HKTypeLambda"
34963499
final override def toString: String =
3497-
if isVariant then
3500+
if isVariantLambda then
34983501
s"HKTypeLambda($paramNames, $paramInfos, $resType, ${givenVariances.map(_.flagsString)})"
34993502
else super.toString
35003503
}

compiler/src/dotty/tools/dotc/core/Variances.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ object Variances {
110110
}
111111

112112
def setStructuralVariances(lam: HKTypeLambda)(implicit ctx: Context): Unit =
113-
assert(!lam.isVariant)
113+
assert(!lam.isVariantLambda)
114114
for param <- lam.typeParams do param.storedVariance = Bivariant
115115
object traverse extends TypeAccumulator[Unit] {
116116
def apply(x: Unit, t: Type): Unit = t match
@@ -141,7 +141,7 @@ object Variances {
141141
def variancesConform(tparams1: List[TypeParamInfo], tparams2: List[TypeParamInfo])(implicit ctx: Context): Boolean =
142142
val needsDetailedCheck = tparams2 match
143143
case (_: Symbol) :: _ => true
144-
case LambdaParam(tl: HKTypeLambda, _) :: _ => tl.isVariant
144+
case LambdaParam(tl: HKTypeLambda, _) :: _ => tl.isVariantLambda
145145
case _ => false
146146
if needsDetailedCheck then tparams1.corresponds(tparams2)(varianceConforms)
147147
else tparams1.hasSameLengthAs(tparams2)

compiler/src/dotty/tools/dotc/core/tasty/TastyUnpickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class TastyUnpickler(reader: TastyReader) {
6262
val originals = until(end)(readName())
6363
val original = if (originals.isEmpty) EmptyTermName else originals.head
6464
uniqueNameKindOfSeparator(separator)(original, num)
65-
case DEFAULTGETTER | VARIANT =>
65+
case DEFAULTGETTER =>
6666
numberedNameKindOfTag(tag)(readName(), readNat())
6767
case SIGNED =>
6868
val original = readName()

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class TreePickler(pickler: TastyPickler) {
135135
}
136136

137137
def pickleVariances(tp: Type)(given Context): Unit = tp match
138-
case tp: HKTypeLambda if tp.isVariant =>
138+
case tp: HKTypeLambda if tp.isVariantLambda =>
139139
for v <- tp.givenVariances do
140140
writeByte(
141141
if v.is(Covariant) then COVARIANT
@@ -240,7 +240,7 @@ class TreePickler(pickler: TastyPickler) {
240240
pickleType(tpe.parent)
241241
case tpe: TypeAlias =>
242242
tpe.alias match
243-
case alias: HKTypeLambda if alias.isVariant =>
243+
case alias: HKTypeLambda if alias.isVariantLambda =>
244244
writeByte(TYPEBOUNDS)
245245
withLength {
246246
pickleType(tpe.alias, richTypes)

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
331331
def decompose(tp: Type) = tp.stripTypeVar match
332332
case lam: HKTypeLambda =>
333333
val names =
334-
if lam.isVariant then
334+
if lam.isVariantLambda then
335335
lam.paramNames.lazyZip(lam.givenVariances).map((name, v) =>
336336
varianceString(varianceToInt(v)) + name)
337337
else lam.paramNames

0 commit comments

Comments
 (0)