Disallow #[primary_span] on LintDiagnostics

This commit is contained in:
Xiretza 2022-08-19 15:04:34 +02:00
parent a960f8304c
commit bd0d3f745d
4 changed files with 30 additions and 6 deletions

View File

@ -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)?;

View File

@ -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]

View File

@ -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 {

View File

@ -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`.