Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up const-hack PRs now that const if / match exist. #67657

Merged
merged 4 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
#![feature(cfg_target_has_atomic)]
#![feature(concat_idents)]
#![feature(const_fn)]
#![feature(const_if_match)]
#![feature(const_panic)]
#![feature(const_fn_union)]
#![feature(const_generics)]
#![feature(const_ptr_offset_from)]
Expand Down
29 changes: 17 additions & 12 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,18 +1416,14 @@ $EndFeature, "
```"),
#[stable(feature = "no_panic_abs", since = "1.13.0")]
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
#[allow_internal_unstable(const_if_match)]
Centril marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
pub const fn wrapping_abs(self) -> Self {
// sign is -1 (all ones) for negative numbers, 0 otherwise.
let sign = self >> ($BITS - 1);
// For positive self, sign == 0 so the expression is simply
// (self ^ 0).wrapping_sub(0) == self == abs(self).
//
// For negative self, self ^ sign == self ^ all_ones.
// But all_ones ^ self == all_ones - self == -1 - self.
// So for negative numbers, (self ^ sign).wrapping_sub(sign) is
// (-1 - self).wrapping_sub(-1) == -self == abs(self).
(self ^ sign).wrapping_sub(sign)
if self.is_negative() {
self.wrapping_neg()
} else {
self
}
}
}

Expand Down Expand Up @@ -1713,8 +1709,13 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
#[allow_internal_unstable(const_if_match)]
pub const fn overflowing_neg(self) -> (Self, bool) {
((!self).wrapping_add(1), self == Self::min_value())
if self == Self::min_value() {
(Self::min_value(), true)
} else {
(-self, false)
}
}
}

Expand Down Expand Up @@ -2041,7 +2042,11 @@ $EndFeature, "
#[rustc_const_unstable(feature = "const_int_sign", issue = "53718")]
#[inline]
pub const fn signum(self) -> Self {
(self > 0) as Self - (self < 0) as Self
match self {
n if n > 0 => 1,
0 => 0,
_ => -1,
}
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/libcore/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,7 @@ impl<T: ?Sized> *const T {
T: Sized,
{
let pointee_size = mem::size_of::<T>();
let ok = 0 < pointee_size && pointee_size <= isize::max_value() as usize;
// assert that the pointee size is valid in a const eval compatible way
// FIXME: do this with a real assert at some point
[()][(!ok) as usize];
assert!(0 < pointee_size && pointee_size <= isize::max_value() as usize);
intrinsics::ptr_offset_from(self, origin)
}

Expand Down