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

Strange scope issue when use lock (cause deadlock) #60107

Closed
TheWaWaR opened this issue Apr 19, 2019 · 3 comments
Closed

Strange scope issue when use lock (cause deadlock) #60107

TheWaWaR opened this issue Apr 19, 2019 · 3 comments

Comments

@TheWaWaR
Copy link

The code:

use std::sync::Mutex;

#[derive(Clone, Debug)]
struct A {}

fn main() {
    let n = Mutex::new(Some(A {}));
    
    if let Some(a) = { let a = n.lock().unwrap(); a.clone() } {
        n.lock();
    }
    
    println!("This is ok");
    
    if let Some(a) = { let a = n.lock(); a.unwrap().clone() } {
        n.lock();
    }

    println!("Never reached !!!!");
}

Output:

    Finished dev [unoptimized + debuginfo] target(s) in 0.56s
     Running `target/debug/playground`
/root/entrypoint.sh: line 8:     7 Killed                  timeout --signal=KILL ${timeout} "$@"
Standard Output
This is ok
@jonas-schievink
Copy link
Contributor

Pretty sure this is expected due to the way scopes in match statements work - the matched expression is dropped only after the match completes

@pnkfelix
Copy link
Member

@jonas-schievink well, what you said, plus the detail about how the temporaries in tail expressions of a block are given the dynamic extent of the block's parent, right?

as demonstrated here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a08de70a5bf898ae5daa2a5a2f538955

cc #46413
cc #37612

@jonas-schievink
Copy link
Contributor

Yes, indeed. Looks like #37612 (comment) explains this quite well, and that issue is basically the same as this (minus the if let -> match desugaring), so I'll close this as a duplicate of #37612.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants