Skip to content

Commit 7e7c337

Browse files
committed
stabilize const_int_wrapping.
1 parent a2b0f24 commit 7e7c337

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

src/libcore/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
#![feature(const_str_as_bytes)]
115115
#![feature(const_str_len)]
116116
#![feature(const_int_rotate)]
117-
#![feature(const_int_wrapping)]
118117
#![feature(const_int_sign)]
119118
#![feature(const_int_conversion)]
120119
#![feature(const_transmute)]

src/libcore/num/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ assert_eq!(", stringify!($SelfT), "::max_value().wrapping_add(2), ", stringify!(
994994
$EndFeature, "
995995
```"),
996996
#[stable(feature = "rust1", since = "1.0.0")]
997-
#[rustc_const_unstable(feature = "const_int_wrapping")]
997+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))]
998998
#[inline]
999999
pub const fn wrapping_add(self, rhs: Self) -> Self {
10001000
unsafe {
@@ -1018,7 +1018,7 @@ stringify!($SelfT), "::max_value());",
10181018
$EndFeature, "
10191019
```"),
10201020
#[stable(feature = "rust1", since = "1.0.0")]
1021-
#[rustc_const_unstable(feature = "const_int_wrapping")]
1021+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))]
10221022
#[inline]
10231023
pub const fn wrapping_sub(self, rhs: Self) -> Self {
10241024
unsafe {
@@ -1041,7 +1041,7 @@ assert_eq!(11i8.wrapping_mul(12), -124);",
10411041
$EndFeature, "
10421042
```"),
10431043
#[stable(feature = "rust1", since = "1.0.0")]
1044-
#[rustc_const_unstable(feature = "const_int_wrapping")]
1044+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))]
10451045
#[inline]
10461046
pub const fn wrapping_mul(self, rhs: Self) -> Self {
10471047
unsafe {
@@ -1205,7 +1205,7 @@ assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(128), -1);",
12051205
$EndFeature, "
12061206
```"),
12071207
#[stable(feature = "num_wrapping", since = "1.2.0")]
1208-
#[rustc_const_unstable(feature = "const_int_wrapping")]
1208+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))]
12091209
#[inline]
12101210
pub const fn wrapping_shl(self, rhs: u32) -> Self {
12111211
unsafe {
@@ -1233,7 +1233,7 @@ assert_eq!((-128i16).wrapping_shr(64), -128);",
12331233
$EndFeature, "
12341234
```"),
12351235
#[stable(feature = "num_wrapping", since = "1.2.0")]
1236-
#[rustc_const_unstable(feature = "const_int_wrapping")]
1236+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))]
12371237
#[inline]
12381238
pub const fn wrapping_shr(self, rhs: u32) -> Self {
12391239
unsafe {
@@ -2884,7 +2884,7 @@ assert_eq!(200", stringify!($SelfT), ".wrapping_add(", stringify!($SelfT), "::ma
28842884
$EndFeature, "
28852885
```"),
28862886
#[stable(feature = "rust1", since = "1.0.0")]
2887-
#[rustc_const_unstable(feature = "const_int_wrapping")]
2887+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))]
28882888
#[inline]
28892889
pub const fn wrapping_add(self, rhs: Self) -> Self {
28902890
unsafe {
@@ -2907,7 +2907,7 @@ assert_eq!(100", stringify!($SelfT), ".wrapping_sub(", stringify!($SelfT), "::ma
29072907
$EndFeature, "
29082908
```"),
29092909
#[stable(feature = "rust1", since = "1.0.0")]
2910-
#[rustc_const_unstable(feature = "const_int_wrapping")]
2910+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))]
29112911
#[inline]
29122912
pub const fn wrapping_sub(self, rhs: Self) -> Self {
29132913
unsafe {
@@ -2931,7 +2931,7 @@ $EndFeature, "
29312931
/// assert_eq!(25u8.wrapping_mul(12), 44);
29322932
/// ```
29332933
#[stable(feature = "rust1", since = "1.0.0")]
2934-
#[rustc_const_unstable(feature = "const_int_wrapping")]
2934+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))]
29352935
#[inline]
29362936
pub const fn wrapping_mul(self, rhs: Self) -> Self {
29372937
unsafe {
@@ -3081,7 +3081,7 @@ Basic usage:
30813081
assert_eq!(1", stringify!($SelfT), ".wrapping_shl(128), 1);", $EndFeature, "
30823082
```"),
30833083
#[stable(feature = "num_wrapping", since = "1.2.0")]
3084-
#[rustc_const_unstable(feature = "const_int_wrapping")]
3084+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))]
30853085
#[inline]
30863086
pub const fn wrapping_shl(self, rhs: u32) -> Self {
30873087
unsafe {
@@ -3111,7 +3111,7 @@ Basic usage:
31113111
assert_eq!(128", stringify!($SelfT), ".wrapping_shr(128), 128);", $EndFeature, "
31123112
```"),
31133113
#[stable(feature = "num_wrapping", since = "1.2.0")]
3114-
#[rustc_const_unstable(feature = "const_int_wrapping")]
3114+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))]
31153115
#[inline]
31163116
pub const fn wrapping_shr(self, rhs: u32) -> Self {
31173117
unsafe {

src/librustc_mir/transform/qualify_min_const_fn.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,11 @@ fn check_terminator(
342342
// some intrinsics are waved through if called inside the
343343
// standard library. Users never need to call them directly
344344
match tcx.fn_sig(def_id).abi() {
345-
abi::Abi::RustIntrinsic => match &tcx.item_name(def_id).as_str()[..] {
346-
| "size_of"
347-
| "min_align_of"
348-
| "needs_drop"
349-
=> {},
350-
_ => return Err((
345+
abi::Abi::RustIntrinsic => if !is_intrinsic_whitelisted(tcx, def_id) {
346+
return Err((
351347
span,
352348
"can only call a curated list of intrinsics in `min_const_fn`".into(),
353-
)),
349+
))
354350
},
355351
abi::Abi::Rust if tcx.is_min_const_fn(def_id) => {},
356352
abi::Abi::Rust => return Err((
@@ -390,3 +386,22 @@ fn check_terminator(
390386
},
391387
}
392388
}
389+
390+
/// Returns true if the `def_id` refers to an intrisic which we've whitelisted.
391+
///
392+
/// Adding more intrinsics requires sign-off from @rust-lang/lang.
393+
fn is_intrinsic_whitelisted(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool {
394+
match &tcx.item_name(def_id).as_str()[..] {
395+
| "size_of"
396+
| "min_align_of"
397+
| "needs_drop"
398+
// Arithmetic:
399+
| "overflowing_add" // ~> wrapping_add
400+
| "overflowing_sub" // ~> wrapping_sub
401+
| "overflowing_mul" // ~> wrapping_mul
402+
| "unchecked_shl" // ~> wrapping_shl
403+
| "unchecked_shr" // ~> wrapping_shr
404+
=> true,
405+
_ => false,
406+
}
407+
}

0 commit comments

Comments
 (0)