@@ -121,6 +121,7 @@ use self::Ordering::*;
121
121
use crate :: cell:: UnsafeCell ;
122
122
use crate :: fmt;
123
123
use crate :: intrinsics;
124
+ use crate :: mem:: align_of;
124
125
125
126
use crate :: hint:: spin_loop;
126
127
@@ -374,7 +375,8 @@ impl AtomicBool {
374
375
#[ inline]
375
376
#[ unstable( feature = "atomic_from_mut" , issue = "none" ) ]
376
377
pub fn from_mut ( v : & mut bool ) -> & Self {
377
- // SAFETY: the mutable reference guarantees unique ownership.
378
+ // SAFETY: the mutable reference guarantees unique ownership, and
379
+ // alignment of both `bool` and `Self` is 1.
378
380
unsafe { & * ( v as * mut bool as * mut Self ) }
379
381
}
380
382
@@ -950,7 +952,11 @@ impl<T> AtomicPtr<T> {
950
952
#[ inline]
951
953
#[ unstable( feature = "atomic_from_mut" , issue = "none" ) ]
952
954
pub fn from_mut ( v : & mut * mut T ) -> & Self {
953
- // SAFETY: the mutable reference guarantees unique ownership,
955
+ let [ ] = [ ( ) ; align_of :: < Self > ( ) - align_of :: < * mut T > ( ) ] ;
956
+ // SAFETY:
957
+ // - the mutable reference guarantees unique ownership.
958
+ // - the alignment of `*mut T` and `Self` is the same on all platforms
959
+ // supported by rust, as verified above.
954
960
unsafe { & * ( v as * mut * mut T as * mut Self ) }
955
961
}
956
962
@@ -1276,6 +1282,12 @@ impl<T> From<*mut T> for AtomicPtr<T> {
1276
1282
}
1277
1283
}
1278
1284
1285
+ macro_rules! if_not_8_bit {
1286
+ ( u8 , $( $tt: tt) * ) => { "" } ;
1287
+ ( i8 , $( $tt: tt) * ) => { "" } ;
1288
+ ( $_: ident, $( $tt: tt) * ) => { $( $tt) * } ;
1289
+ }
1290
+
1279
1291
#[ cfg( target_has_atomic_load_store = "8" ) ]
1280
1292
macro_rules! atomic_int {
1281
1293
( $cfg_cas: meta,
@@ -1287,7 +1299,8 @@ macro_rules! atomic_int {
1287
1299
$stable_nand: meta,
1288
1300
$const_stable: meta,
1289
1301
$stable_init_const: meta,
1290
- $s_int_type: expr, $int_ref: expr,
1302
+ $( from_mut: cfg( $from_mut_cfg: meta) , ) ?
1303
+ $s_int_type: literal, $int_ref: expr,
1291
1304
$extra_feature: expr,
1292
1305
$min_fn: ident, $max_fn: ident,
1293
1306
$align: expr,
@@ -1401,6 +1414,16 @@ assert_eq!(some_var.load(Ordering::SeqCst), 5);
1401
1414
doc_comment! {
1402
1415
concat!( "Get atomic access to a `&mut " , stringify!( $int_type) , "`.
1403
1416
1417
+ " ,
1418
+ if_not_8_bit! {
1419
+ $int_type,
1420
+ concat!(
1421
+ "**Note:** This function is only available on targets where `" ,
1422
+ stringify!( $int_type) , "` has an alignment of " , $align, " bytes."
1423
+ )
1424
+ } ,
1425
+ "
1426
+
1404
1427
# Examples
1405
1428
1406
1429
```
@@ -1414,9 +1437,15 @@ assert_eq!(some_int, 100);
1414
1437
```
1415
1438
" ) ,
1416
1439
#[ inline]
1440
+ $( #[ cfg( $from_mut_cfg) ] ) ?
1417
1441
#[ unstable( feature = "atomic_from_mut" , issue = "none" ) ]
1418
1442
pub fn from_mut( v: & mut $int_type) -> & Self {
1419
- // SAFETY: the mutable reference guarantees unique ownership.
1443
+ let [ ] = [ ( ) ; align_of:: <Self >( ) - align_of:: <$int_type>( ) ] ;
1444
+ // SAFETY:
1445
+ // - the mutable reference guarantees unique ownership.
1446
+ // - the alignment of `$int_type` and `Self` is the
1447
+ // same on all platforms enabled by `$from_mut_cfg`
1448
+ // as verified above.
1420
1449
unsafe { & * ( v as * mut $int_type as * mut Self ) }
1421
1450
}
1422
1451
}
@@ -2265,6 +2294,7 @@ atomic_int! {
2265
2294
stable( feature = "integer_atomics_stable" , since = "1.34.0" ) ,
2266
2295
rustc_const_stable( feature = "const_integer_atomics" , since = "1.34.0" ) ,
2267
2296
unstable( feature = "integer_atomics" , issue = "32976" ) ,
2297
+ from_mut: cfg( not( target_arch = "x86" ) ) ,
2268
2298
"i64" , "../../../std/primitive.i64.html" ,
2269
2299
"" ,
2270
2300
atomic_min, atomic_max,
@@ -2283,6 +2313,7 @@ atomic_int! {
2283
2313
stable( feature = "integer_atomics_stable" , since = "1.34.0" ) ,
2284
2314
rustc_const_stable( feature = "const_integer_atomics" , since = "1.34.0" ) ,
2285
2315
unstable( feature = "integer_atomics" , issue = "32976" ) ,
2316
+ from_mut: cfg( not( target_arch = "x86" ) ) ,
2286
2317
"u64" , "../../../std/primitive.u64.html" ,
2287
2318
"" ,
2288
2319
atomic_umin, atomic_umax,
0 commit comments