mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
fix parser mistaking const closures for const item
This commit is contained in:
parent
ad8e1dc286
commit
679dde7338
@ -2108,7 +2108,7 @@ impl<'a> Parser<'a> {
|
|||||||
ClosureBinder::NotPresent
|
ClosureBinder::NotPresent
|
||||||
};
|
};
|
||||||
|
|
||||||
let constness = self.parse_constness(Case::Sensitive);
|
let constness = self.parse_closure_constness(Case::Sensitive);
|
||||||
|
|
||||||
let movability =
|
let movability =
|
||||||
if self.eat_keyword(kw::Static) { Movability::Static } else { Movability::Movable };
|
if self.eat_keyword(kw::Static) { Movability::Static } else { Movability::Movable };
|
||||||
|
@ -739,9 +739,10 @@ impl<'a> Parser<'a> {
|
|||||||
fn check_const_closure(&self) -> bool {
|
fn check_const_closure(&self) -> bool {
|
||||||
self.is_keyword_ahead(0, &[kw::Const])
|
self.is_keyword_ahead(0, &[kw::Const])
|
||||||
&& self.look_ahead(1, |t| match &t.kind {
|
&& self.look_ahead(1, |t| match &t.kind {
|
||||||
token::Ident(kw::Move | kw::Static | kw::Async, _)
|
// async closures do not work with const closures, so we do not parse that here.
|
||||||
| token::OrOr
|
token::Ident(kw::Move | kw::Static, _) | token::OrOr | token::BinOp(token::Or) => {
|
||||||
| token::BinOp(token::Or) => true,
|
true
|
||||||
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -1203,8 +1204,18 @@ impl<'a> Parser<'a> {
|
|||||||
|
|
||||||
/// Parses constness: `const` or nothing.
|
/// Parses constness: `const` or nothing.
|
||||||
fn parse_constness(&mut self, case: Case) -> Const {
|
fn parse_constness(&mut self, case: Case) -> Const {
|
||||||
// Avoid const blocks to be parsed as const items
|
self.parse_constness_(case, false)
|
||||||
if self.look_ahead(1, |t| t != &token::OpenDelim(Delimiter::Brace))
|
}
|
||||||
|
|
||||||
|
/// 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)
|
&& self.eat_keyword_case(kw::Const, case)
|
||||||
{
|
{
|
||||||
Const::Yes(self.prev_token.uninterpolated_span())
|
Const::Yes(self.prev_token.uninterpolated_span())
|
||||||
|
@ -7,6 +7,6 @@ fn main() {
|
|||||||
enum Foo { Bar }
|
enum Foo { Bar }
|
||||||
fn foo(x: impl Iterator<Item = Foo>) {
|
fn foo(x: impl Iterator<Item = Foo>) {
|
||||||
for <Foo>::Bar in x {}
|
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
|
//~^^ ERROR `for<...>` binders for closures are experimental
|
||||||
}
|
}
|
||||||
|
@ -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
|
--> $DIR/recover-quantified-closure.rs:9:14
|
||||||
|
|
|
|
||||||
LL | for <Foo>::Bar in x {}
|
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
|
error[E0658]: `for<...>` binders for closures are experimental
|
||||||
--> $DIR/recover-quantified-closure.rs:2:5
|
--> $DIR/recover-quantified-closure.rs:2:5
|
||||||
|
@ -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() {}
|
Loading…
Reference in New Issue
Block a user