Skip to content

Commit

Permalink
Rollup merge of rust-lang#103444 - chenyukang:yukang/fix-103425-extra…
Browse files Browse the repository at this point in the history
…-diag, r=davidtwco

Remove extra type error after missing semicolon error

Fixes rust-lang#103425
  • Loading branch information
Dylan-DPC authored Oct 25, 2022
2 parents af5ef92 + f54c336 commit 5a8e481
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
65 changes: 36 additions & 29 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,39 +553,46 @@ impl<'a> Parser<'a> {
match stmt.kind {
// Expression without semicolon.
StmtKind::Expr(ref mut expr)
if self.token != token::Eof && classify::expr_requires_semi_to_be_stmt(expr) =>
{
if self.token != token::Eof && classify::expr_requires_semi_to_be_stmt(expr) => {
// Just check for errors and recover; do not eat semicolon yet.
if let Err(mut e) =
self.expect_one_of(&[], &[token::Semi, token::CloseDelim(Delimiter::Brace)])
{
if let TokenKind::DocComment(..) = self.token.kind {
if let Ok(snippet) = self.span_to_snippet(self.token.span) {
let sp = self.token.span;
let marker = &snippet[..3];
let (comment_marker, doc_comment_marker) = marker.split_at(2);

e.span_suggestion(
sp.with_hi(sp.lo() + BytePos(marker.len() as u32)),
&format!(
"add a space before `{}` to use a regular comment",
doc_comment_marker,
),
format!("{} {}", comment_marker, doc_comment_marker),
Applicability::MaybeIncorrect,
);
// `expect_one_of` returns PResult<'a, bool /* recovered */>
let replace_with_err =
match self.expect_one_of(&[], &[token::Semi, token::CloseDelim(Delimiter::Brace)]) {
// Recover from parser, skip type error to avoid extra errors.
Ok(true) => true,
Err(mut e) => {
if let TokenKind::DocComment(..) = self.token.kind &&
let Ok(snippet) = self.span_to_snippet(self.token.span) {
let sp = self.token.span;
let marker = &snippet[..3];
let (comment_marker, doc_comment_marker) = marker.split_at(2);

e.span_suggestion(
sp.with_hi(sp.lo() + BytePos(marker.len() as u32)),
&format!(
"add a space before `{}` to use a regular comment",
doc_comment_marker,
),
format!("{} {}", comment_marker, doc_comment_marker),
Applicability::MaybeIncorrect,
);
}
}
if let Err(mut e) =
self.check_mistyped_turbofish_with_multiple_type_params(e, expr)
{
if recover.no() {
return Err(e);

if let Err(mut e) =
self.check_mistyped_turbofish_with_multiple_type_params(e, expr)
{
if recover.no() {
return Err(e);
}
e.emit();
self.recover_stmt();
}
e.emit();
self.recover_stmt();
true
}
// Don't complain about type errors in body tail after parse error (#57383).
_ => false
};
if replace_with_err {
// We already emitted an error, so don't emit another type error
let sp = expr.span.to(self.prev_token.span);
*expr = self.mk_expr_err(sp);
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/parser/issue-103425.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fn f() -> f32 {
3
//~^ ERROR expected `;`
5.0
}

fn k() -> f32 {
2_u32
//~^ ERROR expected `;`
3_i8
//~^ ERROR expected `;`
5.0
}

fn main() {}
29 changes: 29 additions & 0 deletions src/test/ui/parser/issue-103425.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error: expected `;`, found `5.0`
--> $DIR/issue-103425.rs:2:6
|
LL | 3
| ^ help: add `;` here
LL |
LL | 5.0
| --- unexpected token

error: expected `;`, found `3_i8`
--> $DIR/issue-103425.rs:8:10
|
LL | 2_u32
| ^ help: add `;` here
LL |
LL | 3_i8
| ---- unexpected token

error: expected `;`, found `5.0`
--> $DIR/issue-103425.rs:10:9
|
LL | 3_i8
| ^ help: add `;` here
LL |
LL | 5.0
| --- unexpected token

error: aborting due to 3 previous errors

0 comments on commit 5a8e481

Please sign in to comment.