Skip to content

Commit

Permalink
give a better error for tuple structs in derive(Diagnostic)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Dec 24, 2023
1 parent 74583db commit df9d457
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
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! {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_macros/src/diagnostics/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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
Expand Down

0 comments on commit df9d457

Please sign in to comment.