diff options
| author | Markus Everling <markuseverling@gmail.com> | 2023-04-22 23:02:45 +0000 |
|---|---|---|
| committer | Markus Everling <markuseverling@gmail.com> | 2023-04-22 23:08:55 +0000 |
| commit | 52833ccbe88ed98b73d0ccd7299f2a667439bb4b (patch) | |
| tree | f9ad04fcc517a0f84ada254a801ee9700c2903fc | |
| parent | afad9c3f644ddbfef3301f617cb9d23ca4e71fe0 (diff) | |
| download | rust-52833ccbe88ed98b73d0ccd7299f2a667439bb4b.tar.gz rust-52833ccbe88ed98b73d0ccd7299f2a667439bb4b.zip | |
Add notes to avoid direct field accesses
| -rw-r--r-- | crates/core_simd/src/vector.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/crates/core_simd/src/vector.rs b/crates/core_simd/src/vector.rs index c1af4af5f57..eee105ff5fc 100644 --- a/crates/core_simd/src/vector.rs +++ b/crates/core_simd/src/vector.rs @@ -76,6 +76,11 @@ use crate::simd::{ /// [`read`]: pointer::read /// [`write`]: pointer::write /// [as_simd]: slice::as_simd +// +// NOTE: Accessing the inner array directly in any way (e.g. by using the `.0` field syntax) or +// directly constructing an instance of the type (i.e. `let vector = Simd(array)`) should be +// avoided, as it will likely become illegal on `#[repr(simd)]` structs in the future. It also +// causes rustc to emit illegal LLVM IR in some cases. #[repr(simd)] pub struct Simd<T, const LANES: usize>([T; LANES]) where @@ -138,6 +143,9 @@ where // SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]` // is always valid and `Simd<T, LANES>` never has a lower alignment // than `[T; LANES]`. + // + // NOTE: This deliberately doesn't just use `&self.0`, see the comment + // on the struct definition for details. unsafe { &*(self as *const Self as *const [T; LANES]) } } @@ -146,6 +154,9 @@ where // SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]` // is always valid and `Simd<T, LANES>` never has a lower alignment // than `[T; LANES]`. + // + // NOTE: This deliberately doesn't just use `&mut self.0`, see the comment + // on the struct definition for details. unsafe { &mut *(self as *mut Self as *mut [T; LANES]) } } @@ -153,6 +164,9 @@ where pub const fn from_array(array: [T; LANES]) -> Self { // SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]` // is always valid. + // + // NOTE: This deliberately doesn't just use `Self(array)`, see the comment + // on the struct definition for details. unsafe { core::mem::transmute_copy(&array) } } @@ -160,6 +174,9 @@ where pub const fn to_array(self) -> [T; LANES] { // SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]` // is always valid. + // + // NOTE: This deliberately doesn't just use `self.0`, see the comment + // on the struct definition for details. unsafe { core::mem::transmute_copy(&self) } } |
