Skip to content

Commit 4940dd4

Browse files
committed
Auto merge of #80962 - jhpratt:const_int_fn-stabilization, r=dtolnay
Stabilize remaining integer methods as `const fn` This pull request stabilizes the following methods as `const fn`: - `i*::checked_div` - `i*::checked_div_euclid` - `i*::checked_rem` - `i*::checked_rem_euclid` - `i*::div_euclid` - `i*::overflowing_div` - `i*::overflowing_div_euclid` - `i*::overflowing_rem` - `i*::overflowing_rem_euclid` - `i*::rem_euclid` - `i*::wrapping_div` - `i*::wrapping_div_euclid` - `i*::wrapping_rem` - `i*::wrapping_rem_euclid` - `u*::checked_div` - `u*::checked_div_euclid` - `u*::checked_rem` - `u*::checked_rem_euclid` - `u*::div_euclid` - `u*::overflowing_div` - `u*::overflowing_div_euclid` - `u*::overflowing_rem` - `u*::overflowing_rem_euclid` - `u*::rem_euclid` - `u*::wrapping_div` - `u*::wrapping_div_euclid` - `u*::wrapping_rem` - `u*::wrapping_rem_euclid` These can all be implemented on the current stable (1.49). There are two unstable details: const likely/unlikely and unchecked division/remainder. Both of these are for optimizations, and are in no way required to make the methods function; there is no exposure of these details publicly. Per comments below, it seems best practice is to stabilize the intrinsics. As such, `intrinsics::unchecked_div` and `intrinsics::unchecked_rem` have been stabilized as `const` as part of this pull request as well. The methods themselves remain unstable. I believe part of the reason these were not stabilized previously was the behavior around division by 0 and modulo 0. After testing on nightly, the diagnostic for something like `const _: i8 = 5i8 % 0i8;` is similar to that of `const _: i8 = 5i8.rem_euclid(0i8);` (assuming the appropriate feature flag is enabled). As such, I believe these methods are ready to be stabilized as `const fn`. This pull request represents the final methods mentioned in #53718. As such, this PR closes #53718. `@rustbot` modify labels to +A-const-fn, +T-libs
2 parents 0b7a598 + f55029a commit 4940dd4

File tree

5 files changed

+30
-38
lines changed

5 files changed

+30
-38
lines changed

Diff for: library/core/src/intrinsics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1586,15 +1586,15 @@ extern "rust-intrinsic" {
15861586
/// Safe wrappers for this intrinsic are available on the integer
15871587
/// primitives via the `checked_div` method. For example,
15881588
/// [`u32::checked_div`]
1589-
#[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1589+
#[rustc_const_stable(feature = "const_int_unchecked_arith", since = "1.51.0")]
15901590
pub fn unchecked_div<T: Copy>(x: T, y: T) -> T;
15911591
/// Returns the remainder of an unchecked division, resulting in
15921592
/// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
15931593
///
15941594
/// Safe wrappers for this intrinsic are available on the integer
15951595
/// primitives via the `checked_rem` method. For example,
15961596
/// [`u32::checked_rem`]
1597-
#[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1597+
#[rustc_const_stable(feature = "const_int_unchecked_arith", since = "1.51.0")]
15981598
pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
15991599

16001600
/// Performs an unchecked left shift, resulting in undefined behavior when

Diff for: library/core/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,8 @@
7373
#![feature(const_discriminant)]
7474
#![feature(const_cell_into_inner)]
7575
#![feature(const_intrinsic_copy)]
76-
#![feature(const_checked_int_methods)]
77-
#![feature(const_euclidean_int_methods)]
7876
#![feature(const_float_classify)]
7977
#![feature(const_float_bits_conv)]
80-
#![feature(const_overflowing_int_methods)]
8178
#![feature(const_int_unchecked_arith)]
8279
#![feature(const_mut_refs)]
8380
#![feature(const_cttz)]

Diff for: library/core/src/num/int_macros.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ macro_rules! int_impl {
513513
#[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);")]
514514
/// ```
515515
#[stable(feature = "rust1", since = "1.0.0")]
516-
#[rustc_const_unstable(feature = "const_checked_int_methods", issue = "53718")]
516+
#[rustc_const_stable(feature = "const_checked_int_methods", since = "1.51.0")]
517517
#[must_use = "this returns the result of the operation, \
518518
without modifying the original"]
519519
#[inline]
@@ -539,7 +539,7 @@ macro_rules! int_impl {
539539
#[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);")]
540540
/// ```
541541
#[stable(feature = "euclidean_division", since = "1.38.0")]
542-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
542+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
543543
#[must_use = "this returns the result of the operation, \
544544
without modifying the original"]
545545
#[inline]
@@ -565,7 +565,7 @@ macro_rules! int_impl {
565565
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);")]
566566
/// ```
567567
#[stable(feature = "wrapping", since = "1.7.0")]
568-
#[rustc_const_unstable(feature = "const_checked_int_methods", issue = "53718")]
568+
#[rustc_const_stable(feature = "const_checked_int_methods", since = "1.51.0")]
569569
#[must_use = "this returns the result of the operation, \
570570
without modifying the original"]
571571
#[inline]
@@ -591,7 +591,7 @@ macro_rules! int_impl {
591591
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);")]
592592
/// ```
593593
#[stable(feature = "euclidean_division", since = "1.38.0")]
594-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
594+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
595595
#[must_use = "this returns the result of the operation, \
596596
without modifying the original"]
597597
#[inline]
@@ -949,7 +949,7 @@ macro_rules! int_impl {
949949
/// assert_eq!((-128i8).wrapping_div(-1), -128);
950950
/// ```
951951
#[stable(feature = "num_wrapping", since = "1.2.0")]
952-
#[rustc_const_unstable(feature = "const_wrapping_int_methods", issue = "53718")]
952+
#[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.51.0")]
953953
#[must_use = "this returns the result of the operation, \
954954
without modifying the original"]
955955
#[inline]
@@ -977,7 +977,7 @@ macro_rules! int_impl {
977977
/// assert_eq!((-128i8).wrapping_div_euclid(-1), -128);
978978
/// ```
979979
#[stable(feature = "euclidean_division", since = "1.38.0")]
980-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
980+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
981981
#[must_use = "this returns the result of the operation, \
982982
without modifying the original"]
983983
#[inline]
@@ -1005,7 +1005,7 @@ macro_rules! int_impl {
10051005
/// assert_eq!((-128i8).wrapping_rem(-1), 0);
10061006
/// ```
10071007
#[stable(feature = "num_wrapping", since = "1.2.0")]
1008-
#[rustc_const_unstable(feature = "const_wrapping_int_methods", issue = "53718")]
1008+
#[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.51.0")]
10091009
#[must_use = "this returns the result of the operation, \
10101010
without modifying the original"]
10111011
#[inline]
@@ -1032,7 +1032,7 @@ macro_rules! int_impl {
10321032
/// assert_eq!((-128i8).wrapping_rem_euclid(-1), 0);
10331033
/// ```
10341034
#[stable(feature = "euclidean_division", since = "1.38.0")]
1035-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1035+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
10361036
#[must_use = "this returns the result of the operation, \
10371037
without modifying the original"]
10381038
#[inline]
@@ -1299,7 +1299,7 @@ macro_rules! int_impl {
12991299
/// ```
13001300
#[inline]
13011301
#[stable(feature = "wrapping", since = "1.7.0")]
1302-
#[rustc_const_unstable(feature = "const_overflowing_int_methods", issue = "53718")]
1302+
#[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.51.0")]
13031303
#[must_use = "this returns the result of the operation, \
13041304
without modifying the original"]
13051305
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
@@ -1329,7 +1329,7 @@ macro_rules! int_impl {
13291329
/// ```
13301330
#[inline]
13311331
#[stable(feature = "euclidean_division", since = "1.38.0")]
1332-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1332+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
13331333
#[must_use = "this returns the result of the operation, \
13341334
without modifying the original"]
13351335
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
@@ -1360,7 +1360,7 @@ macro_rules! int_impl {
13601360
/// ```
13611361
#[inline]
13621362
#[stable(feature = "wrapping", since = "1.7.0")]
1363-
#[rustc_const_unstable(feature = "const_overflowing_int_methods", issue = "53718")]
1363+
#[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.51.0")]
13641364
#[must_use = "this returns the result of the operation, \
13651365
without modifying the original"]
13661366
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
@@ -1390,7 +1390,7 @@ macro_rules! int_impl {
13901390
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
13911391
/// ```
13921392
#[stable(feature = "euclidean_division", since = "1.38.0")]
1393-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1393+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
13941394
#[must_use = "this returns the result of the operation, \
13951395
without modifying the original"]
13961396
#[inline]
@@ -1615,7 +1615,7 @@ macro_rules! int_impl {
16151615
/// assert_eq!((-a).div_euclid(-b), 2); // -7 >= -4 * 2
16161616
/// ```
16171617
#[stable(feature = "euclidean_division", since = "1.38.0")]
1618-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1618+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
16191619
#[must_use = "this returns the result of the operation, \
16201620
without modifying the original"]
16211621
#[inline]
@@ -1653,7 +1653,7 @@ macro_rules! int_impl {
16531653
/// assert_eq!((-a).rem_euclid(-b), 1);
16541654
/// ```
16551655
#[stable(feature = "euclidean_division", since = "1.38.0")]
1656-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1656+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
16571657
#[must_use = "this returns the result of the operation, \
16581658
without modifying the original"]
16591659
#[inline]

Diff for: library/core/src/num/uint_macros.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ macro_rules! uint_impl {
522522
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_div(0), None);")]
523523
/// ```
524524
#[stable(feature = "rust1", since = "1.0.0")]
525-
#[rustc_const_unstable(feature = "const_checked_int_methods", issue = "53718")]
525+
#[rustc_const_stable(feature = "const_checked_int_methods", since = "1.51.0")]
526526
#[must_use = "this returns the result of the operation, \
527527
without modifying the original"]
528528
#[inline]
@@ -548,7 +548,7 @@ macro_rules! uint_impl {
548548
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_div_euclid(0), None);")]
549549
/// ```
550550
#[stable(feature = "euclidean_division", since = "1.38.0")]
551-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
551+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
552552
#[must_use = "this returns the result of the operation, \
553553
without modifying the original"]
554554
#[inline]
@@ -573,7 +573,7 @@ macro_rules! uint_impl {
573573
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
574574
/// ```
575575
#[stable(feature = "wrapping", since = "1.7.0")]
576-
#[rustc_const_unstable(feature = "const_checked_int_methods", issue = "53718")]
576+
#[rustc_const_stable(feature = "const_checked_int_methods", since = "1.51.0")]
577577
#[must_use = "this returns the result of the operation, \
578578
without modifying the original"]
579579
#[inline]
@@ -599,7 +599,7 @@ macro_rules! uint_impl {
599599
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
600600
/// ```
601601
#[stable(feature = "euclidean_division", since = "1.38.0")]
602-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
602+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
603603
#[must_use = "this returns the result of the operation, \
604604
without modifying the original"]
605605
#[inline]
@@ -876,7 +876,7 @@ macro_rules! uint_impl {
876876
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
877877
/// ```
878878
#[stable(feature = "num_wrapping", since = "1.2.0")]
879-
#[rustc_const_unstable(feature = "const_wrapping_int_methods", issue = "53718")]
879+
#[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.51.0")]
880880
#[must_use = "this returns the result of the operation, \
881881
without modifying the original"]
882882
#[inline]
@@ -901,7 +901,7 @@ macro_rules! uint_impl {
901901
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
902902
/// ```
903903
#[stable(feature = "euclidean_division", since = "1.38.0")]
904-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
904+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
905905
#[must_use = "this returns the result of the operation, \
906906
without modifying the original"]
907907
#[inline]
@@ -924,7 +924,7 @@ macro_rules! uint_impl {
924924
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
925925
/// ```
926926
#[stable(feature = "num_wrapping", since = "1.2.0")]
927-
#[rustc_const_unstable(feature = "const_wrapping_int_methods", issue = "53718")]
927+
#[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.51.0")]
928928
#[must_use = "this returns the result of the operation, \
929929
without modifying the original"]
930930
#[inline]
@@ -950,7 +950,7 @@ macro_rules! uint_impl {
950950
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
951951
/// ```
952952
#[stable(feature = "euclidean_division", since = "1.38.0")]
953-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
953+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
954954
#[must_use = "this returns the result of the operation, \
955955
without modifying the original"]
956956
#[inline]
@@ -1185,7 +1185,7 @@ macro_rules! uint_impl {
11851185
/// ```
11861186
#[inline]
11871187
#[stable(feature = "wrapping", since = "1.7.0")]
1188-
#[rustc_const_unstable(feature = "const_overflowing_int_methods", issue = "53718")]
1188+
#[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.51.0")]
11891189
#[must_use = "this returns the result of the operation, \
11901190
without modifying the original"]
11911191
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
@@ -1215,7 +1215,7 @@ macro_rules! uint_impl {
12151215
/// ```
12161216
#[inline]
12171217
#[stable(feature = "euclidean_division", since = "1.38.0")]
1218-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1218+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
12191219
#[must_use = "this returns the result of the operation, \
12201220
without modifying the original"]
12211221
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
@@ -1242,7 +1242,7 @@ macro_rules! uint_impl {
12421242
/// ```
12431243
#[inline]
12441244
#[stable(feature = "wrapping", since = "1.7.0")]
1245-
#[rustc_const_unstable(feature = "const_overflowing_int_methods", issue = "53718")]
1245+
#[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.51.0")]
12461246
#[must_use = "this returns the result of the operation, \
12471247
without modifying the original"]
12481248
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
@@ -1272,7 +1272,7 @@ macro_rules! uint_impl {
12721272
/// ```
12731273
#[inline]
12741274
#[stable(feature = "euclidean_division", since = "1.38.0")]
1275-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1275+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
12761276
#[must_use = "this returns the result of the operation, \
12771277
without modifying the original"]
12781278
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
@@ -1456,7 +1456,7 @@ macro_rules! uint_impl {
14561456
#[doc = concat!("assert_eq!(7", stringify!($SelfT), ".div_euclid(4), 1); // or any other integer type")]
14571457
/// ```
14581458
#[stable(feature = "euclidean_division", since = "1.38.0")]
1459-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1459+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
14601460
#[must_use = "this returns the result of the operation, \
14611461
without modifying the original"]
14621462
#[inline]
@@ -1484,7 +1484,7 @@ macro_rules! uint_impl {
14841484
#[doc = concat!("assert_eq!(7", stringify!($SelfT), ".rem_euclid(4), 3); // or any other integer type")]
14851485
/// ```
14861486
#[stable(feature = "euclidean_division", since = "1.38.0")]
1487-
#[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1487+
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.51.0")]
14881488
#[must_use = "this returns the result of the operation, \
14891489
without modifying the original"]
14901490
#[inline]

Diff for: src/test/ui/consts/const-int-arithmetic.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
// run-pass
22

3-
#![feature(const_checked_int_methods)]
4-
#![feature(const_euclidean_int_methods)]
5-
#![feature(const_overflowing_int_methods)]
6-
#![feature(const_wrapping_int_methods)]
7-
83
macro_rules! suite {
94
($(
105
$fn:ident -> $ty:ty { $( $label:ident : $expr:expr, $result:expr; )* }

0 commit comments

Comments
 (0)