Skip to content

Commit ac7539c

Browse files
Deny unsafe ops in unsafe fns, part 3
1 parent 8a515e9 commit ac7539c

File tree

5 files changed

+181
-113
lines changed

5 files changed

+181
-113
lines changed

src/libcore/num/f32.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! new code should use the associated constants directly on the primitive type.
1010
1111
#![stable(feature = "rust1", since = "1.0.0")]
12+
#![deny(unsafe_op_in_unsafe_fn)]
1213

1314
use crate::convert::FloatToInt;
1415
#[cfg(not(test))]
@@ -629,7 +630,9 @@ impl f32 {
629630
where
630631
Self: FloatToInt<Int>,
631632
{
632-
FloatToInt::<Int>::to_int_unchecked(self)
633+
// SAFETY: the caller must uphold the safety contract for
634+
// `FloatToInt::to_int_unchecked`.
635+
unsafe { FloatToInt::<Int>::to_int_unchecked(self) }
633636
}
634637

635638
/// Raw transmutation to `u32`.

src/libcore/num/f64.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! new code should use the associated constants directly on the primitive type.
1010
1111
#![stable(feature = "rust1", since = "1.0.0")]
12+
#![deny(unsafe_op_in_unsafe_fn)]
1213

1314
use crate::convert::FloatToInt;
1415
#[cfg(not(test))]
@@ -643,7 +644,9 @@ impl f64 {
643644
where
644645
Self: FloatToInt<Int>,
645646
{
646-
FloatToInt::<Int>::to_int_unchecked(self)
647+
// SAFETY: the caller must uphold the safety contract for
648+
// `FloatToInt::to_int_unchecked`.
649+
unsafe { FloatToInt::<Int>::to_int_unchecked(self) }
647650
}
648651

649652
/// Raw transmutation to `u64`.

src/libcore/num/mod.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! Numeric traits and functions for the built-in numeric types.
44
55
#![stable(feature = "rust1", since = "1.0.0")]
6+
#![deny(unsafe_op_in_unsafe_fn)]
67

78
use crate::convert::Infallible;
89
use crate::fmt;
@@ -74,7 +75,8 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
7475
#[rustc_const_stable(feature = "nonzero", since = "1.34.0")]
7576
#[inline]
7677
pub const unsafe fn new_unchecked(n: $Int) -> Self {
77-
Self(n)
78+
// SAFETY: this is guaranteed to be safe by the caller.
79+
unsafe { Self(n) }
7880
}
7981

8082
/// Creates a non-zero if the given value is not zero.
@@ -762,7 +764,9 @@ cannot occur. This results in undefined behavior when `self + rhs > ", stringify
762764
without modifying the original"]
763765
#[inline]
764766
pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
765-
intrinsics::unchecked_add(self, rhs)
767+
// SAFETY: the caller must uphold the safety contract for
768+
// `unchecked_add`.
769+
unsafe { intrinsics::unchecked_add(self, rhs) }
766770
}
767771
}
768772

@@ -804,7 +808,9 @@ cannot occur. This results in undefined behavior when `self - rhs > ", stringify
804808
without modifying the original"]
805809
#[inline]
806810
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
807-
intrinsics::unchecked_sub(self, rhs)
811+
// SAFETY: the caller must uphold the safety contract for
812+
// `unchecked_sub`.
813+
unsafe { intrinsics::unchecked_sub(self, rhs) }
808814
}
809815
}
810816

@@ -846,7 +852,9 @@ cannot occur. This results in undefined behavior when `self * rhs > ", stringify
846852
without modifying the original"]
847853
#[inline]
848854
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
849-
intrinsics::unchecked_mul(self, rhs)
855+
// SAFETY: the caller must uphold the safety contract for
856+
// `unchecked_mul`.
857+
unsafe { intrinsics::unchecked_mul(self, rhs) }
850858
}
851859
}
852860

@@ -2998,7 +3006,9 @@ cannot occur. This results in undefined behavior when `self + rhs > ", stringify
29983006
without modifying the original"]
29993007
#[inline]
30003008
pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
3001-
intrinsics::unchecked_add(self, rhs)
3009+
// SAFETY: the caller must uphold the safety contract for
3010+
// `unchecked_add`.
3011+
unsafe { intrinsics::unchecked_add(self, rhs) }
30023012
}
30033013
}
30043014

@@ -3038,7 +3048,9 @@ cannot occur. This results in undefined behavior when `self - rhs > ", stringify
30383048
without modifying the original"]
30393049
#[inline]
30403050
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
3041-
intrinsics::unchecked_sub(self, rhs)
3051+
// SAFETY: the caller must uphold the safety contract for
3052+
// `unchecked_sub`.
3053+
unsafe { intrinsics::unchecked_sub(self, rhs) }
30423054
}
30433055
}
30443056

@@ -3078,7 +3090,9 @@ cannot occur. This results in undefined behavior when `self * rhs > ", stringify
30783090
without modifying the original"]
30793091
#[inline]
30803092
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
3081-
intrinsics::unchecked_mul(self, rhs)
3093+
// SAFETY: the caller must uphold the safety contract for
3094+
// `unchecked_mul`.
3095+
unsafe { intrinsics::unchecked_mul(self, rhs) }
30823096
}
30833097
}
30843098

0 commit comments

Comments
 (0)