Skip to content

Commit 8e1b60d

Browse files
committed
Replace TypeArgRefs by normal TypeRefs
This commit avoids replaces a TypeArgRef by a normal reference to a type parameter.
1 parent 76524fb commit 8e1b60d

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

compiler/src/dotty/tools/dotc/config/Config.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,6 @@ object Config {
179179

180180
/** When in IDE, turn StaleSymbol errors into warnings instead of crashing */
181181
final val ignoreStaleInIDE = true
182+
183+
val newScheme = true
182184
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,9 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
829829

830830
def compareCaptured(arg1: Type, arg2: Type): Boolean = arg1 match {
831831
case arg1: TypeBounds =>
832-
val captured = TypeArgRef.fromParam(tp1, tparam.asInstanceOf[TypeSymbol])
832+
val captured =
833+
if (Config.newScheme) TypeRef(tp1, tparam.asInstanceOf[TypeSymbol])
834+
else TypeArgRef.fromParam(tp1, tparam.asInstanceOf[TypeSymbol])
833835
isSubArg(captured, arg2)
834836
case _ =>
835837
false

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ object Types {
182182
loop(this)
183183
}
184184

185+
/** True iff `symd` is a denotation of a class type parameter and the reference
186+
* `<this> . <symd>` is an actual argument reference, i.e. `this` is different
187+
* from the ThisType of `symd`'s owner.
188+
*/
189+
def isArgPrefix(symd: SymDenotation)(implicit ctx: Context) =
190+
Config.newScheme && symd.is(ClassTypeParam) && {
191+
this match {
192+
case tp: ThisType => tp.cls ne symd.owner
193+
case _ => true
194+
}
195+
}
196+
185197
/** Returns true if the type is a phantom type
186198
* - true if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
187199
* - false otherwise
@@ -1659,6 +1671,8 @@ object Types {
16591671
val symd = sym.lastKnownDenotation
16601672
if (symd.validFor.runId != ctx.runId && !ctx.stillValid(symd))
16611673
finish(memberDenot(symd.initial.name, allowPrivate = false))
1674+
else if (prefix.isArgPrefix(symd))
1675+
finish(argDenot(sym.asType))
16621676
else if (infoDependsOnPrefix(symd, prefix))
16631677
finish(memberDenot(symd.initial.name, allowPrivate = symd.is(Private)))
16641678
else
@@ -1714,6 +1728,32 @@ object Types {
17141728
private def memberDenot(prefix: Type, name: Name, allowPrivate: Boolean)(implicit ctx: Context): Denotation =
17151729
if (allowPrivate) prefix.member(name) else prefix.nonPrivateMember(name)
17161730

1731+
private def argDenot(param: TypeSymbol)(implicit ctx: Context): Denotation = {
1732+
val cls = param.owner
1733+
val args = prefix.baseType(cls).argInfos
1734+
val typeParams = cls.typeParams
1735+
1736+
def concretize(arg: Type, tparam: TypeSymbol) = arg match {
1737+
case arg: TypeBounds => TypeRef(prefix, tparam)
1738+
case arg => arg
1739+
}
1740+
val concretized = args.zipWithConserve(typeParams)(concretize)
1741+
1742+
def rebase(arg: Type) = arg.subst(typeParams, concretized)
1743+
1744+
val idx = typeParams.indexOf(param)
1745+
val argInfo = args(idx) match {
1746+
case arg: TypeBounds =>
1747+
val v = param.paramVariance
1748+
val pbounds = param.paramInfo
1749+
if (v > 0 && pbounds.loBound.dealias.isBottomType) TypeAlias(arg.hiBound & rebase(pbounds.hiBound))
1750+
else if (v < 0 && pbounds.hiBound.dealias.isTopType) TypeAlias(arg.loBound | rebase(pbounds.loBound))
1751+
else arg recoverable_& rebase(pbounds)
1752+
case arg => TypeAlias(arg)
1753+
}
1754+
param.derivedSingleDenotation(param, argInfo)
1755+
}
1756+
17171757
/** Reload denotation by computing the member with the reference's name as seen
17181758
* from the reference's prefix.
17191759
*/
@@ -1837,7 +1877,9 @@ object Types {
18371877
while (tparams.nonEmpty && args.nonEmpty) {
18381878
if (tparams.head.eq(tparam))
18391879
return args.head match {
1840-
case _: TypeBounds => TypeArgRef(pre, cls.typeRef, idx)
1880+
case _: TypeBounds =>
1881+
if (Config.newScheme) TypeRef(pre, tparam)
1882+
else TypeArgRef(pre, cls.typeRef, idx)
18411883
case arg => arg
18421884
}
18431885
tparams = tparams.tail
@@ -1936,7 +1978,7 @@ object Types {
19361978
else if (lastDenotation == null) NamedType(prefix, designator)
19371979
else designator match {
19381980
case sym: Symbol =>
1939-
if (infoDependsOnPrefix(sym, prefix)) {
1981+
if (infoDependsOnPrefix(sym, prefix) && !prefix.isArgPrefix(sym)) {
19401982
val candidate = reload()
19411983
val falseOverride = sym.isClass && candidate.symbol.exists && candidate.symbol != symbol
19421984
// A false override happens if we rebind an inner class to another type with the same name
@@ -4013,6 +4055,11 @@ object Types {
40134055
case TypeBounds(lo, hi) => range(atVariance(-variance)(reapply(lo)), reapply(hi))
40144056
case arg => reapply(arg)
40154057
}
4058+
case arg @ TypeRef(pre, _) if pre.isArgPrefix(arg.symbol) =>
4059+
arg.info match {
4060+
case TypeBounds(lo, hi) => range(atVariance(-variance)(reapply(lo)), reapply(hi))
4061+
case arg => reapply(arg)
4062+
}
40164063
case arg => reapply(arg)
40174064
}
40184065
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
235235
val name = readName().toTypeName
236236
val prefix = readType()
237237
val space = readType()
238-
TypeRef(prefix, name, space.decl(name))
238+
space.decl(name) match {
239+
case symd: SymDenotation if prefix.isArgPrefix(symd.symbol) => TypeRef(prefix, symd.symbol)
240+
case _ => TypeRef(prefix, name, space.decl(name))
241+
}
239242
case REFINEDtype =>
240243
var name: Name = readName()
241244
val parent = readType()

0 commit comments

Comments
 (0)