Skip to content

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

Closed
@orium

Description

@orium
Member

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.

Activity

andjo403

andjo403 commented on Aug 11, 2022

@andjo403
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

orium commented on Aug 11, 2022

@orium
MemberAuthor
riking

riking commented on Aug 17, 2022

@riking

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @riking@andjo403@orium

        Issue actions

          Cannot mutate the value inside a `Mutex` created in a `const` context · Issue #100417 · rust-lang/rust