Skip to content

Commit 1667cc3

Browse files
committed
Refine variance for NamedTypes in TypeMaps
1 parent c3645d8 commit 1667cc3

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -3713,8 +3713,19 @@ object Types {
37133713
tp match {
37143714
case tp: NamedType =>
37153715
if (stopAtStatic && tp.symbol.isStatic) tp
3716-
else derivedSelect(tp, this(tp.prefix))
3717-
3716+
else {
3717+
val saved = variance
3718+
variance = variance max 0
3719+
// A prefix is never contravariant. Even if say `p.A` is used in a contravariant
3720+
// context, we cannot assume contravariance for `p` because `p`'s lower
3721+
// bound might not have a binding for `A` (e.g. the lower bound could be `Nothing`).
3722+
// By contrast, covariance does translate to the prefix, since we have that
3723+
// if `p <: q` then `p.A <: q.A`, and well-formedness requires that `A` is a member
3724+
// of `p`'s upper bound.
3725+
val prefix1 = this(tp.prefix)
3726+
variance = saved
3727+
derivedSelect(tp, prefix1)
3728+
}
37183729
case _: ThisType
37193730
| _: BoundType
37203731
| NoPrefix => tp

0 commit comments

Comments
 (0)