Skip to content

Commit df72e47

Browse files
Make sure that labels are defined after the primary span in diagnostics
1 parent d819876 commit df72e47

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

+9
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ impl DiagnosticDeriveVariantBuilder {
269269
let field_binding = &binding_info.binding;
270270

271271
let inner_ty = FieldInnerTy::from_type(&field.ty);
272+
let mut seen_label = false;
272273

273274
field
274275
.attrs
@@ -280,6 +281,14 @@ impl DiagnosticDeriveVariantBuilder {
280281
}
281282

282283
let name = attr.path().segments.last().unwrap().ident.to_string();
284+
285+
if name == "primary_span" && seen_label {
286+
span_err(attr.span().unwrap(), format!("`#[primary_span]` must be placed before labels, since it overwrites the span of the diagnostic")).emit();
287+
}
288+
if name == "label" {
289+
seen_label = true;
290+
}
291+
283292
let needs_clone =
284293
name == "primary_span" && matches!(inner_ty, FieldInnerTy::Vec(_));
285294
let (binding, needs_destructure) = if needs_clone {

0 commit comments

Comments
 (0)