rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.57k stars 12.74k forks source link

E0597: `lock` does not live long enough (surprising error, the message could be more helpful) #70844

Open ghost opened 4 years ago

ghost commented 4 years ago

I tried compiling this code:

fn main() {
    let lock = std::sync::Mutex::new(10);
    if let Ok(_) = lock.try_lock() {}
}

Error:

error[E0597]: `lock` does not live long enough
 --> src/main.rs:3:20
  |
3 |     if let Ok(_) = lock.try_lock() {}
  |                    ^^^^-----------
  |                    |
  |                    borrowed value does not live long enough
  |                    a temporary with access to the borrow is created here ...
4 | }
  | -
  | |
  | `lock` dropped here while still borrowed
  | ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::result::Result<std::sync::MutexGuard<'_, i32>, std::sync::TryLockError<std::sync::MutexGuard<'_, i32>>>`
  |
  = note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
error: could not compile `tmp`.

To learn more, run the command again with --verbose.

I'm surprised that this does not compile!

The error message suggests adding a semicolon after the expression, but it's not obvious to me what exactly the expression is and where the semicolon should go. A suggestion on where to place the semicolon would be helpful.

The solution is this and compiles just fine:

fn main() {
    let lock = std::sync::Mutex::new(10);
    if let Ok(_) = lock.try_lock() {};
}
comex commented 4 years ago

This isn't just a diagnostic issue; the compiler should definitely accept the version without the semicolon.

pnkfelix commented 4 years ago

cc #15023 cc #46413