Skip to content

Silent failure of std::sync::Once when in const variable #132028

@CGamesPlay

Description

@CGamesPlay

I tried this code playground link:

use std::sync::Once;

const INIT: Once = Once::new();

fn main() {
    INIT.call_once(|| {
        println!("Global Once::call_once first call");
    });
    INIT.call_once(|| {
        println!("Global Once::call_once second call");
    });
}

The problem is that my Once is in a const and not a static. However, I would expect a compile error or a runtime error instead of broken behavior here. The same behavior is exhibited in std::sync::LazyLock and probably std::sync::OnceLock, due to using a Once internally.

Thanks for considering this!

Meta

Checked on 1.82.0 and on nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-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