Skip to content

Commit 6202515

Browse files
committed
Document remaining internal unsafety, and deny undocumented unsafety
1 parent c44a608 commit 6202515

File tree

8 files changed

+48
-12
lines changed

8 files changed

+48
-12
lines changed

crates/core_simd/src/elements/float.rs

+5
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

+1-1
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

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ macro_rules! impl_element {
6565
const FALSE: Self = 0;
6666
}
6767

68+
// Safety: this is a valid mask element type
6869
unsafe impl MaskElement for $ty {}
6970
}
7071
}

crates/core_simd/src/masks/to_bitmask.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ where
2020
/// # Safety
2121
/// This trait is `unsafe` and sealed, since the `BitMask` type must match the number of lanes in
2222
/// the mask.
23-
pub unsafe trait ToBitMask: Sealed {
23+
pub trait ToBitMask: Sealed {
2424
/// The integer bitmask type.
2525
type BitMask;
2626

@@ -32,9 +32,9 @@ pub unsafe trait ToBitMask: Sealed {
3232
}
3333

3434
macro_rules! impl_integer_intrinsic {
35-
{ $(unsafe impl ToBitMask<BitMask=$int:ty> for Mask<_, $lanes:literal>)* } => {
35+
{ $(impl ToBitMask<BitMask=$int:ty> for Mask<_, $lanes:literal>)* } => {
3636
$(
37-
unsafe impl<T: MaskElement> ToBitMask for Mask<T, $lanes> {
37+
impl<T: MaskElement> ToBitMask for Mask<T, $lanes> {
3838
type BitMask = $int;
3939

4040
fn to_bitmask(self) -> $int {
@@ -50,11 +50,11 @@ macro_rules! impl_integer_intrinsic {
5050
}
5151

5252
impl_integer_intrinsic! {
53-
unsafe impl ToBitMask<BitMask=u8> for Mask<_, 1>
54-
unsafe impl ToBitMask<BitMask=u8> for Mask<_, 2>
55-
unsafe impl ToBitMask<BitMask=u8> for Mask<_, 4>
56-
unsafe impl ToBitMask<BitMask=u8> for Mask<_, 8>
57-
unsafe impl ToBitMask<BitMask=u16> for Mask<_, 16>
58-
unsafe impl ToBitMask<BitMask=u32> for Mask<_, 32>
59-
unsafe impl ToBitMask<BitMask=u64> for Mask<_, 64>
53+
impl ToBitMask<BitMask=u8> for Mask<_, 1>
54+
impl ToBitMask<BitMask=u8> for Mask<_, 2>
55+
impl ToBitMask<BitMask=u8> for Mask<_, 4>
56+
impl ToBitMask<BitMask=u8> for Mask<_, 8>
57+
impl ToBitMask<BitMask=u16> for Mask<_, 16>
58+
impl ToBitMask<BitMask=u32> for Mask<_, 32>
59+
impl ToBitMask<BitMask=u64> for Mask<_, 64>
6060
}

crates/core_simd/src/ops.rs

+3
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

+1
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

+2
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

+25-1
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)