Skip to content

Commit 396f7ed

Browse files
Rollup merge of #155957 - oli-obk:const-closure-std, r=jhpratt
Revert const hacks and use const closures in std This revealed some smaller bugs in stability checking that I fixed where needed: * const closures use the const stability of their parent * trait method default bodies use the const stability of their trait Otherwise trivial reverts of the const hacks that were added fixes #155781
2 parents 267dfb4 + c9725eb commit 396f7ed

8 files changed

Lines changed: 26 additions & 63 deletions

File tree

compiler/rustc_passes/src/stability.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ fn inherit_const_stability(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
5454
match def_kind {
5555
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst { .. } => {
5656
match tcx.def_kind(tcx.local_parent(def_id)) {
57-
DefKind::Impl { .. } => true,
57+
DefKind::Trait | DefKind::Impl { .. } => true,
5858
_ => false,
5959
}
6060
}
61+
DefKind::Closure => true,
6162
_ => false,
6263
}
6364
}

library/alloc/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
#![feature(const_heap)]
109109
#![feature(const_index)]
110110
#![feature(const_option_ops)]
111+
#![feature(const_result_trait_fn)]
111112
#![feature(const_try)]
112113
#![feature(copied_into_inner)]
113114
#![feature(core_intrinsics)]
@@ -173,6 +174,7 @@
173174
#![feature(allocator_internals)]
174175
#![feature(allow_internal_unstable)]
175176
#![feature(cfg_sanitize)]
177+
#![feature(const_closures)]
176178
#![feature(const_precise_live_drops)]
177179
#![feature(const_trait_impl)]
178180
#![feature(coroutine_trait)]

library/alloc/src/raw_vec/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -898,9 +898,5 @@ const fn layout_array(cap: usize, elem_layout: Layout) -> Result<Layout, TryRese
898898
// which lets us use the much-simpler `repeat_packed`.
899899
debug_assert!(elem_layout.size() == elem_layout.pad_to_align().size());
900900

901-
// FIXME(const-hack) return to using `map` and `map_err` once `const_closures` is implemented
902-
match elem_layout.repeat_packed(cap) {
903-
Ok(layout) => Ok(layout),
904-
Err(_) => Err(CapacityOverflow.into()),
905-
}
901+
elem_layout.repeat_packed(cap).map_err(const |_| CapacityOverflow.into())
906902
}

library/alloctests/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![feature(const_destruct)]
2424
#![feature(const_heap)]
2525
#![feature(const_option_ops)]
26+
#![feature(const_result_trait_fn)]
2627
#![feature(const_try)]
2728
#![feature(copied_into_inner)]
2829
#![feature(core_intrinsics)]
@@ -54,6 +55,7 @@
5455
//
5556
// Language features:
5657
// tidy-alphabetical-start
58+
#![feature(const_closures)]
5759
#![feature(const_trait_impl)]
5860
#![feature(dropck_eyepatch)]
5961
#![feature(min_specialization)]

library/core/src/iter/traits/iterator.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,7 @@ pub const trait Iterator {
226226
Self: Sized + [const] Destruct,
227227
Self::Item: [const] Destruct,
228228
{
229-
// FIXME(const-hack): revert this to a const closure
230-
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
231-
#[rustc_inherit_overflow_checks]
232-
const fn plus_one<T: [const] Destruct>(accum: usize, _elem: T) -> usize {
233-
accum + 1
234-
}
235-
self.fold(0, plus_one)
229+
self.fold(0, const |accum, _elem| accum + 1)
236230
}
237231

238232
/// Consumes the iterator, returning the last element.

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
#![feature(cfg_target_has_atomic)]
126126
#![feature(cfg_target_has_atomic_equal_alignment)]
127127
#![feature(cfg_ub_checks)]
128+
#![feature(const_closures)]
128129
#![feature(const_precise_live_drops)]
129130
#![feature(const_trait_impl)]
130131
#![feature(decl_macro)]

library/core/src/ops/try_trait.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::marker::{Destruct, PhantomData};
1+
use crate::marker::Destruct;
22
use crate::ops::ControlFlow;
33

44
/// The `?` operator and `try {}` blocks.
@@ -398,37 +398,21 @@ pub(crate) type ChangeOutputType<T: Try<Residual: Residual<V>>, V> =
398398
/// Not currently planned to be exposed publicly, so just `pub(crate)`.
399399
#[repr(transparent)]
400400
pub(crate) struct NeverShortCircuit<T>(pub T);
401-
// FIXME(const-hack): replace with `|a| NeverShortCircuit(f(a))` when const closures added.
402-
pub(crate) struct Wrapped<T, A, F: FnMut(A) -> T> {
403-
f: F,
404-
p: PhantomData<(T, A)>,
405-
}
406-
#[rustc_const_unstable(feature = "const_never_short_circuit", issue = "none")]
407-
impl<T, A, F: [const] FnMut(A) -> T + [const] Destruct> const FnOnce<(A,)> for Wrapped<T, A, F> {
408-
type Output = NeverShortCircuit<T>;
409-
410-
extern "rust-call" fn call_once(mut self, args: (A,)) -> Self::Output {
411-
self.call_mut(args)
412-
}
413-
}
414-
#[rustc_const_unstable(feature = "const_never_short_circuit", issue = "none")]
415-
impl<T, A, F: [const] FnMut(A) -> T> const FnMut<(A,)> for Wrapped<T, A, F> {
416-
extern "rust-call" fn call_mut(&mut self, (args,): (A,)) -> Self::Output {
417-
NeverShortCircuit((self.f)(args))
418-
}
419-
}
420401

421402
impl<T> NeverShortCircuit<T> {
422403
/// Wraps a unary function to produce one that wraps the output into a `NeverShortCircuit`.
423404
///
424405
/// This is useful for implementing infallible functions in terms of the `try_` ones,
425406
/// without accidentally capturing extra generic parameters in a closure.
426407
#[inline]
427-
pub(crate) const fn wrap_mut_1<A, F>(f: F) -> Wrapped<T, A, F>
408+
#[rustc_const_unstable(feature = "const_array", issue = "147606")]
409+
pub(crate) const fn wrap_mut_1<A, F>(
410+
mut f: F,
411+
) -> impl [const] FnMut(A) -> Self + [const] Destruct
428412
where
429-
F: [const] FnMut(A) -> T,
413+
F: [const] FnMut(A) -> T + [const] Destruct,
430414
{
431-
Wrapped { f, p: PhantomData }
415+
const move |a| NeverShortCircuit(f(a))
432416
}
433417

434418
#[inline]

library/core/src/slice/cmp.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use super::{from_raw_parts, memchr};
44
use crate::ascii;
55
use crate::cmp::{self, BytewiseEq, Ordering};
6-
use crate::convert::Infallible;
76
use crate::intrinsics::compare_bytes;
87
use crate::marker::Destruct;
98
use crate::mem::SizedTypeProperties;
@@ -182,20 +181,12 @@ type AlwaysBreak<B> = ControlFlow<B, crate::convert::Infallible>;
182181
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
183182
impl<A: [const] PartialOrd> const SlicePartialOrd for A {
184183
default fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
185-
// FIXME(const-hack): revert this to a const closure once possible
186-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
187-
const fn elem_chain<A: [const] PartialOrd>(a: &A, b: &A) -> ControlFlow<Option<Ordering>> {
188-
match PartialOrd::partial_cmp(a, b) {
189-
Some(Ordering::Equal) => ControlFlow::Continue(()),
190-
non_eq => ControlFlow::Break(non_eq),
191-
}
192-
}
184+
let elem_chain = const |a, b| match PartialOrd::partial_cmp(a, b) {
185+
Some(Ordering::Equal) => ControlFlow::Continue(()),
186+
non_eq => ControlFlow::Break(non_eq),
187+
};
193188

194-
// FIXME(const-hack): revert this to a const closure once possible
195-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
196-
const fn len_chain(a: &usize, b: &usize) -> ControlFlow<Option<Ordering>, Infallible> {
197-
ControlFlow::Break(usize::partial_cmp(a, b))
198-
}
189+
let len_chain = const |a: &_, b: &_| ControlFlow::Break(usize::partial_cmp(a, b));
199190

200191
let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
201192
b
@@ -293,20 +284,12 @@ const trait SliceOrd: Sized {
293284
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
294285
impl<A: [const] Ord> const SliceOrd for A {
295286
default fn compare(left: &[Self], right: &[Self]) -> Ordering {
296-
// FIXME(const-hack): revert this to a const closure once possible
297-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
298-
const fn elem_chain<A: [const] Ord>(a: &A, b: &A) -> ControlFlow<Ordering> {
299-
match Ord::cmp(a, b) {
300-
Ordering::Equal => ControlFlow::Continue(()),
301-
non_eq => ControlFlow::Break(non_eq),
302-
}
303-
}
287+
let elem_chain = const |a, b| match Ord::cmp(a, b) {
288+
Ordering::Equal => ControlFlow::Continue(()),
289+
non_eq => ControlFlow::Break(non_eq),
290+
};
304291

305-
// FIXME(const-hack): revert this to a const closure once possible
306-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
307-
const fn len_chain(a: &usize, b: &usize) -> ControlFlow<Ordering, Infallible> {
308-
ControlFlow::Break(usize::cmp(a, b))
309-
}
292+
let len_chain = const |a: &_, b: &_| ControlFlow::Break(usize::cmp(a, b));
310293

311294
let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
312295
b

0 commit comments

Comments
 (0)