rust-lang / rust-lang/rust

`E0716` suggests `binding` on 1st assign, rather than merge

Open
#124,274 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#[allow(unused)]
fn main() {
    let x = 0;
    let y;
    y = &&x;
    println!("{y}");
}
Current output
Compiling playground v0.0.1 (/playground)
error[E0716]: temporary value dropped while borrowed
 --> src/main.rs:5:10
  |
5 |     y = &&x;
  |          ^^- temporary value is freed at the end of this statement
  |          |
  |          creates a temporary value which is freed while still in use
6 |     println!("{y}");
  |               --- borrow later used here
  |
help: consider using a `let` binding to create a longer lived value
  |
5 ~     let binding = &x;
6 ~     y = &binding;
  |

For more information about this error, try `rustc --explain E0716`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Desired output
Compiling playground v0.0.1 (/playground)
error[E0716]: temporary value dropped while borrowed
 --> src/main.rs:5:10
  |
5 |     y = &&x;
  |          ^^- temporary value is freed at the end of this statement
  |          |
  |          creates a temporary value which is freed while still in use
6 |     println!("{y}");
  |               --- borrow later used here
  |
help: consider using a `let` binding to create a longer lived value
  |
4 ~     let y = &&x;
  |

For more information about this error, try `rustc --explain E0716`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Rationale and extra context

In "Desired output" IDK what's the proper notation to express "delete line 5"

Other cases

No response

Rust Version
Playground
1.79.0-nightly
2024-04-21
fb898629a26e4acec59c
Anything else?

https://github.com/rust-lang/rust/issues/99430#issuecomment-2071134933

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the E0716 diagnostic with the Rust Playground example and compare the current and desired suggestions. Trace the compiler's E0716 help output and adjust it so the suggested change merges the assignment into let y = &&x;; done means the diagnostic matches the desired output without suggesting a binding variable.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.