Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ConstFnMutClosure with const closures #107551

Merged
merged 2 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2108,7 +2108,7 @@ impl<'a> Parser<'a> {
ClosureBinder::NotPresent
};

let constness = self.parse_constness(Case::Sensitive);
let constness = self.parse_closure_constness(Case::Sensitive);

let movability =
if self.eat_keyword(kw::Static) { Movability::Static } else { Movability::Movable };
Expand Down
21 changes: 16 additions & 5 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,10 @@ impl<'a> Parser<'a> {
fn check_const_closure(&self) -> bool {
self.is_keyword_ahead(0, &[kw::Const])
&& self.look_ahead(1, |t| match &t.kind {
token::Ident(kw::Move | kw::Static | kw::Async, _)
| token::OrOr
| token::BinOp(token::Or) => true,
// async closures do not work with const closures, so we do not parse that here.
token::Ident(kw::Move | kw::Static, _) | token::OrOr | token::BinOp(token::Or) => {
true
}
_ => false,
})
}
Expand Down Expand Up @@ -1203,8 +1204,18 @@ impl<'a> Parser<'a> {

/// Parses constness: `const` or nothing.
fn parse_constness(&mut self, case: Case) -> Const {
// Avoid const blocks to be parsed as const items
if self.look_ahead(1, |t| t != &token::OpenDelim(Delimiter::Brace))
self.parse_constness_(case, false)
}

/// Parses constness for closures
fn parse_closure_constness(&mut self, case: Case) -> Const {
self.parse_constness_(case, true)
}

fn parse_constness_(&mut self, case: Case, is_closure: bool) -> Const {
// Avoid const blocks and const closures to be parsed as const items
if (self.check_const_closure() == is_closure)
&& self.look_ahead(1, |t| t != &token::OpenDelim(Delimiter::Brace))
&& self.eat_keyword_case(kw::Const, case)
{
Const::Yes(self.prev_token.uninterpolated_span())
Expand Down
13 changes: 1 addition & 12 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#![stable(feature = "rust1", since = "1.0.0")]

use crate::const_closure::ConstFnMutClosure;
use crate::marker::Destruct;

use self::Ordering::*;
Expand Down Expand Up @@ -1291,17 +1290,7 @@ where
F: ~const Destruct,
K: ~const Destruct,
{
const fn imp<T, F: ~const FnMut(&T) -> K, K: ~const Ord>(
f: &mut F,
(v1, v2): (&T, &T),
) -> Ordering
where
T: ~const Destruct,
K: ~const Destruct,
{
f(v1).cmp(&f(v2))
}
max_by(v1, v2, ConstFnMutClosure::new(&mut f, imp))
max_by(v1, v2, const |v1, v2| f(v1).cmp(&f(v2)))
}

// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
Expand Down
78 changes: 0 additions & 78 deletions library/core/src/const_closure.rs

This file was deleted.

6 changes: 2 additions & 4 deletions library/core/src/iter/adapters/array_chunks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::array;
use crate::const_closure::ConstFnMutClosure;
use crate::iter::{ByRefSized, FusedIterator, Iterator, TrustedRandomAccessNoCoerce};
use crate::mem::{self, MaybeUninit};
use crate::ops::{ControlFlow, NeverShortCircuit, Try};
Expand Down Expand Up @@ -189,13 +188,12 @@ where
I: Iterator,
{
#[inline]
default fn fold<B, F>(mut self, init: B, mut f: F) -> B
default fn fold<B, F>(mut self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
let fold = ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp);
self.try_fold(init, fold).0
self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).0
}
}

Expand Down
19 changes: 5 additions & 14 deletions library/core/src/iter/adapters/by_ref_sized.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
const_closure::ConstFnMutClosure,
ops::{NeverShortCircuit, Try},
};
use crate::ops::{NeverShortCircuit, Try};

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

#[inline]
fn fold<B, F>(self, init: B, mut f: F) -> B
fn fold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
// `fold` needs ownership, so this can't forward directly.
I::try_fold(self.0, init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp))
.0
I::try_fold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).0
}

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

#[inline]
fn rfold<B, F>(self, init: B, mut f: F) -> B
fn rfold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
// `rfold` needs ownership, so this can't forward directly.
I::try_rfold(
self.0,
init,
ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp),
)
.0
I::try_rfold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).0
}

#[inline]
Expand Down
6 changes: 2 additions & 4 deletions library/core/src/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,13 @@ macro_rules! impl_fold_via_try_fold {
};
(@internal $fold:ident -> $try_fold:ident) => {
#[inline]
fn $fold<AAA, FFF>(mut self, init: AAA, mut fold: FFF) -> AAA
fn $fold<AAA, FFF>(mut self, init: AAA, fold: FFF) -> AAA
where
FFF: FnMut(AAA, Self::Item) -> AAA,
{
use crate::const_closure::ConstFnMutClosure;
use crate::ops::NeverShortCircuit;

let fold = ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp);
self.$try_fold(init, fold).0
self.$try_fold(init, NeverShortCircuit::wrap_mut_2(fold)).0
}
};
}
Expand Down
2 changes: 0 additions & 2 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,6 @@ mod bool;
mod tuple;
mod unit;

mod const_closure;

#[stable(feature = "core_primitive", since = "1.43.0")]
pub mod primitive;

Expand Down
17 changes: 11 additions & 6 deletions library/core/src/ops/try_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,18 @@ pub(crate) type ChangeOutputType<T, V> = <<T as Try>::Residual as Residual<V>>::
pub(crate) struct NeverShortCircuit<T>(pub T);

impl<T> NeverShortCircuit<T> {
/// Implementation for building `ConstFnMutClosure` for wrapping the output of a ~const FnMut in a `NeverShortCircuit`.
#[inline]
pub const fn wrap_mut_2_imp<A, B, F: ~const FnMut(A, B) -> T>(
f: &mut F,
(a, b): (A, B),
) -> NeverShortCircuit<T> {
NeverShortCircuit(f(a, b))
pub fn wrap_mut_2<A, B>(
mut f: impl ~const FnMut(A, B) -> T,
) -> impl ~const FnMut(A, B) -> Self {
cfg_if! {
if #[cfg(bootstrap)] {
#[allow(unused_parens)]
(const move |a, b| NeverShortCircuit(f(a, b)))
} else {
const move |a, b| NeverShortCircuit(f(a, b))
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/recover-quantified-closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ fn main() {
enum Foo { Bar }
fn foo(x: impl Iterator<Item = Foo>) {
for <Foo>::Bar in x {}
//~^ ERROR expected one of `const`, `move`, `static`, `|`
//~^ ERROR expected one of `move`, `static`, `|`
//~^^ ERROR `for<...>` binders for closures are experimental
}
4 changes: 2 additions & 2 deletions tests/ui/parser/recover-quantified-closure.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: expected one of `const`, `move`, `static`, `|`, or `||`, found `::`
error: expected one of `move`, `static`, `|`, or `||`, found `::`
--> $DIR/recover-quantified-closure.rs:9:14
|
LL | for <Foo>::Bar in x {}
| ^^ expected one of `const`, `move`, `static`, `|`, or `||`
| ^^ expected one of `move`, `static`, `|`, or `||`

error[E0658]: `for<...>` binders for closures are experimental
--> $DIR/recover-quantified-closure.rs:2:5
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/rfc-2632-const-trait-impl/const-closure-parse-not-item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// check-pass

#![feature(const_trait_impl, const_closures)]
#![allow(incomplete_features)]

const fn test() -> impl ~const Fn() {
const move || {}
}

fn main() {}