Skip to content

Commit 52833cc

Browse files
committedApr 22, 2023
Add notes to avoid direct field accesses
1 parent afad9c3 commit 52833cc

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
 

‎crates/core_simd/src/vector.rs

+17
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ use crate::simd::{
7676
/// [`read`]: pointer::read
7777
/// [`write`]: pointer::write
7878
/// [as_simd]: slice::as_simd
79+
//
80+
// NOTE: Accessing the inner array directly in any way (e.g. by using the `.0` field syntax) or
81+
// directly constructing an instance of the type (i.e. `let vector = Simd(array)`) should be
82+
// avoided, as it will likely become illegal on `#[repr(simd)]` structs in the future. It also
83+
// causes rustc to emit illegal LLVM IR in some cases.
7984
#[repr(simd)]
8085
pub struct Simd<T, const LANES: usize>([T; LANES])
8186
where
@@ -138,6 +143,9 @@ where
138143
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
139144
// is always valid and `Simd<T, LANES>` never has a lower alignment
140145
// than `[T; LANES]`.
146+
//
147+
// NOTE: This deliberately doesn't just use `&self.0`, see the comment
148+
// on the struct definition for details.
141149
unsafe { &*(self as *const Self as *const [T; LANES]) }
142150
}
143151

@@ -146,20 +154,29 @@ where
146154
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
147155
// is always valid and `Simd<T, LANES>` never has a lower alignment
148156
// than `[T; LANES]`.
157+
//
158+
// NOTE: This deliberately doesn't just use `&mut self.0`, see the comment
159+
// on the struct definition for details.
149160
unsafe { &mut *(self as *mut Self as *mut [T; LANES]) }
150161
}
151162

152163
/// Converts an array to a SIMD vector.
153164
pub const fn from_array(array: [T; LANES]) -> Self {
154165
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
155166
// is always valid.
167+
//
168+
// NOTE: This deliberately doesn't just use `Self(array)`, see the comment
169+
// on the struct definition for details.
156170
unsafe { core::mem::transmute_copy(&array) }
157171
}
158172

159173
/// Converts a SIMD vector to an array.
160174
pub const fn to_array(self) -> [T; LANES] {
161175
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
162176
// is always valid.
177+
//
178+
// NOTE: This deliberately doesn't just use `self.0`, see the comment
179+
// on the struct definition for details.
163180
unsafe { core::mem::transmute_copy(&self) }
164181
}
165182

0 commit comments

Comments
 (0)
Please sign in to comment.