Skip to content

check: keep the borrows that reach each loop break - #4474

Open
KrispWaffle wants to merge 11 commits into
vx-lang:mainfrom
KrispWaffle:fix/issue-4455
Open

KrispWaffle wants to merge 11 commits into
vx-lang:mainfrom
KrispWaffle:fix/issue-4455

Conversation

@KrispWaffle

@KrispWaffle KrispWaffle commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4455

Background

#4451 landed note_borrows_at_break and fixed #4448 (zero-iteration for loops), so main already refuses the basic example in #4455. This PR replaces note_borrows_at_break with scoped loop-exit borrow tracking (LoopExits::borrows and LoopExits::borrows_after) and fixes the remaining soundness and precision bugs around loops, if, and match.

What This Fixes vs Current main

  • Missed borrow conflicts now reported:
    • A borrow carried across a continue to a break on the next pass (break_on_the_next_pass, and references returned by a call across continue).
    • A for loop (over a range or a Vec) that hits continue before reassigning a reference (for_continue_before_reassignment, for_over_vec_continue_before_reassignment).
    • Assigning to an inner let mut r inside a loop while an outer r holds a borrow (only_the_inner_r_gets_a_new_value).
    • A non-exhaustive numeric match whose arms reassign and exit (number_matches_neither_arm, both outside and inside a loop).
  • False borrow errors removed:
    • borrow_ends_before_the_next_break, unreachable_break, while_true_replaces_the_reference, a break in if false, a break in the else of if true, and a break after return.
  • Prover, known values, and pointer facts:
    • if and match joins now use no_path_reaches_the_end instead of branch_ends_early, which also selects known values (consteval.env), pointer facts, and prover facts (consteval.constraints) after branches that end via while true or nested if/match breaks.

Out of Scope

  • Copying a carried reference into a second holder on a later loop pass.
  • Replacing loop borrow seeding (carried_borrow_seeds, carried_from, seeds_missing_at_every_break, and dead carried seeds in live_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 -- --check and vx-format on all modified .vx files (passed)

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

@minzyfox minzyfox left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, and main already 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 current main.
  • "Out of Scope" says that zero-iteration for loops (#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 main misses and this PR reports: break_on_the_next_pass, for_continue_before_reassignment, only_the_inner_r_gets_a_new_value and number_matches_neither_arm. My extra programs found two more. In the first, a call returns a reference, and a continue carries it to the next pass. In the second, a numeric match in a loop gets a number that matches no arm.
  • False errors on main that this PR removes: borrow_ends_before_the_next_break, unreachable_break and while_true_replaces_the_reference. My extra programs found two more: a break in the else of if true, and a break after a return.
  • 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_borrows removes every dead record. This PR keeps dead records carried from the previous pass. See my comment on live_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 break or a continue.

Missing tests.

  • A prover fact after an if whose branch ends only inside a nested if or match. See my comment on then_ends in control.rs.
  • A break in an if false branch, and a break in the else of if true, outside the while true shape.
  • A for loop over a Vec, with a continue.

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_from field on a borrow record
  • forget_carried and seeds_missing_at_every_break
  • the dead seeds that live_borrows keeps

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.

Comment thread src/hir/borrow_cx.rs Resolved
Comment thread src/hir/borrow_cx.rs Resolved
Comment thread src/hir/borrow_cx.rs Resolved
Comment thread src/hir/borrow_cx.rs Resolved
Comment thread src/hir/borrow_cx.rs Resolved
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
@KrispWaffle

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review! I addressed the review comments in 53dfe89 and updated the PR description.

This branch has not been deployed

No deployments
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.

check: a break before a reassignment in a loop loses the reference's borrow A reference that a for loop body sets escapes the borrow check

2 participants