Skip to content

Commit 2ec9571

Browse files
committed
Do not read a type argument by position when the arities differ
Resolving a type class dispatch looked up the type variable's position and then indexed the type arguments without checking the two lists correspond. A method which declares its own type parameters alongside its class's is called with only its own, so position no longer means anything: the class's variable read the method's argument, and a later variable would have run off the end. That is the same positional confusion already fixed for the interpreter, still present in the specializer. Both lookups now bail when the arities disagree, so a dispatch is left for a pass that has matching arguments rather than resolved against whichever type happened to sit at that index. The shape which exposed this, a bounded generic class method with an independent type parameter, is unsupported on Lua by design and still reports an error there; it never produced a wrong result, which was verified by making a wrong dispatch resolvable and confirming it still failed.
1 parent 50b8413 commit 2ec9571

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/EliminateGenerics.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ private static void inheritTypeClassBinding(ImTypeArgument ta, ImType original,
11531153
return;
11541154
}
11551155
int index = indexOfTypeVar(typeVars, ref.getTypeVariable());
1156-
if (index < 0) {
1156+
if (index < 0 || index >= generics.getTypeArguments().size()) {
11571157
return;
11581158
}
11591159
Map<ImTypeClassFunc, Either<ImMethod, ImFunction>> outer =
@@ -1187,6 +1187,12 @@ private static int indexOfTypeVar(List<ImTypeVar> typeVars, ImTypeVar target) {
11871187

11881188
private void resolveTypeClassDispatch(ImTypeVarDispatch e, GenericTypes generics, List<ImTypeVar> typeVars) {
11891189
int index = indexOfTypeVar(typeVars, e.getTypeVariable());
1190+
if (index >= 0 && index >= generics.getTypeArguments().size()) {
1191+
// Fewer arguments than variables: the variables and the arguments are not in
1192+
// correspondence here, so position says nothing. Reading one anyway would dispatch
1193+
// through whichever type happened to sit at that index.
1194+
return;
1195+
}
11901196
if (index < 0) {
11911197
// dispatching on a variable of some enclosing generic; it is resolved when that one is
11921198
// specialised.

0 commit comments

Comments
 (0)