Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9849587

Browse files
committedSep 8, 2024
add FIXME(const-hack)
1 parent e954412 commit 9849587

File tree

17 files changed

+35
-58
lines changed

17 files changed

+35
-58
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
@@ -386,7 +386,7 @@ impl char {
386386
// Force the 6th bit to be set to ensure ascii is lower case.
387387
digit = (self as u32 | 0b10_0000).wrapping_sub('a' as u32).saturating_add(10);
388388
}
389-
// FIXME: once then_some is const fn, use it here
389+
// FIXME(const-hack): once then_some is const fn, use it here
390390
if digit < radix { Some(digit) } else { None }
391391
}
392392

‎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

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

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

@@ -1894,7 +1884,7 @@ impl<T> Option<&T> {
18941884
where
18951885
T: Copy,
18961886
{
1897-
// FIXME: this implementation, which sidesteps using `Option::map` since it's not const
1887+
// FIXME(const-hack): this implementation, which sidesteps using `Option::map` since it's not const
18981888
// ready yet, should be reverted when possible to avoid code repetition
18991889
match self {
19001890
Some(&v) => Some(v),

‎core/src/slice/index.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ where
3333
#[track_caller]
3434
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
3535
const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
36+
// FIXME(const-hack): once integer formatting in panics is possible, we
37+
// should use the same implementation at compiletime and runtime.
3638
const_eval_select((index, len), slice_start_index_len_fail_ct, slice_start_index_len_fail_rt)
3739
}
3840

39-
// FIXME const-hack
4041
#[inline]
4142
#[track_caller]
4243
fn slice_start_index_len_fail_rt(index: usize, len: usize) -> ! {
@@ -54,10 +55,11 @@ const fn slice_start_index_len_fail_ct(_: usize, _: usize) -> ! {
5455
#[track_caller]
5556
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
5657
const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
58+
// FIXME(const-hack): once integer formatting in panics is possible, we
59+
// should use the same implementation at compiletime and runtime.
5760
const_eval_select((index, len), slice_end_index_len_fail_ct, slice_end_index_len_fail_rt)
5861
}
5962

60-
// FIXME const-hack
6163
#[inline]
6264
#[track_caller]
6365
fn slice_end_index_len_fail_rt(index: usize, len: usize) -> ! {
@@ -75,10 +77,11 @@ const fn slice_end_index_len_fail_ct(_: usize, _: usize) -> ! {
7577
#[track_caller]
7678
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
7779
const fn slice_index_order_fail(index: usize, end: usize) -> ! {
80+
// FIXME(const-hack): once integer formatting in panics is possible, we
81+
// should use the same implementation at compiletime and runtime.
7882
const_eval_select((index, end), slice_index_order_fail_ct, slice_index_order_fail_rt)
7983
}
8084

81-
// FIXME const-hack
8285
#[inline]
8386
#[track_caller]
8487
fn slice_index_order_fail_rt(index: usize, end: 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/time.rs

+2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ impl Duration {
213213
// SAFETY: nanos < NANOS_PER_SEC, therefore nanos is within the valid range
214214
Duration { secs, nanos: unsafe { Nanoseconds(nanos) } }
215215
} else {
216+
// FIXME(const-hack): use `.expect` once that is possible.
216217
let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) {
217218
Some(secs) => secs,
218219
None => panic!("overflow in Duration::new"),
@@ -768,6 +769,7 @@ impl Duration {
768769
let total_nanos = self.nanos.0 as u64 * rhs as u64;
769770
let extra_secs = total_nanos / (NANOS_PER_SEC as u64);
770771
let nanos = (total_nanos % (NANOS_PER_SEC as u64)) as u32;
772+
// FIXME(const-hack): use `and_then` once that is possible.
771773
if let Some(s) = self.secs.checked_mul(rhs as u64) {
772774
if let Some(secs) = s.checked_add(extra_secs) {
773775
debug_assert!(nanos < NANOS_PER_SEC);

‎core/src/unicode/unicode_data.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ const fn bitset_search<
1818
let bucket_idx = (needle / 64) as usize;
1919
let chunk_map_idx = bucket_idx / CHUNK_SIZE;
2020
let chunk_piece = bucket_idx % CHUNK_SIZE;
21-
// FIXME: const-hack: Revert to `slice::get` after `const_slice_index`
22-
// feature stabilizes.
21+
// FIXME(const-hack): Revert to `slice::get` when slice indexing becomes possible in const.
2322
let chunk_idx = if chunk_map_idx < chunk_idx_map.len() {
2423
chunk_idx_map[chunk_map_idx]
2524
} else {
2625
return false;
2726
};
2827
let idx = bitset_chunk_idx[chunk_idx as usize][chunk_piece] as usize;
29-
// FIXME: const-hack: Revert to `slice::get` after `const_slice_index`
30-
// feature stabilizes.
28+
// FIXME(const-hack): Revert to `slice::get` when slice indexing becomes possible in const.
3129
let word = if idx < bitset_canonical.len() {
3230
bitset_canonical[idx]
3331
} else {

‎core/tests/hash/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ impl Default for MyHasher {
1616

1717
impl Hasher for MyHasher {
1818
fn write(&mut self, buf: &[u8]) {
19-
// FIXME(const_trait_impl): change to for loop
20-
let mut i = 0;
21-
while i < buf.len() {
22-
self.hash += buf[i] as u64;
23-
i += 1;
19+
for byte in buf {
20+
self.hash += *byte as u64;
2421
}
2522
}
2623
fn write_str(&mut self, s: &str) {

‎std/src/sys/pal/windows/args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{fmt, io, iter, vec};
2020

2121
/// This is the const equivalent to `NonZero::new(n).unwrap()`
2222
///
23-
/// FIXME: This can be removed once `Option::unwrap` is stably const.
23+
/// FIXME(const-hack): This can be removed once `Option::unwrap` is stably const.
2424
/// See the `const_option` feature (#67441).
2525
const fn non_zero_u16(n: u16) -> NonZero<u16> {
2626
match NonZero::new(n) {

0 commit comments

Comments
 (0)
Failed to load comments.