From df72e478b0179581c985ec53ebd6a0d14a330366 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 10 Jul 2024 18:55:45 -0400 Subject: [PATCH 1/2] Make sure that labels are defined after the primary span in diagnostics --- .../rustc_macros/src/diagnostics/diagnostic_builder.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs index 46bd80c2df64d..f93d89d6c0f04 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs @@ -269,6 +269,7 @@ impl DiagnosticDeriveVariantBuilder { let field_binding = &binding_info.binding; let inner_ty = FieldInnerTy::from_type(&field.ty); + let mut seen_label = false; field .attrs @@ -280,6 +281,14 @@ impl DiagnosticDeriveVariantBuilder { } let name = attr.path().segments.last().unwrap().ident.to_string(); + + if name == "primary_span" && seen_label { + span_err(attr.span().unwrap(), format!("`#[primary_span]` must be placed before labels, since it overwrites the span of the diagnostic")).emit(); + } + if name == "label" { + seen_label = true; + } + let needs_clone = name == "primary_span" && matches!(inner_ty, FieldInnerTy::Vec(_)); let (binding, needs_destructure) = if needs_clone { From 12ae282987762a71cf146efa3fb6c6fcf4956475 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 10 Jul 2024 18:55:51 -0400 Subject: [PATCH 2/2] Fix diagnostic and add a test for it --- compiler/rustc_resolve/src/errors.rs | 2 +- tests/ui/tool-attributes/invalid-tool.rs | 6 ++++++ tests/ui/tool-attributes/invalid-tool.stderr | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/ui/tool-attributes/invalid-tool.rs create mode 100644 tests/ui/tool-attributes/invalid-tool.stderr diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index 0620f3d709eb2..cd6503ae444a3 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -1089,8 +1089,8 @@ pub(crate) struct ToolWasAlreadyRegistered { #[derive(Diagnostic)] #[diag(resolve_tool_only_accepts_identifiers)] pub(crate) struct ToolOnlyAcceptsIdentifiers { - #[label] #[primary_span] + #[label] pub(crate) span: Span, pub(crate) tool: Symbol, } diff --git a/tests/ui/tool-attributes/invalid-tool.rs b/tests/ui/tool-attributes/invalid-tool.rs new file mode 100644 index 0000000000000..125333231d217 --- /dev/null +++ b/tests/ui/tool-attributes/invalid-tool.rs @@ -0,0 +1,6 @@ +#![feature(register_tool)] + +#![register_tool(1)] +//~^ ERROR `register_tool` only accepts identifiers + +fn main() {} diff --git a/tests/ui/tool-attributes/invalid-tool.stderr b/tests/ui/tool-attributes/invalid-tool.stderr new file mode 100644 index 0000000000000..deafa6d167c20 --- /dev/null +++ b/tests/ui/tool-attributes/invalid-tool.stderr @@ -0,0 +1,8 @@ +error: `register_tool` only accepts identifiers + --> $DIR/invalid-tool.rs:3:18 + | +LL | #![register_tool(1)] + | ^ not an identifier + +error: aborting due to 1 previous error +