Skip to content

Commit b79fc92

Browse files
fix ICE when parsing lifetime as function argument
1 parent d5f9c40 commit b79fc92

File tree

6 files changed

+60
-15
lines changed

6 files changed

+60
-15
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,9 +1457,9 @@ impl<'a> Parser<'a> {
14571457
} else if self.check(&token::OpenDelim(token::Brace)) || self.token.is_whole_block() {
14581458
self.parse_block_expr(label, lo, BlockCheckMode::Default, attrs)
14591459
} else if !ate_colon && (self.check(&TokenKind::Comma) || self.check(&TokenKind::Gt)) {
1460-
// We're probably inside of a `Path<'a>` that needs a turbofish, so suppress the
1461-
// "must be followed by a colon" error, and the "expected one of" error.
1462-
self.diagnostic().delay_span_bug(lo, "this label wasn't parsed correctly");
1460+
// We're probably inside of a `Path<'a>` that needs a turbofish
1461+
let msg = "expected `while`, `for`, `loop` or `{` after a label";
1462+
self.struct_span_err(self.token.span, msg).span_label(self.token.span, msg).emit();
14631463
consume_colon = false;
14641464
Ok(self.mk_expr_err(lo))
14651465
} else {

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -865,14 +865,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
865865
),
866866
..
867867
}) => {
868-
// We have a situation like `while Some(0) = value.get(0) {`, where `while let`
869-
// was more likely intended.
870-
err.span_suggestion_verbose(
871-
expr.span.shrink_to_lo(),
872-
"you might have meant to use pattern destructuring",
873-
"let ".to_string(),
874-
Applicability::MachineApplicable,
875-
);
868+
// Check if our lhs is a child of the condition of a while loop
869+
let expr_is_ancestor = std::iter::successors(Some(lhs.hir_id), |id| {
870+
self.tcx.hir().find_parent_node(*id)
871+
})
872+
.take_while(|id| *id != parent)
873+
.any(|id| id == expr.hir_id);
874+
// if it is, then we have a situation like `while Some(0) = value.get(0) {`,
875+
// where `while let` was more likely intended.
876+
if expr_is_ancestor {
877+
err.span_suggestion_verbose(
878+
expr.span.shrink_to_lo(),
879+
"you might have meant to use pattern destructuring",
880+
"let ".to_string(),
881+
Applicability::MachineApplicable,
882+
);
883+
}
876884
break;
877885
}
878886
hir::Node::Item(_)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
fn main() {
22
f<'a,>
33
//~^ ERROR expected
4+
//~| ERROR expected
5+
}
6+
7+
fn bar(a: usize, b: usize) -> usize {
8+
a + b
9+
}
10+
11+
fn foo() {
12+
let x = 1;
13+
bar('y, x);
14+
//~^ ERROR expected
415
}

src/test/ui/parser/issues/issue-93282.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error: expected `while`, `for`, `loop` or `{` after a label
2+
--> $DIR/issue-93282.rs:2:9
3+
|
4+
LL | f<'a,>
5+
| ^ expected `while`, `for`, `loop` or `{` after a label
6+
17
error: expected one of `.`, `:`, `;`, `?`, `for`, `loop`, `while`, `{`, `}`, or an operator, found `,`
28
--> $DIR/issue-93282.rs:2:9
39
|
@@ -9,5 +15,11 @@ help: use `::<...>` instead of `<...>` to specify lifetime, type, or const argum
915
LL | f::<'a,>
1016
| ++
1117

12-
error: aborting due to previous error
18+
error: expected `while`, `for`, `loop` or `{` after a label
19+
--> $DIR/issue-93282.rs:13:11
20+
|
21+
LL | bar('y, x);
22+
| ^ expected `while`, `for`, `loop` or `{` after a label
23+
24+
error: aborting due to 3 previous errors
1325

src/test/ui/parser/require-parens-for-chained-comparison.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ fn main() {
2222
let _ = f<'_, i8>();
2323
//~^ ERROR expected one of
2424
//~| HELP use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
25+
//~| ERROR expected
2526

2627
f<'_>();
2728
//~^ comparison operators cannot be chained
2829
//~| HELP use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
30+
//~| ERROR expected
2931

3032
let _ = f<u8>;
3133
//~^ ERROR comparison operators cannot be chained

src/test/ui/parser/require-parens-for-chained-comparison.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ help: use `::<...>` instead of `<...>` to specify lifetime, type, or const argum
5353
LL | let _ = f::<u8, i8>();
5454
| ++
5555

56+
error: expected `while`, `for`, `loop` or `{` after a label
57+
--> $DIR/require-parens-for-chained-comparison.rs:22:17
58+
|
59+
LL | let _ = f<'_, i8>();
60+
| ^ expected `while`, `for`, `loop` or `{` after a label
61+
5662
error: expected one of `.`, `:`, `;`, `?`, `else`, `for`, `loop`, `while`, `{`, or an operator, found `,`
5763
--> $DIR/require-parens-for-chained-comparison.rs:22:17
5864
|
@@ -64,8 +70,14 @@ help: use `::<...>` instead of `<...>` to specify lifetime, type, or const argum
6470
LL | let _ = f::<'_, i8>();
6571
| ++
6672

73+
error: expected `while`, `for`, `loop` or `{` after a label
74+
--> $DIR/require-parens-for-chained-comparison.rs:27:9
75+
|
76+
LL | f<'_>();
77+
| ^ expected `while`, `for`, `loop` or `{` after a label
78+
6779
error: comparison operators cannot be chained
68-
--> $DIR/require-parens-for-chained-comparison.rs:26:6
80+
--> $DIR/require-parens-for-chained-comparison.rs:27:6
6981
|
7082
LL | f<'_>();
7183
| ^ ^
@@ -76,13 +88,13 @@ LL | f::<'_>();
7688
| ++
7789

7890
error: comparison operators cannot be chained
79-
--> $DIR/require-parens-for-chained-comparison.rs:30:14
91+
--> $DIR/require-parens-for-chained-comparison.rs:32:14
8092
|
8193
LL | let _ = f<u8>;
8294
| ^ ^
8395
|
8496
= help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
8597
= help: or use `(...)` if you meant to specify fn arguments
8698

87-
error: aborting due to 8 previous errors
99+
error: aborting due to 10 previous errors
88100

0 commit comments

Comments
 (0)