Skip to content

Commit 32826d8

Browse files
committed
infer: Just check occursIn in tvar instantiate check
1 parent cb8888c commit 32826d8

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,17 +409,14 @@ object Inferencing {
409409
Stats.record("maximizeType")
410410
val vs = variances(tp)
411411
val patternBindings = new mutable.ListBuffer[(Symbol, TypeParamRef)]
412-
val gadtVariances = ctx.gadt.symbols.map(sym => variances(ctx.gadt.bounds(sym).nn))
412+
val gadtBounds = ctx.gadt.symbols.map(ctx.gadt.bounds(_).nn)
413413
vs foreachBinding { (tvar, v) =>
414414
if !tvar.isInstantiated then
415415
// if the tvar is covariant/contravariant (v == 1/-1, respectively) in the input type tp
416-
// then it is safe to instantiate if it has the same variance within the GADT bounds.
417-
// Eg neg/i14983 the C in Node[+C] differs in variance to the GADT bound X >: List[C] so maximising to Node[Any] is unsound
418-
// Eg pos/precise-pattern-type the T in Tree[-T] is in no GADT bound so can maximise to Tree[Type]
419-
val safeToInstantiate = v != 0 && gadtVariances.forall { vmap =>
420-
val v2 = vmap(tvar)
421-
v2 == null || v2 == v
422-
}
416+
// then it is safe to instantiate if it doesn't occur in any of the GADT bounds.
417+
// Eg neg/i14983 the C in Node[+C] occurs in GADT bound X >: List[C] so maximising to Node[Any] is unsound
418+
// Eg pos/precise-pattern-type the T in Tree[-T] doesn't occur in any GADT bound so can maximise to Tree[Type]
419+
val safeToInstantiate = v != 0 && gadtBounds.forall(!tvar.occursIn(_))
423420
if safeToInstantiate then tvar.instantiate(fromBelow = v == -1)
424421
else {
425422
val bounds = TypeComparer.fullBounds(tvar.origin)

0 commit comments

Comments
 (0)