Skip to content
forked from rust-lang/rust
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ae08c58

Browse files
authoredJun 24, 2024
Rollup merge of rust-lang#126213 - zachs18:atomicbool-u8-i8-from-ptr-alignment, r=Nilstrieb
Update docs for AtomicBool/U8/I8 with regard to alignment Fixes rust-lang#126084. Since `AtomicBool`/`AtomicU8`/`AtomicI8` are guaranteed to have size == 1, and Rust guarantees that `size % align == 0`, they also must have alignment equal to 1, so some current docs are contradictory/confusing when describing their alignment requirements. Specifically: * Fix `AtomicBool::from_ptr` claiming that `align_of::<AtomicBool>() > align_of::<bool>()` on some platforms. (same for `AtomicU8::from_ptr`/`AtomicI8::from_ptr`) * Explicitly state that `AtomicU8`/`AtomicI8` have the same alignment as `u8`/`i8` (in addition to size and bit validity) * (internal) Change the `if_not_8_bit` macro to be `if_8_bit` and to allow an "if-else"-like structure, instead of just "if"-like. --- I opted to leave the "`ptr` must be aligned" wording in `from_ptr`'s docs and just clarify that it is always satsified, instead of just removing the wording entirely. If that is instead preferred I can do that.
2 parents c26bd79 + 5840184 commit ae08c58

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed
 

‎core/src/sync/atomic.rs

+33-17
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ impl AtomicBool {
443443
///
444444
/// # Safety
445445
///
446-
/// * `ptr` must be aligned to `align_of::<AtomicBool>()` (note that on some platforms this can
447-
/// be bigger than `align_of::<bool>()`).
446+
/// * `ptr` must be aligned to `align_of::<AtomicBool>()` (note that this is always true, since
447+
/// `align_of::<AtomicBool>() == 1`).
448448
/// * `ptr` must be [valid] for both reads and writes for the whole lifetime `'a`.
449449
/// * You must adhere to the [Memory model for atomic accesses]. In particular, it is not
450450
/// allowed to mix atomic and non-atomic accesses, or atomic accesses of different sizes,
@@ -2091,10 +2091,10 @@ impl<T> From<*mut T> for AtomicPtr<T> {
20912091
}
20922092

20932093
#[allow(unused_macros)] // This macro ends up being unused on some architectures.
2094-
macro_rules! if_not_8_bit {
2095-
(u8, $($tt:tt)*) => { "" };
2096-
(i8, $($tt:tt)*) => { "" };
2097-
($_:ident, $($tt:tt)*) => { $($tt)* };
2094+
macro_rules! if_8_bit {
2095+
(u8, $( yes = [$($yes:tt)*], )? $( no = [$($no:tt)*], )? ) => { concat!("", $($($yes)*)?) };
2096+
(i8, $( yes = [$($yes:tt)*], )? $( no = [$($no:tt)*], )? ) => { concat!("", $($($yes)*)?) };
2097+
($_:ident, $( yes = [$($yes:tt)*], )? $( no = [$($no:tt)*], )? ) => { concat!("", $($($no)*)?) };
20982098
}
20992099

21002100
#[cfg(target_has_atomic_load_store)]
@@ -2116,18 +2116,24 @@ macro_rules! atomic_int {
21162116
$int_type:ident $atomic_type:ident) => {
21172117
/// An integer type which can be safely shared between threads.
21182118
///
2119-
/// This type has the same size and bit validity as the underlying
2120-
/// integer type, [`
2119+
/// This type has the same
2120+
#[doc = if_8_bit!(
2121+
$int_type,
2122+
yes = ["size, alignment, and bit validity"],
2123+
no = ["size and bit validity"],
2124+
)]
2125+
/// as the underlying integer type, [`
21212126
#[doc = $s_int_type]
21222127
/// `].
2123-
#[doc = if_not_8_bit! {
2128+
#[doc = if_8_bit! {
21242129
$int_type,
2125-
concat!(
2130+
no = [
21262131
"However, the alignment of this type is always equal to its ",
21272132
"size, even on targets where [`", $s_int_type, "`] has a ",
21282133
"lesser alignment."
2129-
)
2134+
],
21302135
}]
2136+
///
21312137
/// For more about the differences between atomic types and
21322138
/// non-atomic types as well as information about the portability of
21332139
/// this type, please see the [module-level documentation].
@@ -2220,9 +2226,19 @@ macro_rules! atomic_int {
22202226
///
22212227
/// # Safety
22222228
///
2223-
#[doc = concat!(" * `ptr` must be aligned to \
2224-
`align_of::<", stringify!($atomic_type), ">()` (note that on some platforms this \
2225-
can be bigger than `align_of::<", stringify!($int_type), ">()`).")]
2229+
/// * `ptr` must be aligned to
2230+
#[doc = concat!(" `align_of::<", stringify!($atomic_type), ">()`")]
2231+
#[doc = if_8_bit!{
2232+
$int_type,
2233+
yes = [
2234+
" (note that this is always true, since `align_of::<",
2235+
stringify!($atomic_type), ">() == 1`)."
2236+
],
2237+
no = [
2238+
" (note that on some platforms this can be bigger than `align_of::<",
2239+
stringify!($int_type), ">()`)."
2240+
],
2241+
}]
22262242
/// * `ptr` must be [valid] for both reads and writes for the whole lifetime `'a`.
22272243
/// * You must adhere to the [Memory model for atomic accesses]. In particular, it is not
22282244
/// allowed to mix atomic and non-atomic accesses, or atomic accesses of different sizes,
@@ -2261,12 +2277,12 @@ macro_rules! atomic_int {
22612277

22622278
#[doc = concat!("Get atomic access to a `&mut ", stringify!($int_type), "`.")]
22632279
///
2264-
#[doc = if_not_8_bit! {
2280+
#[doc = if_8_bit! {
22652281
$int_type,
2266-
concat!(
2282+
no = [
22672283
"**Note:** This function is only available on targets where `",
22682284
stringify!($int_type), "` has an alignment of ", $align, " bytes."
2269-
)
2285+
],
22702286
}]
22712287
///
22722288
/// # Examples

0 commit comments

Comments
 (0)
Failed to load comments.