Skip to content

Commit

Permalink
Deny unsafe ops in unsafe fns, part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut committed Jun 30, 2020
1 parent 8a515e9 commit ac7539c
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 113 deletions.
5 changes: 4 additions & 1 deletion src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! new code should use the associated constants directly on the primitive type.

#![stable(feature = "rust1", since = "1.0.0")]
#![deny(unsafe_op_in_unsafe_fn)]

use crate::convert::FloatToInt;
#[cfg(not(test))]
Expand Down Expand Up @@ -629,7 +630,9 @@ impl f32 {
where
Self: FloatToInt<Int>,
{
FloatToInt::<Int>::to_int_unchecked(self)
// SAFETY: the caller must uphold the safety contract for
// `FloatToInt::to_int_unchecked`.
unsafe { FloatToInt::<Int>::to_int_unchecked(self) }
}

/// Raw transmutation to `u32`.
Expand Down
5 changes: 4 additions & 1 deletion src/libcore/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! new code should use the associated constants directly on the primitive type.

#![stable(feature = "rust1", since = "1.0.0")]
#![deny(unsafe_op_in_unsafe_fn)]

use crate::convert::FloatToInt;
#[cfg(not(test))]
Expand Down Expand Up @@ -643,7 +644,9 @@ impl f64 {
where
Self: FloatToInt<Int>,
{
FloatToInt::<Int>::to_int_unchecked(self)
// SAFETY: the caller must uphold the safety contract for
// `FloatToInt::to_int_unchecked`.
unsafe { FloatToInt::<Int>::to_int_unchecked(self) }
}

/// Raw transmutation to `u64`.
Expand Down
28 changes: 21 additions & 7 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Numeric traits and functions for the built-in numeric types.

#![stable(feature = "rust1", since = "1.0.0")]
#![deny(unsafe_op_in_unsafe_fn)]

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

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

Expand Down Expand Up @@ -804,7 +808,9 @@ cannot occur. This results in undefined behavior when `self - rhs > ", stringify
without modifying the original"]
#[inline]
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
intrinsics::unchecked_sub(self, rhs)
// SAFETY: the caller must uphold the safety contract for
// `unchecked_sub`.
unsafe { intrinsics::unchecked_sub(self, rhs) }
}
}

Expand Down Expand Up @@ -846,7 +852,9 @@ cannot occur. This results in undefined behavior when `self * rhs > ", stringify
without modifying the original"]
#[inline]
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
intrinsics::unchecked_mul(self, rhs)
// SAFETY: the caller must uphold the safety contract for
// `unchecked_mul`.
unsafe { intrinsics::unchecked_mul(self, rhs) }
}
}

Expand Down Expand Up @@ -2998,7 +3006,9 @@ cannot occur. This results in undefined behavior when `self + rhs > ", stringify
without modifying the original"]
#[inline]
pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
intrinsics::unchecked_add(self, rhs)
// SAFETY: the caller must uphold the safety contract for
// `unchecked_add`.
unsafe { intrinsics::unchecked_add(self, rhs) }
}
}

Expand Down Expand Up @@ -3038,7 +3048,9 @@ cannot occur. This results in undefined behavior when `self - rhs > ", stringify
without modifying the original"]
#[inline]
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
intrinsics::unchecked_sub(self, rhs)
// SAFETY: the caller must uphold the safety contract for
// `unchecked_sub`.
unsafe { intrinsics::unchecked_sub(self, rhs) }
}
}

Expand Down Expand Up @@ -3078,7 +3090,9 @@ cannot occur. This results in undefined behavior when `self * rhs > ", stringify
without modifying the original"]
#[inline]
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
intrinsics::unchecked_mul(self, rhs)
// SAFETY: the caller must uphold the safety contract for
// `unchecked_mul`.
unsafe { intrinsics::unchecked_mul(self, rhs) }
}
}

Expand Down
Loading

0 comments on commit ac7539c

Please sign in to comment.