Skip to content

bug: struct function calls bypass argument count validation #1587

@SchoolyB

Description

@SchoolyB

Description

Calling a struct function with the wrong number of arguments passes the typechecker and crashes during C compilation. The typechecker validates argument count for regular function calls but not for struct-scoped function calls — they use a separate code path that skips this check entirely.

Reproduction

const Foo struct {
    x int

    do bar(f Foo) -> int {
        return f.x
    }
}

do main() {
    mut f = Foo{x: 5}
    mut r = f.bar(1, 2, 3)
    println(r)
}

Expected: Compile error — bar expects 1 argument (self-dispatched), got 3 extra

Actual: Typechecker passes, C compilation fails:

error: too many arguments to function call, expected 1, have 4
    __auto_type r = ez_fn_Foo_bar(f, 1, 2, 3);
                    ~~~~~~~~~~~~~    ^~~~~~~

Root Cause

Struct function calls take a separate code path in the typechecker that bypasses the argument count and type validation applied to regular function calls. This is a known gap documented in the codebase.

Fix

In the typechecker, when resolving a struct-scoped function call (instance dispatch like f.bar(...) or static call like Foo.bar(...)), validate:

  1. Argument count matches the function's parameter count (minus the self parameter for instance dispatch)
  2. Argument types match the expected parameter types

This should mirror the existing validation logic for regular function calls.

Notes

This likely also affects argument type validation for struct function calls (e.g., passing a string where an int is expected). Both count and type validation should be added in the same fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtypecheckerRelated to type checking and validation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions