Skip to content

check: a borrow in an if or while condition ends before the body - #4409

Merged
hiraditya merged 5 commits into
vx-lang:mainfrom
KrispWaffle:fix/if-condition-borrow-4401
Dec 1, 2026
Merged

hiraditya merged 5 commits into
vx-lang:mainfrom
KrispWaffle:fix/if-condition-borrow-4401

Conversation

@KrispWaffle

@KrispWaffle KrispWaffle commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4401.

if *v.get_ref(0) > 0 { v.push(6) } got E4003, and the same while loop did too. The condition is a bool, so it holds no borrow of v when the body runs. Rust accepts the program. The checker kept the borrows of the condition while it checked the branches.

What changed

After the condition of an if, the checker now ends each borrow that the condition made, unless a variable in scope holds it. The parser turns a while into a loop with an if, so one fix covers both.

A call can store a reference through a &mut argument, as keep(&mut h, v.get_ref(0)) does. The checker gives that borrow to h, so the borrow does not end with the condition. Before, the checker missed three cases:

  • A call in the condition of an if or a while.
  • A call under an operator, as in a && keep(..) or !keep(..).
  • A call where h is a &mut parameter. The caller can read h after the function returns, so the borrow lasts to the end of the function.

v.get_ref(0) on a &mut parameter v makes no borrow of its own. So h now also borrows each &mut variable that the other argument reads. h also keeps the borrows of a reference that the call passes on, as in let r = v.get_ref(0); keep(h, r);.

An argument whose type holds no reference, such as a number or a struct of numbers, gives h no borrow. Without this rule, if is_above(&mut h, v.len()) { v.push(6) } would not compile after this change.

What it can break

The checker now refuses some programs that main accepts. One example is keep(h, v.get_ref(0)); v.push(6); where h and v are &mut parameters. That program can read freed memory. No .vx file in the repository gets a new error.

Not in this PR

main has the last two gaps too.

Tests

  • tests/backend/pass/a_borrow_in_an_if_condition_ends_before_the_body.vx: programs that must compile and run, with both code generators. It fails on the first commit of this PR.
  • tests/frontend/fail/a_reference_kept_across_an_if_keeps_the_vec_borrowed.vx: a reference that a variable or a running task holds across the if still gets E4003.
  • tests/frontend/fail/a_reference_that_a_call_stores_keeps_the_vec_borrowed.vx: the cases above where a call stores a reference.

I removed each rule of the change in turn, in 16 builds. Each time, a check in these files failed at the case for that rule.

I type-checked all 3034 .vx files in the repository with the compiler from main and with the compiler from this branch. The errors and warnings are the same. One macOS-only file gives a message that varies from run to run with both compilers.

…hanges it

The condition of an `if` or a `while` can borrow a Vec to read one of its
numbers, as in `if *v.get_ref(0) > 0 { v.push(6) }`. The condition is a
bool, so the borrow ends before the body runs, and Rust accepts this
program. The checker keeps the borrow, so the push in the body gets E4003.

a_borrow_in_an_if_condition_ends_before_the_body.vx fails on this commit.
Each of its functions gets E4003. The next commit fixes the checker.

a_reference_kept_across_an_if_keeps_the_vec_borrowed.vx passes on this
commit and must keep passing. A reference that a variable keeps across the
`if` still keeps the Vec borrowed.

Refs: vx-lang#4401
The checker kept the borrows of an `if` condition while it checked the
branches. So `if *v.get_ref(0) > 0 { v.push(6) }` got E4003, even though
the condition is a bool that holds no borrow of `v`. A `while` got the same
error, because the parser makes `while c { .. }` into a loop with an `if`.

After the condition, the checker now ends each borrow that the condition
made and that no variable in scope holds. A borrow that a variable holds
stays, as in `if (if c { r = &v; true } else { false }) { .. }`.

It does not put back the borrow table from before the condition, as the end
of a statement does. A borrow that ended in the condition, because its
variable is not used again, must stay ended. Otherwise
`let m = &mut s; if s.x == 2 { let m = &mut s; .. }` gets E4004.

The variable of `let x = if c { .. }` does not hold the borrows of `c`,
only those of the value of the `if`.

Fixes: vx-lang#4401
…ition

The fix for the `if` condition ended each borrow of the condition that no
variable held. A call can store its reference argument through a `&mut`
argument, as `keep(&mut h, v.get_ref(0))` does. No variable held that
borrow by name when the call was in a condition. So the borrow ended, and
the body could change `v` while `h` pointed into it.

The checker already gives such a borrow to `h` when the call is a
statement. It now does the same in three more places:

- In the condition of an `if` or a `while`, before the borrows of the
  condition end.
- Under an operator, as in `a && keep(..)`, `!keep(..)`, `keep(..) == b`
  and `(keep(..) as i32) + 1`. Before, the check did not look at the
  operands of an operator at all.
- When `h` is a `&mut` parameter. The caller can read `h` after the
  function returns, so the borrow lasts to the end of the function.

A call such as `v.get_ref(0)` on a `&mut` parameter `v` makes no borrow
record, because `v` already is a reference. So `h` now also borrows each
`&mut` variable that the other argument reads.

An argument that is a number or a bool carries no reference, so it gives
`h` no borrow. Without this, `if is_above(&mut h, v.len()) { v.push(6) }`
would not compile after this change.

Refs: vx-lang#4401
… borrowed

After `let r = v.get_ref(0); keep(h, r);` the call gives `h` the borrows
that `r` holds. When `h` is a `&mut` parameter, the caller can read `h`
after the function returns, so those borrows must last to the end of the
function. They ended at the last use of `h` in the function. So a later
`v.push(6)` compiled, and the caller then read freed memory through `h`.

The rule that keeps a borrow to the end of the function started only when
the call made a new borrow record. It now also starts when the call copies
a borrow from a variable that it passes on.

Refs: vx-lang#4401

@hiraditya hiraditya left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, the change is well tested. I found one program that main accepts and this branch refuses. A one-line change fixes it.

Must fix: a value with no reference in it makes a borrow

import std::vec;

struct Pair {
  a : i64,
  b : i64,
}

struct Holder {
  r : &i64,
  p : Pair,
}

fn set_pair(h : &mut Holder, p : Pair) -> bool {
  h.p = p;
  return true;
}

fn copy_through_a_parameter(h : &mut Holder, v : &mut Vec<i64>) -> void {
  set_pair(h, Pair {
    a : *v.get_ref(0),
    b : 0i64
  });
  v.push(1i64);
}

main compiles this. This branch reports E4003 at v.push(1i64). The argument reads the &mut variable v, so h now borrows v. But a Pair holds no reference, so h cannot get one from this call. A local Holder gets the same error.

The skip for "a number or a bool" is too narrow. In hold_borrows_a_call_may_store, I changed it to

if !self.type_can_hold_reference(&self.check_expr_type_probe(other)) {
    continue;
}

Then the program above compiles, as on main, and the three new tests still pass. Please add the program to the pass test.

What I checked

I merged the branch into current main.

  • The three new tests pass.
  • A condition can store a reference in a variable or a field that lives after the if. I tried three such conditions: r = v.get_ref(0), h.r = v.get_ref(0), and let t = v.get_ref(0); r = t;. Each still gets E4003 at the push in the body.

CI ran only the CLA check here. A maintainer must approve the workflow run for the other checks.

Generated With Claude Code

The call rule skipped an argument only when it was a number or a bool. So
`set_pair(h, Pair { a : *v.get_ref(0), b : 0 })` made `h` borrow `v`, and
a later `v.push(1)` got E4003. A `Pair` of two numbers holds no reference,
so the call cannot store one in `h`. `main` accepts this program.

The rule now skips each argument whose type cannot hold a reference.

Refs: vx-lang#4401
@KrispWaffle

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I applied your change in aeda094.

  • hold_borrows_a_call_may_store now skips each argument whose type cannot hold a reference.
  • Your program is in the pass test, as copy_through_a_parameter. Before the change, it got E4003 at the push and at no other line.
  • The three new tests pass. Clippy, the library tests and the frontend test groups pass too.

I also ran your program on main (114d88c), and it compiles there, as you wrote.

@hiraditya
hiraditya self-requested a review December 1, 2026 06:02

@hiraditya hiraditya left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. I checked aeda094, merged into current main.

  • The three tests pass.
  • I put back the old skip for Type::Scalar only. The new case in the pass test then fails with E4003 at v.push(1i64). So the test guards the change.
  • My program with a local Holder also compiles now.
  • An argument that holds a reference still gives h a borrow. set_wrap(h, Wrap { r : v.get_ref(0) }) and set_option(h, Option<&i64>::Some(v.get_ref(0))) both get E4003 at the next v.push. main accepts both.
  • The three conditions from my last review still get E4003.
  • I type-checked all 3040 .vx files on main with the compiler from main and with this branch. The errors and warnings are the same.

CI passed.

Generated With Claude Code

@hiraditya
hiraditya merged commit 77a9b49 into vx-lang:main Dec 1, 2026
6 checks passed
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 borrow in an if or while condition lasts into the body, so if *v.get_ref(0) > 0 { v.push(6) } gets E4003

2 participants