Skip to content

bug: codegen emits int64_t for struct return type when calling through typed [func] array index #1553

@SchoolyB

Description

@SchoolyB

Description

When a struct-namespaced function reference is stored in a typed [func(...) -> StructType] array and called via constant index (e.g., ctors[0](42)), the codegen emits int64_t as the return type instead of the actual struct type. The C compiler then fails because member access (.x, .y) is attempted on an int64_t.

Repro

const Vec struct {
    x int
    y int

    do create(val int) -> Vec {
        return Vec{x: val, y: val * 2}
    }
}

do main() {
    mut ctors [func(int) -> Vec] = {()Vec.create}
    mut v = ctors[0](42)
    println(v.x)
}

Expected: Compiles and prints 42.

Actual: C compilation fails — member reference base type 'int64_t' (aka 'long long') is not a structure or union.

Root cause

The typechecker's func_array_refs tracking (added in #1458) only activates for bare [func] arrays (strcmp(type_name, "[func]") == 0). Typed func arrays like [func(int) -> Vec] are not matched, so per-element function references are never recorded. Without that data, the typechecker cannot recover the struct return type at the call site, and codegen falls back to int64_t.

Fix

Change the condition to also match typed func array types using strncmp(type_name, "[func(", 6).

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