Skip to content

Commit 5f51f5f

Browse files
committed
Fix type avoidance in nested Array case
1 parent a71210b commit 5f51f5f

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5856,7 +5856,7 @@ object Types {
58565856
tp.derivedAppliedType(tycon, args.map(rangeToBounds)) match
58575857
case tp1: AppliedType if tp1.isUnreducibleWild =>
58585858
// don't infer a type that would trigger an error later in
5859-
// Checling.checkAppliedType; fall through to default handling instead
5859+
// Checking.checkAppliedType; fall through to default handling instead
58605860
case tp1 =>
58615861
return tp1
58625862
end if
@@ -5865,7 +5865,7 @@ object Types {
58655865
// non-range arguments L1, ..., Ln and H1, ..., Hn such that
58665866
// C[L1, ..., Ln] <: C[H1, ..., Hn] by taking the right limits of
58675867
// ranges that appear in as co- or contravariant arguments.
5868-
// Fail for non-variant argument ranges.
5868+
// Fail for non-variant argument ranges (see use-site else branch below).
58695869
// If successful, the L-arguments are in loBut, the H-arguments in hiBuf.
58705870
// @return operation succeeded for all arguments.
58715871
def distributeArgs(args: List[Type], tparams: List[ParamInfo]): Boolean = args match {
@@ -5886,8 +5886,11 @@ object Types {
58865886
if (distributeArgs(args, tp.tyconTypeParams))
58875887
range(tp.derivedAppliedType(tycon, loBuf.toList),
58885888
tp.derivedAppliedType(tycon, hiBuf.toList))
5889-
else range(defn.NothingType, defn.AnyType)
5890-
// TODO: can we give a better bound than `topType`?
5889+
else if tycon.isLambdaSub then
5890+
range(defn.NothingType, defn.AnyType)
5891+
else
5892+
// See lampepfl/dotty#14152
5893+
range(defn.NothingType, tp.derivedAppliedType(tycon, args.map(rangeToBounds)))
58915894
else tp.derivedAppliedType(tycon, args)
58925895
}
58935896

tests/pos/i14152.min.scala

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/pos/i14152.scala

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
1+
val a1 = {
2+
object O1 extends AnyRef
3+
Array(O1)
4+
}
5+
val a2: Array[_ <: AnyRef] = aa1
6+
17
val aa1 = {
28
object O1 extends AnyRef
39
Array(Array(O1))
410
}
5-
611
val aa2: Array[_ <: Array[_ <: AnyRef]] = aa1
12+
13+
val aaa1 = {
14+
object O1 extends AnyRef
15+
Array(Array(Array(O1)))
16+
}
17+
val aaa2: Array[_ <: Array[_ <: Array[_ <: AnyRef]]] = aaa1
18+
19+
20+
// Let's make sure avoidance still does the right thing given abstract type constructors
21+
22+
class Inv[T](x: T)
23+
24+
def foo[F[_]](fn: [A] => Inv[A] => F[A]) =
25+
object O1 extends AnyRef
26+
val res0 = fn(new Inv(fn(new Inv[O1.type](O1))))
27+
val res1: F[F[O1.type]] = res0
28+
res1 // checked with -Xprint:typer that this widens to Any
29+
// instead of the original F[F[O1.type]]
30+
// or the incorrectly avoided F[? <: F[? <: Object]]

0 commit comments

Comments
 (0)