mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-07 04:23:30 +00:00
Recover some items that expect braces and don't take semicolons
This commit is contained in:
parent
f3fafbb006
commit
4d0d688a3c
@ -158,3 +158,6 @@ parser_remove_let = expected pattern, found `let`
|
||||
|
||||
parser_use_eq_instead = unexpected `==`
|
||||
.suggestion = try using `=` instead
|
||||
|
||||
parser_use_empty_block_not_semi = expected { "`{}`" }, found `;`
|
||||
.suggestion = try using { "`{}`" } instead
|
||||
|
@ -745,6 +745,14 @@ pub(crate) struct UseEqInstead {
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(parser::use_empty_block_not_semi)]
|
||||
pub(crate) struct UseEmptyBlockNotSemi {
|
||||
#[primary_span]
|
||||
#[suggestion_hidden(applicability = "machine-applicable", code = "{{}}")]
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
// SnapshotParser is used to create a snapshot of the parser
|
||||
// without causing duplicate errors being emitted when the `Parser`
|
||||
// is dropped.
|
||||
|
@ -1,4 +1,4 @@
|
||||
use super::diagnostics::{dummy_arg, ConsumeClosingDelim, Error};
|
||||
use super::diagnostics::{dummy_arg, ConsumeClosingDelim, Error, UseEmptyBlockNotSemi};
|
||||
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
|
||||
use super::{AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, TrailingToken};
|
||||
|
||||
@ -664,6 +664,14 @@ impl<'a> Parser<'a> {
|
||||
mut parse_item: impl FnMut(&mut Parser<'a>) -> PResult<'a, Option<Option<T>>>,
|
||||
) -> PResult<'a, Vec<T>> {
|
||||
let open_brace_span = self.token.span;
|
||||
|
||||
// Recover `impl Ty;` instead of `impl Ty {}`
|
||||
if self.token == TokenKind::Semi {
|
||||
self.sess.emit_err(UseEmptyBlockNotSemi { span: self.token.span });
|
||||
self.bump();
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
self.expect(&token::OpenDelim(Delimiter::Brace))?;
|
||||
attrs.extend(self.parse_inner_attributes()?);
|
||||
|
||||
@ -1305,12 +1313,19 @@ impl<'a> Parser<'a> {
|
||||
let mut generics = self.parse_generics()?;
|
||||
generics.where_clause = self.parse_where_clause()?;
|
||||
|
||||
let (variants, _) = self
|
||||
.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant())
|
||||
.map_err(|e| {
|
||||
self.recover_stmt();
|
||||
e
|
||||
})?;
|
||||
// Possibly recover `enum Foo;` instead of `enum Foo {}`
|
||||
let (variants, _) = if self.token == TokenKind::Semi {
|
||||
self.sess.emit_err(UseEmptyBlockNotSemi { span: self.token.span });
|
||||
self.bump();
|
||||
(vec![], false)
|
||||
} else {
|
||||
self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant()).map_err(
|
||||
|e| {
|
||||
self.recover_stmt();
|
||||
e
|
||||
},
|
||||
)?
|
||||
};
|
||||
|
||||
let enum_definition = EnumDef { variants: variants.into_iter().flatten().collect() };
|
||||
Ok((id, ItemKind::Enum(enum_definition, generics)))
|
||||
|
@ -1 +1,4 @@
|
||||
impl Foo; //~ ERROR expected one of `!`, `(`, `+`, `::`, `<`, `for`, `where`, or `{`, found `;`
|
||||
struct Foo;
|
||||
impl Foo; //~ ERROR expected `{}`, found `;`
|
||||
|
||||
fn main() {}
|
||||
|
@ -1,8 +1,10 @@
|
||||
error: expected one of `!`, `(`, `+`, `::`, `<`, `for`, `where`, or `{`, found `;`
|
||||
--> $DIR/empty-impl-semicolon.rs:1:9
|
||||
error: expected `{}`, found `;`
|
||||
--> $DIR/empty-impl-semicolon.rs:2:9
|
||||
|
|
||||
LL | impl Foo;
|
||||
| ^ expected one of 8 possible tokens
|
||||
| ^
|
||||
|
|
||||
= help: try using `{}` instead
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
10
src/test/ui/parser/item-needs-block.rs
Normal file
10
src/test/ui/parser/item-needs-block.rs
Normal file
@ -0,0 +1,10 @@
|
||||
trait Trait;
|
||||
//~^ ERROR expected `{}`, found `;`
|
||||
|
||||
impl Trait for ();
|
||||
//~^ ERROR expected `{}`, found `;`
|
||||
|
||||
enum Enum;
|
||||
//~^ ERROR expected `{}`, found `;`
|
||||
|
||||
fn main() {}
|
26
src/test/ui/parser/item-needs-block.stderr
Normal file
26
src/test/ui/parser/item-needs-block.stderr
Normal file
@ -0,0 +1,26 @@
|
||||
error: expected `{}`, found `;`
|
||||
--> $DIR/item-needs-block.rs:1:12
|
||||
|
|
||||
LL | trait Trait;
|
||||
| ^
|
||||
|
|
||||
= help: try using `{}` instead
|
||||
|
||||
error: expected `{}`, found `;`
|
||||
--> $DIR/item-needs-block.rs:4:18
|
||||
|
|
||||
LL | impl Trait for ();
|
||||
| ^
|
||||
|
|
||||
= help: try using `{}` instead
|
||||
|
||||
error: expected `{}`, found `;`
|
||||
--> $DIR/item-needs-block.rs:7:10
|
||||
|
|
||||
LL | enum Enum;
|
||||
| ^
|
||||
|
|
||||
= help: try using `{}` instead
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
Loading…
Reference in New Issue
Block a user