Skip to content

make atomic operations const - #160079

Open
RalfJung wants to merge 1 commit into
rust-lang:mainfrom
RalfJung:const-atomic
Open

make atomic operations const#160079
RalfJung wants to merge 1 commit into
rust-lang:mainfrom
RalfJung:const-atomic

Conversation

@RalfJung

@RalfJung RalfJung commented Jul 28, 2026

Copy link
Copy Markdown
Member

In a similar vein to #159094, it makes sense to make these const fn so that code can be shared between compile-time and run-time even if the runtime version has to deal with concurrency.

This constifies the following:

// core::sync::atomic

impl AtomicBool {
    pub const fn get_mut(&mut self) -> &mut bool;
    pub const fn from_mut(v: &mut bool) -> &mut Self;
    pub const fn get_mut_slice(this: &mut [Self]) -> &mut [bool];
    pub const fn from_mut_slice(v: &mut [bool]) -> &mut [Self];
    pub const fn load(&self, order: Ordering) -> bool;
    pub const fn store(&self, val: bool, order: Ordering);
    pub const fn swap(&self, val: bool, order: Ordering) -> bool;
    pub const fn compare_and_swap(&self, current: bool, new: bool, order: Ordering) -> bool;
    pub const fn compare_exchange(
        &self,
        current: bool,
        new: bool,
        success: Ordering,
        failure: Ordering,
    ) -> Result<bool, bool>;
    pub const fn compare_exchange_weak(
        &self,
        current: bool,
        new: bool,
        success: Ordering,
        failure: Ordering,
    ) -> Result<bool, bool>;
    pub const fn fetch_and(&self, val: bool, order: Ordering) -> bool;
    pub const fn fetch_nand(&self, val: bool, order: Ordering) -> bool;
    pub const fn fetch_or(&self, val: bool, order: Ordering) -> bool;
    pub const fn fetch_xor(&self, val: bool, order: Ordering) -> bool;
    pub const fn fetch_not(&self, order: Ordering) -> bool;
}

impl AtomicPtr<T> {
    pub const fn get_mut(&mut self) -> &mut *mut T;
    pub const fn from_mut(v: &mut *mut T) -> &mut Self;
    pub const fn get_mut_slice(this: &mut [Self]) -> &mut [*mut T];
    pub const fn from_mut_slice(v: &mut [*mut T]) -> &mut [Self];
    pub const fn load(&self, order: Ordering) -> *mut T;
    pub const fn store(&self, ptr: *mut T, order: Ordering);
    pub const fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T;
}

impl Atomic$Int {
    pub const fn get_mut(&mut self) -> &mut $int_type;
    pub const fn from_mut(v: &mut $int_type) -> &mut Self;
    pub const fn get_mut_slice(this: &mut [Self]) -> &mut [$int_type];
    pub const fn from_mut_slice(v: &mut [$int_type]) -> &mut [Self];
    pub const fn load(&self, order: Ordering) -> $int_type;
    pub const fn store(&self, val: $int_type, order: Ordering);
    pub const fn swap(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn compare_and_swap(&self,
                                    current: $int_type,
                                    new: $int_type,
                                    order: Ordering) -> $int_type;
    pub const fn compare_exchange(&self,
                                    current: $int_type,
                                    new: $int_type,
                                    success: Ordering,
                                    failure: Ordering) -> Result<$int_type, $int_type>;
    pub const fn compare_exchange_weak(&self,
                                         current: $int_type,
                                         new: $int_type,
                                         success: Ordering,
                                         failure: Ordering) -> Result<$int_type, $int_type>;
    pub const fn fetch_add(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_sub(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_and(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_nand(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_or(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_xor(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_max(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_min(&self, val: $int_type, order: Ordering) -> $int_type;
}

pub const fn fence(order: Ordering);
pub const fn compiler_fence(order: Ordering);

However, this excludes the fetch_* and compare_* operations on AtomicPtr, as that kind of pointer arithmetic is not possible at compile time.

Tracking issue: #160078
Cc @rust-lang/wg-const-eval @rust-lang/opsem

@rustbot

rustbot commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @oli-obk, @lcnr

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

Some changes occurred to the CTFE machinery

cc @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 28, 2026
@rustbot

rustbot commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

r? @folkertdev

rustbot has assigned @folkertdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, mir
  • compiler, mir expanded to 74 candidates
  • Random selection from 16 candidates

@rust-log-analyzer

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants