Skip to content

codegen: thread the function name explicitly instead of ambient current_fn_name - #279

Merged
0xGeorgii merged 3 commits into
mainfrom
172-refactoring-thread-fn-context
Jul 19, 2026
Merged

codegen: thread the function name explicitly instead of ambient current_fn_name#279
0xGeorgii merged 3 commits into
mainfrom
172-refactoring-thread-fn-context

Conversation

@0xGeorgii

@0xGeorgii 0xGeorgii commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #172. Stacked on #278 (167-refactoring-shared-statement-walker) — merge that first (both live in compiler.rs).

Removes the mutable ambient Compiler::current_fn_name: String and threads the function name explicitly as fn_name: &str from visit_function_definition_body through the statement-lowering walker (lower_statementlower_block/lower_if_statement/lower_loop_statement) to its sole runtime reader, the sret-return invariant panic in lower_sret_return.

  • Plain &str threading, no context struct: there is exactly one reader, and this matches how arena/ctx/module_path already flow. The sibling current_fn_key is also ambient but out of current_fn_name Mutable State on Compiler #172's scope — left for a separate cleanup (noted intentionally).
  • Latent-bug check (per the issue's ambient-context concern): none found — the field was written fresh per function and only read within that function's lowering; the empty-string init was unreachable as a read.
  • Byte identity: golden suite green with zero regenerated expectations (542 codegen tests); the examiner independently built both branches and compiled the array-sret and struct-sret fixtures — identical SHA256. Full default-workspace cargo test 4359/0; default clippy clean, zero new pedantic findings vs baseline (990 = 990).

Confidence Score: 5/5

Safe to merge — purely mechanical refactoring with no behavioral change and byte-identical WASM output confirmed by the golden test suite.

The change removes a write-once-per-function ambient string field and replaces it with an explicit &str parameter. Every call site has been updated, the borrow lifetime is sound (the owning String lives in visit_function_definition for the duration of all nested calls), and the sole reader (the sret panic message) is unchanged in meaning. No logic paths were added or altered.

No files require special attention.

Important Files Changed

Filename Overview
core/wasm-codegen/src/compiler.rs Removes current_fn_name: String from Compiler, adds fn_name: &str parameter to five lowering functions; threading is mechanically correct and all call sites are updated.
CHANGELOG.md Adds a correct changelog entry for #172 and the corresponding reference link; prose accurately describes the change.

Reviews (2): Last reviewed commit: "Merge branch 'main' into 172-refactoring..." | Re-trigger Greptile

The WASM code generator's function-body passes each recursed into
Block/If/Loop independently to discover what they needed: pre_scan_locals
numbered WASM locals, collect_compound_slots allocated frame slots, and
body_has_dynamic_array_index decided the bounds-check scratch. They were
kept in sync only by convention, so a future block-bearing statement kind
(or a changed traversal order) could be handled by one pass and silently
missed by another, corrupting the frame layout.

Introduce `nested_blocks`, the single classifier that names which
statement kinds carry sub-blocks and how their allocations combine
(Sequential vs the mutually-exclusive Alternatives of an `if`). A thin
pure-enumeration walker `walk_statements` is built on it and drives the
two monotonic passes; collect_compound_slots consults the classifier
directly because its if-arm frame-slot overlay is a genuine per-branch
decision that a flat enumeration cannot express. All three passes now
descend through one source of truth, so a new block kind is taught once
and cannot desynchronize them.

Purely internal: emitted WASM is byte-identical. The full codegen golden
suite passes unmodified, and before/after binary diffs on nesting-heavy
fixtures (arrays in a bare block, both if-arms of differing sizes, and a
loop body) are identical. Adds unit tripwires pinning the walker's
visitation order and the classifier's per-kind result.

Fixes #167
…_name

The WASM code generator held the name of the function being compiled as a
mutable `Compiler::current_fn_name` field, set at the top of
`visit_function_definition` and read implicitly far away. Its only reader is
the sret-return invariant panic in `lower_sret_return`.

Remove the field and thread the value explicitly as a `fn_name: &str`
parameter from `visit_function_definition` through the statement-lowering
walker (`lower_statement`, `lower_block`, `lower_if_statement`,
`lower_loop_statement`) down to `lower_sret_return`. This forecloses a class
of stale-read hazards that would surface once method, incremental, or
parallel function compilation is added.

Pure refactor: emitted WASM is byte-identical. The full codegen golden suite
passes with zero expectation changes.

Fixes #172
Base automatically changed from 167-refactoring-shared-statement-walker to main July 19, 2026 07:46
@0xGeorgii 0xGeorgii self-assigned this Jul 19, 2026
@0xGeorgii 0xGeorgii added the codegen Bytecode emitting label Jul 19, 2026
Signed-off-by: Georgii Plotnikov <accembler@gmail.com>
@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
core/wasm-codegen/src/compiler.rs 92.85% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@0xGeorgii
0xGeorgii merged commit b6bbf7b into main Jul 19, 2026
7 checks passed
@0xGeorgii
0xGeorgii deleted the 172-refactoring-thread-fn-context branch July 19, 2026 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Bytecode emitting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

current_fn_name Mutable State on Compiler

1 participant