Repository navigation
Fix #1317: a for binding borrows the iterated place for the whole loop - #1333
Conversation
7728099 to
1e35df8
Compare
|
Battery (seed-driven, rebased on ae609cd): build ok, :fixpoint ok, :move-audit ok, :test red. Reds: The loop-lifetime borrow does not hold. |
|
Fixed at a49c865 — and the root cause is on main, not in this PR's rule: Fix: save the caller's borrow table (8 columns) and loop-view bindings at body entry, check the body on empty tables, restore after pop_scope. New fixtures: err_for_binding_push_body_tail, err_for_binding_mutate_then_statement, err_view_live_across_generic_instantiation. Check sweep (1132 behavior, 860 compile_errors, examples, 43 UAT fixtures) fixed-vs-unfixed: only those fixtures change. Re-battery queued. |
… `for e in xs: xs.push(..)` is rejected as the live-view mutation it is (#1317) Fixes #1317. `for e in xs` is `xs.iter()` (§13, D44) and `e` is a view into `xs` (§3.4), but check_for registered no borrow for the binding, so `xs.push(..)`, `xs.clear()`, `xs.remove(0)` and `m.insert(..)` inside the body compiled and the next iteration read the reallocated or shifted buffer through `e` — the same use-after-free §21.1 rule 1 already rejects for `let e = xs.get(0); xs.push(..); use e`. register_for_binding_borrow records a SHARED borrow keyed on the field path of the iterated place (for_iterated_place: the place itself, or the receiver of a spelled `@[iter_of_self]` call such as `xs.iter()` / `m.keys()`), with the binding as its ref, through the same check_borrow_create_direct / check_mutation_against_views mechanism the let-bound views use. Keying on the path, not the root, keeps `for e in self.items: self.count += 1` accepted (disjoint paths) while `self.items.push(..)` conflicts. A pattern binding (`for (k, v) in m`) registers the same borrow for each of its ephemeral bindings. The borrow never expires at a lexical last use of the binding: the loop reads the place again on its next iteration, so check_mutation_against_views keeps a borrow whose ref is in for_view_binding_syms (a stack check_for maintains) and adds a note saying so. It expires with the loop's scope. record_pattern_view_bindings now walks pattern_binding_syms, the one pattern-binding collector both paths use, and for_iterable_yields_views' method tail is factored into iter_of_self_call_receiver. Rejected (new fixtures): push, clear, remove on the iterated Vec, push on `self.items`, insert on the iterated map. Accepted (behav_for_binding_disjoint_mutation): `self.count += e.n`, pushing the element's clone onto another Vec, `for e in xs.iter()`, mutating `xs` after the loop. `with check src/main.w`, tools/*.w and examples/ compile under the rule; a scan of src/, lib/, rt/, build/, tools/, examples/ and test/ for a `for` whose body mutates its own iterable found no site. A Copy-element loop (`for i in idxs: idxs.push(..)`) binds no view and is not covered here.
…inding's view; index loops stay accepted (#1317) The loop-binding borrow never expires at a lexical last use, because the next iteration reads the iterated place again. When the mutation's next statement is `return`, or an unlabeled `break` of the very `for` that binds the view (loop_depth equals the body's depth recorded in for_view_binding_depths), no iteration follows: find-then-grow and find-then-clear are sound and now check, as Rust's NLL accepts them. The borrow is kept, not removed, since other paths through the body may still loop. `continue`, a labeled break, and a break of an inner loop still conflict (new fixtures). behav_for_index_loop_mutates_collection pins what migrated C does: a range over `xs.len()` pushing onto `xs`, a bounded `while` over an index, and reading `xs[j]` in the iteration that pushes (as the argument, through a Copy-annotated binding, and through a view dead before the push). The binding is an i32, not a view, so the rule does not reach them. An unannotated `let v = zs[j]` used as the pushed argument is rejected by the existing D27 let-view rule (seed and stage1 alike), not by this change.
…nstead of wiping them — `print(e.key)` erased the `for` binding's borrow of `xs` (#1317) Root cause: check_fn_body_with_sig_at (src/SemaCheck.w) opened every body with `while self.borrow_kinds.len() > 0: self.remove_borrow_at(...)`, treating the borrow table as free at body entry. It is not: the first call of a generic function reaches it mid-body, through check_call -> check_generic_call -> check_fn_body_concrete -> check_fn_body_with_sig_at, while the caller's rows are live. check_fn_body_concrete saves the caller's scope-stack family but not the borrow columns, so the caller's borrows were discarded. Since #1325 made print generic (print[T: Display](v: &T)), the first `print(x)` of each T erased every live view of the calling body. Evidence (lldb, with-stage2 check err_for_binding_iterable_clear.w, function breakpoints): register_for_binding_borrow -> check_borrow_create_direct appended the row; the next remove_borrow_at hit came from check_fn_body_with_sig_at <- check_fn_body_concrete <- check_generic_call <- check_call (the body's `print(e.key)`); check_mutation_against_views then ran on an empty table, so for_view_binding_depth was never reached. The same wipe accepted `let e = xs.get(0); print(e.key); xs.push(..); print(e.key)`, which the installed v0.15.2.1 (before print was generic) rejects. Fix: set the caller's borrow columns and loop view bindings aside at body entry and restore them after the body's pop_scope; the body's own rows die with it. borrow_path_data is append-only, so saved path indices stay valid. Fixtures: err_for_binding_push_body_tail (push as the body tail, no later use of `e`), err_for_binding_mutate_then_statement (clear, then a non-exiting statement), err_view_live_across_generic_instantiation (a let view across the first call of a local generic fn). All three, and err_for_binding_iterable_clear, checked ok before this change.
a49c865 to
c0ff515
Compare
Stacked on #1327 — GitHub stack (base
fix-1297-field-view-origin; retarget tomainonce #1327 merges).Fixes #1317.
What changed
for e in xsregisters a SHARED borrow of the iterated place witheas its ref (§21.1 rule 1, §3.4, D22/D27/D44), through the samecheck_borrow_create_direct/check_mutation_against_viewspath aslet e = xs.get(0).xs.push/clear/remove(..),xs = ..,m.insert(..)in the body are rejected: "cannot mutatexswhileeis a live view into it", with a note that the loop reads the place again on its next iteration.self.items), not the root, sofor e in self.items: self.count += 1andfor e in xs: other.push(e.clone())stay accepted. Amut fncall onselfinsidefor e in self.itemsis still a conflict, matching the existinglet e = self.items.get(0); self.bump(); use erule.returnor an unlabeledbreakof that same loop. In that case no iteration follows, so find-then-grow and find-then-clear check (Rust's NLL accepts the same thing).continue, a labeled break, and an inner loop'sbreakstill conflict.for i in 0..xs.len(): xs.push(..), a boundedwhile i < xs.len(), and readingxs[j]in the iteration that pushes all bind an i32, not a view. A Copy-elementfor x in xsbinds no view either. Its MIR reads the length once and fetches each element by index from the current buffer, so there is nothing to dangle.Tests (stage1 from
with build :dev)New, all pass:
err_for_binding_iterable_{push,clear,remove},err_for_binding_self_field_push,err_for_pattern_binding_map_insert,err_for_binding_mutate_then_continue,err_for_binding_inner_loop_breakbehav_for_binding_disjoint_mutation,behav_for_binding_mutate_then_exit(alsoleak count=0underrun --debug-alloc),behav_for_index_loop_mutates_collectionExisting tests still pass:
err_view_of_local_*, everyerr_*ephemeral*,behav_for_*,behav_view_*,behav_typed_let_borrows_place. Everytest/debug_alloc/*.wchecks, and the map and consuming-iteration cells reportleak count=0underrun --debug-alloc.Sweep
with-stage1 check src/main.w: ok..wwith afor … inunder src, lib, rt, build, tools, examples and test (458 files) looked for a body that mutates or assigns its own iterable. It found only the new fixtures.forloops. None gets a Mutating the iterated collection insidefor x in xsis accepted (no live-view diagnostic for the loop binding) #1317 diagnostic. 21 fail with the same errors under the pinned seed: implicit-main tools undercheck, stale tools, andimplduplicates in stdlib modules that are checked alone.lib/std/{re,zl,c_algorithms,tommyds}/bundle.wall check (rc=0) under stage1. None of the four bundles and nothing undertest/corpora/has aforstatement, so the rule cannot reach migrated C. Thetest/corpora/c_algorithms/programs/*modules importstd.calg_testing.*, which only exists once the build assembles it, so they cannot be checked on their own.The battery (
:fixpoint/:test) has not run on this branch yet. The PR stays a draft until it passes.