Skip to content

Commit 2493ae2

Browse files
oderskyallanrenucci
authored andcommitted
Refine TypeVarsMissContext for MatchTypes
Match-pattern bound variables should not assume to match all other types, even if TypeVarsMissContext is true.
1 parent 10e4494 commit 2493ae2

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,13 @@ trait ConstraintHandling {
363363
final def canConstrain(param: TypeParamRef): Boolean =
364364
(!frozenConstraint || (caseLambda `eq` param.binder)) && constraint.contains(param)
365365

366+
/** Is `param` assumed to be a sub- and super-type of any other type?
367+
* This holds if `TypeVarsMissContext` is set unless `param` is a part
368+
* of a MatchType that is currently normalized.
369+
*/
370+
final def assumedTrue(param: TypeParamRef): Boolean =
371+
ctx.mode.is(Mode.TypevarsMissContext) && (caseLambda `ne` param.binder)
372+
366373
/** Add constraint `param <: bound` if `fromBelow` is false, `param >: bound` otherwise.
367374
* `bound` is assumed to be in normalized form, as specified in `firstTry` and
368375
* `secondTry` of `TypeComparer`. In particular, it should not be an alias type,

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling {
325325
true
326326
}
327327
def compareTypeParamRef =
328-
ctx.mode.is(Mode.TypevarsMissContext) ||
328+
assumedTrue(tp1) ||
329329
isSubTypeWhenFrozen(bounds(tp1).hi, tp2) || {
330330
if (canConstrain(tp1) && !approx.high)
331331
addConstraint(tp1, tp2, fromBelow = false) && flagNothingBound
@@ -428,7 +428,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling {
428428
thirdTryNamed(tp2)
429429
case tp2: TypeParamRef =>
430430
def compareTypeParamRef =
431-
(ctx.mode is Mode.TypevarsMissContext) || {
431+
assumedTrue(tp2) || {
432432
val alwaysTrue =
433433
// The following condition is carefully formulated to catch all cases
434434
// where the subtype relation is true without needing to add a constraint
@@ -787,9 +787,8 @@ class TypeComparer(initctx: Context) extends ConstraintHandling {
787787
tl => tparams1.map(tparam => tl.integrate(tparams, tparam.paramInfo).bounds),
788788
tl => tp1base.tycon.appliedTo(args1.take(lengthDiff) ++
789789
tparams1.indices.toList.map(tl.paramRefs(_))))
790-
(ctx.mode.is(Mode.TypevarsMissContext) ||
791-
tryInstantiate(tycon2, tycon1.ensureLambdaSub)) &&
792-
recur(tp1, tycon1.appliedTo(args2))
790+
(assumedTrue(tycon2) || tryInstantiate(tycon2, tycon1.ensureLambdaSub)) &&
791+
recur(tp1, tycon1.appliedTo(args2))
793792
}
794793
}
795794
case _ => false

0 commit comments

Comments
 (0)