Skip to content

Commit bd0d3f7

Browse files
committed
Disallow #[primary_span] on LintDiagnostics
1 parent a960f83 commit bd0d3f7

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,20 @@ impl DiagnosticDeriveBuilder {
340340
Ok(quote! {})
341341
}
342342
"primary_span" => {
343-
report_error_if_not_applied_to_span(attr, &info)?;
343+
match self.kind {
344+
DiagnosticDeriveKind::SessionDiagnostic => {
345+
report_error_if_not_applied_to_span(attr, &info)?;
344346

345-
Ok(quote! {
346-
#diag.set_span(#binding);
347-
})
347+
Ok(quote! {
348+
#diag.set_span(#binding);
349+
})
350+
}
351+
DiagnosticDeriveKind::LintDiagnostic => {
352+
throw_invalid_attr!(attr, &meta, |diag| {
353+
diag.help("the `primary_span` field attribute is not valid for lint diagnostics")
354+
})
355+
}
356+
}
348357
}
349358
"label" => {
350359
report_error_if_not_applied_to_span(attr, &info)?;

compiler/rustc_passes/src/errors.rs

-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,6 @@ pub struct NonExportedMacroInvalidAttrs {
613613
#[derive(LintDiagnostic)]
614614
#[lint(passes::unused_duplicate)]
615615
pub struct UnusedDuplicate {
616-
#[primary_span]
617616
#[suggestion(code = "", applicability = "machine-applicable")]
618617
pub this: Span,
619618
#[note]

src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs

+8
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,14 @@ struct LintsGood {
551551
struct ErrorsBad {
552552
}
553553

554+
#[derive(LintDiagnostic)]
555+
#[lint(typeck::ambiguous_lifetime_bound)]
556+
struct PrimarySpanOnLint {
557+
#[primary_span]
558+
//~^ ERROR `#[primary_span]` is not a valid attribute
559+
span: Span,
560+
}
561+
554562
#[derive(SessionDiagnostic)]
555563
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
556564
struct ErrorWithMultiSpan {

src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ LL | #[error(typeck::ambiguous_lifetime_bound)]
355355
|
356356
= help: use the `#[lint(...)]` attribute to create a lint
357357

358+
error: `#[primary_span]` is not a valid attribute
359+
--> $DIR/diagnostic-derive.rs:557:5
360+
|
361+
LL | #[primary_span]
362+
| ^^^^^^^^^^^^^^^
363+
|
364+
= help: the `primary_span` field attribute is not valid for lint diagnostics
365+
358366
error: cannot find attribute `nonsense` in this scope
359367
--> $DIR/diagnostic-derive.rs:53:3
360368
|
@@ -387,7 +395,7 @@ LL | arg: impl IntoDiagnosticArg,
387395
| ^^^^^^^^^^^^^^^^^ required by this bound in `DiagnosticBuilder::<'a, G>::set_arg`
388396
= note: this error originates in the derive macro `SessionDiagnostic` (in Nightly builds, run with -Z macro-backtrace for more info)
389397

390-
error: aborting due to 46 previous errors
398+
error: aborting due to 47 previous errors
391399

392400
Some errors have detailed explanations: E0277, E0425.
393401
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)