Skip to content

Commit b50f1a4

Browse files
committed
Add notes to avoid direct field syntax
1 parent afad9c3 commit b50f1a4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

crates/core_simd/src/vector.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ 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) should
81+
// be avoided, as it will likely become illegal on `#[repr(simd)]` structs in the future. It also
82+
// causes rustc to emit illegal LLVM IR in some cases.
7983
#[repr(simd)]
8084
pub struct Simd<T, const LANES: usize>([T; LANES])
8185
where
@@ -138,6 +142,9 @@ where
138142
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
139143
// is always valid and `Simd<T, LANES>` never has a lower alignment
140144
// than `[T; LANES]`.
145+
//
146+
// NOTE: This deliberately doesn't just use `&self.0`, see the comment
147+
// on the struct definition for details.
141148
unsafe { &*(self as *const Self as *const [T; LANES]) }
142149
}
143150

@@ -146,20 +153,29 @@ where
146153
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
147154
// is always valid and `Simd<T, LANES>` never has a lower alignment
148155
// than `[T; LANES]`.
156+
//
157+
// NOTE: This deliberately doesn't just use `&mut self.0`, see the comment
158+
// on the struct definition for details.
149159
unsafe { &mut *(self as *mut Self as *mut [T; LANES]) }
150160
}
151161

152162
/// Converts an array to a SIMD vector.
153163
pub const fn from_array(array: [T; LANES]) -> Self {
154164
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
155165
// is always valid.
166+
//
167+
// NOTE: This deliberately doesn't just use `Self(array)`, see the comment
168+
// on the struct definition for details.
156169
unsafe { core::mem::transmute_copy(&array) }
157170
}
158171

159172
/// Converts a SIMD vector to an array.
160173
pub const fn to_array(self) -> [T; LANES] {
161174
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
162175
// is always valid.
176+
//
177+
// NOTE: This deliberately doesn't just use `self.0`, see the comment
178+
// on the struct definition for details.
163179
unsafe { core::mem::transmute_copy(&self) }
164180
}
165181

0 commit comments

Comments
 (0)