1
Fork 0

improve maybe_consume_incorrect_semicolon

This commit is contained in:
ardi 2024-05-14 12:23:33 +02:00
parent bdfd941f4d
commit 8dc6a5d145
6 changed files with 30 additions and 28 deletions

View file

@ -3250,6 +3250,7 @@ pub enum ItemKind {
} }
impl ItemKind { impl ItemKind {
/// "a" or "an"
pub fn article(&self) -> &'static str { pub fn article(&self) -> &'static str {
use ItemKind::*; use ItemKind::*;
match self { match self {

View file

@ -83,7 +83,7 @@ pub(crate) struct IncorrectSemicolon<'a> {
#[suggestion(style = "short", code = "", applicability = "machine-applicable")] #[suggestion(style = "short", code = "", applicability = "machine-applicable")]
pub span: Span, pub span: Span,
#[help] #[help]
pub opt_help: Option<()>, pub show_help: bool,
pub name: &'a str, pub name: &'a str,
} }

View file

@ -1817,34 +1817,31 @@ impl<'a> Parser<'a> {
Ok(P(T::recovered(Some(P(QSelf { ty, path_span, position: 0 })), path))) 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 { /// This function gets called in places where a semicolon is NOT expected and if there's a
if self.token.kind == TokenKind::Semi { /// semicolon it emits the appropriate error and returns true.
self.bump(); pub fn maybe_consume_incorrect_semicolon(&mut self, previous_item: Option<&Item>) -> bool {
if self.token.kind != TokenKind::Semi {
return false;
}
let mut err = // Check previous item to add it to the diagnostic, for example to say
IncorrectSemicolon { span: self.prev_token.span, opt_help: None, name: "" }; // `enum declarations are not followed by a semicolon`
let err = match previous_item {
if !items.is_empty() { Some(previous_item) => {
let previous_item = &items[items.len() - 1]; let name = match previous_item.kind {
let previous_item_kind_name = match previous_item.kind {
// Say "braced struct" because tuple-structs and // Say "braced struct" because tuple-structs and
// braceless-empty-struct declarations do take a semicolon. // braceless-empty-struct declarations do take a semicolon.
ItemKind::Struct(..) => Some("braced struct"), ItemKind::Struct(..) => "braced struct",
ItemKind::Enum(..) => Some("enum"), _ => previous_item.kind.descr(),
ItemKind::Trait(..) => Some("trait"),
ItemKind::Union(..) => Some("union"),
_ => None,
}; };
if let Some(name) = previous_item_kind_name { IncorrectSemicolon { span: self.token.span, name, show_help: true }
err.opt_help = Some(());
err.name = name;
}
} }
self.dcx().emit_err(err); None => IncorrectSemicolon { span: self.token.span, name: "", show_help: false },
true };
} else { self.dcx().emit_err(err);
false
} self.bump();
true
} }
/// Creates a `Diag` for an unexpected token `t` and tries to recover if it is a /// Creates a `Diag` for an unexpected token `t` and tries to recover if it is a

View file

@ -59,13 +59,13 @@ impl<'a> Parser<'a> {
let post_attr_lo = self.token.span; let post_attr_lo = self.token.span;
let mut items = ThinVec::new(); let mut items = ThinVec::new();
while let Some(item) = self.parse_item(ForceCollect::No)? { while let Some(item) = self.parse_item(ForceCollect::No)? {
self.maybe_consume_incorrect_semicolon(Some(&item));
items.push(item); items.push(item);
self.maybe_consume_incorrect_semicolon(&items);
} }
if !self.eat(term) { if !self.eat(term) {
let token_str = super::token_descr(&self.token); 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 msg = format!("expected item, found {token_str}");
let mut err = self.dcx().struct_span_err(self.token.span, msg); let mut err = self.dcx().struct_span_err(self.token.span, msg);
let span = self.token.span; let span = self.token.span;

View file

@ -686,9 +686,9 @@ pub(crate) fn make_test(
} }
} }
// The supplied slice is only used for diagnostics, // The supplied item is only used for diagnostics,
// which are swallowed here anyway. // which are swallowed here anyway.
parser.maybe_consume_incorrect_semicolon(&[]); parser.maybe_consume_incorrect_semicolon(None);
} }
// Reset errors so that they won't be reported as compiler bugs when dropping the // Reset errors so that they won't be reported as compiler bugs when dropping the

View file

@ -3,6 +3,8 @@ error: expected item, found `;`
| |
LL | mod M {}; LL | mod M {};
| ^ help: remove this semicolon | ^ help: remove this semicolon
|
= help: module declarations are not followed by a semicolon
error: expected item, found `;` error: expected item, found `;`
--> $DIR/recover-from-semicolon-trailing-item.rs:4:12 --> $DIR/recover-from-semicolon-trailing-item.rs:4:12
@ -17,6 +19,8 @@ error: expected item, found `;`
| |
LL | fn foo(a: usize) {}; LL | fn foo(a: usize) {};
| ^ help: remove this semicolon | ^ help: remove this semicolon
|
= help: function declarations are not followed by a semicolon
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/recover-from-semicolon-trailing-item.rs:10:20 --> $DIR/recover-from-semicolon-trailing-item.rs:10:20