improve maybe_consume_incorrect_semicolon
This commit is contained in:
parent
bdfd941f4d
commit
8dc6a5d145
6 changed files with 30 additions and 28 deletions
|
@ -83,7 +83,7 @@ pub(crate) struct IncorrectSemicolon<'a> {
|
|||
#[suggestion(style = "short", code = "", applicability = "machine-applicable")]
|
||||
pub span: Span,
|
||||
#[help]
|
||||
pub opt_help: Option<()>,
|
||||
pub show_help: bool,
|
||||
pub name: &'a str,
|
||||
}
|
||||
|
||||
|
|
|
@ -1817,34 +1817,31 @@ impl<'a> Parser<'a> {
|
|||
Ok(P(T::recovered(Some(P(QSelf { ty, path_span, position: 0 })), path)))
|
||||
}
|
||||
|
||||
pub fn maybe_consume_incorrect_semicolon(&mut self, items: &[P<Item>]) -> bool {
|
||||
if self.token.kind == TokenKind::Semi {
|
||||
self.bump();
|
||||
/// This function gets called in places where a semicolon is NOT expected and if there's a
|
||||
/// semicolon it emits the appropriate error and returns true.
|
||||
pub fn maybe_consume_incorrect_semicolon(&mut self, previous_item: Option<&Item>) -> bool {
|
||||
if self.token.kind != TokenKind::Semi {
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut err =
|
||||
IncorrectSemicolon { span: self.prev_token.span, opt_help: None, name: "" };
|
||||
|
||||
if !items.is_empty() {
|
||||
let previous_item = &items[items.len() - 1];
|
||||
let previous_item_kind_name = match previous_item.kind {
|
||||
// Check previous item to add it to the diagnostic, for example to say
|
||||
// `enum declarations are not followed by a semicolon`
|
||||
let err = match previous_item {
|
||||
Some(previous_item) => {
|
||||
let name = match previous_item.kind {
|
||||
// Say "braced struct" because tuple-structs and
|
||||
// braceless-empty-struct declarations do take a semicolon.
|
||||
ItemKind::Struct(..) => Some("braced struct"),
|
||||
ItemKind::Enum(..) => Some("enum"),
|
||||
ItemKind::Trait(..) => Some("trait"),
|
||||
ItemKind::Union(..) => Some("union"),
|
||||
_ => None,
|
||||
ItemKind::Struct(..) => "braced struct",
|
||||
_ => previous_item.kind.descr(),
|
||||
};
|
||||
if let Some(name) = previous_item_kind_name {
|
||||
err.opt_help = Some(());
|
||||
err.name = name;
|
||||
}
|
||||
IncorrectSemicolon { span: self.token.span, name, show_help: true }
|
||||
}
|
||||
self.dcx().emit_err(err);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
None => IncorrectSemicolon { span: self.token.span, name: "", show_help: false },
|
||||
};
|
||||
self.dcx().emit_err(err);
|
||||
|
||||
self.bump();
|
||||
true
|
||||
}
|
||||
|
||||
/// Creates a `Diag` for an unexpected token `t` and tries to recover if it is a
|
||||
|
|
|
@ -59,13 +59,13 @@ impl<'a> Parser<'a> {
|
|||
let post_attr_lo = self.token.span;
|
||||
let mut items = ThinVec::new();
|
||||
while let Some(item) = self.parse_item(ForceCollect::No)? {
|
||||
self.maybe_consume_incorrect_semicolon(Some(&item));
|
||||
items.push(item);
|
||||
self.maybe_consume_incorrect_semicolon(&items);
|
||||
}
|
||||
|
||||
if !self.eat(term) {
|
||||
let token_str = super::token_descr(&self.token);
|
||||
if !self.maybe_consume_incorrect_semicolon(&items) {
|
||||
if !self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {
|
||||
let msg = format!("expected item, found {token_str}");
|
||||
let mut err = self.dcx().struct_span_err(self.token.span, msg);
|
||||
let span = self.token.span;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue