Skip to content

fix #418: fix fn* lambda codegen#426

Open
tox1kly wants to merge 2 commits into
zenc-lang:mainfrom
tox1kly:fix/issue-418
Open

fix #418: fix fn* lambda codegen#426
tox1kly wants to merge 2 commits into
zenc-lang:mainfrom
tox1kly:fix/issue-418

Conversation

@tox1kly
Copy link
Copy Markdown

@tox1kly tox1kly commented May 18, 2026

fix #418: fix fn* lambda codegen

This PR fixes a bug in lambda code generation when a closure captures raw function pointers (fn*).

Previously, Zen C could generate invalid C for closures like these because function pointer fields inside the lambda context struct were emitted incorrectly, and the captured values were assigned using an unsafe cast-based pattern.

For code like this:

alias fp = fn*(int) -> int;
alias clos = fn(int) -> int;

fn compose(f: fp, g: fp) -> clos {
    return fn(x: int) -> int {
        return f(g(x));
    };
}

BEFORE
Zen C could generate invalid C such as

struct Lambda_0_Ctx {
    int32_t (*)(int32_t) f;
    int32_t (*)(int32_t) g;
};

*(int32_t (*)(int32_t)*)(&_z_ctx_0->f) = f;
*(int32_t (*)(int32_t)*)(&_z_ctx_0->g) = g;

AFTER

struct Lambda_0_Ctx {
    int32_t (*f)(int32_t);
    int32_t (*g)(int32_t);
};

_z_ctx_0->f = f;
_z_ctx_0->g = g;

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Closure unable to capture raw function pointers

1 participant