Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extra type error after missing semicolon error #103444

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved

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