diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 1abd7577d92..5a7e28ce523 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -1446,38 +1446,6 @@ pub(crate) struct ExpectedTraitInTraitImplFoundType { pub span: Span, } -#[derive(Diagnostic)] -#[diag(parse_non_item_in_item_list)] -pub(crate) struct NonItemInItemList { - #[primary_span] - pub span: Span, - #[subdiagnostic] - pub sub: NonItemInItemListSub, - #[suggestion(suggestion_remove_semicolon, code = "", applicability = "maybe-incorrect")] - pub remove_semicolon: Option, -} - -#[derive(Subdiagnostic)] -pub(crate) enum NonItemInItemListSub { - #[suggestion( - suggestion_use_const_not_let, - code = "const", - applicability = "machine-applicable" - )] - Let { - #[primary_span] - span: Span, - }, - Other { - #[label(label_list_start)] - list_start: Span, - #[label(label_non_item)] - non_item: Span, - #[label(label_list_end)] - list_end: Span, - }, -} - #[derive(Diagnostic)] #[diag(parse_bounds_not_allowed_on_trait_aliases)] pub(crate) struct BoundsNotAllowedOnTraitAliases { diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 5d6202509a1..e401570912f 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -4,9 +4,9 @@ DefaultNotFollowedByItem, DocCommentDoesNotDocumentAnything, EnumStructMutuallyExclusive, ExpectedTraitInTraitImplFoundType, ExternCrateNameWithDashes, ExternCrateNameWithDashesSugg, ExternItemCannotBeConst, MissingConstType, MissingForInTraitImpl, - MissingKeywordForItemDefinition, MissingTraitInTraitImpl, NonItemInItemList, - NonItemInItemListSub, SelfArgumentPointer, TraitAliasCannotBeAuto, TraitAliasCannotBeUnsafe, - UnexpectedTokenAfterStructName, UseEmptyBlockNotSemi, VisibilityNotFollowedByItem, + MissingKeywordForItemDefinition, MissingTraitInTraitImpl, SelfArgumentPointer, + TraitAliasCannotBeAuto, TraitAliasCannotBeUnsafe, UnexpectedTokenAfterStructName, + UseEmptyBlockNotSemi, VisibilityNotFollowedByItem, }; use super::diagnostics::{dummy_arg, ConsumeClosingDelim}; @@ -703,22 +703,29 @@ fn parse_item_list( let non_item_span = self.token.span; let is_let = self.token.is_keyword(kw::Let); + let mut err = self.struct_span_err(non_item_span, "non-item in item list"); self.consume_block(Delimiter::Brace, ConsumeClosingDelim::Yes); - - self.sess.emit_err(NonItemInItemList { - span: non_item_span, - sub: if is_let { - NonItemInItemListSub::Let { span: non_item_span } - } else { - NonItemInItemListSub::Other { - list_start: open_brace_span, - non_item: non_item_span, - list_end: self.prev_token.span, - } - }, - remove_semicolon: is_unnecessary_semicolon.then_some(semicolon_span), - }); - + if is_let { + err.span_suggestion( + non_item_span, + "consider using `const` instead of `let` for associated const", + "const", + Applicability::MachineApplicable, + ); + } else { + err.span_label(open_brace_span, "item list starts here") + .span_label(non_item_span, "non-item starts here") + .span_label(self.prev_token.span, "item list ends here"); + } + if is_unnecessary_semicolon { + err.span_suggestion( + semicolon_span, + "consider removing this semicolon", + "", + Applicability::MaybeIncorrect, + ); + } + err.emit(); break; } Ok(Some(item)) => items.extend(item),