Skip to content

Footgun with const declarations of type Atomic* #65822

Open
@asajeffrey

Description

@asajeffrey

I just got caught by a footgun where I'd declared a global counter as:

const COUNTER: AtomicUsize = AtomicUsize::new(0);

and incremented it using COUNTER.fetch_add(1, Ordering::SeqCst). It took me a while to realize why I was always getting 0! There are checks for const declarations not having interior mutability to catch these footguns, but they don't apply to atomics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-atomicArea: Atomics, barriers, and sync primitivesA-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language 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