Skip to content

Commit 335351b

Browse files
committed
Hardening in the presence of kind mismatches
Three fixes for when the number of type-parameters disagrees with the number of type arguments. One is in TypeAccumulator, two more are in Substituters. There's a tricky balance here. On the one hand, crashing early on mismatches is an excellent means to pinpoint errors in the compiler. On the other hand, it makes legal but nonsensical programs crash the compiler. So, we should do the minimum so that "on the other hand" is not observed. Which means doing this driven by counter-examples (coming from fuzzing or elsewhere) is not a bad way to tackle the problem.
1 parent b537220 commit 335351b

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ trait Substituters { this: Context =>
5858
val sym = tp.symbol
5959
var fs = from
6060
var ts = to
61-
while (fs.nonEmpty) {
61+
while (fs.nonEmpty && ts.nonEmpty) {
6262
if (fs.head eq sym) return ts.head
6363
fs = fs.tail
6464
ts = ts.tail
@@ -203,7 +203,7 @@ trait Substituters { this: Context =>
203203
val sym = tp.symbol
204204
var fs = from
205205
var ts = to
206-
while (fs.nonEmpty) {
206+
while (fs.nonEmpty && ts.nonEmpty) {
207207
if (fs.head eq sym)
208208
return ts.head match {
209209
case TypeBounds(lo, hi) => range(lo, hi)

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4791,10 +4791,7 @@ object Types {
47914791

47924792
case tp @ AppliedType(tycon, args) =>
47934793
@tailrec def foldArgs(x: T, tparams: List[ParamInfo], args: List[Type]): T =
4794-
if (args.isEmpty) {
4795-
assert(tparams.isEmpty)
4796-
x
4797-
}
4794+
if (args.isEmpty || tparams.isEmpty) x
47984795
else {
47994796
val tparam = tparams.head
48004797
val acc = args.head match {

tests/neg/i6064.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
trait Trait[X] {
2+
def f[C[_]](arg: C[X]): Int
3+
val a = f[(Int, String)] // error
4+
}

0 commit comments

Comments
 (0)