Skip to content

Refactor: Function call argument type checking code duplication #140

Description

@clank-hayen

Problem

In typechecker/check_function_call.cpp, the same argument type checking pattern is repeated 6 times for:

  1. Qualified function calls (lines 234-246)
  2. Local function type calls (lines 267-275)
  3. User-defined functions (lines 297-305)
  4. Extern functions (lines 319-326)
  5. Using aliases for functions (lines 347-355)
  6. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions