Skip to content

C, LLVM: a match-arm binder shadows a prelude fn when a closure captures it - #92

Merged
284km merged 1 commit into
mainfrom
mere-closure-pattern-capture
Nov 25, 2026
Merged

284km merged 1 commit into
mainfrom
mere-closure-pattern-capture

Conversation

@284km

@284km 284km commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

What was wrong

A closure that captures a variable bound by a match arm did not compile on the C backend when the binder had the name of a prelude function (id, incr, ...):

type V = VObj of int | VNone;
let use = fn (k: int) -> fn (s: str) -> k + str_len s;
let f = fn (v: V) ->
  match v with
  | VObj id -> (let g = fn (n: int) -> use id "x" + n in g 1)
  | VNone -> 0;
print (str_of_int (f (VObj 41)))

The interpreter prints 43. mere -c emitted C that read mu_id_as_value (the prelude's id closure) where the int was meant, and clang refused it (use of undeclared identifier 'mu_id_as_value', then a closure passed to an int parameter). Naming the binder x9 or binding it with let, or taking it as a fn parameter, all worked.

LLVM had the same hole and refused incr at emit time (fn-as-value on non-arrow type).

Why

When an inner fn is lifted, its captures are its free variables minus known (top-level fns, builtins, externs). A local of the host that shadows one of those is subtracted from known first, through host_locals. let binders and fn parameters were added to host_locals; match-arm binders were not, so an arm binder named like a prelude fn was taken for that fn and not captured.

The change

  • codegen_c.ml (walk_children, the Match case) and codegen_llvm.ml (walk, the Match case): each arm's pattern_vars are added to host_locals for its guard and body.
  • Wasm already did this (arm_locals), and the RISC-V backend computes captures structurally; both were right before the change.
  • test/parity/match_binder_named_like_prelude_fn.mere: id and incr as arm binders read from an inner let-bound fn, inside a tuple pattern and a guard, from an inner let rec, and from an anonymous closure passed as a value. README's parity count is 257.

Checked

  • The new parity case before the change: c:MISCOMPILE llvm:EMITFAIL wasm:MATCH; after: c:MATCH llvm:MATCH wasm:MATCH. It also gives the interpreter's output on RV32I and RV64 (memu's rv32i_run / rv64i_run, --ram 32).
  • dune test: 3305 passed, 0 failed (the README's count; parity is not part of it).
  • scripts/parity.sh (all cases): 257 passed, 0 failed; the failing programs 31 passed, 0 failed. The unchecked and declared-divergence lists are the same as on main (schema_reflect on LLVM, coro_pingpong on Wasm, int_width_boundary).
  • A large downstream program (about 525k lines of emitted C) gives byte-identical C before and after.

🤖 Generated with Claude Code

…res it

A closure lifted out of a match arm did not capture the arm's binder when the
binder had the name of a prelude fn (`| VObj id -> ... fn n -> use id ...`):
the binder stayed in `known` and the emitted code read the prelude fn instead.
C did not compile (`mu_id_as_value`); LLVM refused at emit time ("fn-as-value
on non-arrow type", with `incr`). `let` binders and fn parameters were already
added to `host_locals`; arm binders now are too, for the arm's guard and body.
Wasm already did this, and RISC-V computes captures structurally.

test/parity/match_binder_named_like_prelude_fn.mere covers `id` and `incr`.

@284km 284km left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 6cba0d5. No Critical / Major / Minor findings.

  • Cause and fix are right: when lifting inner fns, host_locals decides which names are captured rather than referenced as globals. let binders and fn parameters were added; match-arm binders were not, so an arm binder named like a prelude fn (id, incr) was taken for the global. Both lift_inner_fns (C) and lift_inner_fns_llvm now add pattern_vars pat for each arm, covering the guard and the body. The note that Wasm already counted arm binders (arm_locals) and RV counts captures from syntax matches what the parity results show.
  • Ran the author's build at this head: the 7-line repro from the report prints 43 from the interpreter and from the C backend (mere -c + clang); the old build (0.1.732) fails to compile it with use of undeclared identifier 'mu_id_as_value'. The new parity program match_binder_named_like_prelude_fn.mere (id and incr, a tuple pattern with a guard, an inner let rec, an anonymous closure passed as a value) prints the same 7 values from the interpreter and from C.
  • The author's results: dune test 3305 passed / 0 failed; parity 257 passed (the program fails on main as c:MISCOMPILE and llvm:EMITFAIL), the expected-failure set 31 passed; and the C emitted for mere-ruby's main.mere is byte-identical before and after, so downstream is unaffected.
  • No review-bot comments.

Merging (squash).

@284km
284km merged commit 4f2c660 into main Nov 25, 2026
17 checks passed
@284km
284km deleted the mere-closure-pattern-capture branch November 25, 2026 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant