Skip to content

Commit 047a4bb

Browse files
authored
Rollup merge of #67657 - jumbatm:cleanup-const-hack, r=oli-obk
Clean up const-hack PRs now that const if / match exist. Closes #67627. Cleans up these merged PRs tagged with `const-hack`: - #63810 - #63786 - #61635 - #58044 reverting their contents to have the match or if expressions they originally contained. r? @oli-obk There's one more PR in those tagged with `const-hack` that originally wasn't merged (#65107). Reading the thread, it looks like it was originally closed because the `const-hack` for the checked arithmetic non-negligibly hurt performance, and because there was no way to manipulate the returned Option at compile time anyway (with neither const if nor const match). Would you like me to add these changes to the changes from this PR here too, now that we have the necessary features?
2 parents 0eb19dc + 91c2f78 commit 047a4bb

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/libcore/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
#![feature(cfg_target_has_atomic)]
7272
#![feature(concat_idents)]
7373
#![feature(const_fn)]
74+
#![feature(const_if_match)]
75+
#![feature(const_panic)]
7476
#![feature(const_fn_union)]
7577
#![feature(const_generics)]
7678
#![feature(const_ptr_offset_from)]

src/libcore/num/mod.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -1416,18 +1416,14 @@ $EndFeature, "
14161416
```"),
14171417
#[stable(feature = "no_panic_abs", since = "1.13.0")]
14181418
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1419+
#[allow_internal_unstable(const_if_match)]
14191420
#[inline]
14201421
pub const fn wrapping_abs(self) -> Self {
1421-
// sign is -1 (all ones) for negative numbers, 0 otherwise.
1422-
let sign = self >> ($BITS - 1);
1423-
// For positive self, sign == 0 so the expression is simply
1424-
// (self ^ 0).wrapping_sub(0) == self == abs(self).
1425-
//
1426-
// For negative self, self ^ sign == self ^ all_ones.
1427-
// But all_ones ^ self == all_ones - self == -1 - self.
1428-
// So for negative numbers, (self ^ sign).wrapping_sub(sign) is
1429-
// (-1 - self).wrapping_sub(-1) == -self == abs(self).
1430-
(self ^ sign).wrapping_sub(sign)
1422+
if self.is_negative() {
1423+
self.wrapping_neg()
1424+
} else {
1425+
self
1426+
}
14311427
}
14321428
}
14331429

@@ -1713,8 +1709,13 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self
17131709
#[inline]
17141710
#[stable(feature = "wrapping", since = "1.7.0")]
17151711
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1712+
#[allow_internal_unstable(const_if_match)]
17161713
pub const fn overflowing_neg(self) -> (Self, bool) {
1717-
((!self).wrapping_add(1), self == Self::min_value())
1714+
if self == Self::min_value() {
1715+
(Self::min_value(), true)
1716+
} else {
1717+
(-self, false)
1718+
}
17181719
}
17191720
}
17201721

@@ -2041,7 +2042,11 @@ $EndFeature, "
20412042
#[rustc_const_unstable(feature = "const_int_sign", issue = "53718")]
20422043
#[inline]
20432044
pub const fn signum(self) -> Self {
2044-
(self > 0) as Self - (self < 0) as Self
2045+
match self {
2046+
n if n > 0 => 1,
2047+
0 => 0,
2048+
_ => -1,
2049+
}
20452050
}
20462051
}
20472052

src/libcore/ptr/const_ptr.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,7 @@ impl<T: ?Sized> *const T {
288288
T: Sized,
289289
{
290290
let pointee_size = mem::size_of::<T>();
291-
let ok = 0 < pointee_size && pointee_size <= isize::max_value() as usize;
292-
// assert that the pointee size is valid in a const eval compatible way
293-
// FIXME: do this with a real assert at some point
294-
[()][(!ok) as usize];
291+
assert!(0 < pointee_size && pointee_size <= isize::max_value() as usize);
295292
intrinsics::ptr_offset_from(self, origin)
296293
}
297294

0 commit comments

Comments
 (0)