Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that labels are defined after the primary span in diagnostics #127591

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/tool-attributes/invalid-tool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(register_tool)]

#![register_tool(1)]
//~^ ERROR `register_tool` only accepts identifiers

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/tool-attributes/invalid-tool.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: `register_tool` only accepts identifiers
--> $DIR/invalid-tool.rs:3:18
|
LL | #![register_tool(1)]
| ^ not an identifier
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On master:

error: `register_tool` only accepts identifiers
 --> src/main.rs:3:18
  |
3 | #![register_tool(1)]
  |                  ^

See that the label is missing 😅


error: aborting due to 1 previous error

Loading