-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
On temporary value dropped while borrowed error use structured suggestion #61405
Comments
I want to attempt this issue. I see where the diagnostic is being generated, looking for how to get the current line/indentation. |
Getting those things will require a bit of trial and error. You can take a look at Feel free to reach out if you need extra guidance. |
Okay, thank you. |
What's the best way to debug this? Right now I'm running a specific test with |
Right now I'm creating a temporary variable solely to satisfy the compiler. It's not even possible within an anonymous block, and that bit I don't fully understand. I'd think it's possible for the compiler to perform the suggested action internally (and silently), but I'm also not familiar with the exact details of rustc. Is this not the case? |
@kennethbgoodin would you mind me picking this up? |
@estebank do you maybe have a pointer for an existing suggestion structured similarly? |
I can't think of an example off the top of my head, and we don't really do this heavy text wrangling for suggestions much because it is very error prone. |
This would be a nice feature to have. Took me some minutes today to find my mistake, an error like this would have speed up the process. |
could be a great thing for new comers :) |
This is already improved with a multipart suggestion in the nightly version. #![allow(unused_variables)]
fn foo() -> i32 { 22 }
fn bar(x: &i32) -> &i32 { x }
fn main() {
let p = bar(&foo());
let q = *p;
} Gives this diagnostic for error[E0716]: temporary value dropped while borrowed
--> src/main.rs:5:18
|
5 | let p = bar(&foo());
| ^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
6 | let q = *p;
| -- borrow later used here
|
help: consider using a `let` binding to create a longer lived value
|
5 ~ let binding = foo();
6 ~ let p = bar(&binding);
|
For more information about this error, try `rustc --explain E0716`. Seems to have been done in #99258 by @estebank yourself, so this can probably be closed :) |
From https://www.reddit.com/r/rust/comments/bv90s7/temporary_value_dropped_while_borrowed/
We currently supply the following diagnostic:
The note should be a structured suggestion that looks at the current line, figures out the indentation, finds the span for the beginning of the line and suggests adding a line above with a
let
binding. This should make it clearer what we're suggesting. Additionally, it'd be nice if we could add either a succinct explanation or a link to the book with an explanation of what might be happening here.The text was updated successfully, but these errors were encountered: