Problem
In typechecker/check_function_call.cpp, the same argument type checking pattern is repeated 6 times for:
- Qualified function calls (lines 234-246)
- Local function type calls (lines 267-275)
- User-defined functions (lines 297-305)
- Extern functions (lines 319-326)
- Using aliases for functions (lines 347-355)
- Static method calls (lines 425-433)
Each follows the identical pattern:
for (size_t i = 0; i < call.args.size() && i < func->params.size(); i++) {
TypeInfo arg_type = infer_type(state, *call.args[i]);
TypeInfo param_type = {func->params[i].type, false, false};
if (!types_compatible(param_type, arg_type)) {
error(state, "argument " + to_string(i + 1) + " of function '" + call.name +
"' expects '" + format_type(param_type) + "', got '" + format_type(arg_type) + "'", call.line);
}
}
Suggested Solution
Extract a helper function:
void check_call_arguments(TypeCheckerState& state,
const vector<unique_ptr<ASTNode>>& args,
const vector<FunctionParam>& params,
const string& func_name,
int line) {
if (args.size() != params.size()) {
error(state, "function '" + func_name + "' expects " + to_string(params.size()) +
" arguments, got " + to_string(args.size()), line);
}
for (size_t i = 0; i < args.size() && i < params.size(); i++) {
TypeInfo arg_type = infer_type(state, *args[i]);
TypeInfo param_type = {params[i].type, false, false};
if (!types_compatible(param_type, arg_type)) {
error(state, "argument " + to_string(i + 1) + " of function '" + func_name +
"' expects '" + format_type(param_type) + "', got '" +
format_type(arg_type) + "'", line);
}
}
}
Impact
- Eliminates ~60 lines of repeated code
- Single point for argument checking improvements
- Consistent error messages
Problem
In
typechecker/check_function_call.cpp, the same argument type checking pattern is repeated 6 times for:Each follows the identical pattern:
Suggested Solution
Extract a helper function:
Impact