Repository navigation
C, LLVM: a match-arm binder shadows a prelude fn when a closure captures it - #92
Merged
Merged
Conversation
…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
commented
Nov 25, 2026
284km
left a comment
Collaborator
Author
There was a problem hiding this comment.
Reviewed 6cba0d5. No Critical / Major / Minor findings.
- Cause and fix are right: when lifting inner fns,
host_localsdecides which names are captured rather than referenced as globals.letbinders 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. Bothlift_inner_fns(C) andlift_inner_fns_llvmnow addpattern_vars patfor 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 withuse of undeclared identifier 'mu_id_as_value'. The new parity programmatch_binder_named_like_prelude_fn.mere(id and incr, a tuple pattern with a guard, an innerlet rec, an anonymous closure passed as a value) prints the same 7 values from the interpreter and from C. - The author's results:
dune test3305 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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
A closure that captures a variable bound by a
matcharm did not compile on the C backend when the binder had the name of a prelude function (id,incr, ...):The interpreter prints 43.
mere -cemitted C that readmu_id_as_value(the prelude'sidclosure) where the int was meant, and clang refused it (use of undeclared identifier 'mu_id_as_value', then a closure passed to anintparameter). Naming the binderx9or binding it withlet, or taking it as a fn parameter, all worked.LLVM had the same hole and refused
incrat 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 fromknownfirst, throughhost_locals.letbinders and fn parameters were added tohost_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, theMatchcase) andcodegen_llvm.ml(walk, theMatchcase): each arm'spattern_varsare added tohost_localsfor its guard and body.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:idandincras arm binders read from an innerlet-bound fn, inside a tuple pattern and a guard, from an innerlet rec, and from an anonymous closure passed as a value. README's parity count is 257.Checked
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'srv32i_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_reflecton LLVM,coro_pingpongon Wasm,int_width_boundary).🤖 Generated with Claude Code