1
Fork 0

give a better error for tuple structs in derive(Diagnostic)

This commit is contained in:
jyn 2023-12-24 18:34:31 -05:00
parent d792e1f50a
commit 3141a65d25
6 changed files with 142 additions and 139 deletions

View file

@ -253,7 +253,10 @@ impl DiagnosticDeriveVariantBuilder {
let mut field_binding = binding_info.binding.clone();
field_binding.set_span(field.ty.span());
let ident = field.ident.as_ref().unwrap();
let Some(ident) = field.ident.as_ref() else {
span_err(field.span().unwrap(), "tuple structs are not supported").emit();
return TokenStream::new();
};
let ident = format_ident!("{}", ident); // strip `r#` prefix, if present
quote! {

View file

@ -56,7 +56,7 @@ fn path_to_string(path: &syn::Path) -> String {
/// Returns an error diagnostic on span `span` with msg `msg`.
#[must_use]
pub(crate) fn span_err<T: Into<String>>(span: impl MultiSpan, msg: T) -> Diagnostic {
Diagnostic::spanned(span, Level::Error, msg)
Diagnostic::spanned(span, Level::Error, format!("derive(Diagnostic): {}", msg.into()))
}
/// Emit a diagnostic on span `$span` with msg `$msg` (optionally performing additional decoration

View file

@ -243,7 +243,7 @@ impl<T> SetOnce<T> for SpannedOption<T> {
*self = Some((value, span));
}
Some((_, prev_span)) => {
span_err(span, "specified multiple times")
span_err(span, "attribute specified multiple times")
.span_note(*prev_span, "previously specified here")
.emit();
}