Skip to content

Commit

Permalink
rustc_parse: revert conversion of "non-item in item list" diagnostic
Browse files Browse the repository at this point in the history
#[derive(Subdiagnostic)] does not allow multiple subdiagnostics on one
variant, as in NonItemInItemListSub::Other.
  • Loading branch information
Xiretza committed Feb 1, 2023
1 parent 7631b12 commit a476683
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 50 deletions.
32 changes: 0 additions & 32 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Span>,
}

#[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 {
Expand Down
43 changes: 25 additions & 18 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::errors::{
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};
Expand Down Expand Up @@ -703,22 +703,29 @@ impl<'a> Parser<'a> {
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),
Expand Down

0 comments on commit a476683

Please sign in to comment.