Skip to content

Commit ef4ecc1

Browse files
committed
Fill in missing #Apply projections in checkNonCyclic
They might fall through the cracks in appliedTo because symbols are still completing then.
1 parent dc44f88 commit ef4ecc1

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,17 @@ class TypeApplications(val self: Type) extends AnyVal {
188188
if (args.isEmpty || ctx.erasedTypes) self
189189
else {
190190
val res = instantiate(self, self)
191-
if (res.isInstantiatedLambda) res.select(tpnme.Apply) else res
191+
if (res.isInstantiatedLambda)
192+
// Note: isInstantiatedLambda needs to be conservative, using isSafeLambda
193+
// in order to avoid cyclic reference errors. But this means that some fully
194+
// instantiated types will remain unprojected, which essentially means
195+
// that they stay as higher-kinded types. checkNonCyclic checks the type again
196+
// and potentially inserts an #Apply then. Hopefully, this catches all types
197+
// that fall through the hole. Not adding an #Apply typically manifests itself
198+
// with a <:< failure of two types that "look the same". An example is #779,
199+
// where compiling scala.immutable.Map gives a bounds violation.
200+
res.select(tpnme.Apply)
201+
else res
192202
}
193203
}
194204

src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,18 @@ object Checking {
115115
val parent1 = this(parent)
116116
val saved = cycleOK
117117
cycleOK = nestedCycleOK
118-
try tp.derivedRefinedType(parent1, name, this(tp.refinedInfo))
119-
finally cycleOK = saved
118+
119+
/** A derived refined type with two possible tweaks:
120+
* (1) LazyRefs in parents are pulled out,
121+
* (2) #Apply is added if the type is a fully applied type lambda.
122+
*/
123+
def derivedType(p: Type): Type = p match {
124+
case p: LazyRef => LazyRef(() => derivedType(p.ref))
125+
case _ =>
126+
val res = tp.derivedRefinedType(p, name, this(tp.refinedInfo))
127+
if (res.isSafeLambda && res.typeParams.isEmpty) res.select(tpnme.Apply) else res
128+
}
129+
try derivedType(parent1) finally cycleOK = saved
120130
case tp @ TypeRef(pre, name) =>
121131
try {
122132
// A prefix is interesting if it might contain (transitively) a reference
@@ -130,6 +140,9 @@ object Checking {
130140
case SuperType(thistp, _) => isInteresting(thistp)
131141
case AndType(tp1, tp2) => isInteresting(tp1) || isInteresting(tp2)
132142
case OrType(tp1, tp2) => isInteresting(tp1) && isInteresting(tp2)
143+
case _: RefinedType => false
144+
// Note: it's important not to visit parents of RefinedTypes,
145+
// since otherwise spurious #Apply projections might be inserted.
133146
case _ => false
134147
}
135148
// If prefix is interesting, check info of typeref recursively, marking the referred symbol
@@ -158,6 +171,9 @@ object Checking {
158171
* @pre sym is not yet initialized (i.e. its type is a Completer).
159172
* @return `info` where every legal F-bounded reference is proctected
160173
* by a `LazyRef`, or `ErrorType` if a cycle was detected and reported.
174+
* Furthermore: Add an #Apply to a fully instantiated type lambda, if none was
175+
* given before. This is necessary here because sometimes type lambdas are not
176+
* recognized when they are first formed.
161177
*/
162178
def checkNonCyclic(sym: Symbol, info: Type, reportErrors: Boolean)(implicit ctx: Context): Type = {
163179
val checker = new CheckNonCyclicMap(sym, reportErrors)(ctx.addMode(Mode.CheckCyclic))

0 commit comments

Comments
 (0)