Skip to content

Commit

Permalink
Rollup merge of rust-lang#68809 - ecstatic-morse:const-int-functions,…
Browse files Browse the repository at this point in the history
… r=oli-obk

Make more arithmetic functions unstably const

This is a smaller version of rust-lang#66884 (thanks @9999years) that constifies many of the arithmetic functions on integer primitives from rust-lang#53718 that were blocked on rust-lang#49146.

This makes the following things unstably const.

- `feature = const_int_unchecked_arith`
  - `intrinsics::unchecked_add`
  - `intrinsics::unchecked_sub`
  - `intrinsics::unchecked_mul`
  - `intrinsics::unchecked_div`
  - `intrinsics::unchecked_rem`

- `feature = const_checked_int_methods`
  - `checked_add`
  - `checked_sub`
  - `checked_mul`
  - `checked_div` (Uses `intrinsics::unchecked_div` internally)
  - `checked_rem` (Uses `intrinsics::unchecked_rem` internally)
  - `checked_neg`
  - `checked_shl`
  - `checked_shr`
  - `checked_abs`

- `feature = const_saturating_int_methods`
  - `saturating_mul`
  - `saturating_neg`  (Uses `intrinsics::unchecked_sub` internally)
  - `saturating_abs` (Uses `intrinsics::unchecked_sub` internally)

- `feature = const_wrapping_int_methods`
  - `wrapping_div`
  - `wrapping_rem`

- `feature = const_overflowing_int_methods`
  - `overflowing_div`
  - `overflowing_rem`

- `feature = const_euclidean_int_methods`
  - `checked_div_euclid`
  - `checked_rem_euclid`
  - `wrapping_div_euclid`
  - `wrapping_rem_euclid`
  - `overflowing_div_euclid`
  - `overflowing_rem_euclid`

Exponentiation and operations on the `NonZero` types are left to a later PR.

r? @oli-obk
cc @rust-lang/wg-const-eval @rust-lang/libs
  • Loading branch information
Dylan-DPC authored Feb 5, 2020
2 parents c177941 + 78f8ad3 commit 16555ac
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 51 deletions.
5 changes: 5 additions & 0 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,9 +1305,11 @@ extern "rust-intrinsic" {

/// Performs an unchecked division, resulting in undefined behavior
/// where y = 0 or x = `T::min_value()` and y = -1
#[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
pub fn unchecked_div<T>(x: T, y: T) -> T;
/// Returns the remainder of an unchecked division, resulting in
/// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
#[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
pub fn unchecked_rem<T>(x: T, y: T) -> T;

/// Performs an unchecked left shift, resulting in undefined behavior when
Expand All @@ -1321,14 +1323,17 @@ extern "rust-intrinsic" {

/// Returns the result of an unchecked addition, resulting in
/// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`.
#[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
pub fn unchecked_add<T>(x: T, y: T) -> T;

/// Returns the result of an unchecked subtraction, resulting in
/// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`.
#[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
pub fn unchecked_sub<T>(x: T, y: T) -> T;

/// Returns the result of an unchecked multiplication, resulting in
/// undefined behavior when `x * y > T::max_value()` or `x * y < T::min_value()`.
#[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
pub fn unchecked_mul<T>(x: T, y: T) -> T;

/// Performs rotate left.
Expand Down
5 changes: 5 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
#![feature(concat_idents)]
#![feature(const_alloc_layout)]
#![feature(const_if_match)]
#![feature(const_checked_int_methods)]
#![feature(const_euclidean_int_methods)]
#![feature(const_overflowing_int_methods)]
#![feature(const_saturating_int_methods)]
#![feature(const_int_unchecked_arith)]
#![feature(const_panic)]
#![feature(const_fn_union)]
#![feature(const_generics)]
Expand Down
Loading

0 comments on commit 16555ac

Please sign in to comment.