Skip to content

bug: returning more values than declared leaks to C compilation #1588

@SchoolyB

Description

@SchoolyB

Description

A function declared with a single return type can return multiple comma-separated values without the typechecker catching it. The codegen then emits a multi-return struct (EzMulti_foo) that was never typedef'd, causing a C compilation crash.

Reproduction

do foo() -> int {
    return 1, 2
}

do main() {
    println(foo())
}

Expected: Compile error — foo declares a single int return but returns 2 values

Actual: Typechecker passes, C compilation fails:

error: use of undeclared identifier 'EzMulti_foo'
    { EzMulti_foo _ret = (EzMulti_foo){1, 2}; ez_exit_func(); return _ret; }
      ^

Root Cause

The typechecker does not validate that the number of values in a return statement matches the number of declared return types. When a function is declared as -> int (single return), returning 1, 2 should be rejected. The codegen assumes the typechecker has validated this and emits a multi-return struct path for the comma-separated values, but no such struct was defined since the function signature only declares one return value.

Fix

In the typechecker's return statement handler, count the number of comma-separated return values and compare against the function's declared return count. If they don't match, emit an error like:

E3XXX: function 'foo' returns 1 value but return statement has 2 values

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