make atomic operations const - #160079
Merged
Merged
Conversation
Collaborator
|
Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter cc @rust-lang/miri, @oli-obk, @lcnr
cc @rust-lang/miri Some changes occurred to the CTFE machinery Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri |
Collaborator
|
r? @folkertdev rustbot has assigned @folkertdev. Use Why was this reviewer chosen?The reviewer was selected based on:
|
4 tasks
This comment has been minimized.
This comment has been minimized.
Contributor
jhpratt
added a commit
to jhpratt/rust
that referenced
this pull request
Aug 1, 2026
make atomic operations const In a similar vein to rust-lang#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: ```rust // 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: rust-lang#160078 Cc @rust-lang/wg-const-eval @rust-lang/opsem
jhpratt
added a commit
to jhpratt/rust
that referenced
this pull request
Aug 1, 2026
make atomic operations const In a similar vein to rust-lang#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: ```rust // 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: rust-lang#160078 Cc @rust-lang/wg-const-eval @rust-lang/opsem
This was referenced Aug 1, 2026
rust-bors Bot
pushed a commit
that referenced
this pull request
Aug 1, 2026
Rollup of 14 pull requests Successful merges: - #159245 (Emit retags in codegen to support BorrowSanitizer (part 5)) - #159864 (Report "capacity overflow" for oversized Rc<[T]>/Arc<[T]>) - #160079 (make atomic operations const) - #160124 (Structurally prevent zero-count `BackendRepr::SimdVector`s) - #160162 (Make `#[fundamental]` only apply to the first argument of `Box`) - #160210 (Remove an outdated FIXME) - #160282 (Improve diagnostic for patterns in function pointer types) - #157928 (Eagerly fetch typeck results when linting) - #159672 (Improve suggestions when multiples tuples implement the same trait) - #159861 (Add documentation for the `non_exhaustive` attribute) - #159907 (Fix `hidden_glob_reexports` in `rustc_ast`) - #159998 (Align expect messages with guidance) - #160145 (Expand checks for register_tool) - #160307 (Update `minifier` version to `0.4.0`)
rust-timer
added a commit
that referenced
this pull request
Aug 1, 2026
Rollup merge of #160079 - RalfJung:const-atomic, r=folkertdev make atomic operations const 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: ```rust // 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
pull Bot
pushed a commit
to xtqqczze/rust-lang-miri
that referenced
this pull request
Aug 2, 2026
Rollup of 14 pull requests Successful merges: - rust-lang/rust#159245 (Emit retags in codegen to support BorrowSanitizer (part 5)) - rust-lang/rust#159864 (Report "capacity overflow" for oversized Rc<[T]>/Arc<[T]>) - rust-lang/rust#160079 (make atomic operations const) - rust-lang/rust#160124 (Structurally prevent zero-count `BackendRepr::SimdVector`s) - rust-lang/rust#160162 (Make `#[fundamental]` only apply to the first argument of `Box`) - rust-lang/rust#160210 (Remove an outdated FIXME) - rust-lang/rust#160282 (Improve diagnostic for patterns in function pointer types) - rust-lang/rust#157928 (Eagerly fetch typeck results when linting) - rust-lang/rust#159672 (Improve suggestions when multiples tuples implement the same trait) - rust-lang/rust#159861 (Add documentation for the `non_exhaustive` attribute) - rust-lang/rust#159907 (Fix `hidden_glob_reexports` in `rustc_ast`) - rust-lang/rust#159998 (Align expect messages with guidance) - rust-lang/rust#160145 (Expand checks for register_tool) - rust-lang/rust#160307 (Update `minifier` version to `0.4.0`)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In a similar vein to #159094, it makes sense to make these
const fnso 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:
However, this excludes the
fetch_*andcompare_*operations onAtomicPtr, as that kind of pointer arithmetic is not possible at compile time.Tracking issue: #160078
Cc @rust-lang/wg-const-eval @rust-lang/opsem