Skip to content

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

Open
@ghost

Description

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() {};
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions