]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #55278 - Centril:constification-1, r=alexcrichton
authorbors <bors@rust-lang.org>
Mon, 12 Nov 2018 18:54:11 +0000 (18:54 +0000)
committerbors <bors@rust-lang.org>
Mon, 12 Nov 2018 18:54:11 +0000 (18:54 +0000)
Minor standard library constification

This PR makes some bits of the standard library into `const fn`s.
I've tried to be as aggressive as I possibly could in the constification.
The list is rather small due to how restrictive `const fn` is at the moment.

r? @oli-obk cc @rust-lang/libs

Stable public APIs affected:
+ [x] `Cell::as_ptr`
+ [x] `UnsafeCell::get`
+ [x] `char::is_ascii`
+ [x] `iter::empty`
+ [x] `ManuallyDrop::{new, into_inner}`
+ [x] `RangeInclusive::{start, end}`
+ [x] `NonNull::as_ptr`
+ [x] `{[T], str}::as_ptr`
+ [x] `Duration::{as_secs, subsec_millis, subsec_micros, subsec_nanos}`
+ [x] `CStr::as_ptr`
+ [x] `Ipv4Addr::is_unspecified`
+ [x] `Ipv6Addr::new`
+ [x] `Ipv6Addr::octets`

Unstable public APIs affected:
+ [x] `Duration::{as_millis, as_micros, as_nanos, as_float_secs}`
+ [x] `Wrapping::{count_ones, count_zeros, trailing_zeros, rotate_left, rotate_right, swap_bytes, reverse_bits, from_be, from_le, to_be, to_le, leading_zeros, is_positive, is_negative, leading_zeros}`
+ [x] `core::convert::identity`

--------------------------

## Removed from list in first pass:

Stable public APIs affected:
+ [ ] `BTree{Map, Set}::{len, is_empty}`
+ [ ] `VecDeque::is_empty`
+ [ ] `String::{is_empty, len}`
+ [ ] `FromUtf8Error::utf8_error`
+ [ ] `Vec<T>::{is_empty, len}`
+ [ ] `Layout::size`
+ [ ] `DecodeUtf16Error::unpaired_surrogate`
+ [ ] `core::fmt::{fill, width, precision, sign_plus, sign_minus, alternate, sign_aware_zero_pad}`
+ [ ] `panic::Location::{file, line, column}`
+ [ ] `{ChunksExact, RChunksExact}::remainder`
+ [ ] `Utf8Error::valid_up_to`
+ [ ] `VacantEntry::key`
+ [ ] `NulError::nul_position`
+ [ ] `IntoStringError::utf8_error`
+ [ ] `IntoInnerError::error`
+ [ ] `io::Chain::get_ref`
+ [ ] `io::Take::{limit, get_ref}`
+ [ ] `SocketAddrV6::{flowinfo, scope_id}`
+ [ ] `PrefixComponent::{kind, as_os_str}`
+ [ ] `Path::{ancestors, display}`
+ [ ] `WaitTimeoutResult::timed_out`
+ [ ] `Receiver::{iter, try_iter}`
+ [ ] `thread::JoinHandle::thread`
+ [ ] `SystemTimeError::duration`

Unstable public APIs affected:
+ [ ] `core::fmt::Arguments::new_v1`
+ [ ] `core::fmt::Arguments::new_v1_formatted`
+ [ ] `Pin::{get_ref, into_ref}`
+ [ ] `Utf8Lossy::chunks`
+ [ ] `LocalWaker::as_waker`
+ [ ] `panic::PanicInfo::{internal_constructor, message, location}`
+ [ ] `panic::Location::{internal_constructor }`

## Removed from list in 2nd pass:

Stable public APIs affected:
+ [ ] `LinkedList::{new, iter, is_empty, len}`
+ [ ] `mem::forget`
+ [ ] `Cursor::{new, get_ref, position}`
+ [ ] `io::{empty, repeat, sink}`
+ [ ] `PoisonError::new`
+ [ ] `thread::Builder::new`
+ [ ] `process::Stdio::{piped, inherit, null}`

Unstable public APIs affected:
+ [ ] `io::Initializer::{zeroing, should_initialize}`

28 files changed:
src/libcore/cell.rs
src/libcore/char/methods.rs
src/libcore/convert.rs
src/libcore/iter/sources.rs
src/libcore/lib.rs
src/libcore/mem.rs
src/libcore/num/flt2dec/estimator.rs
src/libcore/num/flt2dec/mod.rs
src/libcore/num/wrapping.rs
src/libcore/ops/range.rs
src/libcore/ptr.rs
src/libcore/slice/mod.rs
src/libcore/str/mod.rs
src/libcore/time.rs
src/libcore/unicode/tables.rs
src/libstd/ffi/c_str.rs
src/libstd/net/ip.rs
src/test/ui/consts/const-eval/duration_conversion.rs
src/test/ui/consts/const-eval/mod-static-with-const-fn.rs
src/test/ui/consts/const-eval/mod-static-with-const-fn.stderr
src/test/ui/consts/std/cell.rs [new file with mode: 0644]
src/test/ui/consts/std/cell.stderr [new file with mode: 0644]
src/test/ui/consts/std/char.rs [new file with mode: 0644]
src/test/ui/consts/std/iter.rs [new file with mode: 0644]
src/test/ui/consts/std/slice.rs [new file with mode: 0644]
src/test/ui/rfc-2306/convert-id-const-no-gate.rs [deleted file]
src/test/ui/rfc-2306/convert-id-const-no-gate.stderr [deleted file]
src/test/ui/rfc-2306/convert-id-const-with-gate.rs

index 689cf319bd750bd1c710e135c4ee0bd2f54d157d..9cf42eff219ba05a87bbce40768967e8e1e45529 100644 (file)
@@ -474,7 +474,7 @@ impl<T: ?Sized> Cell<T> {
     /// ```
     #[inline]
     #[stable(feature = "cell_as_ptr", since = "1.12.0")]
-    pub fn as_ptr(&self) -> *mut T {
+    pub const fn as_ptr(&self) -> *mut T {
         self.value.get()
     }
 
@@ -1508,7 +1508,7 @@ impl<T: ?Sized> UnsafeCell<T> {
     /// ```
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn get(&self) -> *mut T {
+    pub const fn get(&self) -> *mut T {
         &self.value as *const T as *mut T
     }
 }
index 64a17786b0a6b1b7bb92ec7df180190c2458bc1f..35181afea3da6ce72b6a5d83722737984f953fb3 100644 (file)
@@ -903,7 +903,7 @@ pub fn to_uppercase(self) -> ToUppercase {
     /// ```
     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
     #[inline]
-    pub fn is_ascii(&self) -> bool {
+    pub const fn is_ascii(&self) -> bool {
         *self as u32 <= 0x7F
     }
 
index b900990d0a7266a90a1abb983cb697ea7148c0c7..dbc28ef7cf6a9ce8f3826bf21a3e9065ad809acd 100644 (file)
 /// assert_eq!(vec![1, 3], filtered);
 /// ```
 #[unstable(feature = "convert_id", issue = "53500")]
-#[rustc_const_unstable(feature = "const_convert_id")]
 #[inline]
 pub const fn identity<T>(x: T) -> T { x }
 
index d500cc99fa13c2641dabc0e3bfc3d898e4fd194e..7fa3a4bcce7bb2357ae1c1e05952c83e89844b24 100644 (file)
@@ -283,7 +283,7 @@ fn default() -> Empty<T> {
 /// assert_eq!(None, nope.next());
 /// ```
 #[stable(feature = "iter_empty", since = "1.2.0")]
-pub fn empty<T>() -> Empty<T> {
+pub const fn empty<T>() -> Empty<T> {
     Empty(marker::PhantomData)
 }
 
index c69d4441121ce9d9f6ad72e6403bec7e2929be4a..6cf6417974a0dc367267621d7ddf1372dd12a035 100644 (file)
@@ -82,7 +82,6 @@
 #![feature(const_fn)]
 #![feature(const_int_ops)]
 #![feature(const_fn_union)]
-#![feature(const_manually_drop_new)]
 #![feature(custom_attribute)]
 #![feature(doc_cfg)]
 #![feature(doc_spotlight)]
index 8c4ff02aa140f65afec7d23c05edfee71287d46f..d4b7094d6fb645bb48166d0d45f12d22b0a6eeb8 100644 (file)
@@ -942,7 +942,6 @@ impl<T> ManuallyDrop<T> {
     /// ManuallyDrop::new(Box::new(()));
     /// ```
     #[stable(feature = "manually_drop", since = "1.20.0")]
-    #[rustc_const_unstable(feature = "const_manually_drop_new")]
     #[inline]
     pub const fn new(value: T) -> ManuallyDrop<T> {
         ManuallyDrop { value }
@@ -961,7 +960,7 @@ pub const fn new(value: T) -> ManuallyDrop<T> {
     /// ```
     #[stable(feature = "manually_drop", since = "1.20.0")]
     #[inline]
-    pub fn into_inner(slot: ManuallyDrop<T>) -> T {
+    pub const fn into_inner(slot: ManuallyDrop<T>) -> T {
         slot.value
     }
 
index d42e05a91f140f2f150c297914a93faf147f4fb9..4e33fcfd76e61cf210d786833b59e131b5e7a1a4 100644 (file)
@@ -22,4 +22,3 @@ pub fn estimate_scaling_factor(mant: u64, exp: i16) -> i16 {
     // therefore this always underestimates (or is exact), but not much.
     (((nbits + exp as i64) * 1292913986) >> 32) as i16
 }
-
index 21a2e72dac8c37f7411b18841087689a37bc1b24..d58015beecb1e7172f38cf54706dfe5d3411cf13 100644 (file)
@@ -658,4 +658,3 @@ pub fn to_exact_fixed_str<'a, T, F>(mut format_exact: F, v: T,
         }
     }
 }
-
index 1c826c2fa76bdd6c08fd13096cd269e70ba5ac79..00134a58d30f1c2e00a5917b4db3a25a0516fbaa 100644 (file)
@@ -387,7 +387,7 @@ pub const fn max_value() -> Self {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn count_ones(self) -> u32 {
+                pub const fn count_ones(self) -> u32 {
                     self.0.count_ones()
                 }
             }
@@ -407,7 +407,7 @@ pub fn count_ones(self) -> u32 {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn count_zeros(self) -> u32 {
+                pub const fn count_zeros(self) -> u32 {
                     self.0.count_zeros()
                 }
             }
@@ -430,7 +430,7 @@ pub fn count_zeros(self) -> u32 {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn trailing_zeros(self) -> u32 {
+                pub const fn trailing_zeros(self) -> u32 {
                     self.0.trailing_zeros()
                 }
             }
@@ -456,7 +456,7 @@ pub fn trailing_zeros(self) -> u32 {
             /// ```
             #[inline]
             #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-            pub fn rotate_left(self, n: u32) -> Self {
+            pub const fn rotate_left(self, n: u32) -> Self {
                 Wrapping(self.0.rotate_left(n))
             }
 
@@ -481,7 +481,7 @@ pub fn rotate_left(self, n: u32) -> Self {
             /// ```
             #[inline]
             #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-            pub fn rotate_right(self, n: u32) -> Self {
+            pub const fn rotate_right(self, n: u32) -> Self {
                 Wrapping(self.0.rotate_right(n))
             }
 
@@ -505,7 +505,7 @@ pub fn rotate_right(self, n: u32) -> Self {
             /// ```
             #[inline]
             #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-            pub fn swap_bytes(self) -> Self {
+            pub const fn swap_bytes(self) -> Self {
                 Wrapping(self.0.swap_bytes())
             }
 
@@ -532,7 +532,7 @@ pub fn swap_bytes(self) -> Self {
             /// ```
             #[unstable(feature = "reverse_bits", issue = "48763")]
             #[inline]
-            pub fn reverse_bits(self) -> Self {
+            pub const fn reverse_bits(self) -> Self {
                 Wrapping(self.0.reverse_bits())
             }
 
@@ -560,7 +560,7 @@ pub fn reverse_bits(self) -> Self {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn from_be(x: Self) -> Self {
+                pub const fn from_be(x: Self) -> Self {
                     Wrapping(<$t>::from_be(x.0))
                 }
             }
@@ -589,7 +589,7 @@ pub fn from_be(x: Self) -> Self {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn from_le(x: Self) -> Self {
+                pub const fn from_le(x: Self) -> Self {
                     Wrapping(<$t>::from_le(x.0))
                 }
             }
@@ -618,7 +618,7 @@ pub fn from_le(x: Self) -> Self {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn to_be(self) -> Self {
+                pub const fn to_be(self) -> Self {
                     Wrapping(self.0.to_be())
                 }
             }
@@ -647,7 +647,7 @@ pub fn to_be(self) -> Self {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn to_le(self) -> Self {
+                pub const fn to_le(self) -> Self {
                     Wrapping(self.0.to_le())
                 }
             }
@@ -707,7 +707,7 @@ impl Wrapping<$t> {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn leading_zeros(self) -> u32 {
+                pub const fn leading_zeros(self) -> u32 {
                     self.0.leading_zeros()
                 }
             }
@@ -784,7 +784,7 @@ pub fn signum(self) -> Wrapping<$t> {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn is_positive(self) -> bool {
+                pub const fn is_positive(self) -> bool {
                     self.0.is_positive()
                 }
             }
@@ -806,7 +806,7 @@ pub fn is_positive(self) -> bool {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn is_negative(self) -> bool {
+                pub const fn is_negative(self) -> bool {
                     self.0.is_negative()
                 }
             }
@@ -836,7 +836,7 @@ impl Wrapping<$t> {
 ```"),
                 #[inline]
                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
-                pub fn leading_zeros(self) -> u32 {
+                pub const fn leading_zeros(self) -> u32 {
                     self.0.leading_zeros()
                 }
             }
index 6cfb1005325ba644223311f773bcdcfc4cd0033c..908490e1c839e5eb53d9eee6acf6ea2a414686f5 100644 (file)
@@ -416,7 +416,7 @@ pub const fn new(start: Idx, end: Idx) -> Self {
     /// ```
     #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
     #[inline]
-    pub fn start(&self) -> &Idx {
+    pub const fn start(&self) -> &Idx {
         &self.start
     }
 
@@ -440,7 +440,7 @@ pub fn start(&self) -> &Idx {
     /// ```
     #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
     #[inline]
-    pub fn end(&self) -> &Idx {
+    pub const fn end(&self) -> &Idx {
         &self.end
     }
 
index 827e297c84d1f427918508b885a71395a50884f9..a7bfc3f51244720b5990f3bde99239b695250e38 100644 (file)
@@ -2905,7 +2905,7 @@ pub fn new(ptr: *mut T) -> Option<Self> {
     /// Acquires the underlying `*mut` pointer.
     #[stable(feature = "nonnull", since = "1.25.0")]
     #[inline]
-    pub fn as_ptr(self) -> *mut T {
+    pub const fn as_ptr(self) -> *mut T {
         self.pointer.0 as *mut T
     }
 
index 8a6b212020b4e4a0d74f661d675a604bafce40b1..fece328f51f474d45a7a3ae86c171bbf31341a1f 100644 (file)
@@ -385,7 +385,6 @@ pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
-    #[rustc_const_unstable(feature = "const_slice_as_ptr")]
     pub const fn as_ptr(&self) -> *const T {
         self as *const [T] as *const T
     }
index a2782dd8e2e430f7905c0049d335101bdcadf9b6..e710cbffe4d35da71ffd472d2201a002162a5861 100644 (file)
@@ -2277,7 +2277,6 @@ pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
-    #[rustc_const_unstable(feature = "const_str_as_ptr")]
     pub const fn as_ptr(&self) -> *const u8 {
         self as *const str as *const u8
     }
index cfbd431aef0a00e87d5d9f0bf82cfbf15cf90919..938e97503deb6d716f88d693f6f68cf0933294ad 100644 (file)
@@ -209,7 +209,6 @@ pub const fn from_nanos(nanos: u64) -> Duration {
     ///
     /// [`subsec_nanos`]: #method.subsec_nanos
     #[stable(feature = "duration", since = "1.3.0")]
-    #[rustc_const_unstable(feature="duration_getters")]
     #[inline]
     pub const fn as_secs(&self) -> u64 { self.secs }
 
@@ -229,7 +228,6 @@ pub const fn as_secs(&self) -> u64 { self.secs }
     /// assert_eq!(duration.subsec_millis(), 432);
     /// ```
     #[stable(feature = "duration_extras", since = "1.27.0")]
-    #[rustc_const_unstable(feature="duration_getters")]
     #[inline]
     pub const fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI }
 
@@ -249,7 +247,6 @@ pub const fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI }
     /// assert_eq!(duration.subsec_micros(), 234_567);
     /// ```
     #[stable(feature = "duration_extras", since = "1.27.0")]
-    #[rustc_const_unstable(feature="duration_getters")]
     #[inline]
     pub const fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO }
 
@@ -269,7 +266,6 @@ pub const fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO }
     /// assert_eq!(duration.subsec_nanos(), 10_000_000);
     /// ```
     #[stable(feature = "duration", since = "1.3.0")]
-    #[rustc_const_unstable(feature="duration_getters")]
     #[inline]
     pub const fn subsec_nanos(&self) -> u32 { self.nanos }
 
@@ -286,7 +282,7 @@ pub const fn subsec_nanos(&self) -> u32 { self.nanos }
     /// ```
     #[unstable(feature = "duration_as_u128", issue = "50202")]
     #[inline]
-    pub fn as_millis(&self) -> u128 {
+    pub const fn as_millis(&self) -> u128 {
         self.secs as u128 * MILLIS_PER_SEC as u128 + (self.nanos / NANOS_PER_MILLI) as u128
     }
 
@@ -303,7 +299,7 @@ pub fn as_millis(&self) -> u128 {
     /// ```
     #[unstable(feature = "duration_as_u128", issue = "50202")]
     #[inline]
-    pub fn as_micros(&self) -> u128 {
+    pub const fn as_micros(&self) -> u128 {
         self.secs as u128 * MICROS_PER_SEC as u128 + (self.nanos / NANOS_PER_MICRO) as u128
     }
 
@@ -320,7 +316,7 @@ pub fn as_micros(&self) -> u128 {
     /// ```
     #[unstable(feature = "duration_as_u128", issue = "50202")]
     #[inline]
-    pub fn as_nanos(&self) -> u128 {
+    pub const fn as_nanos(&self) -> u128 {
         self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos as u128
     }
 
@@ -478,7 +474,7 @@ pub fn checked_div(self, rhs: u32) -> Option<Duration> {
     /// ```
     #[unstable(feature = "duration_float", issue = "54361")]
     #[inline]
-    pub fn as_float_secs(&self) -> f64 {
+    pub const fn as_float_secs(&self) -> f64 {
         (self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64)
     }
 
index 3de855ac943157091696c035096c70399d5bd96e..e525c0574002b2d83e96d9007dcf289ee16b31ca 100644 (file)
@@ -2598,4 +2598,3 @@ pub mod conversions {
     ];
 
 }
-
index 87ffe0f15e45461032f31da6de9159016b7795c6..66718b95408ca160f90bb3fd6f40506addc8a1be 100644 (file)
@@ -1091,7 +1091,7 @@ pub fn from_bytes_with_nul(bytes: &[u8])
     /// [`CString`]: struct.CString.html
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn as_ptr(&self) -> *const c_char {
+    pub const fn as_ptr(&self) -> *const c_char {
         self.inner.as_ptr()
     }
 
index d45a66ef6653257455594c7239fe002bc91acbb9..2517c45696a2db4875c82fcf8d05b87b3800cf9e 100644 (file)
@@ -424,7 +424,7 @@ pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
     /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
     /// ```
     #[stable(feature = "ip_shared", since = "1.12.0")]
-    pub fn is_unspecified(&self) -> bool {
+    pub const fn is_unspecified(&self) -> bool {
         self.inner.s_addr == 0
     }
 
@@ -862,7 +862,6 @@ impl Ipv6Addr {
     /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    #[rustc_const_unstable(feature = "const_ip")]
     pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16,
                      g: u16, h: u16) -> Ipv6Addr {
         Ipv6Addr {
@@ -1224,7 +1223,7 @@ pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
     ///            [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
     /// ```
     #[stable(feature = "ipv6_to_octets", since = "1.12.0")]
-    pub fn octets(&self) -> [u8; 16] {
+    pub const fn octets(&self) -> [u8; 16] {
         self.inner.s6_addr
     }
 }
index 4481b75840487440d363502b2ec0f832fce9164e..c8bed4a2b77e147c29913c9477b1edb6a9339e58 100644 (file)
@@ -10,8 +10,6 @@
 
 // compile-pass
 
-#![feature(duration_getters)]
-
 use std::time::Duration;
 
 fn main() {
index d6ffca09e96d84a2717d443efac48cd384ed76e9..4136a7b6a724f2416970fd87009541dfc377fe78 100644 (file)
@@ -23,13 +23,16 @@ unsafe impl Sync for Foo {}
 
 static FOO: Foo = Foo(UnsafeCell::new(42));
 
+fn foo() {}
+
 static BAR: () = unsafe {
     *FOO.0.get() = 5;
-    //~^ ERROR calls in statics are limited to constant functions, tuple structs and tuple variants
-
+    //~^ ERROR statements in statics are unstable (see issue #48821)
     // This error is caused by a separate bug that the feature gate error is reported
     // even though the feature gate "const_let" is active.
-    //~| statements in statics are unstable (see issue #48821)
+
+    foo();
+    //~^ ERROR calls in statics are limited to constant functions, tuple structs and tuple variants
 };
 
 fn main() {
index 8eaed1dcab116b099a8d952bd4475e17c0ec73f7..c2bba27e4d1e29992595d93cd3daef068e290857 100644 (file)
@@ -1,17 +1,17 @@
-error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
-  --> $DIR/mod-static-with-const-fn.rs:27:6
-   |
-LL |     *FOO.0.get() = 5;
-   |      ^^^^^^^^^^^
-
 error[E0658]: statements in statics are unstable (see issue #48821)
-  --> $DIR/mod-static-with-const-fn.rs:27:5
+  --> $DIR/mod-static-with-const-fn.rs:29:5
    |
 LL |     *FOO.0.get() = 5;
    |     ^^^^^^^^^^^^^^^^
    |
    = help: add #![feature(const_let)] to the crate attributes to enable
 
+error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
+  --> $DIR/mod-static-with-const-fn.rs:34:5
+   |
+LL |     foo();
+   |     ^^^^^
+
 error: aborting due to 2 previous errors
 
 Some errors occurred: E0015, E0658.
diff --git a/src/test/ui/consts/std/cell.rs b/src/test/ui/consts/std/cell.rs
new file mode 100644 (file)
index 0000000..cf6c0f2
--- /dev/null
@@ -0,0 +1,30 @@
+use std::cell::*;
+
+// not ok, because this would create a silent constant with interior mutability.
+// the rules could be relaxed in the future
+static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
+//~^ ERROR cannot borrow a constant which may contain interior mutability
+
+static FOO3: Wrap<Cell<u32>> = Wrap(Cell::new(42));
+// ok
+static FOO4: Wrap<*mut u32> = Wrap(FOO3.0.as_ptr());
+
+// not ok, because the `as_ptr` call takes a reference to a type with interior mutability
+// which is not allowed in constants
+const FOO2: *mut u32 = Cell::new(42).as_ptr();
+//~^ ERROR cannot borrow a constant which may contain interior mutability
+
+struct IMSafeTrustMe(UnsafeCell<u32>);
+unsafe impl Send for IMSafeTrustMe {}
+unsafe impl Sync for IMSafeTrustMe {}
+
+static BAR: IMSafeTrustMe = IMSafeTrustMe(UnsafeCell::new(5));
+
+
+struct Wrap<T>(T);
+unsafe impl<T> Send for Wrap<T> {}
+unsafe impl<T> Sync for Wrap<T> {}
+
+static BAR_PTR: Wrap<*mut u32> = Wrap(BAR.0.get());
+
+fn main() {}
diff --git a/src/test/ui/consts/std/cell.stderr b/src/test/ui/consts/std/cell.stderr
new file mode 100644 (file)
index 0000000..f75aadf
--- /dev/null
@@ -0,0 +1,15 @@
+error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
+  --> $DIR/cell.rs:5:35
+   |
+LL | static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
+   |                                   ^^^^^^^^^^^^^
+
+error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
+  --> $DIR/cell.rs:14:24
+   |
+LL | const FOO2: *mut u32 = Cell::new(42).as_ptr();
+   |                        ^^^^^^^^^^^^^
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0492`.
diff --git a/src/test/ui/consts/std/char.rs b/src/test/ui/consts/std/char.rs
new file mode 100644 (file)
index 0000000..fe79059
--- /dev/null
@@ -0,0 +1,9 @@
+// run-pass
+
+static X: bool = 'a'.is_ascii();
+static Y: bool = 'รค'.is_ascii();
+
+fn main() {
+    assert!(X);
+    assert!(!Y);
+}
diff --git a/src/test/ui/consts/std/iter.rs b/src/test/ui/consts/std/iter.rs
new file mode 100644 (file)
index 0000000..e9af781
--- /dev/null
@@ -0,0 +1,9 @@
+// run-pass
+
+const I: std::iter::Empty<u32> = std::iter::empty();
+
+fn main() {
+    for i in I {
+        panic!("magical value creation: {}", i);
+    }
+}
diff --git a/src/test/ui/consts/std/slice.rs b/src/test/ui/consts/std/slice.rs
new file mode 100644 (file)
index 0000000..ad38105
--- /dev/null
@@ -0,0 +1,10 @@
+// compile-pass
+
+struct Wrap<T>(T);
+unsafe impl<T> Send for Wrap<T> {}
+unsafe impl<T> Sync for Wrap<T> {}
+
+static FOO: Wrap<*const u32> = Wrap([42, 44, 46].as_ptr());
+static BAR: Wrap<*const u8> = Wrap("hello".as_ptr());
+
+fn main() {}
diff --git a/src/test/ui/rfc-2306/convert-id-const-no-gate.rs b/src/test/ui/rfc-2306/convert-id-const-no-gate.rs
deleted file mode 100644 (file)
index 545c179..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// This test should fail since identity is not stable as a const fn yet.
-
-#![feature(convert_id)]
-
-fn main() {
-    const _FOO: u8 = ::std::convert::identity(42u8);
-}
diff --git a/src/test/ui/rfc-2306/convert-id-const-no-gate.stderr b/src/test/ui/rfc-2306/convert-id-const-no-gate.stderr
deleted file mode 100644 (file)
index dfd8619..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-error: `std::convert::identity` is not yet stable as a const fn
-  --> $DIR/convert-id-const-no-gate.rs:16:22
-   |
-LL |     const _FOO: u8 = ::std::convert::identity(42u8);
-   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   |
-   = help: in Nightly builds, add `#![feature(const_convert_id)]` to the crate attributes to enable
-
-error: aborting due to previous error
-
index c546f11914f814eacf7e734a1bedaf632a6ed031..2e71aee57c2f18abcf1b5f014b0537cf2310c2cd 100644 (file)
@@ -8,12 +8,11 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// This test should pass since we've opted into 'identity' as an
-// unstable const fn.
+// This test should pass since 'identity' is const fn.
 
 // compile-pass
 
-#![feature(convert_id, const_convert_id)]
+#![feature(convert_id)]
 
 fn main() {
     const _FOO: u8 = ::std::convert::identity(42u8);