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

Cannot mutate the value inside a Mutex created in a const context #100417

Closed
orium opened this issue Aug 11, 2022 · 3 comments
Closed

Cannot mutate the value inside a Mutex created in a const context #100417

orium opened this issue Aug 11, 2022 · 3 comments
Labels
C-bug Category: This is a bug.

Comments

@orium
Copy link
Member

orium commented Aug 11, 2022

Hi. I was trying the new Mutex::new() in const a context (new in rust 1.63) and I'm able to mutate it's value:

I tried this code:

use std::sync::Mutex;

const VAR: Mutex<usize> = Mutex::new(0);

fn main() {
    println!("var: {}", *VAR.lock().unwrap());
    *VAR.lock().unwrap() = 3;
    println!("var: {}", *VAR.lock().unwrap());
}

The output is

var: 0
var: 0

Playground link here.

@orium orium added the C-bug Category: This is a bug. label Aug 11, 2022
@andjo403
Copy link
Contributor

The problem is that you try to mutate a const this creates a temporary that you mutate, if you change const to static you get the expected result.
cc #40543

@orium
Copy link
Member Author

orium commented Aug 11, 2022

Closing since this is the expected behavior: https://www.reddit.com/r/rust/comments/wltcf8/comment/ijvx9ym/?context=3

@orium orium closed this as completed Aug 11, 2022
@riking
Copy link

riking commented Aug 17, 2022

Diagnostics part of issue is covered in #40543

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants