-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
[NLL] "borrowed value must be valid for lifetime '_#2r..." #48428
Comments
The problem steps from this call to rust/src/librustc_mir/borrow_check/error_reporting.rs Lines 527 to 529 in 27a046e
There is another call here: rust/src/librustc_mir/borrow_check/error_reporting.rs Lines 505 to 507 in 27a046e
which I guess would be triggered by this test case: #![feature(nll)]
fn gimme(x: &(u32,)) -> &u32 { &x.0 }
fn main() {
let x = gimme({ let v = (22,); &v });
println!("{:?}", x);
} |
In NLL mode, there is already a helper for printing out these messages in NLL mode, it's called rust/src/librustc_mir/borrow_check/error_reporting.rs Lines 527 to 530 in 27a046e
I would say that we almost just want to remove
|
cc @rust-lang/wg-compiler-nll -- in general, I think we need someone to "take charge" of the "dump-cause" code, which is the code that tries to label the "third use" in NLL, indicating why a borrow lasts for a certain time. This code is currently only enabled when you pass |
I'd be happy to work closely with someone on this! I don't think it's particularly hard, will just take a bit of focus. |
@spastorino has indicated some interest here... |
So I think there are a lot of moving parts. Here is an attempt at making a list:
|
[NLL] Avoid borrowed value must be valid for lifetime '_#2r..." in errors Closes #48428 - [x] If NLL is enabled, [do not invoke `note_and_explain_region`](#48428 (comment)) - [x] Modify `-Zdump-nll-cause` to not print [the overwhelming debug output here](https://github.com/rust-lang/rust/blob/master/src/librustc_mir/borrow_check/nll/region_infer/mod.rs#L1288-L1299). This way we should I believe at least get nice-ish output for [our original example](#48428 (comment)). - [x] Extend `explain_why_borrow_contains_point` to also work for "universal lifetimes" like the `'a` in [the example at the end of this comment](#48428 (comment)). - [ ] Figure out how to enable causal information all the time (but that is #46590).
In NLL, there is one rule of diagnostics:
Unfortunately, we break it. Twice. Here is one example:
This prints the following:
Note the final line. In comparison, the AST-based version gets the following error, which is much better:
We probably want to integrate with the "dump-cause" mode and something like this:
The text was updated successfully, but these errors were encountered: