Skip to content

Commit afb6118

Browse files
committed
give a better error for tuple structs in derive(Diagnostic)
1 parent 74583db commit afb6118

File tree

6 files changed

+141
-138
lines changed

6 files changed

+141
-138
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,10 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
263263
let mut field_binding = binding_info.binding.clone();
264264
field_binding.set_span(field.ty.span());
265265

266-
let ident = field.ident.as_ref().unwrap();
266+
let Some(ident) = field.ident.as_ref() else {
267+
span_err(field.span().unwrap(), "tuple structs are not supported").emit();
268+
return TokenStream::new();
269+
};
267270
let ident = format_ident!("{}", ident); // strip `r#` prefix, if present
268271

269272
quote! {

compiler/rustc_macros/src/diagnostics/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn path_to_string(path: &syn::Path) -> String {
5555
/// Returns an error diagnostic on span `span` with msg `msg`.
5656
#[must_use]
5757
pub(crate) fn span_err<T: Into<String>>(span: impl MultiSpan, msg: T) -> Diagnostic {
58-
Diagnostic::spanned(span, Level::Error, msg)
58+
Diagnostic::spanned(span, Level::Error, format!("derive(Diagnostic): {}", msg.into()))
5959
}
6060

6161
/// Emit a diagnostic on span `$span` with msg `$msg` (optionally performing additional decoration

compiler/rustc_macros/src/diagnostics/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl<T> SetOnce<T> for SpannedOption<T> {
242242
*self = Some((value, span));
243243
}
244244
Some((_, prev_span)) => {
245-
span_err(span, "specified multiple times")
245+
span_err(span, "attribute specified multiple times")
246246
.span_note(*prev_span, "previously specified here")
247247
.emit();
248248
}

0 commit comments

Comments
 (0)