Skip to content

Commit da98337

Browse files
committed
give a better error for tuple structs in derive(Diagnostic)
1 parent 2fa000c commit da98337

File tree

6 files changed

+142
-139
lines changed

6 files changed

+142
-139
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ impl DiagnosticDeriveVariantBuilder {
253253
let mut field_binding = binding_info.binding.clone();
254254
field_binding.set_span(field.ty.span());
255255

256-
let ident = field.ident.as_ref().unwrap();
256+
let Some(ident) = field.ident.as_ref() else {
257+
span_err(field.span().unwrap(), "tuple structs are not supported").emit();
258+
return TokenStream::new();
259+
};
257260
let ident = format_ident!("{}", ident); // strip `r#` prefix, if present
258261

259262
quote! {

compiler/rustc_macros/src/diagnostics/error.rs

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

6262
/// 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
@@ -243,7 +243,7 @@ impl<T> SetOnce<T> for SpannedOption<T> {
243243
*self = Some((value, span));
244244
}
245245
Some((_, prev_span)) => {
246-
span_err(span, "specified multiple times")
246+
span_err(span, "attribute specified multiple times")
247247
.span_note(*prev_span, "previously specified here")
248248
.emit();
249249
}

0 commit comments

Comments
 (0)