Skip to content

Commit 65204a9

Browse files
committed
Auto merge of #55278 - Centril:constification-1, r=alexcrichton
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}`
2 parents 0195812 + ac1c6b0 commit 65204a9

28 files changed

+118
-85
lines changed

src/libcore/cell.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ impl<T: ?Sized> Cell<T> {
474474
/// ```
475475
#[inline]
476476
#[stable(feature = "cell_as_ptr", since = "1.12.0")]
477-
pub fn as_ptr(&self) -> *mut T {
477+
pub const fn as_ptr(&self) -> *mut T {
478478
self.value.get()
479479
}
480480

@@ -1508,7 +1508,7 @@ impl<T: ?Sized> UnsafeCell<T> {
15081508
/// ```
15091509
#[inline]
15101510
#[stable(feature = "rust1", since = "1.0.0")]
1511-
pub fn get(&self) -> *mut T {
1511+
pub const fn get(&self) -> *mut T {
15121512
&self.value as *const T as *mut T
15131513
}
15141514
}

src/libcore/char/methods.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ impl char {
903903
/// ```
904904
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
905905
#[inline]
906-
pub fn is_ascii(&self) -> bool {
906+
pub const fn is_ascii(&self) -> bool {
907907
*self as u32 <= 0x7F
908908
}
909909

src/libcore/convert.rs

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
/// assert_eq!(vec![1, 3], filtered);
105105
/// ```
106106
#[unstable(feature = "convert_id", issue = "53500")]
107-
#[rustc_const_unstable(feature = "const_convert_id")]
108107
#[inline]
109108
pub const fn identity<T>(x: T) -> T { x }
110109

src/libcore/iter/sources.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl<T> Default for Empty<T> {
283283
/// assert_eq!(None, nope.next());
284284
/// ```
285285
#[stable(feature = "iter_empty", since = "1.2.0")]
286-
pub fn empty<T>() -> Empty<T> {
286+
pub const fn empty<T>() -> Empty<T> {
287287
Empty(marker::PhantomData)
288288
}
289289

src/libcore/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
#![feature(const_fn)]
8383
#![feature(const_int_ops)]
8484
#![feature(const_fn_union)]
85-
#![feature(const_manually_drop_new)]
8685
#![feature(custom_attribute)]
8786
#![feature(doc_cfg)]
8887
#![feature(doc_spotlight)]

src/libcore/mem.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,6 @@ impl<T> ManuallyDrop<T> {
942942
/// ManuallyDrop::new(Box::new(()));
943943
/// ```
944944
#[stable(feature = "manually_drop", since = "1.20.0")]
945-
#[rustc_const_unstable(feature = "const_manually_drop_new")]
946945
#[inline]
947946
pub const fn new(value: T) -> ManuallyDrop<T> {
948947
ManuallyDrop { value }
@@ -961,7 +960,7 @@ impl<T> ManuallyDrop<T> {
961960
/// ```
962961
#[stable(feature = "manually_drop", since = "1.20.0")]
963962
#[inline]
964-
pub fn into_inner(slot: ManuallyDrop<T>) -> T {
963+
pub const fn into_inner(slot: ManuallyDrop<T>) -> T {
965964
slot.value
966965
}
967966

src/libcore/num/flt2dec/estimator.rs

-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ pub fn estimate_scaling_factor(mant: u64, exp: i16) -> i16 {
2222
// therefore this always underestimates (or is exact), but not much.
2323
(((nbits + exp as i64) * 1292913986) >> 32) as i16
2424
}
25-

src/libcore/num/flt2dec/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,3 @@ pub fn to_exact_fixed_str<'a, T, F>(mut format_exact: F, v: T,
658658
}
659659
}
660660
}
661-

src/libcore/num/wrapping.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ assert_eq!(n.count_ones(), 3);
387387
```"),
388388
#[inline]
389389
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
390-
pub fn count_ones(self) -> u32 {
390+
pub const fn count_ones(self) -> u32 {
391391
self.0.count_ones()
392392
}
393393
}
@@ -407,7 +407,7 @@ assert_eq!(Wrapping(!0", stringify!($t), ").count_zeros(), 0);
407407
```"),
408408
#[inline]
409409
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
410-
pub fn count_zeros(self) -> u32 {
410+
pub const fn count_zeros(self) -> u32 {
411411
self.0.count_zeros()
412412
}
413413
}
@@ -430,7 +430,7 @@ assert_eq!(n.trailing_zeros(), 3);
430430
```"),
431431
#[inline]
432432
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
433-
pub fn trailing_zeros(self) -> u32 {
433+
pub const fn trailing_zeros(self) -> u32 {
434434
self.0.trailing_zeros()
435435
}
436436
}
@@ -456,7 +456,7 @@ assert_eq!(n.trailing_zeros(), 3);
456456
/// ```
457457
#[inline]
458458
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
459-
pub fn rotate_left(self, n: u32) -> Self {
459+
pub const fn rotate_left(self, n: u32) -> Self {
460460
Wrapping(self.0.rotate_left(n))
461461
}
462462

@@ -481,7 +481,7 @@ assert_eq!(n.trailing_zeros(), 3);
481481
/// ```
482482
#[inline]
483483
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
484-
pub fn rotate_right(self, n: u32) -> Self {
484+
pub const fn rotate_right(self, n: u32) -> Self {
485485
Wrapping(self.0.rotate_right(n))
486486
}
487487

@@ -505,7 +505,7 @@ assert_eq!(n.trailing_zeros(), 3);
505505
/// ```
506506
#[inline]
507507
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
508-
pub fn swap_bytes(self) -> Self {
508+
pub const fn swap_bytes(self) -> Self {
509509
Wrapping(self.0.swap_bytes())
510510
}
511511

@@ -532,7 +532,7 @@ assert_eq!(n.trailing_zeros(), 3);
532532
/// ```
533533
#[unstable(feature = "reverse_bits", issue = "48763")]
534534
#[inline]
535-
pub fn reverse_bits(self) -> Self {
535+
pub const fn reverse_bits(self) -> Self {
536536
Wrapping(self.0.reverse_bits())
537537
}
538538

@@ -560,7 +560,7 @@ if cfg!(target_endian = \"big\") {
560560
```"),
561561
#[inline]
562562
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
563-
pub fn from_be(x: Self) -> Self {
563+
pub const fn from_be(x: Self) -> Self {
564564
Wrapping(<$t>::from_be(x.0))
565565
}
566566
}
@@ -589,7 +589,7 @@ if cfg!(target_endian = \"little\") {
589589
```"),
590590
#[inline]
591591
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
592-
pub fn from_le(x: Self) -> Self {
592+
pub const fn from_le(x: Self) -> Self {
593593
Wrapping(<$t>::from_le(x.0))
594594
}
595595
}
@@ -618,7 +618,7 @@ if cfg!(target_endian = \"big\") {
618618
```"),
619619
#[inline]
620620
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
621-
pub fn to_be(self) -> Self {
621+
pub const fn to_be(self) -> Self {
622622
Wrapping(self.0.to_be())
623623
}
624624
}
@@ -647,7 +647,7 @@ if cfg!(target_endian = \"little\") {
647647
```"),
648648
#[inline]
649649
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
650-
pub fn to_le(self) -> Self {
650+
pub const fn to_le(self) -> Self {
651651
Wrapping(self.0.to_le())
652652
}
653653
}
@@ -707,7 +707,7 @@ assert_eq!(n.leading_zeros(), 3);
707707
```"),
708708
#[inline]
709709
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
710-
pub fn leading_zeros(self) -> u32 {
710+
pub const fn leading_zeros(self) -> u32 {
711711
self.0.leading_zeros()
712712
}
713713
}
@@ -784,7 +784,7 @@ assert!(!Wrapping(-10", stringify!($t), ").is_positive());
784784
```"),
785785
#[inline]
786786
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
787-
pub fn is_positive(self) -> bool {
787+
pub const fn is_positive(self) -> bool {
788788
self.0.is_positive()
789789
}
790790
}
@@ -806,7 +806,7 @@ assert!(!Wrapping(10", stringify!($t), ").is_negative());
806806
```"),
807807
#[inline]
808808
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
809-
pub fn is_negative(self) -> bool {
809+
pub const fn is_negative(self) -> bool {
810810
self.0.is_negative()
811811
}
812812
}
@@ -836,7 +836,7 @@ assert_eq!(n.leading_zeros(), 2);
836836
```"),
837837
#[inline]
838838
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
839-
pub fn leading_zeros(self) -> u32 {
839+
pub const fn leading_zeros(self) -> u32 {
840840
self.0.leading_zeros()
841841
}
842842
}

src/libcore/ops/range.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<Idx> RangeInclusive<Idx> {
416416
/// ```
417417
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
418418
#[inline]
419-
pub fn start(&self) -> &Idx {
419+
pub const fn start(&self) -> &Idx {
420420
&self.start
421421
}
422422

@@ -440,7 +440,7 @@ impl<Idx> RangeInclusive<Idx> {
440440
/// ```
441441
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
442442
#[inline]
443-
pub fn end(&self) -> &Idx {
443+
pub const fn end(&self) -> &Idx {
444444
&self.end
445445
}
446446

src/libcore/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2905,7 +2905,7 @@ impl<T: ?Sized> NonNull<T> {
29052905
/// Acquires the underlying `*mut` pointer.
29062906
#[stable(feature = "nonnull", since = "1.25.0")]
29072907
#[inline]
2908-
pub fn as_ptr(self) -> *mut T {
2908+
pub const fn as_ptr(self) -> *mut T {
29092909
self.pointer.0 as *mut T
29102910
}
29112911

src/libcore/slice/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,6 @@ impl<T> [T] {
385385
/// ```
386386
#[stable(feature = "rust1", since = "1.0.0")]
387387
#[inline]
388-
#[rustc_const_unstable(feature = "const_slice_as_ptr")]
389388
pub const fn as_ptr(&self) -> *const T {
390389
self as *const [T] as *const T
391390
}

src/libcore/str/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2277,7 +2277,6 @@ impl str {
22772277
/// ```
22782278
#[stable(feature = "rust1", since = "1.0.0")]
22792279
#[inline]
2280-
#[rustc_const_unstable(feature = "const_str_as_ptr")]
22812280
pub const fn as_ptr(&self) -> *const u8 {
22822281
self as *const str as *const u8
22832282
}

src/libcore/time.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ impl Duration {
209209
///
210210
/// [`subsec_nanos`]: #method.subsec_nanos
211211
#[stable(feature = "duration", since = "1.3.0")]
212-
#[rustc_const_unstable(feature="duration_getters")]
213212
#[inline]
214213
pub const fn as_secs(&self) -> u64 { self.secs }
215214

@@ -229,7 +228,6 @@ impl Duration {
229228
/// assert_eq!(duration.subsec_millis(), 432);
230229
/// ```
231230
#[stable(feature = "duration_extras", since = "1.27.0")]
232-
#[rustc_const_unstable(feature="duration_getters")]
233231
#[inline]
234232
pub const fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI }
235233

@@ -249,7 +247,6 @@ impl Duration {
249247
/// assert_eq!(duration.subsec_micros(), 234_567);
250248
/// ```
251249
#[stable(feature = "duration_extras", since = "1.27.0")]
252-
#[rustc_const_unstable(feature="duration_getters")]
253250
#[inline]
254251
pub const fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO }
255252

@@ -269,7 +266,6 @@ impl Duration {
269266
/// assert_eq!(duration.subsec_nanos(), 10_000_000);
270267
/// ```
271268
#[stable(feature = "duration", since = "1.3.0")]
272-
#[rustc_const_unstable(feature="duration_getters")]
273269
#[inline]
274270
pub const fn subsec_nanos(&self) -> u32 { self.nanos }
275271

@@ -286,7 +282,7 @@ impl Duration {
286282
/// ```
287283
#[unstable(feature = "duration_as_u128", issue = "50202")]
288284
#[inline]
289-
pub fn as_millis(&self) -> u128 {
285+
pub const fn as_millis(&self) -> u128 {
290286
self.secs as u128 * MILLIS_PER_SEC as u128 + (self.nanos / NANOS_PER_MILLI) as u128
291287
}
292288

@@ -303,7 +299,7 @@ impl Duration {
303299
/// ```
304300
#[unstable(feature = "duration_as_u128", issue = "50202")]
305301
#[inline]
306-
pub fn as_micros(&self) -> u128 {
302+
pub const fn as_micros(&self) -> u128 {
307303
self.secs as u128 * MICROS_PER_SEC as u128 + (self.nanos / NANOS_PER_MICRO) as u128
308304
}
309305

@@ -320,7 +316,7 @@ impl Duration {
320316
/// ```
321317
#[unstable(feature = "duration_as_u128", issue = "50202")]
322318
#[inline]
323-
pub fn as_nanos(&self) -> u128 {
319+
pub const fn as_nanos(&self) -> u128 {
324320
self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos as u128
325321
}
326322

@@ -478,7 +474,7 @@ impl Duration {
478474
/// ```
479475
#[unstable(feature = "duration_float", issue = "54361")]
480476
#[inline]
481-
pub fn as_float_secs(&self) -> f64 {
477+
pub const fn as_float_secs(&self) -> f64 {
482478
(self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64)
483479
}
484480

src/libcore/unicode/tables.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2598,4 +2598,3 @@ pub mod conversions {
25982598
];
25992599

26002600
}
2601-

src/libstd/ffi/c_str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ impl CStr {
10911091
/// [`CString`]: struct.CString.html
10921092
#[inline]
10931093
#[stable(feature = "rust1", since = "1.0.0")]
1094-
pub fn as_ptr(&self) -> *const c_char {
1094+
pub const fn as_ptr(&self) -> *const c_char {
10951095
self.inner.as_ptr()
10961096
}
10971097

src/libstd/net/ip.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ impl Ipv4Addr {
424424
/// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
425425
/// ```
426426
#[stable(feature = "ip_shared", since = "1.12.0")]
427-
pub fn is_unspecified(&self) -> bool {
427+
pub const fn is_unspecified(&self) -> bool {
428428
self.inner.s_addr == 0
429429
}
430430

@@ -862,7 +862,6 @@ impl Ipv6Addr {
862862
/// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
863863
/// ```
864864
#[stable(feature = "rust1", since = "1.0.0")]
865-
#[rustc_const_unstable(feature = "const_ip")]
866865
pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16,
867866
g: u16, h: u16) -> Ipv6Addr {
868867
Ipv6Addr {
@@ -1224,7 +1223,7 @@ impl Ipv6Addr {
12241223
/// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
12251224
/// ```
12261225
#[stable(feature = "ipv6_to_octets", since = "1.12.0")]
1227-
pub fn octets(&self) -> [u8; 16] {
1226+
pub const fn octets(&self) -> [u8; 16] {
12281227
self.inner.s6_addr
12291228
}
12301229
}

src/test/ui/consts/const-eval/duration_conversion.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
// compile-pass
1212

13-
#![feature(duration_getters)]
14-
1513
use std::time::Duration;
1614

1715
fn main() {

src/test/ui/consts/const-eval/mod-static-with-const-fn.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ unsafe impl Sync for Foo {}
2323

2424
static FOO: Foo = Foo(UnsafeCell::new(42));
2525

26+
fn foo() {}
27+
2628
static BAR: () = unsafe {
2729
*FOO.0.get() = 5;
28-
//~^ ERROR calls in statics are limited to constant functions, tuple structs and tuple variants
29-
30+
//~^ ERROR statements in statics are unstable (see issue #48821)
3031
// This error is caused by a separate bug that the feature gate error is reported
3132
// even though the feature gate "const_let" is active.
32-
//~| statements in statics are unstable (see issue #48821)
33+
34+
foo();
35+
//~^ ERROR calls in statics are limited to constant functions, tuple structs and tuple variants
3336
};
3437

3538
fn main() {

0 commit comments

Comments
 (0)