Skip to content

Commit a49368f

Browse files
committed
Correctly handle while-let-chains
1 parent 9455259 commit a49368f

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

Diff for: compiler/rustc_parse/src/lexer/tokentrees.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'a> TokenTreesReader<'a> {
129129
while parser.token != token::Eof {
130130
if let Err(diff_err) = parser.err_diff_marker() {
131131
diff_errs.push(diff_err);
132-
} else if parser.token.is_keyword(kw::If) {
132+
} else if parser.is_keyword_ahead(0, &[kw::If, kw::While]) {
133133
in_cond = true;
134134
} else if matches!(
135135
parser.token.kind,

Diff for: compiler/rustc_parse/src/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ impl<'a> Parser<'a> {
11151115
}
11161116

11171117
/// Returns whether any of the given keywords are `dist` tokens ahead of the current one.
1118-
fn is_keyword_ahead(&self, dist: usize, kws: &[Symbol]) -> bool {
1118+
pub fn is_keyword_ahead(&self, dist: usize, kws: &[Symbol]) -> bool {
11191119
self.look_ahead(dist, |t| kws.iter().any(|&kw| t.is_keyword(kw)))
11201120
}
11211121

Diff for: tests/ui/parser/brace-in-let-chain.rs

+21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ fn main() {
99
}
1010
}
1111

12+
fn quux() {
13+
while let () = ()
14+
&& let () = () { //~ERROR: found a `{` in the middle of a let-chain
15+
&& let () = ()
16+
{
17+
}
18+
}
19+
20+
fn foobar() {
21+
while false {}
22+
{
23+
&& let () = ()
24+
}
25+
26+
fn fubar() {
27+
while false {
28+
{
29+
&& let () = ()
30+
}
31+
}
32+
1233
fn qux() {
1334
let foo = false;
1435
match foo {

Diff for: tests/ui/parser/brace-in-let-chain.stderr

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
error: this file contains an unclosed delimiter
2-
--> $DIR/brace-in-let-chain.rs:37:54
2+
--> $DIR/brace-in-let-chain.rs:58:54
33
|
44
LL | fn main() {
55
| - unclosed delimiter
66
...
7+
LL | fn quux() {
8+
| - unclosed delimiter
9+
...
10+
LL | fn foobar() {
11+
| - unclosed delimiter
12+
...
13+
LL | fn fubar() {
14+
| - unclosed delimiter
15+
...
716
LL | fn qux() {
817
| - unclosed delimiter
918
...
@@ -24,6 +33,23 @@ LL | }
2433
LL | }
2534
| ^
2635

36+
error: found a `{` in the middle of a let-chain
37+
--> $DIR/brace-in-let-chain.rs:14:24
38+
|
39+
LL | && let () = () {
40+
| ^
41+
|
42+
note: you might have meant to continue the let-chain here
43+
--> $DIR/brace-in-let-chain.rs:15:9
44+
|
45+
LL | && let () = ()
46+
| ^^^^^^
47+
help: consider removing this brace to parse the `let` as part of the same chain
48+
|
49+
LL - && let () = () {
50+
LL + && let () = ()
51+
|
52+
2753
error: found a `{` in the middle of a let-chain
2854
--> $DIR/brace-in-let-chain.rs:6:24
2955
|
@@ -41,5 +67,5 @@ LL - && let () = () {
4167
LL + && let () = ()
4268
|
4369

44-
error: aborting due to 2 previous errors
70+
error: aborting due to 3 previous errors
4571

0 commit comments

Comments
 (0)