Skip to content

Commit 2eb44e6

Browse files
committed
Use parse_cond_expr instead parse_expr
in the case of if guards
1 parent 4a689da commit 2eb44e6

File tree

3 files changed

+6
-28
lines changed

3 files changed

+6
-28
lines changed

src/librustc_parse/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,7 @@ impl<'a> Parser<'a> {
18201820
let attrs = self.parse_outer_attributes()?;
18211821
let lo = self.token.span;
18221822
let pat = self.parse_top_pat(GateOr::No)?;
1823-
let guard = if self.eat_keyword(kw::If) { Some(self.parse_expr()?) } else { None };
1823+
let guard = if self.eat_keyword(kw::If) { Some(self.parse_cond_expr()?) } else { None };
18241824
let arrow_span = self.token.span;
18251825
self.expect(&token::FatArrow)?;
18261826
let arm_start_span = self.token.span;

src/test/ui/parser/issue-15980.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@ fn main(){
44
let x: io::Result<()> = Ok(());
55
match x {
66
Err(ref e) if e.kind == io::EndOfFile {
7-
//~^ NOTE while parsing this struct
7+
//~^ ERROR expected one of `!`, `.`, `::`, `=>`, `?`, or an operator, found `{`
88
return
9-
//~^ ERROR expected identifier, found keyword `return`
10-
//~| NOTE expected identifier, found keyword
119
}
12-
//~^ NOTE expected one of `.`, `=>`, `?`, or an operator
1310
_ => {}
14-
//~^ ERROR expected one of `.`, `=>`, `?`, or an operator, found reserved identifier `_`
15-
//~| NOTE unexpected token
1611
}
1712
}

src/test/ui/parser/issue-15980.stderr

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
1-
error: expected identifier, found keyword `return`
2-
--> $DIR/issue-15980.rs:8:13
1+
error: expected one of `!`, `.`, `::`, `=>`, `?`, or an operator, found `{`
2+
--> $DIR/issue-15980.rs:6:47
33
|
44
LL | Err(ref e) if e.kind == io::EndOfFile {
5-
| ------------- while parsing this struct
6-
LL |
7-
LL | return
8-
| ^^^^^^ expected identifier, found keyword
9-
|
10-
help: you can escape reserved keywords to use them as identifiers
11-
|
12-
LL | r#return
13-
|
14-
15-
error: expected one of `.`, `=>`, `?`, or an operator, found reserved identifier `_`
16-
--> $DIR/issue-15980.rs:13:9
17-
|
18-
LL | }
19-
| - expected one of `.`, `=>`, `?`, or an operator
20-
LL |
21-
LL | _ => {}
22-
| ^ unexpected token
5+
| ^ expected one of `!`, `.`, `::`, `=>`, `?`, or an operator
236

24-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
258

0 commit comments

Comments
 (0)