diff --git a/compiler/rustc_attr_parsing/src/attributes/diagnostic/mod.rs b/compiler/rustc_attr_parsing/src/attributes/diagnostic/mod.rs index 32d73ff32361c..00e753eea97cd 100644 --- a/compiler/rustc_attr_parsing/src/attributes/diagnostic/mod.rs +++ b/compiler/rustc_attr_parsing/src/attributes/diagnostic/mod.rs @@ -1,6 +1,6 @@ use std::ops::Range; -use rustc_errors::E0232; +use rustc_errors::{Diagnostic, E0232}; use rustc_hir::AttrPath; use rustc_hir::attrs::diagnostic::{ Directive, FilterFormatString, Flag, FormatArg, FormatString, LitOrArg, Name, NameValue, @@ -18,6 +18,10 @@ use rustc_span::{Ident, InnerSpan, Span, Symbol, kw, sym}; use thin_vec::{ThinVec, thin_vec}; use crate::context::{AcceptContext, Stage}; +use crate::errors::{ + DisallowedPlaceholder, DisallowedPositionalArgument, IgnoredDiagnosticOption, + InvalidFormatSpecifier, MalFormedDiagnosticAttributeLint, WrappedParserError, +}; use crate::parser::{ArgParser, MetaItemListParser, MetaItemOrLitParser, MetaItemParser}; pub(crate) mod do_not_recommend; @@ -112,12 +116,12 @@ fn merge( match (first, later) { (Some(_) | None, None) => {} (Some((first_span, _)), Some((later_span, _))) => { - cx.emit_lint( + let first_span = *first_span; + cx.emit_dyn_lint( MALFORMED_DIAGNOSTIC_ATTRIBUTES, - AttributeLintKind::IgnoredDiagnosticOption { - first_span: *first_span, - later_span, - option_name, + move |dcx, level| { + IgnoredDiagnosticOption { first_span, later_span, option_name } + .into_diag(dcx, level) }, later_span, ); @@ -157,12 +161,15 @@ fn parse_list<'p, S: Stage>( ); } ArgParser::NameValue(_) => { - cx.emit_lint( + cx.emit_dyn_lint( MALFORMED_DIAGNOSTIC_ATTRIBUTES, - AttributeLintKind::MalFormedDiagnosticAttribute { - attribute: mode.as_str(), - options: mode.allowed_options(), - span, + move |dcx, level| { + MalFormedDiagnosticAttributeLint { + attribute: mode.as_str(), + options: mode.allowed_options(), + span, + } + .into_diag(dcx, level) }, span, ); @@ -188,12 +195,15 @@ fn parse_directive_items<'p, S: Stage>( let span = item.span(); macro malformed() {{ - cx.emit_lint( + cx.emit_dyn_lint( MALFORMED_DIAGNOSTIC_ATTRIBUTES, - AttributeLintKind::MalFormedDiagnosticAttribute { - attribute: mode.as_str(), - options: mode.allowed_options(), - span, + move |dcx, level| { + MalFormedDiagnosticAttributeLint { + attribute: mode.as_str(), + options: mode.allowed_options(), + span, + } + .into_diag(dcx, level) }, span, ); @@ -210,13 +220,14 @@ fn parse_directive_items<'p, S: Stage>( }} macro duplicate($name: ident, $($first_span:tt)*) {{ - cx.emit_lint( + let first_span = $($first_span)*; + cx.emit_dyn_lint( MALFORMED_DIAGNOSTIC_ATTRIBUTES, - AttributeLintKind::IgnoredDiagnosticOption { - first_span: $($first_span)*, + move |dcx, level| IgnoredDiagnosticOption { + first_span, later_span: span, option_name: $name, - }, + }.into_diag(dcx, level), span, ); }} @@ -245,9 +256,19 @@ fn parse_directive_items<'p, S: Stage>( let (FormatWarning::InvalidSpecifier { span, .. } | FormatWarning::PositionalArgument { span, .. } | FormatWarning::DisallowedPlaceholder { span }) = warning; - cx.emit_lint( + cx.emit_dyn_lint( MALFORMED_DIAGNOSTIC_FORMAT_LITERALS, - AttributeLintKind::MalformedDiagnosticFormat { warning }, + move |dcx, level| match warning { + FormatWarning::PositionalArgument { .. } => { + DisallowedPositionalArgument.into_diag(dcx, level) + } + FormatWarning::InvalidSpecifier { .. } => { + InvalidFormatSpecifier.into_diag(dcx, level) + } + FormatWarning::DisallowedPlaceholder { .. } => { + DisallowedPlaceholder.into_diag(dcx, level) + } + }, span, ); } @@ -255,12 +276,15 @@ fn parse_directive_items<'p, S: Stage>( f } Err(e) => { - cx.emit_lint( + cx.emit_dyn_lint( MALFORMED_DIAGNOSTIC_FORMAT_LITERALS, - AttributeLintKind::DiagnosticWrappedParserError { - description: e.description, - label: e.label, - span: slice_span(input.span, e.span, is_snippet), + move |dcx, level| { + WrappedParserError { + description: &e.description, + label: &e.label, + span: slice_span(input.span, e.span.clone(), is_snippet), + } + .into_diag(dcx, level) }, input.span, ); diff --git a/compiler/rustc_attr_parsing/src/attributes/doc.rs b/compiler/rustc_attr_parsing/src/attributes/doc.rs index 2b6d56c411939..321f6e08a733f 100644 --- a/compiler/rustc_attr_parsing/src/attributes/doc.rs +++ b/compiler/rustc_attr_parsing/src/attributes/doc.rs @@ -5,7 +5,6 @@ use rustc_hir::Target; use rustc_hir::attrs::{ AttributeKind, CfgEntry, CfgHideShow, CfgInfo, DocAttribute, DocInline, HideOrShow, }; -use rustc_hir::lints::AttributeLintKind; use rustc_session::parse::feature_err; use rustc_span::{Span, Symbol, edition, sym}; use thin_vec::ThinVec; @@ -17,7 +16,8 @@ use crate::errors::{ AttrCrateLevelOnly, DocAliasDuplicated, DocAutoCfgExpectsHideOrShow, DocAutoCfgHideShowExpectsList, DocAutoCfgHideShowUnexpectedItem, DocAutoCfgWrongLiteral, DocTestLiteral, DocTestTakesList, DocTestUnknown, DocUnknownAny, DocUnknownInclude, - DocUnknownPasses, DocUnknownPlugins, DocUnknownSpotlight, IllFormedAttributeInput, + DocUnknownPasses, DocUnknownPlugins, DocUnknownSpotlight, ExpectedNameValue, ExpectedNoArgs, + IllFormedAttributeInput, MalformedDoc, }; use crate::parser::{ArgParser, MetaItemOrLitParser, MetaItemParser, OwnedPathParser}; use crate::session_diagnostics::{ @@ -84,18 +84,18 @@ fn expected_name_value( span: Span, _name: Option, ) { - cx.emit_lint( + cx.emit_dyn_lint( rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES, - AttributeLintKind::ExpectedNameValue, + |dcx, level| ExpectedNameValue.into_diag(dcx, level), span, ); } // FIXME: remove this method once merged and use `cx.expected_no_args(span)` instead. fn expected_no_args(cx: &mut AcceptContext<'_, '_, S>, span: Span) { - cx.emit_lint( + cx.emit_dyn_lint( rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES, - AttributeLintKind::ExpectedNoArgs, + |dcx, level| ExpectedNoArgs.into_diag(dcx, level), span, ); } @@ -107,9 +107,9 @@ fn expected_string_literal( span: Span, _actual_literal: Option<&MetaItemLit>, ) { - cx.emit_lint( + cx.emit_dyn_lint( rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES, - AttributeLintKind::MalformedDoc, + |dcx, level| MalformedDoc.into_diag(dcx, level), span, ); } @@ -203,9 +203,9 @@ impl DocParser { // FIXME: remove this method once merged and uncomment the line below instead. // cx.expected_list(cx.attr_span, args); let span = cx.attr_span; - cx.emit_lint( + cx.emit_dyn_lint( rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES, - AttributeLintKind::MalformedDoc, + |dcx, level| MalformedDoc.into_diag(dcx, level), span, ); return; @@ -399,9 +399,9 @@ impl DocParser { // FIXME: remove this method once merged and uncomment the line // below instead. // cx.expected_identifier(sub_item.path().span()); - cx.emit_lint( + cx.emit_dyn_lint( rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES, - AttributeLintKind::MalformedDoc, + |dcx, level| MalformedDoc.into_diag(dcx, level), sub_item.path().span(), ); continue; @@ -605,9 +605,9 @@ impl DocParser { // FIXME: remove this method once merged and uncomment the line // below instead. // cx.unexpected_literal(lit.span); - cx.emit_lint( + cx.emit_dyn_lint( rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES, - AttributeLintKind::MalformedDoc, + |dcx, level| MalformedDoc.into_diag(dcx, level), lit.span, ); } diff --git a/compiler/rustc_attr_parsing/src/errors.rs b/compiler/rustc_attr_parsing/src/errors.rs index d3e6845799e7f..b421e85688958 100644 --- a/compiler/rustc_attr_parsing/src/errors.rs +++ b/compiler/rustc_attr_parsing/src/errors.rs @@ -325,3 +325,70 @@ pub(crate) struct IncorrectDoNotRecommendLocation { #[label("not a trait implementation")] pub target_span: Span, } + +#[derive(Diagnostic)] +#[diag("malformed `doc` attribute input")] +#[warning( + "this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!" +)] +pub(crate) struct MalformedDoc; + +#[derive(Diagnostic)] +#[diag("didn't expect any arguments here")] +#[warning( + "this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!" +)] +pub(crate) struct ExpectedNoArgs; + +#[derive(Diagnostic)] +#[diag("expected this to be of the form `... = \"...\"`")] +#[warning( + "this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!" +)] +pub(crate) struct ExpectedNameValue; + +#[derive(Diagnostic)] +#[diag("malformed `{$attribute}` attribute")] +#[help("{$options}")] +pub(crate) struct MalFormedDiagnosticAttributeLint { + pub attribute: &'static str, + pub options: &'static str, + #[label("invalid option found here")] + pub span: Span, +} + +#[derive(Diagnostic)] +#[diag("positional format arguments are not allowed here")] +#[help( + "only named format arguments with the name of one of the generic types are allowed in this context" +)] +pub(crate) struct DisallowedPositionalArgument; + +#[derive(Diagnostic)] +#[diag("format arguments are not allowed here")] +#[help("consider removing this format argument")] +pub(crate) struct DisallowedPlaceholder; + +#[derive(Diagnostic)] +#[diag("invalid format specifier")] +#[help("no format specifier are supported in this position")] +pub(crate) struct InvalidFormatSpecifier; + +#[derive(Diagnostic)] +#[diag("{$description}")] +pub(crate) struct WrappedParserError<'a> { + pub description: &'a str, + #[label("{$label}")] + pub span: Span, + pub label: &'a str, +} + +#[derive(Diagnostic)] +#[diag("`{$option_name}` is ignored due to previous definition of `{$option_name}`")] +pub(crate) struct IgnoredDiagnosticOption { + pub option_name: Symbol, + #[label("`{$option_name}` is first declared here")] + pub first_span: Span, + #[label("`{$option_name}` is later redundantly declared here")] + pub later_span: Span, +} diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index 92c4748483278..29c302ff2802a 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -2,7 +2,7 @@ use std::any::Any; use rustc_data_structures::sync::DynSend; use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level}; -use rustc_hir::lints::{AttributeLintKind, FormatWarning}; +use rustc_hir::lints::AttributeLintKind; use rustc_middle::ty::TyCtxt; use rustc_session::Session; @@ -43,33 +43,6 @@ impl<'a> Diagnostic<'a, ()> for DecorateAttrLint<'_, '_, '_> { .into_diag(dcx, level) } - &AttributeLintKind::MalformedDoc => lints::MalformedDoc.into_diag(dcx, level), - - &AttributeLintKind::ExpectedNoArgs => lints::ExpectedNoArgs.into_diag(dcx, level), - - &AttributeLintKind::ExpectedNameValue => lints::ExpectedNameValue.into_diag(dcx, level), - &AttributeLintKind::MalFormedDiagnosticAttribute { attribute, options, span } => { - lints::MalFormedDiagnosticAttributeLint { attribute, options, span } - .into_diag(dcx, level) - } - AttributeLintKind::MalformedDiagnosticFormat { warning } => match warning { - FormatWarning::PositionalArgument { .. } => { - lints::DisallowedPositionalArgument.into_diag(dcx, level) - } - FormatWarning::InvalidSpecifier { .. } => { - lints::InvalidFormatSpecifier.into_diag(dcx, level) - } - FormatWarning::DisallowedPlaceholder { .. } => { - lints::DisallowedPlaceholder.into_diag(dcx, level) - } - }, - AttributeLintKind::DiagnosticWrappedParserError { description, label, span } => { - lints::WrappedParserError { description, label, span: *span }.into_diag(dcx, level) - } - &AttributeLintKind::IgnoredDiagnosticOption { option_name, first_span, later_span } => { - lints::IgnoredDiagnosticOption { option_name, first_span, later_span } - .into_diag(dcx, level) - } &AttributeLintKind::MissingOptionsForDiagnosticAttribute { attribute, options } => { lints::MissingOptionsForDiagnosticAttribute { attribute, options } .into_diag(dcx, level) diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index ccbc325648e38..c2c140a3c6d92 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -3282,63 +3282,6 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion { } } -#[derive(Diagnostic)] -#[diag("malformed `doc` attribute input")] -#[warning( - "this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!" -)] -pub(crate) struct MalformedDoc; - -#[derive(Diagnostic)] -#[diag("didn't expect any arguments here")] -#[warning( - "this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!" -)] -pub(crate) struct ExpectedNoArgs; - -#[derive(Diagnostic)] -#[diag("expected this to be of the form `... = \"...\"`")] -#[warning( - "this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!" -)] -pub(crate) struct ExpectedNameValue; - -#[derive(Diagnostic)] -#[diag("positional format arguments are not allowed here")] -#[help( - "only named format arguments with the name of one of the generic types are allowed in this context" -)] -pub(crate) struct DisallowedPositionalArgument; - -#[derive(Diagnostic)] -#[diag("format arguments are not allowed here")] -#[help("consider removing this format argument")] -pub(crate) struct DisallowedPlaceholder; - -#[derive(Diagnostic)] -#[diag("invalid format specifier")] -#[help("no format specifier are supported in this position")] -pub(crate) struct InvalidFormatSpecifier; - -#[derive(Diagnostic)] -#[diag("{$description}")] -pub(crate) struct WrappedParserError<'a> { - pub description: &'a str, - #[label("{$label}")] - pub span: Span, - pub label: &'a str, -} - -#[derive(Diagnostic)] -#[diag("`{$option_name}` is ignored due to previous definition of `{$option_name}`")] -pub(crate) struct IgnoredDiagnosticOption { - pub option_name: Symbol, - #[label("`{$option_name}` is first declared here")] - pub first_span: Span, - #[label("`{$option_name}` is later redundantly declared here")] - pub later_span: Span, -} - #[derive(Diagnostic)] #[diag("missing options for `{$attribute}` attribute")] #[help("{$options}")] @@ -3358,13 +3301,3 @@ pub(crate) struct EqInternalMethodImplemented; "only literals are allowed as values for the `message`, `note` and `label` options. These options must be separated by a comma" )] pub(crate) struct NonMetaItemDiagnosticAttribute; - -#[derive(Diagnostic)] -#[diag("malformed `{$attribute}` attribute")] -#[help("{$options}")] -pub(crate) struct MalFormedDiagnosticAttributeLint { - pub attribute: &'static str, - pub options: &'static str, - #[label("invalid option found here")] - pub span: Span, -} diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 45ac2c59b04e9..aef62c768337b 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -656,13 +656,6 @@ pub enum DeprecatedSinceKind { pub enum AttributeLintKind { UnexpectedCfgName((Symbol, Span), Option<(Symbol, Span)>), UnexpectedCfgValue((Symbol, Span), Option<(Symbol, Span)>), - MalformedDoc, - ExpectedNoArgs, - ExpectedNameValue, - MalFormedDiagnosticAttribute { attribute: &'static str, options: &'static str, span: Span }, - MalformedDiagnosticFormat { warning: FormatWarning }, - DiagnosticWrappedParserError { description: String, label: String, span: Span }, - IgnoredDiagnosticOption { option_name: Symbol, first_span: Span, later_span: Span }, MissingOptionsForDiagnosticAttribute { attribute: &'static str, options: &'static str }, NonMetaItemDiagnosticAttribute, }