Skip to content

Commit 8c94b2c

Browse files
authored
Rollup merge of #91634 - terrarier2111:fix-recover-from-variant-ice, r=nagisa
Do not attempt to suggest help for overly malformed struct/function call This fixes: #91461
2 parents 876f9ff + b4c4bc0 commit 8c94b2c

File tree

3 files changed

+64
-20
lines changed

3 files changed

+64
-20
lines changed

compiler/rustc_parse/src/parser/expr.rs

+27-20
Original file line numberDiff line numberDiff line change
@@ -1100,30 +1100,37 @@ impl<'a> Parser<'a> {
11001100
snapshot.bump(); // `(`
11011101
match snapshot.parse_struct_fields(path, false, token::Paren) {
11021102
Ok((fields, ..)) if snapshot.eat(&token::CloseDelim(token::Paren)) => {
1103-
// We have are certain we have `Enum::Foo(a: 3, b: 4)`, suggest
1103+
// We are certain we have `Enum::Foo(a: 3, b: 4)`, suggest
11041104
// `Enum::Foo { a: 3, b: 4 }` or `Enum::Foo(3, 4)`.
11051105
*self = snapshot;
11061106
let close_paren = self.prev_token.span;
11071107
let span = lo.to(self.prev_token.span);
1108-
err.cancel();
1109-
self.struct_span_err(
1110-
span,
1111-
"invalid `struct` delimiters or `fn` call arguments",
1112-
)
1113-
.multipart_suggestion(
1114-
&format!("if `{}` is a struct, use braces as delimiters", name),
1115-
vec![(open_paren, " { ".to_string()), (close_paren, " }".to_string())],
1116-
Applicability::MaybeIncorrect,
1117-
)
1118-
.multipart_suggestion(
1119-
&format!("if `{}` is a function, use the arguments directly", name),
1120-
fields
1121-
.into_iter()
1122-
.map(|field| (field.span.until(field.expr.span), String::new()))
1123-
.collect(),
1124-
Applicability::MaybeIncorrect,
1125-
)
1126-
.emit();
1108+
if !fields.is_empty() {
1109+
err.cancel();
1110+
let mut err = self.struct_span_err(
1111+
span,
1112+
"invalid `struct` delimiters or `fn` call arguments",
1113+
);
1114+
err.multipart_suggestion(
1115+
&format!("if `{}` is a struct, use braces as delimiters", name),
1116+
vec![
1117+
(open_paren, " { ".to_string()),
1118+
(close_paren, " }".to_string()),
1119+
],
1120+
Applicability::MaybeIncorrect,
1121+
);
1122+
err.multipart_suggestion(
1123+
&format!("if `{}` is a function, use the arguments directly", name),
1124+
fields
1125+
.into_iter()
1126+
.map(|field| (field.span.until(field.expr.span), String::new()))
1127+
.collect(),
1128+
Applicability::MaybeIncorrect,
1129+
);
1130+
err.emit();
1131+
} else {
1132+
err.emit();
1133+
}
11271134
return Some(self.mk_expr_err(span));
11281135
}
11291136
Ok(_) => {}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
a(_:b:,)
3+
//~^ ERROR: expected identifier, found reserved identifier `_`
4+
//~| ERROR: expected type, found `,`
5+
//~| ERROR: expected type, found `,`
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: expected identifier, found reserved identifier `_`
2+
--> $DIR/issue-91461.rs:2:7
3+
|
4+
LL | a(_:b:,)
5+
| ^ expected identifier, found reserved identifier
6+
7+
error: expected type, found `,`
8+
--> $DIR/issue-91461.rs:2:11
9+
|
10+
LL | a(_:b:,)
11+
| - -^ expected type
12+
| | |
13+
| | tried to parse a type due to this type ascription
14+
| while parsing this struct
15+
|
16+
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
17+
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
18+
19+
error: expected type, found `,`
20+
--> $DIR/issue-91461.rs:2:11
21+
|
22+
LL | a(_:b:,)
23+
| -^ expected type
24+
| |
25+
| tried to parse a type due to this type ascription
26+
|
27+
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
28+
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
29+
30+
error: aborting due to 3 previous errors
31+

0 commit comments

Comments
 (0)