Skip to content

Commit 4260757

Browse files
committed
Make missed precondition-free float intrinsics safe
1 parent f6092f2 commit 4260757

File tree

6 files changed

+22
-19
lines changed

6 files changed

+22
-19
lines changed

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
9090
| sym::contract_check_ensures
9191
| sym::contract_check_requires
9292
| sym::contract_checks
93+
| sym::copysignf16
94+
| sym::copysignf32
95+
| sym::copysignf64
96+
| sym::copysignf128
9397
| sym::cosf16
9498
| sym::cosf32
9599
| sym::cosf64
@@ -106,6 +110,10 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
106110
| sym::expf32
107111
| sym::expf64
108112
| sym::expf128
113+
| sym::fabsf16
114+
| sym::fabsf32
115+
| sym::fabsf64
116+
| sym::fabsf128
109117
| sym::fadd_algebraic
110118
| sym::fdiv_algebraic
111119
| sym::floorf16

library/core/src/intrinsics/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,7 +3170,7 @@ pub const fn maximumf128(x: f128, y: f128) -> f128 {
31703170
/// [`f16::abs`](../../std/primitive.f16.html#method.abs)
31713171
#[rustc_nounwind]
31723172
#[rustc_intrinsic]
3173-
pub const unsafe fn fabsf16(x: f16) -> f16;
3173+
pub const fn fabsf16(x: f16) -> f16;
31743174

31753175
/// Returns the absolute value of an `f32`.
31763176
///
@@ -3179,7 +3179,7 @@ pub const unsafe fn fabsf16(x: f16) -> f16;
31793179
#[rustc_nounwind]
31803180
#[rustc_intrinsic_const_stable_indirect]
31813181
#[rustc_intrinsic]
3182-
pub const unsafe fn fabsf32(x: f32) -> f32;
3182+
pub const fn fabsf32(x: f32) -> f32;
31833183

31843184
/// Returns the absolute value of an `f64`.
31853185
///
@@ -3188,23 +3188,23 @@ pub const unsafe fn fabsf32(x: f32) -> f32;
31883188
#[rustc_nounwind]
31893189
#[rustc_intrinsic_const_stable_indirect]
31903190
#[rustc_intrinsic]
3191-
pub const unsafe fn fabsf64(x: f64) -> f64;
3191+
pub const fn fabsf64(x: f64) -> f64;
31923192

31933193
/// Returns the absolute value of an `f128`.
31943194
///
31953195
/// The stabilized version of this intrinsic is
31963196
/// [`f128::abs`](../../std/primitive.f128.html#method.abs)
31973197
#[rustc_nounwind]
31983198
#[rustc_intrinsic]
3199-
pub const unsafe fn fabsf128(x: f128) -> f128;
3199+
pub const fn fabsf128(x: f128) -> f128;
32003200

32013201
/// Copies the sign from `y` to `x` for `f16` values.
32023202
///
32033203
/// The stabilized version of this intrinsic is
32043204
/// [`f16::copysign`](../../std/primitive.f16.html#method.copysign)
32053205
#[rustc_nounwind]
32063206
#[rustc_intrinsic]
3207-
pub const unsafe fn copysignf16(x: f16, y: f16) -> f16;
3207+
pub const fn copysignf16(x: f16, y: f16) -> f16;
32083208

32093209
/// Copies the sign from `y` to `x` for `f32` values.
32103210
///
@@ -3213,23 +3213,23 @@ pub const unsafe fn copysignf16(x: f16, y: f16) -> f16;
32133213
#[rustc_nounwind]
32143214
#[rustc_intrinsic_const_stable_indirect]
32153215
#[rustc_intrinsic]
3216-
pub const unsafe fn copysignf32(x: f32, y: f32) -> f32;
3216+
pub const fn copysignf32(x: f32, y: f32) -> f32;
32173217
/// Copies the sign from `y` to `x` for `f64` values.
32183218
///
32193219
/// The stabilized version of this intrinsic is
32203220
/// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
32213221
#[rustc_nounwind]
32223222
#[rustc_intrinsic_const_stable_indirect]
32233223
#[rustc_intrinsic]
3224-
pub const unsafe fn copysignf64(x: f64, y: f64) -> f64;
3224+
pub const fn copysignf64(x: f64, y: f64) -> f64;
32253225

32263226
/// Copies the sign from `y` to `x` for `f128` values.
32273227
///
32283228
/// The stabilized version of this intrinsic is
32293229
/// [`f128::copysign`](../../std/primitive.f128.html#method.copysign)
32303230
#[rustc_nounwind]
32313231
#[rustc_intrinsic]
3232-
pub const unsafe fn copysignf128(x: f128, y: f128) -> f128;
3232+
pub const fn copysignf128(x: f128, y: f128) -> f128;
32333233

32343234
/// Generates the LLVM body for the automatic differentiation of `f` using Enzyme,
32353235
/// with `df` as the derivative function and `args` as its arguments.

library/core/src/num/f128.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,7 @@ impl f128 {
13661366
#[rustc_const_unstable(feature = "f128", issue = "116909")]
13671367
#[must_use = "method returns a new number and does not mutate the original value"]
13681368
pub const fn copysign(self, sign: f128) -> f128 {
1369-
// SAFETY: this is actually a safe intrinsic
1370-
unsafe { intrinsics::copysignf128(self, sign) }
1369+
intrinsics::copysignf128(self, sign)
13711370
}
13721371

13731372
/// Float addition that allows optimizations based on algebraic rules.

library/core/src/num/f16.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,7 @@ impl f16 {
13431343
#[rustc_const_unstable(feature = "f16", issue = "116909")]
13441344
#[must_use = "method returns a new number and does not mutate the original value"]
13451345
pub const fn copysign(self, sign: f16) -> f16 {
1346-
// SAFETY: this is actually a safe intrinsic
1347-
unsafe { intrinsics::copysignf16(self, sign) }
1346+
intrinsics::copysignf16(self, sign)
13481347
}
13491348

13501349
/// Float addition that allows optimizations based on algebraic rules.

library/core/src/num/f32.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ impl f32 {
14501450
#[inline]
14511451
pub const fn abs(self) -> f32 {
14521452
// SAFETY: this is actually a safe intrinsic
1453-
unsafe { intrinsics::fabsf32(self) }
1453+
intrinsics::fabsf32(self)
14541454
}
14551455

14561456
/// Returns a number that represents the sign of `self`.
@@ -1508,8 +1508,7 @@ impl f32 {
15081508
#[stable(feature = "copysign", since = "1.35.0")]
15091509
#[rustc_const_stable(feature = "const_float_methods", since = "1.85.0")]
15101510
pub const fn copysign(self, sign: f32) -> f32 {
1511-
// SAFETY: this is actually a safe intrinsic
1512-
unsafe { intrinsics::copysignf32(self, sign) }
1511+
intrinsics::copysignf32(self, sign)
15131512
}
15141513

15151514
/// Float addition that allows optimizations based on algebraic rules.

library/core/src/num/f64.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,8 +1447,7 @@ impl f64 {
14471447
#[rustc_const_stable(feature = "const_float_methods", since = "1.85.0")]
14481448
#[inline]
14491449
pub const fn abs(self) -> f64 {
1450-
// SAFETY: this is actually a safe intrinsic
1451-
unsafe { intrinsics::fabsf64(self) }
1450+
intrinsics::fabsf64(self)
14521451
}
14531452

14541453
/// Returns a number that represents the sign of `self`.
@@ -1506,8 +1505,7 @@ impl f64 {
15061505
#[rustc_const_stable(feature = "const_float_methods", since = "1.85.0")]
15071506
#[inline]
15081507
pub const fn copysign(self, sign: f64) -> f64 {
1509-
// SAFETY: this is actually a safe intrinsic
1510-
unsafe { intrinsics::copysignf64(self, sign) }
1508+
intrinsics::copysignf64(self, sign)
15111509
}
15121510

15131511
/// Float addition that allows optimizations based on algebraic rules.

0 commit comments

Comments
 (0)