Skip to content

Commit ac68273

Browse files
authored
Rollup merge of rust-lang#130101 - RalfJung:const-cleanup, r=fee1-dead
some const cleanup: remove unnecessary attributes, add const-hack indications I learned that we use `FIXME(const-hack)` on top of the "const-hack" label. That seems much better since it marks the right place in the code and moves around with the code. So I went through the PRs with that label and added appropriate FIXMEs in the code. IMO this means we can then remove the label -- Cc ``@rust-lang/wg-const-eval.`` I also noticed some const stability attributes that don't do anything useful, and removed them. r? ``@fee1-dead``
2 parents 27049a0 + 1b763fc commit ac68273

File tree

21 files changed

+34
-77
lines changed

21 files changed

+34
-77
lines changed

alloc/src/collections/vec_deque/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ impl<T> VecDeque<T> {
554554
#[rustc_const_stable(feature = "const_vec_deque_new", since = "1.68.0")]
555555
#[must_use]
556556
pub const fn new() -> VecDeque<T> {
557-
// FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable.
558-
VecDeque { head: 0, len: 0, buf: RawVec::NEW }
557+
// FIXME(const-hack): This should just be `VecDeque::new_in(Global)` once that hits stable.
558+
VecDeque { head: 0, len: 0, buf: RawVec::new() }
559559
}
560560

561561
/// Creates an empty deque with space for at least `capacity` elements.

alloc/src/raw_vec.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ struct RawVecInner<A: Allocator = Global> {
9696
}
9797

9898
impl<T> RawVec<T, Global> {
99-
/// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so
100-
/// they cannot call `Self::new()`.
101-
///
102-
/// If you change `RawVec<T>::new` or dependencies, please take care to not introduce anything
103-
/// that would truly const-call something unstable.
104-
pub const NEW: Self = Self::new();
105-
10699
/// Creates the biggest possible `RawVec` (on the system heap)
107100
/// without allocating. If `T` has positive size, then this makes a
108101
/// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a
@@ -111,7 +104,7 @@ impl<T> RawVec<T, Global> {
111104
#[must_use]
112105
#[rustc_const_stable(feature = "raw_vec_internals_const", since = "1.81")]
113106
pub const fn new() -> Self {
114-
Self { inner: RawVecInner::new::<T>(), _marker: PhantomData }
107+
Self::new_in(Global)
115108
}
116109

117110
/// Creates a `RawVec` (on the system heap) with exactly the
@@ -149,12 +142,6 @@ impl<T> RawVec<T, Global> {
149142
}
150143

151144
impl RawVecInner<Global> {
152-
#[must_use]
153-
#[rustc_const_stable(feature = "raw_vec_internals_const", since = "1.81")]
154-
const fn new<T>() -> Self {
155-
Self::new_in(Global, core::mem::align_of::<T>())
156-
}
157-
158145
#[cfg(not(any(no_global_oom_handling, test)))]
159146
#[must_use]
160147
#[inline]

alloc/src/vec/in_place_collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ const fn in_place_collectible<DEST, SRC>(
191191

192192
const fn needs_realloc<SRC, DEST>(src_cap: usize, dst_cap: usize) -> bool {
193193
if const { mem::align_of::<SRC>() != mem::align_of::<DEST>() } {
194-
// FIXME: use unreachable! once that works in const
194+
// FIXME(const-hack): use unreachable! once that works in const
195195
panic!("in_place_collectible() prevents this");
196196
}
197197

alloc/src/vec/into_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<T, A: Allocator> IntoIter<T, A> {
142142
// struct and then overwriting &mut self.
143143
// this creates less assembly
144144
self.cap = 0;
145-
self.buf = RawVec::NEW.non_null();
145+
self.buf = RawVec::new().non_null();
146146
self.ptr = self.buf;
147147
self.end = self.buf.as_ptr();
148148

alloc/src/vec/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl<T> Vec<T> {
419419
#[stable(feature = "rust1", since = "1.0.0")]
420420
#[must_use]
421421
pub const fn new() -> Self {
422-
Vec { buf: RawVec::NEW, len: 0 }
422+
Vec { buf: RawVec::new(), len: 0 }
423423
}
424424

425425
/// Constructs a new, empty `Vec<T>` with at least the specified capacity.

core/src/char/convert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::ub_checks::assert_unsafe_precondition;
1111
#[must_use]
1212
#[inline]
1313
pub(super) const fn from_u32(i: u32) -> Option<char> {
14-
// FIXME: once Result::ok is const fn, use it here
14+
// FIXME(const-hack): once Result::ok is const fn, use it here
1515
match char_try_from_u32(i) {
1616
Ok(c) => Some(c),
1717
Err(_) => None,

core/src/char/methods.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl char {
383383
// Force the 6th bit to be set to ensure ascii is lower case.
384384
digit = (self as u32 | 0b10_0000).wrapping_sub('a' as u32).saturating_add(10);
385385
}
386-
// FIXME: once then_some is const fn, use it here
386+
// FIXME(const-hack): once then_some is const fn, use it here
387387
if digit < radix { Some(digit) } else { None }
388388
}
389389

core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@
149149
#![feature(const_size_of_val_raw)]
150150
#![feature(const_slice_from_raw_parts_mut)]
151151
#![feature(const_slice_from_ref)]
152-
#![feature(const_slice_index)]
153152
#![feature(const_slice_split_at_mut)]
154153
#![feature(const_str_from_utf8_unchecked_mut)]
155154
#![feature(const_strict_overflow_ops)]

core/src/num/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::str::FromStr;
66
use crate::ub_checks::assert_unsafe_precondition;
77
use crate::{ascii, intrinsics, mem};
88

9-
// Used because the `?` operator is not allowed in a const context.
9+
// FIXME(const-hack): Used because the `?` operator is not allowed in a const context.
1010
macro_rules! try_opt {
1111
($e:expr) => {
1212
match $e {

core/src/option.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ impl<T> Option<T> {
739739
#[stable(feature = "pin", since = "1.33.0")]
740740
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
741741
pub const fn as_pin_ref(self: Pin<&Self>) -> Option<Pin<&T>> {
742+
// FIXME(const-hack): use `map` once that is possible
742743
match Pin::get_ref(self).as_ref() {
743744
// SAFETY: `x` is guaranteed to be pinned because it comes from `self`
744745
// which is pinned.
@@ -758,6 +759,7 @@ impl<T> Option<T> {
758759
// SAFETY: `get_unchecked_mut` is never used to move the `Option` inside `self`.
759760
// `x` is guaranteed to be pinned because it comes from `self` which is pinned.
760761
unsafe {
762+
// FIXME(const-hack): use `map` once that is possible
761763
match Pin::get_unchecked_mut(self).as_mut() {
762764
Some(x) => Some(Pin::new_unchecked(x)),
763765
None => None,
@@ -1290,10 +1292,7 @@ impl<T> Option<T> {
12901292
where
12911293
T: Deref,
12921294
{
1293-
match self.as_ref() {
1294-
Some(t) => Some(t.deref()),
1295-
None => None,
1296-
}
1295+
self.as_ref().map(|t| t.deref())
12971296
}
12981297

12991298
/// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`.
@@ -1316,10 +1315,7 @@ impl<T> Option<T> {
13161315
where
13171316
T: DerefMut,
13181317
{
1319-
match self.as_mut() {
1320-
Some(t) => Some(t.deref_mut()),
1321-
None => None,
1322-
}
1318+
self.as_mut().map(|t| t.deref_mut())
13231319
}
13241320

13251321
/////////////////////////////////////////////////////////////////////////
@@ -1632,13 +1628,7 @@ impl<T> Option<T> {
16321628
#[inline]
16331629
#[stable(feature = "option_entry", since = "1.20.0")]
16341630
pub fn get_or_insert(&mut self, value: T) -> &mut T {
1635-
if let None = *self {
1636-
*self = Some(value);
1637-
}
1638-
1639-
// SAFETY: a `None` variant for `self` would have been replaced by a `Some`
1640-
// variant in the code above.
1641-
unsafe { self.as_mut().unwrap_unchecked() }
1631+
self.get_or_insert_with(|| value)
16421632
}
16431633

16441634
/// Inserts the default value into the option if it is [`None`], then
@@ -1724,7 +1714,7 @@ impl<T> Option<T> {
17241714
#[stable(feature = "rust1", since = "1.0.0")]
17251715
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
17261716
pub const fn take(&mut self) -> Option<T> {
1727-
// FIXME replace `mem::replace` by `mem::take` when the latter is const ready
1717+
// FIXME(const-hack) replace `mem::replace` by `mem::take` when the latter is const ready
17281718
mem::replace(self, None)
17291719
}
17301720

core/src/ptr/alignment.rs

-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ impl From<Alignment> for usize {
199199
}
200200
}
201201

202-
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
203202
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
204203
impl cmp::Ord for Alignment {
205204
#[inline]
@@ -208,7 +207,6 @@ impl cmp::Ord for Alignment {
208207
}
209208
}
210209

211-
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
212210
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
213211
impl cmp::PartialOrd for Alignment {
214212
#[inline]

core/src/slice/index.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ where
3131
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
3232
#[cfg_attr(feature = "panic_immediate_abort", inline)]
3333
#[track_caller]
34-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
3534
const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
35+
// FIXME(const-hack): once integer formatting in panics is possible, we
36+
// should use the same implementation at compiletime and runtime.
3637
const_eval_select((index, len), slice_start_index_len_fail_ct, slice_start_index_len_fail_rt)
3738
}
3839

39-
// FIXME const-hack
4040
#[inline]
4141
#[track_caller]
4242
fn slice_start_index_len_fail_rt(index: usize, len: usize) -> ! {
@@ -52,12 +52,12 @@ const fn slice_start_index_len_fail_ct(_: usize, _: usize) -> ! {
5252
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
5353
#[cfg_attr(feature = "panic_immediate_abort", inline)]
5454
#[track_caller]
55-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
5655
const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
56+
// FIXME(const-hack): once integer formatting in panics is possible, we
57+
// should use the same implementation at compiletime and runtime.
5758
const_eval_select((index, len), slice_end_index_len_fail_ct, slice_end_index_len_fail_rt)
5859
}
5960

60-
// FIXME const-hack
6161
#[inline]
6262
#[track_caller]
6363
fn slice_end_index_len_fail_rt(index: usize, len: usize) -> ! {
@@ -73,12 +73,12 @@ const fn slice_end_index_len_fail_ct(_: usize, _: usize) -> ! {
7373
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
7474
#[cfg_attr(feature = "panic_immediate_abort", inline)]
7575
#[track_caller]
76-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
7776
const fn slice_index_order_fail(index: usize, end: usize) -> ! {
77+
// FIXME(const-hack): once integer formatting in panics is possible, we
78+
// should use the same implementation at compiletime and runtime.
7879
const_eval_select((index, end), slice_index_order_fail_ct, slice_index_order_fail_rt)
7980
}
8081

81-
// FIXME const-hack
8282
#[inline]
8383
#[track_caller]
8484
fn slice_index_order_fail_rt(index: usize, end: usize) -> ! {
@@ -246,7 +246,6 @@ pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
246246

247247
/// The methods `index` and `index_mut` panic if the index is out of bounds.
248248
#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
249-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
250249
unsafe impl<T> SliceIndex<[T]> for usize {
251250
type Output = T;
252251

@@ -386,7 +385,6 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
386385
/// - the start of the range is greater than the end of the range or
387386
/// - the end of the range is out of bounds.
388387
#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
389-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
390388
unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
391389
type Output = [T];
392390

@@ -522,7 +520,6 @@ unsafe impl<T> SliceIndex<[T]> for range::Range<usize> {
522520

523521
/// The methods `index` and `index_mut` panic if the end of the range is out of bounds.
524522
#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
525-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
526523
unsafe impl<T> SliceIndex<[T]> for ops::RangeTo<usize> {
527524
type Output = [T];
528525

@@ -561,7 +558,6 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeTo<usize> {
561558

562559
/// The methods `index` and `index_mut` panic if the start of the range is out of bounds.
563560
#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
564-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
565561
unsafe impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
566562
type Output = [T];
567563

@@ -644,7 +640,6 @@ unsafe impl<T> SliceIndex<[T]> for range::RangeFrom<usize> {
644640
}
645641

646642
#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
647-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
648643
unsafe impl<T> SliceIndex<[T]> for ops::RangeFull {
649644
type Output = [T];
650645

@@ -684,7 +679,6 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFull {
684679
/// - the start of the range is greater than the end of the range or
685680
/// - the end of the range is out of bounds.
686681
#[stable(feature = "inclusive_range", since = "1.26.0")]
687-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
688682
unsafe impl<T> SliceIndex<[T]> for ops::RangeInclusive<usize> {
689683
type Output = [T];
690684

@@ -766,7 +760,6 @@ unsafe impl<T> SliceIndex<[T]> for range::RangeInclusive<usize> {
766760

767761
/// The methods `index` and `index_mut` panic if the end of the range is out of bounds.
768762
#[stable(feature = "inclusive_range", since = "1.26.0")]
769-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
770763
unsafe impl<T> SliceIndex<[T]> for ops::RangeToInclusive<usize> {
771764
type Output = [T];
772765

core/src/slice/memchr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const fn memchr_naive(x: u8, text: &[u8]) -> Option<usize> {
5151
}
5252

5353
#[rustc_allow_const_fn_unstable(const_cmp)]
54-
#[rustc_allow_const_fn_unstable(const_slice_index)]
5554
#[rustc_allow_const_fn_unstable(const_align_offset)]
5655
#[rustc_const_stable(feature = "const_memchr", since = "1.65.0")]
5756
const fn memchr_aligned(x: u8, text: &[u8]) -> Option<usize> {

core/src/slice/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ impl<T> [T] {
529529
None
530530
} else {
531531
// SAFETY: We manually verified the bounds of the slice.
532-
// FIXME: Without const traits, we need this instead of `get_unchecked`.
532+
// FIXME(const-hack): Without const traits, we need this instead of `get_unchecked`.
533533
let last = unsafe { self.split_at_unchecked(self.len() - N).1 };
534534

535535
// SAFETY: We explicitly check for the correct number of elements,
@@ -563,7 +563,7 @@ impl<T> [T] {
563563
None
564564
} else {
565565
// SAFETY: We manually verified the bounds of the slice.
566-
// FIXME: Without const traits, we need this instead of `get_unchecked`.
566+
// FIXME(const-hack): Without const traits, we need this instead of `get_unchecked`.
567567
let last = unsafe { self.split_at_mut_unchecked(self.len() - N).1 };
568568

569569
// SAFETY: We explicitly check for the correct number of elements,
@@ -1952,7 +1952,7 @@ impl<T> [T] {
19521952
#[inline]
19531953
#[must_use]
19541954
pub const unsafe fn split_at_unchecked(&self, mid: usize) -> (&[T], &[T]) {
1955-
// HACK: the const function `from_raw_parts` is used to make this
1955+
// FIXME(const-hack): the const function `from_raw_parts` is used to make this
19561956
// function const; previously the implementation used
19571957
// `(self.get_unchecked(..mid), self.get_unchecked(mid..))`
19581958

core/src/str/converts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ use crate::{mem, ptr};
8585
#[rustc_allow_const_fn_unstable(str_internals)]
8686
#[rustc_diagnostic_item = "str_from_utf8"]
8787
pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
88-
// FIXME: This should use `?` again, once it's `const`
88+
// FIXME(const-hack): This should use `?` again, once it's `const`
8989
match run_utf8_validation(v) {
9090
Ok(_) => {
9191
// SAFETY: validation succeeded.
@@ -129,7 +129,7 @@ pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
129129
#[rustc_const_unstable(feature = "const_str_from_utf8", issue = "91006")]
130130
#[rustc_diagnostic_item = "str_from_utf8_mut"]
131131
pub const fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
132-
// This should use `?` again, once it's `const`
132+
// FIXME(const-hack): This should use `?` again, once it's `const`
133133
match run_utf8_validation(v) {
134134
Ok(_) => {
135135
// SAFETY: validation succeeded.

core/src/str/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl Utf8Error {
100100
#[must_use]
101101
#[inline]
102102
pub const fn error_len(&self) -> Option<usize> {
103-
// FIXME: This should become `map` again, once it's `const`
103+
// FIXME(const-hack): This should become `map` again, once it's `const`
104104
match self.error_len {
105105
Some(len) => Some(len as usize),
106106
None => None,

core/src/str/traits.rs

-6
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ const fn str_index_overflow_fail() -> ! {
9292
///
9393
/// Equivalent to `&self[0 .. len]` or `&mut self[0 .. len]`.
9494
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
95-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
9695
unsafe impl SliceIndex<str> for ops::RangeFull {
9796
type Output = str;
9897
#[inline]
@@ -157,7 +156,6 @@ unsafe impl SliceIndex<str> for ops::RangeFull {
157156
/// // &s[3 .. 100];
158157
/// ```
159158
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
160-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
161159
unsafe impl SliceIndex<str> for ops::Range<usize> {
162160
type Output = str;
163161
#[inline]
@@ -429,7 +427,6 @@ unsafe impl SliceIndex<str> for (ops::Bound<usize>, ops::Bound<usize>) {
429427
/// Panics if `end` does not point to the starting byte offset of a
430428
/// character (as defined by `is_char_boundary`), or if `end > len`.
431429
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
432-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
433430
unsafe impl SliceIndex<str> for ops::RangeTo<usize> {
434431
type Output = str;
435432
#[inline]
@@ -498,7 +495,6 @@ unsafe impl SliceIndex<str> for ops::RangeTo<usize> {
498495
/// Panics if `begin` does not point to the starting byte offset of
499496
/// a character (as defined by `is_char_boundary`), or if `begin > len`.
500497
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
501-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
502498
unsafe impl SliceIndex<str> for ops::RangeFrom<usize> {
503499
type Output = str;
504500
#[inline]
@@ -625,7 +621,6 @@ unsafe impl SliceIndex<str> for range::RangeFrom<usize> {
625621
/// to the ending byte offset of a character (`end + 1` is either a starting
626622
/// byte offset or equal to `len`), if `begin > end`, or if `end >= len`.
627623
#[stable(feature = "inclusive_range", since = "1.26.0")]
628-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
629624
unsafe impl SliceIndex<str> for ops::RangeInclusive<usize> {
630625
type Output = str;
631626
#[inline]
@@ -714,7 +709,6 @@ unsafe impl SliceIndex<str> for range::RangeInclusive<usize> {
714709
/// (`end + 1` is either a starting byte offset as defined by
715710
/// `is_char_boundary`, or equal to `len`), or if `end >= len`.
716711
#[stable(feature = "inclusive_range", since = "1.26.0")]
717-
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
718712
unsafe impl SliceIndex<str> for ops::RangeToInclusive<usize> {
719713
type Output = str;
720714
#[inline]

0 commit comments

Comments
 (0)