Skip to content

Commit 82a3b16

Browse files
authored
Rollup merge of rust-lang#107551 - fee1-dead-contrib:rm_const_fnmut_helper, r=oli-obk
Replace `ConstFnMutClosure` with const closures Also fixes a parser bug. cc `@oli-obk` for compiler changes
2 parents 3cfada8 + f4e0ea7 commit 82a3b16

File tree

13 files changed

+53
-130
lines changed

13 files changed

+53
-130
lines changed

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ impl<'a> Parser<'a> {
21092109
ClosureBinder::NotPresent
21102110
};
21112111

2112-
let constness = self.parse_constness(Case::Sensitive);
2112+
let constness = self.parse_closure_constness(Case::Sensitive);
21132113

21142114
let movability =
21152115
if self.eat_keyword(kw::Static) { Movability::Static } else { Movability::Movable };

compiler/rustc_parse/src/parser/mod.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,10 @@ impl<'a> Parser<'a> {
739739
fn check_const_closure(&self) -> bool {
740740
self.is_keyword_ahead(0, &[kw::Const])
741741
&& self.look_ahead(1, |t| match &t.kind {
742-
token::Ident(kw::Move | kw::Static | kw::Async, _)
743-
| token::OrOr
744-
| token::BinOp(token::Or) => true,
742+
// async closures do not work with const closures, so we do not parse that here.
743+
token::Ident(kw::Move | kw::Static, _) | token::OrOr | token::BinOp(token::Or) => {
744+
true
745+
}
745746
_ => false,
746747
})
747748
}
@@ -1203,8 +1204,18 @@ impl<'a> Parser<'a> {
12031204

12041205
/// Parses constness: `const` or nothing.
12051206
fn parse_constness(&mut self, case: Case) -> Const {
1206-
// Avoid const blocks to be parsed as const items
1207-
if self.look_ahead(1, |t| t != &token::OpenDelim(Delimiter::Brace))
1207+
self.parse_constness_(case, false)
1208+
}
1209+
1210+
/// Parses constness for closures
1211+
fn parse_closure_constness(&mut self, case: Case) -> Const {
1212+
self.parse_constness_(case, true)
1213+
}
1214+
1215+
fn parse_constness_(&mut self, case: Case, is_closure: bool) -> Const {
1216+
// Avoid const blocks and const closures to be parsed as const items
1217+
if (self.check_const_closure() == is_closure)
1218+
&& self.look_ahead(1, |t| t != &token::OpenDelim(Delimiter::Brace))
12081219
&& self.eat_keyword_case(kw::Const, case)
12091220
{
12101221
Const::Yes(self.prev_token.uninterpolated_span())

library/core/src/cmp.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
2323
#![stable(feature = "rust1", since = "1.0.0")]
2424

25-
use crate::const_closure::ConstFnMutClosure;
2625
use crate::marker::Destruct;
2726

2827
use self::Ordering::*;
@@ -1291,17 +1290,7 @@ where
12911290
F: ~const Destruct,
12921291
K: ~const Destruct,
12931292
{
1294-
const fn imp<T, F: ~const FnMut(&T) -> K, K: ~const Ord>(
1295-
f: &mut F,
1296-
(v1, v2): (&T, &T),
1297-
) -> Ordering
1298-
where
1299-
T: ~const Destruct,
1300-
K: ~const Destruct,
1301-
{
1302-
f(v1).cmp(&f(v2))
1303-
}
1304-
max_by(v1, v2, ConstFnMutClosure::new(&mut f, imp))
1293+
max_by(v1, v2, const |v1, v2| f(v1).cmp(&f(v2)))
13051294
}
13061295

13071296
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types

library/core/src/const_closure.rs

-78
This file was deleted.

library/core/src/iter/adapters/array_chunks.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::array;
2-
use crate::const_closure::ConstFnMutClosure;
32
use crate::iter::{ByRefSized, FusedIterator, Iterator, TrustedRandomAccessNoCoerce};
43
use crate::mem::{self, MaybeUninit};
54
use crate::ops::{ControlFlow, NeverShortCircuit, Try};
@@ -189,13 +188,12 @@ where
189188
I: Iterator,
190189
{
191190
#[inline]
192-
default fn fold<B, F>(mut self, init: B, mut f: F) -> B
191+
default fn fold<B, F>(mut self, init: B, f: F) -> B
193192
where
194193
Self: Sized,
195194
F: FnMut(B, Self::Item) -> B,
196195
{
197-
let fold = ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp);
198-
self.try_fold(init, fold).0
196+
self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).0
199197
}
200198
}
201199

library/core/src/iter/adapters/by_ref_sized.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
const_closure::ConstFnMutClosure,
3-
ops::{NeverShortCircuit, Try},
4-
};
1+
use crate::ops::{NeverShortCircuit, Try};
52

63
/// Like `Iterator::by_ref`, but requiring `Sized` so it can forward generics.
74
///
@@ -39,13 +36,12 @@ impl<I: Iterator> Iterator for ByRefSized<'_, I> {
3936
}
4037

4138
#[inline]
42-
fn fold<B, F>(self, init: B, mut f: F) -> B
39+
fn fold<B, F>(self, init: B, f: F) -> B
4340
where
4441
F: FnMut(B, Self::Item) -> B,
4542
{
4643
// `fold` needs ownership, so this can't forward directly.
47-
I::try_fold(self.0, init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp))
48-
.0
44+
I::try_fold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).0
4945
}
5046

5147
#[inline]
@@ -76,17 +72,12 @@ impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
7672
}
7773

7874
#[inline]
79-
fn rfold<B, F>(self, init: B, mut f: F) -> B
75+
fn rfold<B, F>(self, init: B, f: F) -> B
8076
where
8177
F: FnMut(B, Self::Item) -> B,
8278
{
8379
// `rfold` needs ownership, so this can't forward directly.
84-
I::try_rfold(
85-
self.0,
86-
init,
87-
ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp),
88-
)
89-
.0
80+
I::try_rfold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).0
9081
}
9182

9283
#[inline]

library/core/src/iter/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,13 @@ macro_rules! impl_fold_via_try_fold {
362362
};
363363
(@internal $fold:ident -> $try_fold:ident) => {
364364
#[inline]
365-
fn $fold<AAA, FFF>(mut self, init: AAA, mut fold: FFF) -> AAA
365+
fn $fold<AAA, FFF>(mut self, init: AAA, fold: FFF) -> AAA
366366
where
367367
FFF: FnMut(AAA, Self::Item) -> AAA,
368368
{
369-
use crate::const_closure::ConstFnMutClosure;
370369
use crate::ops::NeverShortCircuit;
371370

372-
let fold = ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp);
373-
self.$try_fold(init, fold).0
371+
self.$try_fold(init, NeverShortCircuit::wrap_mut_2(fold)).0
374372
}
375373
};
376374
}

library/core/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,6 @@ mod bool;
376376
mod tuple;
377377
mod unit;
378378

379-
mod const_closure;
380-
381379
#[stable(feature = "core_primitive", since = "1.43.0")]
382380
pub mod primitive;
383381

library/core/src/ops/try_trait.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,18 @@ pub(crate) type ChangeOutputType<T, V> = <<T as Try>::Residual as Residual<V>>::
379379
pub(crate) struct NeverShortCircuit<T>(pub T);
380380

381381
impl<T> NeverShortCircuit<T> {
382-
/// Implementation for building `ConstFnMutClosure` for wrapping the output of a ~const FnMut in a `NeverShortCircuit`.
383382
#[inline]
384-
pub const fn wrap_mut_2_imp<A, B, F: ~const FnMut(A, B) -> T>(
385-
f: &mut F,
386-
(a, b): (A, B),
387-
) -> NeverShortCircuit<T> {
388-
NeverShortCircuit(f(a, b))
383+
pub fn wrap_mut_2<A, B>(
384+
mut f: impl ~const FnMut(A, B) -> T,
385+
) -> impl ~const FnMut(A, B) -> Self {
386+
cfg_if! {
387+
if #[cfg(bootstrap)] {
388+
#[allow(unused_parens)]
389+
(const move |a, b| NeverShortCircuit(f(a, b)))
390+
} else {
391+
const move |a, b| NeverShortCircuit(f(a, b))
392+
}
393+
}
389394
}
390395
}
391396

library/core/src/option.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,8 @@ impl<T> const Default for Option<T> {
19691969
}
19701970

19711971
#[stable(feature = "rust1", since = "1.0.0")]
1972-
impl<T> IntoIterator for Option<T> {
1972+
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
1973+
impl<T> const IntoIterator for Option<T> {
19731974
type Item = T;
19741975
type IntoIter = IntoIter<T>;
19751976

tests/ui/parser/recover-quantified-closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn main() {
77
enum Foo { Bar }
88
fn foo(x: impl Iterator<Item = Foo>) {
99
for <Foo>::Bar in x {}
10-
//~^ ERROR expected one of `const`, `move`, `static`, `|`
10+
//~^ ERROR expected one of `move`, `static`, `|`
1111
//~^^ ERROR `for<...>` binders for closures are experimental
1212
}

tests/ui/parser/recover-quantified-closure.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `const`, `move`, `static`, `|`, or `||`, found `::`
1+
error: expected one of `move`, `static`, `|`, or `||`, found `::`
22
--> $DIR/recover-quantified-closure.rs:9:14
33
|
44
LL | for <Foo>::Bar in x {}
5-
| ^^ expected one of `const`, `move`, `static`, `|`, or `||`
5+
| ^^ expected one of `move`, `static`, `|`, or `||`
66

77
error[E0658]: `for<...>` binders for closures are experimental
88
--> $DIR/recover-quantified-closure.rs:2:5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// check-pass
2+
3+
#![feature(const_trait_impl, const_closures)]
4+
#![allow(incomplete_features)]
5+
6+
const fn test() -> impl ~const Fn() {
7+
const move || {}
8+
}
9+
10+
fn main() {}

0 commit comments

Comments
 (0)