Repository navigation
check: a borrow in an if or while condition ends before the body - #4409
Conversation
…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
left a comment
There was a problem hiding this comment.
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), andlet t = v.get_ref(0); r = t;. Each still gets E4003 at thepushin 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
|
Thanks for the review. I applied your change in aeda094.
I also ran your program on |
hiraditya
left a comment
There was a problem hiding this comment.
Thanks. I checked aeda094, merged into current main.
- The three tests pass.
- I put back the old skip for
Type::Scalaronly. The new case in the pass test then fails with E4003 atv.push(1i64). So the test guards the change. - My program with a local
Holderalso compiles now. - An argument that holds a reference still gives
ha borrow.set_wrap(h, Wrap { r : v.get_ref(0) })andset_option(h, Option<&i64>::Some(v.get_ref(0)))both get E4003 at the nextv.push.mainaccepts both. - The three conditions from my last review still get E4003.
- I type-checked all 3040
.vxfiles onmainwith the compiler frommainand with this branch. The errors and warnings are the same.
CI passed.
Generated With Claude Code
Fixes #4401.
if *v.get_ref(0) > 0 { v.push(6) }got E4003, and the samewhileloop did too. The condition is abool, so it holds no borrow ofvwhen 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 awhileinto a loop with anif, so one fix covers both.A call can store a reference through a
&mutargument, askeep(&mut h, v.get_ref(0))does. The checker gives that borrow toh, so the borrow does not end with the condition. Before, the checker missed three cases:ifor awhile.a && keep(..)or!keep(..).his a&mutparameter. The caller can readhafter the function returns, so the borrow lasts to the end of the function.v.get_ref(0)on a&mutparametervmakes no borrow of its own. Sohnow also borrows each&mutvariable that the other argument reads.halso keeps the borrows of a reference that the call passes on, as inlet 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
hno 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
mainaccepts. One example iskeep(h, v.get_ref(0)); v.push(6);wherehandvare&mutparameters. That program can read freed memory. No.vxfile in the repository gets a new error.Not in this PR
match *v.get_ref(0) { .. }still gets E4003. The arms of amatchcan use a reference that the tested value holds, so the tested value keeps its borrows.let hr = &mut h; keep(hr, ..).mainhas 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 theifstill 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
.vxfiles in the repository with the compiler frommainand 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.