diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2018-12-24 18:53:28 +0100 |
|---|---|---|
| committer | Mazdak Farrokhzad <twingoow@gmail.com> | 2018-12-31 03:36:46 +0100 |
| commit | 7e7c337aef6e87f6152771351eaed32b31d666f7 (patch) | |
| tree | efaa2a1144787c3045440f8176fc2858aa20cf71 | |
| parent | a2b0f247bf741a1a9729363dda8628a938f1fe58 (diff) | |
| download | rust-7e7c337aef6e87f6152771351eaed32b31d666f7.tar.gz rust-7e7c337aef6e87f6152771351eaed32b31d666f7.zip | |
stabilize const_int_wrapping.
| -rw-r--r-- | src/libcore/lib.rs | 1 | ||||
| -rw-r--r-- | src/libcore/num/mod.rs | 20 | ||||
| -rw-r--r-- | src/librustc_mir/transform/qualify_min_const_fn.rs | 29 |
3 files changed, 32 insertions, 18 deletions
diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 5ea765d3585..a613c1afd09 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -114,7 +114,6 @@ #![feature(const_str_as_bytes)] #![feature(const_str_len)] #![feature(const_int_rotate)] -#![feature(const_int_wrapping)] #![feature(const_int_sign)] #![feature(const_int_conversion)] #![feature(const_transmute)] diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 97bf582df5a..58ea651aa83 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -994,7 +994,7 @@ assert_eq!(", stringify!($SelfT), "::max_value().wrapping_add(2), ", stringify!( $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_int_wrapping")] + #[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))] #[inline] pub const fn wrapping_add(self, rhs: Self) -> Self { unsafe { @@ -1018,7 +1018,7 @@ stringify!($SelfT), "::max_value());", $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_int_wrapping")] + #[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))] #[inline] pub const fn wrapping_sub(self, rhs: Self) -> Self { unsafe { @@ -1041,7 +1041,7 @@ assert_eq!(11i8.wrapping_mul(12), -124);", $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_int_wrapping")] + #[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))] #[inline] pub const fn wrapping_mul(self, rhs: Self) -> Self { unsafe { @@ -1205,7 +1205,7 @@ assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(128), -1);", $EndFeature, " ```"), #[stable(feature = "num_wrapping", since = "1.2.0")] - #[rustc_const_unstable(feature = "const_int_wrapping")] + #[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))] #[inline] pub const fn wrapping_shl(self, rhs: u32) -> Self { unsafe { @@ -1233,7 +1233,7 @@ assert_eq!((-128i16).wrapping_shr(64), -128);", $EndFeature, " ```"), #[stable(feature = "num_wrapping", since = "1.2.0")] - #[rustc_const_unstable(feature = "const_int_wrapping")] + #[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))] #[inline] pub const fn wrapping_shr(self, rhs: u32) -> Self { unsafe { @@ -2884,7 +2884,7 @@ assert_eq!(200", stringify!($SelfT), ".wrapping_add(", stringify!($SelfT), "::ma $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_int_wrapping")] + #[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))] #[inline] pub const fn wrapping_add(self, rhs: Self) -> Self { unsafe { @@ -2907,7 +2907,7 @@ assert_eq!(100", stringify!($SelfT), ".wrapping_sub(", stringify!($SelfT), "::ma $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_int_wrapping")] + #[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))] #[inline] pub const fn wrapping_sub(self, rhs: Self) -> Self { unsafe { @@ -2931,7 +2931,7 @@ $EndFeature, " /// assert_eq!(25u8.wrapping_mul(12), 44); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_int_wrapping")] + #[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))] #[inline] pub const fn wrapping_mul(self, rhs: Self) -> Self { unsafe { @@ -3081,7 +3081,7 @@ Basic usage: assert_eq!(1", stringify!($SelfT), ".wrapping_shl(128), 1);", $EndFeature, " ```"), #[stable(feature = "num_wrapping", since = "1.2.0")] - #[rustc_const_unstable(feature = "const_int_wrapping")] + #[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))] #[inline] pub const fn wrapping_shl(self, rhs: u32) -> Self { unsafe { @@ -3111,7 +3111,7 @@ Basic usage: assert_eq!(128", stringify!($SelfT), ".wrapping_shr(128), 128);", $EndFeature, " ```"), #[stable(feature = "num_wrapping", since = "1.2.0")] - #[rustc_const_unstable(feature = "const_int_wrapping")] + #[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_wrapping"))] #[inline] pub const fn wrapping_shr(self, rhs: u32) -> Self { unsafe { diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index c1c5b18915a..f271fbaa55b 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -342,15 +342,11 @@ fn check_terminator( // some intrinsics are waved through if called inside the // standard library. Users never need to call them directly match tcx.fn_sig(def_id).abi() { - abi::Abi::RustIntrinsic => match &tcx.item_name(def_id).as_str()[..] { - | "size_of" - | "min_align_of" - | "needs_drop" - => {}, - _ => return Err(( + abi::Abi::RustIntrinsic => if !is_intrinsic_whitelisted(tcx, def_id) { + return Err(( span, "can only call a curated list of intrinsics in `min_const_fn`".into(), - )), + )) }, abi::Abi::Rust if tcx.is_min_const_fn(def_id) => {}, abi::Abi::Rust => return Err(( @@ -390,3 +386,22 @@ fn check_terminator( }, } } + +/// Returns true if the `def_id` refers to an intrisic which we've whitelisted. +/// +/// Adding more intrinsics requires sign-off from @rust-lang/lang. +fn is_intrinsic_whitelisted(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool { + match &tcx.item_name(def_id).as_str()[..] { + | "size_of" + | "min_align_of" + | "needs_drop" + // Arithmetic: + | "overflowing_add" // ~> wrapping_add + | "overflowing_sub" // ~> wrapping_sub + | "overflowing_mul" // ~> wrapping_mul + | "unchecked_shl" // ~> wrapping_shl + | "unchecked_shr" // ~> wrapping_shr + => true, + _ => false, + } +} |
