Skip to content

Commit 05c92c7

Browse files
committed
Document remaining internal unsafety, and deny undocumented unsafety
1 parent 5e32004 commit 05c92c7

File tree

8 files changed

+50
-22
lines changed

8 files changed

+50
-22
lines changed

crates/core_simd/src/elements/float.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,20 @@ macro_rules! impl_trait {
202202
#[inline]
203203
fn to_bits(self) -> Simd<$bits_ty, LANES> {
204204
assert_eq!(core::mem::size_of::<Self>(), core::mem::size_of::<Self::Bits>());
205+
// Safety: transmuting between vector types is safe
205206
unsafe { core::mem::transmute_copy(&self) }
206207
}
207208

208209
#[inline]
209210
fn from_bits(bits: Simd<$bits_ty, LANES>) -> Self {
210211
assert_eq!(core::mem::size_of::<Self>(), core::mem::size_of::<Self::Bits>());
212+
// Safety: transmuting between vector types is safe
211213
unsafe { core::mem::transmute_copy(&bits) }
212214
}
213215

214216
#[inline]
215217
fn abs(self) -> Self {
218+
// Safety: `self` is a float vector
216219
unsafe { intrinsics::simd_fabs(self) }
217220
}
218221

@@ -283,11 +286,13 @@ macro_rules! impl_trait {
283286

284287
#[inline]
285288
fn simd_min(self, other: Self) -> Self {
289+
// Safety: `self` and `other` are float vectors
286290
unsafe { intrinsics::simd_fmin(self, other) }
287291
}
288292

289293
#[inline]
290294
fn simd_max(self, other: Self) -> Self {
295+
// Safety: `self` and `other` are floating point vectors
291296
unsafe { intrinsics::simd_fmax(self, other) }
292297
}
293298

crates/core_simd/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#![cfg_attr(feature = "generic_const_exprs", feature(generic_const_exprs))]
1313
#![cfg_attr(feature = "generic_const_exprs", allow(incomplete_features))]
1414
#![warn(missing_docs)]
15-
#![deny(unsafe_op_in_unsafe_fn)]
15+
#![deny(unsafe_op_in_unsafe_fn, clippy::undocumented_unsafe_blocks)]
1616
#![unstable(feature = "portable_simd", issue = "86656")]
1717
//! Portable SIMD module.
1818

crates/core_simd/src/masks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ macro_rules! impl_element {
6868
const FALSE: Self = 0;
6969
}
7070

71+
// Safety: this is a valid mask element type
7172
unsafe impl MaskElement for $ty {}
7273
}
7374
}

crates/core_simd/src/masks/to_bitmask.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ where
1616
/// Converts masks to and from integer bitmasks.
1717
///
1818
/// Each bit of the bitmask corresponds to a mask lane, starting with the LSB.
19-
///
20-
/// # Safety
21-
/// This trait is `unsafe` and sealed, since the `BitMask` type must match the number of lanes in
22-
/// the mask.
23-
pub unsafe trait ToBitMask: Sealed {
19+
pub trait ToBitMask: Sealed {
2420
/// The integer bitmask type.
2521
type BitMask;
2622

@@ -34,12 +30,8 @@ pub unsafe trait ToBitMask: Sealed {
3430
/// Converts masks to and from byte array bitmasks.
3531
///
3632
/// Each bit of the bitmask corresponds to a mask lane, starting with the LSB of the first byte.
37-
///
38-
/// # Safety
39-
/// This trait is `unsafe` and sealed, since the `BYTES` value must match the number of lanes in
40-
/// the mask.
4133
#[cfg(feature = "generic_const_exprs")]
42-
pub unsafe trait ToBitMaskArray: Sealed {
34+
pub trait ToBitMaskArray: Sealed {
4335
/// The length of the bitmask array.
4436
const BYTES: usize;
4537

@@ -51,9 +43,9 @@ pub unsafe trait ToBitMaskArray: Sealed {
5143
}
5244

5345
macro_rules! impl_integer_intrinsic {
54-
{ $(unsafe impl ToBitMask<BitMask=$int:ty> for Mask<_, $lanes:literal>)* } => {
46+
{ $(impl ToBitMask<BitMask=$int:ty> for Mask<_, $lanes:literal>)* } => {
5547
$(
56-
unsafe impl<T: MaskElement> ToBitMask for Mask<T, $lanes> {
48+
impl<T: MaskElement> ToBitMask for Mask<T, $lanes> {
5749
type BitMask = $int;
5850

5951
fn to_bitmask(self) -> $int {
@@ -69,13 +61,13 @@ macro_rules! impl_integer_intrinsic {
6961
}
7062

7163
impl_integer_intrinsic! {
72-
unsafe impl ToBitMask<BitMask=u8> for Mask<_, 1>
73-
unsafe impl ToBitMask<BitMask=u8> for Mask<_, 2>
74-
unsafe impl ToBitMask<BitMask=u8> for Mask<_, 4>
75-
unsafe impl ToBitMask<BitMask=u8> for Mask<_, 8>
76-
unsafe impl ToBitMask<BitMask=u16> for Mask<_, 16>
77-
unsafe impl ToBitMask<BitMask=u32> for Mask<_, 32>
78-
unsafe impl ToBitMask<BitMask=u64> for Mask<_, 64>
64+
impl ToBitMask<BitMask=u8> for Mask<_, 1>
65+
impl ToBitMask<BitMask=u8> for Mask<_, 2>
66+
impl ToBitMask<BitMask=u8> for Mask<_, 4>
67+
impl ToBitMask<BitMask=u8> for Mask<_, 8>
68+
impl ToBitMask<BitMask=u16> for Mask<_, 16>
69+
impl ToBitMask<BitMask=u32> for Mask<_, 32>
70+
impl ToBitMask<BitMask=u64> for Mask<_, 64>
7971
}
8072

8173
/// Returns the minimum numnber of bytes in a bitmask with `lanes` lanes.
@@ -85,7 +77,7 @@ pub const fn bitmask_len(lanes: usize) -> usize {
8577
}
8678

8779
#[cfg(feature = "generic_const_exprs")]
88-
unsafe impl<T: MaskElement, const LANES: usize> ToBitMaskArray for Mask<T, LANES>
80+
impl<T: MaskElement, const LANES: usize> ToBitMaskArray for Mask<T, LANES>
8981
where
9082
LaneCount<LANES>: SupportedLaneCount,
9183
{

crates/core_simd/src/ops.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ where
3333

3434
macro_rules! unsafe_base {
3535
($lhs:ident, $rhs:ident, {$simd_call:ident}, $($_:tt)*) => {
36+
// Safety: $lhs and $rhs are vectors
3637
unsafe { $crate::simd::intrinsics::$simd_call($lhs, $rhs) }
3738
};
3839
}
@@ -49,6 +50,7 @@ macro_rules! unsafe_base {
4950
macro_rules! wrap_bitshift {
5051
($lhs:ident, $rhs:ident, {$simd_call:ident}, $int:ident) => {
5152
#[allow(clippy::suspicious_arithmetic_impl)]
53+
// Safety: $lhs and the bitand result are vectors
5254
unsafe {
5355
$crate::simd::intrinsics::$simd_call(
5456
$lhs,
@@ -91,6 +93,7 @@ macro_rules! int_divrem_guard {
9193
// Nice base case to make it easy to const-fold away the other branch.
9294
$rhs
9395
};
96+
// Safety: $lhs and rhs are vectors
9497
unsafe { $crate::simd::intrinsics::$simd_call($lhs, rhs) }
9598
}
9699
};

crates/core_simd/src/ops/unary.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ macro_rules! neg {
1414
#[inline]
1515
#[must_use = "operator returns a new vector without mutating the input"]
1616
fn neg(self) -> Self::Output {
17+
// Safety: `self` is a signed vector
1718
unsafe { intrinsics::simd_neg(self) }
1819
}
1920
})*

crates/core_simd/src/round.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ macro_rules! implement {
3030
$type: FloatToInt<I>,
3131
I: SimdElement,
3232
{
33+
// Safety: `self` is a vector, and `FloatToInt` ensures the type can be casted to
34+
// an integer.
3335
unsafe { intrinsics::simd_cast(self) }
3436
}
3537
}

crates/core_simd/src/vector.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ where
213213
#[inline]
214214
#[cfg(not(bootstrap))]
215215
pub fn cast<U: SimdElement>(self) -> Simd<U, LANES> {
216-
// Safety: The input argument is a vector of a known SIMD type.
216+
// Safety: The input argument is a vector of a valid SIMD element type.
217217
unsafe { intrinsics::simd_as(self) }
218218
}
219219

@@ -624,61 +624,85 @@ pub unsafe trait SimdElement: Sealed + Copy {
624624
}
625625

626626
impl Sealed for u8 {}
627+
628+
// Safety: u8 is a valid SIMD element type, and is supported by this API
627629
unsafe impl SimdElement for u8 {
628630
type Mask = i8;
629631
}
630632

631633
impl Sealed for u16 {}
634+
635+
// Safety: u16 is a valid SIMD element type, and is supported by this API
632636
unsafe impl SimdElement for u16 {
633637
type Mask = i16;
634638
}
635639

636640
impl Sealed for u32 {}
641+
642+
// Safety: u32 is a valid SIMD element type, and is supported by this API
637643
unsafe impl SimdElement for u32 {
638644
type Mask = i32;
639645
}
640646

641647
impl Sealed for u64 {}
648+
649+
// Safety: u64 is a valid SIMD element type, and is supported by this API
642650
unsafe impl SimdElement for u64 {
643651
type Mask = i64;
644652
}
645653

646654
impl Sealed for usize {}
655+
656+
// Safety: usize is a valid SIMD element type, and is supported by this API
647657
unsafe impl SimdElement for usize {
648658
type Mask = isize;
649659
}
650660

651661
impl Sealed for i8 {}
662+
663+
// Safety: i8 is a valid SIMD element type, and is supported by this API
652664
unsafe impl SimdElement for i8 {
653665
type Mask = i8;
654666
}
655667

656668
impl Sealed for i16 {}
669+
670+
// Safety: i16 is a valid SIMD element type, and is supported by this API
657671
unsafe impl SimdElement for i16 {
658672
type Mask = i16;
659673
}
660674

661675
impl Sealed for i32 {}
676+
677+
// Safety: i32 is a valid SIMD element type, and is supported by this API
662678
unsafe impl SimdElement for i32 {
663679
type Mask = i32;
664680
}
665681

666682
impl Sealed for i64 {}
683+
684+
// Safety: i64 is a valid SIMD element type, and is supported by this API
667685
unsafe impl SimdElement for i64 {
668686
type Mask = i64;
669687
}
670688

671689
impl Sealed for isize {}
690+
691+
// Safety: isize is a valid SIMD element type, and is supported by this API
672692
unsafe impl SimdElement for isize {
673693
type Mask = isize;
674694
}
675695

676696
impl Sealed for f32 {}
697+
698+
// Safety: f32 is a valid SIMD element type, and is supported by this API
677699
unsafe impl SimdElement for f32 {
678700
type Mask = i32;
679701
}
680702

681703
impl Sealed for f64 {}
704+
705+
// Safety: f64 is a valid SIMD element type, and is supported by this API
682706
unsafe impl SimdElement for f64 {
683707
type Mask = i64;
684708
}

0 commit comments

Comments
 (0)