Skip to content
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

Closed
estebank opened this issue May 31, 2019 · 11 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

From https://www.reddit.com/r/rust/comments/bv90s7/temporary_value_dropped_while_borrowed/

We currently supply the following diagnostic:

let mut words = con.unwrap().split_whitespace().peekable();
                ^^^^^^^^^^^^                              - temporary value is freed at the
                |                                                     end of this statement
   |            |
   |            creates a temporary which is freed while still in use
   |
   |             while words.peek().is_some() {
   |                   ----- borrow later used here
   |
   = note: consider using a `let` binding to create a longer lived value

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.

@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels May 31, 2019
@kungfukennyg
Copy link
Contributor

kungfukennyg commented Jun 1, 2019

I want to attempt this issue. I see where the diagnostic is being generated, looking for how to get the current line/indentation.

@estebank
Copy link
Contributor Author

estebank commented Jun 1, 2019

Getting those things will require a bit of trial and error. You can take a look at source_map::span_to_margin for the way to get the indentation of a given span, and you'll need to add a new similar method to give you the span corresponding to the beginning of the line.

Feel free to reach out if you need extra guidance.

@kungfukennyg
Copy link
Contributor

Okay, thank you.

@kungfukennyg
Copy link
Contributor

What's the best way to debug this? Right now I'm running a specific test with ./x.py test -i src/test/ui/span/slice-borrow.rs --stage 1 --keep-stage 1 but that still takes a while to compile and doesn't give me much in the way of debug logging. I do have debug enabled in my local config.toml.

@jhpratt
Copy link
Member

jhpratt commented Jul 27, 2019

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?

@basil-cow
Copy link
Contributor

@kennethbgoodin would you mind me picking this up?

@basil-cow
Copy link
Contributor

@estebank do you maybe have a pointer for an existing suggestion structured similarly?

@estebank
Copy link
Contributor Author

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.

@RobertLemmens
Copy link

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.

@Xenunxenunowski
Copy link

could be a great thing for new comers :)

@V0ldek
Copy link
Contributor

V0ldek commented Nov 5, 2022

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 cargo +nightly check:

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 :)

@estebank estebank closed this as completed Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants