Fixed broken varargs overload selector logic

This commit is contained in:
bigguy345 committed 2026-03-07 00:40:04 +02:00
1 parent 9f26b27210
commit fb3d6546e5
1 file changed
+24 -1
@@ -21,7 +21,7 @@ public class OverloadSelector {
// Scoring constants
private static final int ARITY_MISMATCH_BASE = 10000;
private static final int VARARGS_PENALTY = 1000;
private static final int VARARGS_PENALTY = 50;
private static final int UNKNOWN_TYPE_PENALTY = 10;
private static final int COMPATIBLE_TYPE_PENALTY = 5;
private static final int INCOMPATIBLE_TYPE_PENALTY = 100;
@@ -208,6 +208,12 @@ public class OverloadSelector {
java.lang.reflect.Method javaMethod = method.getJavaMethod();
if (javaMethod != null) {
isVarArgs = javaMethod.isVarArgs();
} else {
// JS methods from .d.ts have no backing Java method; vararg info lives on the last FieldInfo
List<FieldInfo> checkParams = method.getParameters();
if (!checkParams.isEmpty()) {
isVarArgs = checkParams.get(checkParams.size() - 1).isVarArg();
}
}
// Determine arity applicability
@@ -256,6 +262,23 @@ public class OverloadSelector {
}
}
// For vararg methods, also score extra args (index >= paramCount) against the element type
if (isVarArgs && argCount > paramCount && !params.isEmpty()) {
TypeInfo varargElemType = params.get(paramCount - 1).getTypeInfo();
for (int i = paramCount; i < argCount; i++) {
TypeInfo argType = (argTypes != null && i < argTypes.length) ? argTypes[i] : null;
if (argType == null || varargElemType == null) {
score += UNKNOWN_TYPE_PENALTY;
} else if (varargElemType.equals(argType)) {
// exact match
} else if (TypeChecker.isTypeCompatible(varargElemType, argType)) {
score += COMPATIBLE_TYPE_PENALTY;
} else {
score += INCOMPATIBLE_TYPE_PENALTY;
}
}
}
// Penalize extra args that don't match params (for non-varargs)
if (!isVarArgs && argCount > paramCount) {
score += (argCount - paramCount) * INCOMPATIBLE_TYPE_PENALTY;