Skip to content

Commit b31d599

Browse files
oderskysmarter
authored andcommitted
Harden copmpareHkApply for ill-typed programs
Turn assertion into test. Without this, neg/tcpoly_overloaded.scala fails.
1 parent 5f598e8 commit b31d599

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
580580
*/
581581
def compareHkApply2(tp1: Type, tp2: HKApply, tycon2: Type, args2: List[Type]): Boolean = {
582582
val tparams = tycon2.typeParams
583-
assert(tparams.nonEmpty)
583+
if (tparams.isEmpty) return false // can happen for ill-typed programs, e.g. neg/tcpoly_overloaded.scala
584584

585585
/** True if `tp1` and `tp2` have compatible type constructors and their
586586
* corresponding arguments are subtypes relative to their variance (see `isSubArgs`).

tests/neg/tcpoly_overloaded.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
trait Monad[T <: Bound[T], MyType[x <: Bound[x]], Bound[_]] {
2+
def flatMap[S <: RBound[S], RContainer[x <: RBound[x]], RBound[_],
3+
Result[x <: RBound[x]] <: Monad[x, RContainer, RBound]]
4+
(f: T => Result[S]): Result[S]
5+
def flatMap[S <: RBound[S], RContainer[x <: RBound[x]], RBound[_],
6+
Result[x <: RBound[x]] <: Monad[x, RContainer, RBound]]
7+
(f: T => Result[S], foo: String): Result[S]
8+
def flatMap[S <: Bound[S]]
9+
(f: T => MyType[S], foo: Int): MyType[S]
10+
}
11+
12+
trait Test {
13+
def moo: MList[Int]
14+
class MList[T](el: T) extends Monad[T, List, Any] { // error: does not conform to upper bound // error
15+
def flatMap[S <: RBound[S], RContainer[x <: RBound[x]], RBound[_],
16+
Result[x <: RBound[x]] <: Monad[x, RContainer, RBound]]
17+
(f: T => Result[S]): Result[S] = sys.error("foo")
18+
def flatMap[S <: RBound[S], RContainer[x <: RBound[x]], RBound[_],
19+
Result[x <: RBound[x]] <: Monad[x, RContainer, RBound]]
20+
(f: T => Result[S], foo: String): Result[S] = sys.error("foo")
21+
def flatMap[S]
22+
(f: T => List[S], foo: Int): List[S] = sys.error("foo")
23+
}
24+
val l: MList[String] = moo.flatMap[String, List, Any, MList]((x: Int) => new MList("String")) // error: does not conform to upper bound // error
25+
}

tests/pending/pos/tcpoly_bounds1.scala renamed to tests/pos/tcpoly_bounds1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object foo extends Foo[MyPair]
77

88

99
trait Monad[m[x <: Bound[x]], Bound[x], a] // TODO: variances!
10-
trait ListMonad[a] extends Monad[List, Any, a]
10+
trait ListMonad[a] extends Monad[List, [X] -> Any, a] // Dotty difference: Any is not a legal argument for hk type.
1111

1212
trait MyOrdered[a]
1313
trait MySet[x <: MyOrdered[x]]

tests/pending/pos/tcpoly_overloaded.scala renamed to tests/pos/tcpoly_overloaded.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ trait Monad[T <: Bound[T], MyType[x <: Bound[x]], Bound[_]] {
1111

1212
trait Test {
1313
def moo: MList[Int]
14-
class MList[T](el: T) extends Monad[T, List, Any] {
14+
class MList[T](el: T) extends Monad[T, List, [X] -> Any] {
1515
def flatMap[S <: RBound[S], RContainer[x <: RBound[x]], RBound[_],
1616
Result[x <: RBound[x]] <: Monad[x, RContainer, RBound]]
1717
(f: T => Result[S]): Result[S] = sys.error("foo")
@@ -21,5 +21,5 @@ trait Test {
2121
def flatMap[S]
2222
(f: T => List[S], foo: Int): List[S] = sys.error("foo")
2323
}
24-
val l: MList[String] = moo.flatMap[String, List, Any, MList]((x: Int) => new MList("String"))
24+
val l: MList[String] = moo.flatMap[String, List, [X] -> Any, MList]((x: Int) => new MList("String"))
2525
}

0 commit comments

Comments
 (0)