Skip to content

Commit a3cb6b0

Browse files
committed
Merge pull request #802 from dotty-staging/change-hk-1
Change hk take 2
2 parents 84590eb + 1ba3d61 commit a3cb6b0

24 files changed

+1102
-239
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ object Config {
7171
/** If this flag is set, take the fast path when comparing same-named type-aliases and types */
7272
final val fastPathForRefinedSubtype = true
7373

74+
/** If this flag is set, $apply projections are checked that they apply to a
75+
* higher-kinded type.
76+
*/
77+
final val checkProjections = false
78+
7479
/** When set, use new signature-based matching.
7580
* Advantage of doing so: It's supposed to be faster
7681
* Disadvantage: It might hide inconsistencies, so while debugging it's better to turn it off

src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ class Definitions {
494494
/** The set of HigherKindedXYZ traits encountered so far */
495495
def lambdaTraits: Set[Symbol] = myLambdaTraits
496496

497-
private var lambdaTraitForVariances = mutable.Map[List[Int], ClassSymbol]()
497+
private var LambdaTraitForVariances = mutable.Map[List[Int], ClassSymbol]()
498498

499499
/** The HigherKinded trait corresponding to symbols `boundSyms` (which are assumed
500500
* to be the type parameters of a higher-kided type). This is a class symbol that
@@ -513,7 +513,7 @@ class Definitions {
513513
* - for each positive or negative variance v_i there is a parent trait Pj which
514514
* is the same as LambdaXYZ except that it has `I` in i-th position.
515515
*/
516-
def lambdaTrait(vcs: List[Int]): ClassSymbol = {
516+
def LambdaTrait(vcs: List[Int]): ClassSymbol = {
517517
assert(vcs.nonEmpty)
518518

519519
def varianceFlags(v: Int) = v match {
@@ -527,17 +527,17 @@ class Definitions {
527527
val cls = denot.asClass.classSymbol
528528
val paramDecls = newScope
529529
for (i <- 0 until vcs.length)
530-
newTypeParam(cls, tpnme.lambdaArgName(i), varianceFlags(vcs(i)), paramDecls)
531-
newTypeField(cls, tpnme.Apply, Covariant, paramDecls)
530+
newTypeParam(cls, tpnme.LambdaArgName(i), varianceFlags(vcs(i)), paramDecls)
531+
newTypeField(cls, tpnme.hkApply, Covariant, paramDecls)
532532
val parentTraitRefs =
533533
for (i <- 0 until vcs.length if vcs(i) != 0)
534-
yield lambdaTrait(vcs.updated(i, 0)).typeRef
534+
yield LambdaTrait(vcs.updated(i, 0)).typeRef
535535
denot.info = ClassInfo(
536536
ScalaPackageClass.thisType, cls, ObjectClass.typeRef :: parentTraitRefs.toList, paramDecls)
537537
}
538538
}
539539

540-
val traitName = tpnme.lambdaTraitName(vcs)
540+
val traitName = tpnme.LambdaTraitName(vcs)
541541

542542
def createTrait = {
543543
val cls = newClassSymbol(
@@ -549,7 +549,7 @@ class Definitions {
549549
cls
550550
}
551551

552-
lambdaTraitForVariances.getOrElseUpdate(vcs, createTrait)
552+
LambdaTraitForVariances.getOrElseUpdate(vcs, createTrait)
553553
}
554554

555555
// ----- primitive value class machinery ------------------------------------------

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ object NameOps {
110110
/** The index of the higher-kinded type parameter with this name.
111111
* Pre: isLambdaArgName.
112112
*/
113-
def lambdaArgIndex: Int =
113+
def LambdaArgIndex: Int =
114114
name.drop(tpnme.LAMBDA_ARG_PREFIX.length).toString.toInt
115115

116116
/** If the name ends with $nn where nn are

src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ object StdNames {
173173
final val WILDCARD_STAR: N = "_*"
174174
final val REIFY_TREECREATOR_PREFIX: N = "$treecreator"
175175
final val REIFY_TYPECREATOR_PREFIX: N = "$typecreator"
176-
final val LAMBDA_ARG_PREFIX: N = "HK$"
176+
final val LAMBDA_ARG_PREFIX: N = "hk$"
177177
final val LAMBDA_ARG_PREFIXhead: Char = LAMBDA_ARG_PREFIX.head
178178

179179
final val Any: N = "Any"
@@ -311,7 +311,7 @@ object StdNames {
311311

312312
val AnnotatedType: N = "AnnotatedType"
313313
val AppliedTypeTree: N = "AppliedTypeTree"
314-
val Apply: N = "Apply"
314+
val hkApply: N = "$apply"
315315
val ArrayAnnotArg: N = "ArrayAnnotArg"
316316
val Constant: N = "Constant"
317317
val ConstantType: N = "ConstantType"
@@ -739,8 +739,8 @@ object StdNames {
739739
def syntheticTypeParamNames(num: Int): List[TypeName] =
740740
(0 until num).map(syntheticTypeParamName)(breakOut)
741741

742-
def lambdaTraitName(vcs: List[Int]): TypeName = LambdaPrefix ++ vcs.map(varianceSuffix).mkString
743-
def lambdaArgName(n: Int) = LAMBDA_ARG_PREFIX ++ n.toString
742+
def LambdaTraitName(vcs: List[Int]): TypeName = LambdaPrefix ++ vcs.map(varianceSuffix).mkString
743+
def LambdaArgName(n: Int) = LAMBDA_ARG_PREFIX ++ n.toString
744744

745745
final val Conforms = encode("<:<")
746746

src/dotty/tools/dotc/core/Substituters.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,31 @@ trait Substituters { this: Context =>
277277
final class SubstParamsMap(from: BindingType, to: List[Type]) extends DeepTypeMap {
278278
def apply(tp: Type) = substParams(tp, from, to, this)
279279
}
280+
281+
/** A map for "cycle safe substitutions" which do not force the denotation
282+
* of a TypeRef unless the name matches up with one of the substituted symbols.
283+
*/
284+
final class SafeSubstMap(from: List[Symbol], to: List[Type]) extends TypeMap {
285+
def apply(tp: Type): Type = tp match {
286+
case tp: NamedType =>
287+
try {
288+
var sym: Symbol = null
289+
var fs = from
290+
var ts = to
291+
while (fs.nonEmpty) {
292+
if (fs.head.name == tp.name) {
293+
if (sym == null) sym = tp.symbol
294+
if (fs.head eq sym) return ts.head
295+
}
296+
fs = fs.tail
297+
ts = ts.tail
298+
}
299+
tp.newLikeThis(apply(tp.prefix))
300+
}
301+
catch {
302+
case ex: CyclicReference => tp.derivedSelect(apply(tp.prefix))
303+
}
304+
case _ => mapOver(tp)
305+
}
306+
}
280307
}

0 commit comments

Comments
 (0)