Skip to content

Commit

Permalink
Auto merge of #55278 - Centril:constification-1, r=alexcrichton
Browse files Browse the repository at this point in the history
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}`
  • Loading branch information
bors committed Nov 12, 2018
2 parents 0195812 + ac1c6b0 commit 65204a9
Show file tree
Hide file tree
Showing 28 changed files with 118 additions and 85 deletions.
4 changes: 2 additions & 2 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ impl char {
/// ```
#[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
}

Expand Down
1 change: 0 additions & 1 deletion src/libcore/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
/// 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 }

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/iter/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl<T> Default for 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)
}

Expand Down
1 change: 0 additions & 1 deletion src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
3 changes: 1 addition & 2 deletions src/libcore/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -961,7 +960,7 @@ impl<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
}

Expand Down
1 change: 0 additions & 1 deletion src/libcore/num/flt2dec/estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

1 change: 0 additions & 1 deletion src/libcore/num/flt2dec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,3 @@ pub fn to_exact_fixed_str<'a, T, F>(mut format_exact: F, v: T,
}
}
}

30 changes: 15 additions & 15 deletions src/libcore/num/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ assert_eq!(n.count_ones(), 3);
```"),
#[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()
}
}
Expand All @@ -407,7 +407,7 @@ assert_eq!(Wrapping(!0", stringify!($t), ").count_zeros(), 0);
```"),
#[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()
}
}
Expand All @@ -430,7 +430,7 @@ assert_eq!(n.trailing_zeros(), 3);
```"),
#[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()
}
}
Expand All @@ -456,7 +456,7 @@ assert_eq!(n.trailing_zeros(), 3);
/// ```
#[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))
}

Expand All @@ -481,7 +481,7 @@ assert_eq!(n.trailing_zeros(), 3);
/// ```
#[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))
}

Expand All @@ -505,7 +505,7 @@ assert_eq!(n.trailing_zeros(), 3);
/// ```
#[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())
}

Expand All @@ -532,7 +532,7 @@ assert_eq!(n.trailing_zeros(), 3);
/// ```
#[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())
}

Expand Down Expand Up @@ -560,7 +560,7 @@ if cfg!(target_endian = \"big\") {
```"),
#[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))
}
}
Expand Down Expand Up @@ -589,7 +589,7 @@ if cfg!(target_endian = \"little\") {
```"),
#[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))
}
}
Expand Down Expand Up @@ -618,7 +618,7 @@ if cfg!(target_endian = \"big\") {
```"),
#[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())
}
}
Expand Down Expand Up @@ -647,7 +647,7 @@ if cfg!(target_endian = \"little\") {
```"),
#[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())
}
}
Expand Down Expand Up @@ -707,7 +707,7 @@ assert_eq!(n.leading_zeros(), 3);
```"),
#[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()
}
}
Expand Down Expand Up @@ -784,7 +784,7 @@ assert!(!Wrapping(-10", stringify!($t), ").is_positive());
```"),
#[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()
}
}
Expand All @@ -806,7 +806,7 @@ assert!(!Wrapping(10", stringify!($t), ").is_negative());
```"),
#[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()
}
}
Expand Down Expand Up @@ -836,7 +836,7 @@ assert_eq!(n.leading_zeros(), 2);
```"),
#[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()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/ops/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ impl<Idx> RangeInclusive<Idx> {
/// ```
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
#[inline]
pub fn start(&self) -> &Idx {
pub const fn start(&self) -> &Idx {
&self.start
}

Expand All @@ -440,7 +440,7 @@ impl<Idx> RangeInclusive<Idx> {
/// ```
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
#[inline]
pub fn end(&self) -> &Idx {
pub const fn end(&self) -> &Idx {
&self.end
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2905,7 +2905,7 @@ impl<T: ?Sized> NonNull<T> {
/// 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
}

Expand Down
1 change: 0 additions & 1 deletion src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ impl<T> [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
}
Expand Down
1 change: 0 additions & 1 deletion src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,6 @@ impl str {
/// ```
#[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
}
Expand Down
12 changes: 4 additions & 8 deletions src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ impl 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 }

Expand All @@ -229,7 +228,6 @@ impl Duration {
/// 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 }

Expand All @@ -249,7 +247,6 @@ impl Duration {
/// 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 }

Expand All @@ -269,7 +266,6 @@ impl Duration {
/// 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 }

Expand All @@ -286,7 +282,7 @@ impl Duration {
/// ```
#[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
}

Expand All @@ -303,7 +299,7 @@ impl Duration {
/// ```
#[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
}

Expand All @@ -320,7 +316,7 @@ impl Duration {
/// ```
#[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
}

Expand Down Expand Up @@ -478,7 +474,7 @@ impl 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)
}

Expand Down
1 change: 0 additions & 1 deletion src/libcore/unicode/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2598,4 +2598,3 @@ pub mod conversions {
];

}

2 changes: 1 addition & 1 deletion src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ impl CStr {
/// [`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()
}

Expand Down
5 changes: 2 additions & 3 deletions src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl 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
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1224,7 +1223,7 @@ impl 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
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/consts/const-eval/duration_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

// compile-pass

#![feature(duration_getters)]

use std::time::Duration;

fn main() {
Expand Down
9 changes: 6 additions & 3 deletions src/test/ui/consts/const-eval/mod-static-with-const-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit 65204a9

Please sign in to comment.