Skip to content

Commit 2ff2b37

Browse files
committed
Fix upper bound constraints, that are higher-kinded
When recording an upper bound, when it's a some higher-kinded type variable applied to some type arguments, TypeComparer was re-applying the type arguments to the type parameter (the type variable's origin). Meaning that the instantiation of the type variable won't reflect in the upper bound. See the error message: `F$1[Int]` correctly appears as the lower bound, but `F$1[Int]` isn't the upper bound, `F[Int]` is, which is the original type parameter, in `Foo.Bar.apply[F](..)`. -- [E007] Type Mismatch Error: i12478.scala:8:13 ------------------------------- 8 | Foo.Bar(fu1) | ^^^^^^^^^^^^ |Found: Foo.Bar[F$1] |Required: Foo[T1] | |where: F$1 is a type in method id1 with bounds <: [_] =>> Any | T1 is a type in method id1 with bounds >: F$1[Int] and <: F[Int] | | longer explanation available when compiling with `-explain` -- [E007] Type Mismatch Error: i12478.scala:18:13 ------------------------------ 18 | Foo.Bar(fu3) | ^^^^^^^^^^^^ |Found: Foo.Bar[F$2] |Required: Foo[T3] | |where: F$2 is a type in method id3 with bounds <: [_] =>> Any | T3 is a type in method id3 with bounds >: F$2[Int] and <: F[Int] | | longer explanation available when compiling with `-explain`
1 parent d99d9bf commit 2ff2b37

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1338,8 +1338,11 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
13381338
}
13391339
} || tryLiftedToThis2
13401340

1341-
case _: TypeVar =>
1342-
recur(tp1, tp2.superType)
1341+
case tv: TypeVar =>
1342+
if tv.isInstantiated then
1343+
recur(tp1, tp2.superType)
1344+
else
1345+
compareAppliedType2(tp2, tv.origin, args2)
13431346
case tycon2: AnnotatedType if !tycon2.isRefining =>
13441347
recur(tp1, tp2.superType)
13451348
case tycon2: AppliedType =>

i12478.scala

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
sealed trait Foo[T]
2+
3+
object Foo:
4+
case class Bar[F[_]](fu: List[F[Unit]]) extends Foo[F[Unit]]
5+
6+
class Test:
7+
def id1[T1](foo1: Foo[T1]): Foo[T1] = foo1 match
8+
case Foo.Bar(fu) =>
9+
Foo.Bar(fu)
10+
11+
def id2[T2](foo2: Foo[T2]): Foo[T2] = foo2 match
12+
case bar2 @ (_: Foo.Bar[f]) =>
13+
val fu2 = bar2.fu
14+
Foo.Bar(fu2)
15+
16+
def id3[T3](foo3: Foo[T3]): Foo[T3] = foo3 match
17+
case bar3 @ Foo.Bar(_) =>
18+
val fu3 = bar3.fu
19+
Foo.Bar(fu3)

0 commit comments

Comments
 (0)