Closed
Description
The following code:
// exec-env:RUST_POISON_ON_FREE=1
fn free<T>(-x: @T) {}
fn lend(+x: @{f: @{g: int}}) -> int {
let y = &x.f.g;
free(x);
*y
}
fn main() {
assert lend(@{f: @{g: 22}}) == 22;
}
is handled incorrectly by borrowck right now. It will opt not to root the x.f
box even though it ought to. This is because it sees that x.f
is found in immutable data which will outlive the lifetime of the borrow: but it doesn't consider moves. To be correct, it should either (1) know which variables might be moved (liveness can tell us) and conservatively root boxes that are found in those variables or (2) take out a loan on x
so that an error would be reported. But (2) seems suboptimal since this is not really an error, just a failure of borrowck to make the right call on whether to root x.f
.