/// ```
#[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()
}
/// ```
#[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
}
}
/// ```
#[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
}
/// 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 }
/// 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)
}
#![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)]
/// 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 }
/// ```
#[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
}
// therefore this always underestimates (or is exact), but not much.
(((nbits + exp as i64) * 1292913986) >> 32) as i16
}
-
```"),
#[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()
}
}
```"),
#[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()
}
}
```"),
#[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()
}
}
/// ```
#[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))
}
/// ```
#[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))
}
/// ```
#[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())
}
/// ```
#[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())
}
```"),
#[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))
}
}
```"),
#[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))
}
}
```"),
#[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())
}
}
```"),
#[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())
}
}
```"),
#[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()
}
}
```"),
#[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()
}
}
```"),
#[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()
}
}
```"),
#[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()
}
}
/// ```
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
#[inline]
- pub fn start(&self) -> &Idx {
+ pub const fn start(&self) -> &Idx {
&self.start
}
/// ```
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
#[inline]
- pub fn end(&self) -> &Idx {
+ pub const fn end(&self) -> &Idx {
&self.end
}
/// 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
}
/// ```
#[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
}
/// ```
#[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
}
///
/// [`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 }
/// 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 }
/// 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 }
/// 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 }
/// ```
#[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
}
/// ```
#[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
}
/// ```
#[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
}
/// ```
#[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)
}
/// [`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()
}
/// 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
}
/// 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 {
/// [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
}
}
// compile-pass
-#![feature(duration_getters)]
-
use std::time::Duration;
fn main() {
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() {
-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.
--- /dev/null
+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() {}
--- /dev/null
+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`.
--- /dev/null
+// run-pass
+
+static X: bool = 'a'.is_ascii();
+static Y: bool = 'รค'.is_ascii();
+
+fn main() {
+ assert!(X);
+ assert!(!Y);
+}
--- /dev/null
+// run-pass
+
+const I: std::iter::Empty<u32> = std::iter::empty();
+
+fn main() {
+ for i in I {
+ panic!("magical value creation: {}", i);
+ }
+}
--- /dev/null
+// 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() {}
+++ /dev/null
-// 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);
-}
+++ /dev/null
-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
-
// 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);