Skip to content

Commit fb11399

Browse files
committed
remove mismatched errors when recovered
1 parent e92eb4d commit fb11399

5 files changed

Lines changed: 18 additions & 15 deletions

File tree

compiler/rustc_parse/src/parser/item.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,9 +2699,17 @@ impl<'a> Parser<'a> {
26992699
} else if self.check(exp!(OpenBrace)) || self.token.is_metavar_block() {
27002700
let prev_in_fn_body = self.in_fn_body;
27012701
self.in_fn_body = true;
2702-
let res = self
2703-
.parse_block_common(self.token.span, BlockCheckMode::Default, None)
2704-
.map(|(attrs, body)| (attrs, Some(body)));
2702+
let res = self.parse_block_common(self.token.span, BlockCheckMode::Default, None).map(
2703+
|(attrs, mut body)| {
2704+
if let Some(guar) = self.fn_body_missing_semi_guar.take() {
2705+
body.stmts.push(self.mk_stmt(
2706+
body.span,
2707+
StmtKind::Expr(self.mk_expr(body.span, ExprKind::Err(guar))),
2708+
));
2709+
}
2710+
(attrs, Some(body))
2711+
},
2712+
);
27052713
self.in_fn_body = prev_in_fn_body;
27062714
res?
27072715
} else if self.token == token::Eq {
@@ -3478,6 +3486,7 @@ impl<'a> Parser<'a> {
34783486
return None;
34793487
}
34803488
if let Some((span, guar)) = self.missing_semi_from_binop("const", rhs) {
3489+
self.fn_body_missing_semi_guar = Some(guar);
34813490
Some(self.mk_expr(span, ExprKind::Err(guar)))
34823491
} else {
34833492
None

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ pub struct Parser<'a> {
236236
recovery: Recovery = Recovery::Allowed,
237237
/// Whether we're parsing a function body.
238238
in_fn_body: bool = false,
239+
/// Whether we have detected a missing semicolon in the function body.
240+
pub fn_body_missing_semi_guar: Option<ErrorGuaranteed> = None,
239241
}
240242

241243
// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,10 +930,10 @@ impl<'a> Parser<'a> {
930930
LocalKind::Decl => return None,
931931
};
932932
if let Some((span, guar)) = self.missing_semi_from_binop("`let` binding", expr) {
933+
self.fn_body_missing_semi_guar = Some(guar);
933934
*expr = self.mk_expr(span, ExprKind::Err(guar));
934935
return Some(guar);
935936
}
936-
937937
None
938938
}
939939

tests/ui/parser/const-recover-semi-issue-151149.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const fn val() -> u8 {
1515
const C: u8 = u8::const_default()
1616
&1 //~ ERROR expected `;`, found keyword `const`
1717

18-
const fn foo() -> &'static u8 { //~ ERROR mismatched types
18+
const fn foo() -> &'static u8 {
1919
const C: u8 = u8::const_default() //~ ERROR expected `;`
2020
&C
2121
}
@@ -30,7 +30,7 @@ const fn baz() -> u8 { //~ ERROR mismatched types
3030
+ val() //~ ERROR expected `;`, found `}`
3131
}
3232

33-
fn buzz() {
33+
fn buzz() -> &'static u8 {
3434
let r = 1 //~ ERROR expected `;`
3535
&r
3636
}

tests/ui/parser/const-recover-semi-issue-151149.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@ help: you may have meant to write a `;` to terminate the `let` binding earlier
7171
LL | let r = 1;
7272
| +
7373

74-
error[E0308]: mismatched types
75-
--> $DIR/const-recover-semi-issue-151149.rs:18:19
76-
|
77-
LL | const fn foo() -> &'static u8 {
78-
| --- ^^^^^^^^^^^ expected `&u8`, found `()`
79-
| |
80-
| implicitly returns `()` as its body has no tail or `return` expression
81-
8274
error[E0308]: mismatched types
8375
--> $DIR/const-recover-semi-issue-151149.rs:23:19
8476
|
@@ -95,6 +87,6 @@ LL | const fn baz() -> u8 {
9587
| |
9688
| implicitly returns `()` as its body has no tail or `return` expression
9789

98-
error: aborting due to 8 previous errors
90+
error: aborting due to 7 previous errors
9991

10092
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)