diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs index 6a5997512bd..31ea6bb779e 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs @@ -340,11 +340,20 @@ impl DiagnosticDeriveBuilder { Ok(quote! {}) } "primary_span" => { - report_error_if_not_applied_to_span(attr, &info)?; + match self.kind { + DiagnosticDeriveKind::SessionDiagnostic => { + report_error_if_not_applied_to_span(attr, &info)?; - Ok(quote! { - #diag.set_span(#binding); - }) + Ok(quote! { + #diag.set_span(#binding); + }) + } + DiagnosticDeriveKind::LintDiagnostic => { + throw_invalid_attr!(attr, &meta, |diag| { + diag.help("the `primary_span` field attribute is not valid for lint diagnostics") + }) + } + } } "label" => { report_error_if_not_applied_to_span(attr, &info)?; diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index cd465380867..f41ff2c9b17 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -613,7 +613,6 @@ pub struct NonExportedMacroInvalidAttrs { #[derive(LintDiagnostic)] #[lint(passes::unused_duplicate)] pub struct UnusedDuplicate { - #[primary_span] #[suggestion(code = "", applicability = "machine-applicable")] pub this: Span, #[note] diff --git a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs index 8634fbc7aca..842fdee9221 100644 --- a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs +++ b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs @@ -551,6 +551,14 @@ struct LintsGood { struct ErrorsBad { } +#[derive(LintDiagnostic)] +#[lint(typeck::ambiguous_lifetime_bound)] +struct PrimarySpanOnLint { + #[primary_span] + //~^ ERROR `#[primary_span]` is not a valid attribute + span: Span, +} + #[derive(SessionDiagnostic)] #[error(typeck::ambiguous_lifetime_bound, code = "E0123")] struct ErrorWithMultiSpan { diff --git a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr index 8c826c9a284..081139c7a70 100644 --- a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr +++ b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr @@ -355,6 +355,14 @@ LL | #[error(typeck::ambiguous_lifetime_bound)] | = help: use the `#[lint(...)]` attribute to create a lint +error: `#[primary_span]` is not a valid attribute + --> $DIR/diagnostic-derive.rs:557:5 + | +LL | #[primary_span] + | ^^^^^^^^^^^^^^^ + | + = help: the `primary_span` field attribute is not valid for lint diagnostics + error: cannot find attribute `nonsense` in this scope --> $DIR/diagnostic-derive.rs:53:3 | @@ -387,7 +395,7 @@ LL | arg: impl IntoDiagnosticArg, | ^^^^^^^^^^^^^^^^^ required by this bound in `DiagnosticBuilder::<'a, G>::set_arg` = note: this error originates in the derive macro `SessionDiagnostic` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 46 previous errors +error: aborting due to 47 previous errors Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`.