Skip to content

Commit 471b929

Browse files
committed
remove mismatched errors when recovered
1 parent 68d28b5 commit 471b929

5 files changed

Lines changed: 19 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
@@ -2655,9 +2655,17 @@ impl<'a> Parser<'a> {
26552655
} else if self.check(exp!(OpenBrace)) || self.token.is_metavar_block() {
26562656
let prev_in_fn_body = self.in_fn_body;
26572657
self.in_fn_body = true;
2658-
let res = self
2659-
.parse_block_common(self.token.span, BlockCheckMode::Default, None)
2660-
.map(|(attrs, body)| (attrs, Some(body)));
2658+
let res = self.parse_block_common(self.token.span, BlockCheckMode::Default, None).map(
2659+
|(attrs, mut body)| {
2660+
if let Some(guar) = self.fn_body_missing_semi_guar.take() {
2661+
body.stmts.push(self.mk_stmt(
2662+
body.span,
2663+
StmtKind::Expr(self.mk_expr(body.span, ExprKind::Err(guar))),
2664+
));
2665+
}
2666+
(attrs, Some(body))
2667+
},
2668+
);
26612669
self.in_fn_body = prev_in_fn_body;
26622670
res?
26632671
} else if self.token == token::Eq {
@@ -3434,6 +3442,7 @@ impl<'a> Parser<'a> {
34343442
return None;
34353443
}
34363444
if let Some((span, guar)) = self.missing_semi_from_binop("const", rhs) {
3445+
self.fn_body_missing_semi_guar = Some(guar);
34373446
Some(self.mk_expr(span, ExprKind::Err(guar)))
34383447
} else {
34393448
None

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ pub struct Parser<'a> {
226226
recovery: Recovery,
227227
/// Whether we're parsing a function body.
228228
in_fn_body: bool,
229+
/// Whether we have detected a missing semicolon in the function body.
230+
pub fn_body_missing_semi_guar: Option<ErrorGuaranteed>,
229231
}
230232

231233
// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with
@@ -376,6 +378,7 @@ impl<'a> Parser<'a> {
376378
current_closure: None,
377379
recovery: Recovery::Allowed,
378380
in_fn_body: false,
381+
fn_body_missing_semi_guar: None,
379382
};
380383

381384
// Make parser point to the first token.

compiler/rustc_parse/src/parser/stmt.rs

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

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)