Repository navigation
check: keep the borrows that reach each loop break - #4474
KrispWaffle wants to merge 11 commits into
Conversation
A reference can point to a different value at each break. Check that changing any of those values gives an error while the reference is still in use. Cover nested loops, a name hidden inside the loop, and mutations after the last use. The checker currently reports only the borrow at the final break. Refs: vx-lang#4455
Save the borrows held when a path breaks out of a loop. A later assignment on another path can then replace its reference without erasing what the earlier break kept. Keep hidden owners with their bindings when branches join. For a break on a later iteration, use the references an earlier iteration actually kept. Ignore exits after a path has already ended. Fixes: vx-lang#4455
Remove unnecessary comments from the loop-exit borrow change and replace unclear types and helpers with explicit names. Name the shadow-stack slot in BorrowOwner, replace the optional depth parameter in scoped_borrows with BorrowScopeView, and extract holders_carrying_to_break from borrows_after. Rename match_can_miss and holds_a_jump to state what they check. Refs: vx-lang#4455
Record a copied or stored borrow at the depth of its holder. Replacing the holder ends its old borrow. Assigning to an inner binding with the same name preserves the outer binding's borrow. Remember which owner and holder each loop predicted. A later break can keep a reference copied from another variable or stored through a call. Keep those borrows when the loop made no prediction for that pair. An owner read can skip an inactive prediction for that read. The holder can still carry its previous value to a break. The branch changes if and match joins from branch_ends_early to no_path_reaches_the_end, inside and outside loops. This selects the known values, pointer facts, prover facts, and borrows that can reach the next statement. Test a numeric match outside a loop where an unlisted number skips both arms that replace the reference and return. Refs: vx-lang#4455
Remove non-essential comments added across the borrow checker and its new loop-exit tests. Rename the live-prefix locals in live_borrows so the returned slice is clear without a comment. Refs: vx-lang#4455
# Conflicts: # src/hir/check/escape.rs # src/hir/stmt.rs
# Conflicts: # src/hir/borrow_cx.rs
# Conflicts: # src/hir/borrow_cx.rs
# Conflicts: # src/hir/check/control.rs
There was a problem hiding this comment.
Thank you for this PR. I built it and ran the fixtures, and I also ran about 20 extra programs on latest main (dcc1f37). I also ran them on this PR merged into latest main (dcc1f37), because CI tests that merge. I found no case where the PR accepts a program that it must refuse. The inline comments explain the details. These are the general points.
The description is out of date.
- #4451 merged on Dec 2. It added
note_borrows_at_break, andmainalready refuses the program in #4455 with E4003. This PR replaces that code, but the description does not say so. Please describe what the PR changes compared with currentmain. - "Out of Scope" says that zero-iteration
forloops (#4448) stay open. #4451 fixed #4448, and GitHub closed that issue. - The second gap ("copying a carried reference into a second holder on a later pass") has no issue. Please file one and link it.
What the PR fixes on latest main (dcc1f37). I compared the two compilers on each case.
- Errors that
mainmisses and this PR reports:break_on_the_next_pass,for_continue_before_reassignment,only_the_inner_r_gets_a_new_valueandnumber_matches_neither_arm. My extra programs found two more. In the first, a call returns a reference, and acontinuecarries it to the next pass. In the second, a numericmatchin a loop gets a number that matches no arm. - False errors on
mainthat this PR removes:borrow_ends_before_the_next_break,unreachable_breakandwhile_true_replaces_the_reference. My extra programs found two more: abreakin theelseofif true, and abreakafter areturn. - Please say this in the description. Then a reader knows which checks show the fix.
Docs. Please update docs/discussions/borrow_checker_architecture.md, section "When a borrow ends".
- It says that
live_borrowsremoves every dead record. This PR keeps dead records carried from the previous pass. See my comment onlive_borrows. - It says that a new value for a reference does not end its old borrow. That was already wrong on
main, and this PR changes the rule again. - It says nothing about the borrows at a
breakor acontinue.
Missing tests.
- A prover fact after an
ifwhose branch ends only inside a nestediformatch. See my comment onthen_endsincontrol.rs. - A
breakin anif falsebranch, and abreakin theelseofif true, outside thewhile trueshape. - A
forloop over aVec, with acontinue.
Before you rely on a new check, break the code that it covers. Then make sure that the check fails, and read which assertion reported the failure (see the Testing section in AGENTS.md).
Design. The loop code now has several parts that each cover a gap in another part:
- the seeds from
carried_borrow_seeds - the
carried_fromfield on a borrow record forget_carriedandseeds_missing_at_every_break- the dead seeds that
live_borrowskeeps
A fixed point would replace all of them: check the body again until the borrows at its start stop changing. I do not ask for that in this PR. But please file an issue, or say why a fixed point does not work here.
Compile time. I tried 200, 400 and 800 nested ifs in a loop, each with a break and 16 references. The type check took the same time with this PR (0.16 s, 0.33 s, 0.98 s) as on latest main (dcc1f37) (0.16 s, 0.33 s, 0.99 s). I do not think a benchmark is necessary.
Assert that shadowed borrow snapshots match the expected owner name in BorrowCx::records_of. Use bool_literal for while true in no_path_reaches_the_end, and compute match exhaustiveness once during match type checking. Restore and add doc comments for LoopExits, borrows_after, live_borrows, carried_borrow_seeds, exits_before_dead_code, and settle_loop_exits. Update docs/discussions/borrow_checker_architecture.md to describe dead carried seeds, reassignment, and loop exits. Add tests for prover facts after a while true branch, known values after nested if and match breaks, breaks inside if false and else of if true, and for over a Vec with continue. Add header and inline comments to the new borrow test fixtures. Refs: vx-lang#4455
|
Thanks for the thorough review! I addressed the review comments in 53dfe89 and updated the PR description. |
Fixes #4455
Background
#4451 landed
note_borrows_at_breakand fixed #4448 (zero-iterationforloops), somainalready refuses the basic example in #4455. This PR replacesnote_borrows_at_breakwith scoped loop-exit borrow tracking (LoopExits::borrowsandLoopExits::borrows_after) and fixes the remaining soundness and precision bugs around loops,if, andmatch.What This Fixes vs Current
maincontinueto abreakon the next pass (break_on_the_next_pass, and references returned by a call acrosscontinue).forloop (over a range or aVec) that hitscontinuebefore reassigning a reference (for_continue_before_reassignment,for_over_vec_continue_before_reassignment).let mut rinside a loop while an outerrholds a borrow (only_the_inner_r_gets_a_new_value).matchwhose arms reassign and exit (number_matches_neither_arm, both outside and inside a loop).borrow_ends_before_the_next_break,unreachable_break,while_true_replaces_the_reference, abreakinif false, abreakin theelseofif true, and abreakafterreturn.ifandmatchjoins now useno_path_reaches_the_endinstead ofbranch_ends_early, which also selects known values (consteval.env), pointer facts, and prover facts (consteval.constraints) after branches that end viawhile trueor nestedif/matchbreaks.Out of Scope
carried_borrow_seeds,carried_from,seeds_missing_at_every_break, and dead carried seeds inlive_borrows) with a fixed-point loop borrow pass.Verification
cargo test --locked --lib -- --test-threads=6(78 + 1,195 passed, 2 ignored)cargo test --locked --test integration_test integration_test::compile_test::test_frontend_ -- --test-threads=1(7 passed)cargo clippy --locked --workspace --all-targets --all-features -- -D warnings(passed with only pre-existing manifest dependency warnings)cargo fmt --all -- --checkandvx-formaton all modified.vxfiles (passed)