-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[nll] optimize + fix check_if_reassignment_to_immutable_state
#53189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think what we should do:
|
As a bonus, I think we could remove rust/src/librustc_mir/borrow_check/nll/invalidation.rs Lines 358 to 372 in 52c785b
and both should now be just |
@spastorino would like to take this on. |
At some point I thought we had considered allowing a structs fields to be initialized independently .... don’t remember how far that went at the moment .... |
We were considering it, but we don't presently allow it. Even with NLL this does not compile, for example: #![feature(nll)]
fn main() {
let x: (u32, u32);
x.0 = 1;
x.1 = 2;
println!("{:?}", x);
} |
It's true though that the existing borrow checker is sort of inconsistent here -- if you do |
I was suggesting that we match the existing behavior because it's simple to do — I am not opposed to being smarter later on though. I guess to do this the "right" way we would do something like:
Sound about right @pnkfelix ? |
Following a similar table format from #53114:
|
@memoryruins I guess that the In any case, I can go either way here. I'd be fine with matching the AST checker, because it's easy to do, and then trying to revisit this whole question more carefully (in particular, I would want later references to But it's also not that hard to make the current system more efficient, and I think it is more internally consistent than what the AST checker does. |
Correct, the 4 | let mut x: (u32, u32);
| ----^
| |
| help: remove this `mut` |
…-immutable-state, r=pnkfelix optimize reassignment immutable state This is the "simple fix" when it comes to checking for reassignment. We just shoot for compatibility with the AST-based checker. Makes no attempt to solve #21232. I opted for this simpler fix because I didn't want to think about complications [like the ones described here](#21232 (comment)). Let's do some profiling measurements. Fixes #53189 r? @pnkfelix
check_if_reassignment_to_immutable_state
has the of handling assignments to immutable local variables. These are a bit tricky: you are allowed to assign to an immutable local variable, but only if it has never been assigned to before. Actually, the logic implemented in the NLL borrow checker is kind of wrong (or at least inconsistent with the AST checker). It's also just slow.Here is an example of the sort of assignments that should be accepted (playground):
This example is rightly rejected (but we should check to ensure we have a test) (playground):
This example should be rejected but currently is not (playground):
In addition to being slightly wrong, the current implementation is also slow. It iterates over all initialized paths and checks that the path being assigned to (e.g.,
x.0
in the third example) is disjoint from all of them:rust/src/librustc_mir/borrow_check/mod.rs
Lines 1612 to 1619 in 52c785b
I will write below how I think it should work.
The text was updated successfully, but these errors were encountered: