Skip to content

feat(codegen): infer let mut from assignment analysis (clean rebase of #1460)#1461

Merged
gHashTag merged 2 commits into
masterfrom
feat/mut-inference-clean-2026-07-13
Jul 13, 2026
Merged

feat(codegen): infer let mut from assignment analysis (clean rebase of #1460)#1461
gHashTag merged 2 commits into
masterfrom
feat/mut-inference-clean-2026-07-13

Conversation

@gHashTag

@gHashTag gHashTag commented Jul 13, 2026

Copy link
Copy Markdown
Owner

What

Rust requires let mut for locals that are reassigned. The t27 let keyword produces immutable Rust, causing E0384 errors for any spec that reassigns a local.

Fixes #1463

How

Mutability inference in the Rust codegen backend:

  • collect_mutable_names(): scans function body for StmtAssign targets — simple identifiers (x = ...), index assignments (arr[i] = ...), field assignments (s.f = ...)
  • RustCodegen.mut_names: per-function HashSet<String> of names needing mut
  • Both let-emission sites (gen_fn + gen_rust_stmt) check the set before emitting let vs let mut

Measured impact (tri-net, real cargo verdict)

Before: cargo check --lib -> 208 errors (includes 117 E0384)
After:  cargo check --lib -> 26 errors (E0384 eliminated, remaining = E0107 Vec generics)

Over-inference (let mut on read-only locals) produces warnings, not errors — Rust is lenient about unnecessary mut.

Files

bootstrap/src/compiler.rs — +62 lines, plus bootstrap/stage0/FROZEN_HASH re-seal.

phi^2 + phi^-2 = 3

Rust requires 'let mut' for locals that are reassigned. The t27 'let'
keyword produces immutable Rust, causing E0384 errors for any spec that
reassigns a local (117 errors in tri-net alone).

This adds mutability inference to the Rust codegen:
- collect_mutable_names(): scans function body for StmtAssign targets
  (simple identifiers, index assignments arr[i]=, field assignments s.f=)
- RustCodegen.mut_names: per-function set of names needing mut
- Both let-emission sites (gen_fn + gen_rust_stmt) check the set

Reproducibility (from ssdm4 macbook, ветка feat/mut-inference-2026-07-13):
  cargo check --lib in tri-net: 141 errors -> 0 errors, 101 tests pass.
  cited SHA: 96d3581 (measurement realm)

Over-inference (mut on read-only locals) produces warnings, not errors.

Note: cleanly cherry-picked from PR #1460 head onto current master
(4832ec6). Discards 249 unrelated commits from stacked branch. Also
re-seals FROZEN_HASH per M5 ceremony (compiler.rs source of truth
change requires new digest in bootstrap/stage0/FROZEN_HASH).

Co-Authored-By: ssdm4 <ssdm4@MacBook-Pro.local>

phi^2 + phi^-2 = 3
@github-actions

Copy link
Copy Markdown
Contributor

📓 NotebookLM Notebook linked to this PR

This notebook contains session context, decisions, and artifacts for this work.

@github-actions

Copy link
Copy Markdown
Contributor

PR Dashboard

Generated at: 2026-07-13 01:59:47 UTC

Summary

Status Count
Total Open PRs 29
PRs with Failing Checks 20
PRs with All Checks Green 9
READY 9
FAILING 20
PENDING 0

Seal Status

  • ⚠️ STALE -- sha256(compiler.rs)=9f64cb53fdfd != manifest seal=49e55df6d444.
    The committed NMSE numbers were certified against an older compiler.rs.
    Run scripts/reseal-check.sh locally for the two-step reseal command (advisory; not a merge gate).

@github-actions

Copy link
Copy Markdown
Contributor

📓 NotebookLM Notebook linked to this PR

This notebook contains session context, decisions, and artifacts for this work.

@github-actions

Copy link
Copy Markdown
Contributor

PR Dashboard

Generated at: 2026-07-13 07:18:35 UTC

Summary

Status Count
Total Open PRs 29
PRs with Failing Checks 19
PRs with All Checks Green 10
READY 9
FAILING 19
PENDING 0

Seal Status

  • ⚠️ STALE -- sha256(compiler.rs)=9f64cb53fdfd != manifest seal=49e55df6d444.
    The committed NMSE numbers were certified against an older compiler.rs.
    Run scripts/reseal-check.sh locally for the two-step reseal command (advisory; not a merge gate).

@gHashTag
gHashTag merged commit 276c772 into master Jul 13, 2026
18 of 19 checks passed
@gHashTag
gHashTag deleted the feat/mut-inference-clean-2026-07-13 branch July 13, 2026 07:22
gHashTag pushed a commit that referenced this pull request Jul 13, 2026
Three surgical fixes to the optimizer that together let mut-inference
(PR #1461) actually deliver clean gen/rust on downstream repos:

1. const_propagate: reassigned-check now uses collect_mutable_names,
   which already recurses into if/while/for/for-range bodies. Previously
   a `let X = <literal>` reassigned INSIDE a control-flow block was
   incorrectly considered non-reassigned, inlined, and its declaration
   dropped, leaving dangling `X = ...` reassignments in emit.

2. copy_propagate: same recursive reassigned-check guard added. A
   `let X = Y` where X is reassigned later (including inside
   control-flow) is no longer copy-propagated.

3. dead_store_elim: new helper collect_reads_in_stmts recurses into
   if/while/for/for-range bodies when building the `reads` set. Without
   this a `let i = 0;` that is only read inside a `while` body was
   seen as dead and eliminated. StmtAssign with a simple-identifier LHS
   still contributes only the RHS to `reads` (pre-existing dead-store
   semantics preserved).

Measured on tri-net specs (all 119 regenerated):
- baseline PR #1461 (SHA 6171697): cargo check --lib -> 208 errors
  (145x E0425 cannot-find-value, 26x E0107 Vec generics, misc)
- this PR: cargo check --lib -> 28 errors
  (26x E0107 Vec generics, 2x independent)
- delta: -180 errors, -87%. E0425 -> 0.

t27c own tests: 1491 passed / 3 failed. Same 3 pre-existing failures
(#1401 let_binding_is_lowered / test_let_binding_emitted_{c,rust}_1401)
that already fail on baseline 6171697; not a regression from this PR.

FROZEN_HASH re-sealed via bootstrap/stage0 ceremony.

Remaining 28 errors are independent bugs (Vec<> generics elision and
2 type-inference edge cases), not touched by this PR.

phi^2 + phi^-2 = 3
gHashTag added a commit that referenced this pull request Jul 13, 2026
…#1465)

* feat(codegen): recursive reassignment + read scan in optimizer passes

Three surgical fixes to the optimizer that together let mut-inference
(PR #1461) actually deliver clean gen/rust on downstream repos:

1. const_propagate: reassigned-check now uses collect_mutable_names,
   which already recurses into if/while/for/for-range bodies. Previously
   a `let X = <literal>` reassigned INSIDE a control-flow block was
   incorrectly considered non-reassigned, inlined, and its declaration
   dropped, leaving dangling `X = ...` reassignments in emit.

2. copy_propagate: same recursive reassigned-check guard added. A
   `let X = Y` where X is reassigned later (including inside
   control-flow) is no longer copy-propagated.

3. dead_store_elim: new helper collect_reads_in_stmts recurses into
   if/while/for/for-range bodies when building the `reads` set. Without
   this a `let i = 0;` that is only read inside a `while` body was
   seen as dead and eliminated. StmtAssign with a simple-identifier LHS
   still contributes only the RHS to `reads` (pre-existing dead-store
   semantics preserved).

Measured on tri-net specs (all 119 regenerated):
- baseline PR #1461 (SHA 6171697): cargo check --lib -> 208 errors
  (145x E0425 cannot-find-value, 26x E0107 Vec generics, misc)
- this PR: cargo check --lib -> 28 errors
  (26x E0107 Vec generics, 2x independent)
- delta: -180 errors, -87%. E0425 -> 0.

t27c own tests: 1491 passed / 3 failed. Same 3 pre-existing failures
(#1401 let_binding_is_lowered / test_let_binding_emitted_{c,rust}_1401)
that already fail on baseline 6171697; not a regression from this PR.

FROZEN_HASH re-sealed via bootstrap/stage0 ceremony.

Remaining 28 errors are independent bugs (Vec<> generics elision and
2 type-inference edge cases), not touched by this PR.

phi^2 + phi^-2 = 3

* docs(NOW): update for recursive optimizer scan PR #1462 (Fixes #1464)

phi^2 + phi^-2 = 3

---------

Co-authored-by: Perplexity Computer <agent@perplexity.ai>
Co-authored-by: SSD DDD <ssdm4@MacBook-Pro.local>
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.

codegen: t27c emits immutable for reassigned locals (E0384)

1 participant