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

[WIP; Crater] Use parse_cond_expr instead parse_expr in if guards #74315

Closed
wants to merge 2 commits into from
Closed
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 src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,7 @@ impl<'a> Parser<'a> {
let attrs = self.parse_outer_attributes()?;
let lo = self.token.span;
let pat = self.parse_top_pat(GateOr::No)?;
let guard = if self.eat_keyword(kw::If) { Some(self.parse_expr()?) } else { None };
let guard = if self.eat_keyword(kw::If) { Some(self.parse_cond_expr()?) } else { None };
let arrow_span = self.token.span;
self.expect(&token::FatArrow)?;
let arm_start_span = self.token.span;
Expand Down
7 changes: 1 addition & 6 deletions src/test/ui/parser/issue-15980.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ fn main(){
let x: io::Result<()> = Ok(());
match x {
Err(ref e) if e.kind == io::EndOfFile {
//~^ NOTE while parsing this struct
//~^ ERROR expected one of `!`, `.`, `::`, `=>`, `?`, or an operator, found `{`
return
//~^ ERROR expected identifier, found keyword `return`
//~| NOTE expected identifier, found keyword
}
//~^ NOTE expected one of `.`, `=>`, `?`, or an operator
_ => {}
//~^ ERROR expected one of `.`, `=>`, `?`, or an operator, found reserved identifier `_`
//~| NOTE unexpected token
}
}
25 changes: 4 additions & 21 deletions src/test/ui/parser/issue-15980.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
error: expected identifier, found keyword `return`
--> $DIR/issue-15980.rs:8:13
error: expected one of `!`, `.`, `::`, `=>`, `?`, or an operator, found `{`
--> $DIR/issue-15980.rs:6:47
|
LL | Err(ref e) if e.kind == io::EndOfFile {
| ------------- while parsing this struct
LL |
LL | return
| ^^^^^^ expected identifier, found keyword
|
help: you can escape reserved keywords to use them as identifiers
|
LL | r#return
|

error: expected one of `.`, `=>`, `?`, or an operator, found reserved identifier `_`
--> $DIR/issue-15980.rs:13:9
|
LL | }
| - expected one of `.`, `=>`, `?`, or an operator
LL |
LL | _ => {}
| ^ unexpected token
| ^ expected one of `!`, `.`, `::`, `=>`, `?`, or an operator

error: aborting due to 2 previous errors
error: aborting due to previous error

13 changes: 13 additions & 0 deletions src/test/ui/rfc-2294-if-let-guard/non-implemented.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This function is to check whether `if-let` guard error points to
// `let_chains` feature gate, which is wrong and confuse people.
pub fn foo(a: &[u8], b: bool) -> bool {
match b {
true if let [1, 2, 3, ..] = a => true,
Comment on lines +1 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it is a subset of "let chains" (or rather, let expressions in more positions), at most you should change what feature-gate it points to.

Looking around at the let chains tests, you want all of these cases to emit feature-gate errors:

fn _macros() {
macro_rules! noop_expr { ($e:expr) => {}; }
noop_expr!((let 0 = 1));
//~^ ERROR `let` expressions in this position are experimental [E0658]
macro_rules! use_expr {
($e:expr) => {
if $e {}
while $e {}
}
}
use_expr!((let 0 = 1 && 0 == 0));
//~^ ERROR `let` expressions in this position are experimental [E0658]
//~| ERROR `let` expressions are not supported here
//~| ERROR `let` expressions are not supported here
use_expr!((let 0 = 1));
//~^ ERROR `let` expressions in this position are experimental [E0658]
//~| ERROR `let` expressions are not supported here
//~| ERROR `let` expressions are not supported here
#[cfg(FALSE)] (let 0 = 1);
//~^ ERROR `let` expressions in this position are experimental [E0658]
use_expr!(let 0 = 1);
//~^ ERROR no rules expected the token `let`
// ^--- FIXME(53667): Consider whether `Let` can be added to `ident_can_begin_expr`.
}

Note that noop_expr! and #[cfg(FALSE)] must emit feature-gate errors despite those AST nodes never reaching HIR lowering.

I think you should copy that entire test, remove _while, and then put every if let in a guard, e.g.:

match () { () if let ... = ... => {} }

//~^ ERROR `let` expressions are not supported here
//~^^ NOTE only supported directly in conditions of `if`- and `while`-expressions
//~^^^ NOTE as well as when nested within `&&` and parenthesis in those conditions
_ => false,
}
}

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/rfc-2294-if-let-guard/non-implemented.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: `let` expressions are not supported here
--> $DIR/non-implemented.rs:5:17
|
LL | true if let [1, 2, 3, ..] = a => true,
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: only supported directly in conditions of `if`- and `while`-expressions
= note: as well as when nested within `&&` and parenthesis in those conditions

error: aborting due to previous error