Issue #2906: The checker accepts a mut struct passed on while one of its fields is being walked; rustc then refuses the lowered code (E0502, NK1343)

Open and answer this issue on GitHub.

ready · opened 2026-11-28 · updated 2026-11-28 · 0 comments

The builder of “The bounds prover asks each question once per body (#2885)” found this. The checker accepts a program that the generated Rust does not compile.

Probe

struct W { goals: Vec[i64], n: i64 }
fn bump(mut w: W, g: i64) -> bool { w.n += g; return w.n > 3 }
fn scan(mut w: W) -> bool { for g in w.goals { if bump(w, g) { return true } } return false }
fn main() throws { let mut w = W { goals: [1, 2, 3], n: 0 }; println(f"{scan(w)}") }

nikaia run gives an internal error, NK1343. The lowered code is refused by rustc with E0502: w.goals is borrowed by the loop, and bump borrows w mutably inside the loop.

Expected

The checker refuses this with its own borrow error, which names the loop over w.goals and the mut hand-over of w inside it. It is not an internal error. The builder worked around it in bounding_could_pay by walking missed_goals by index.

To build

  1. Find where the checker’s borrow rules (mut parameter hand-over) skip a for over a field of the same binding. Refuse it there with a normal diagnostic.
  2. Add a test in tests/compiler/tests/ with the probe and its .stderr.
  3. After this fix, walking a field and handing the whole struct on mut inside the loop is either refused or compiled correctly. It never reaches rustc unchecked.

Done when

Back to Open Issues