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

Mutex giving dropped while still borrowed errors. #44801

Closed
Mr-Byte opened this issue Sep 24, 2017 · 2 comments
Closed

Mutex giving dropped while still borrowed errors. #44801

Mr-Byte opened this issue Sep 24, 2017 · 2 comments
Labels
A-borrow-checker Area: The borrow checker C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Mr-Byte
Copy link

Mr-Byte commented Sep 24, 2017

With the following program:

use std::sync::Mutex;

pub fn main() {
    let foo = Mutex::new(0u32);
    
    if let Ok(foo) = foo.lock() {
        println!("{}", foo);
    }
}

I get the following compiler error:

error[E0597]: `foo` does not live long enough
 --> src/main.rs:9:1
  |
6 |     if let Ok(foo) = foo.lock() {
  |                      --- borrow occurs here
...
9 | }
  | ^ `foo` dropped here while still borrowed
  |
  = note: values in a scope are dropped in the opposite order they are created

However when I modify the program as follows:

use std::sync::Mutex;

pub fn main() {
    let foo = Mutex::new(0u32);
    
    if let Ok(foo) = foo.lock() {
        println!("{}", foo);
    }
    
    {}
}

The program compiles and runs as expected.

It seems odd to me that inserting {} after the lock fixes the issue. It appears that any code following the lock allows for the program to compile and run as expected, with an empty {} or () being the simplest way of doing this.

@bluss
Copy link
Member

bluss commented Sep 24, 2017

Thanks, this is far from ideal and an issue that is already tracked. Semicolon is another symbol that can help you until it's fixed.

Duplicate of #22323

@TimNN TimNN added A-borrow-checker Area: The borrow checker C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 24, 2017
@pnkfelix
Copy link
Member

pnkfelix commented Sep 19, 2018

(This is actually a duplicate of #46413 (and #21114), not #22323 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

5 participants