Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output better error messages when deriving ContentHash for an enum fails #3106

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions lib/proc-macros/src/content_hash.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, quote_spanned};
use syn::spanned::Spanned;
use syn::{parse_quote, Data, Field, Fields, GenericParam, Generics, Index};
use syn::{parse_quote, Data, Field, Fields, GenericParam, Generics, Index, Type};

pub fn add_trait_bounds(mut generics: Generics) -> Generics {
for param in &mut generics.params {
Expand Down Expand Up @@ -54,21 +54,21 @@ pub fn generate_hash_impl(data: &Data) -> TokenStream {
match &v.fields {
Fields::Named(fields) => {
let bindings = enum_bindings(fields.named.iter());
let ix = index_to_ordinal(i);
let hash_statements =
hash_statements_for_enum_fields(i, fields.named.iter());
quote_spanned! {v.span() =>
Self::#variant_id{ #(#bindings),* } => {
::jj_lib::content_hash::ContentHash::hash(&#ix, state);
#( ::jj_lib::content_hash::ContentHash::hash(#bindings, state); )*
#(#hash_statements)*
}
}
}
Fields::Unnamed(fields) => {
let bindings = enum_bindings(fields.unnamed.iter());
let ix = index_to_ordinal(i);
let hash_statements =
hash_statements_for_enum_fields(i, fields.unnamed.iter());
quote_spanned! {v.span() =>
Self::#variant_id( #(#bindings),* ) => {
::jj_lib::content_hash::ContentHash::hash(&#ix, state);
#( ::jj_lib::content_hash::ContentHash::hash(#bindings, state); )*
#(#hash_statements)*
}
}
}
Expand Down Expand Up @@ -99,13 +99,40 @@ fn index_to_ordinal(ix: usize) -> u32 {
u32::try_from(ix).expect("The number of enum variants overflows a u32.")
}

fn enum_bindings<'a>(fields: impl IntoIterator<Item = &'a Field>) -> Vec<Ident> {
fn enum_bindings_with_type<'a>(fields: impl IntoIterator<Item = &'a Field>) -> Vec<(Type, Ident)> {
fields
.into_iter()
.enumerate()
.map(|(i, f)| {
// If the field is named, use the name, otherwise generate a placeholder name.
f.ident.clone().unwrap_or(format_ident!("field_{}", i))
(
f.ty.clone(),
f.ident.clone().unwrap_or(format_ident!("field_{}", i)),
)
})
.collect::<Vec<_>>()
}

fn enum_bindings<'a>(fields: impl IntoIterator<Item = &'a Field>) -> Vec<Ident> {
enum_bindings_with_type(fields)
.into_iter()
.map(|(_, b)| b)
.collect()
}

fn hash_statements_for_enum_fields<'a>(
index: usize,
fields: impl IntoIterator<Item = &'a Field>,
) -> Vec<TokenStream> {
let ix = index_to_ordinal(index);
let typed_bindings = enum_bindings_with_type(fields);
let mut hash_statements = Vec::with_capacity(typed_bindings.len() + 1);
hash_statements.push(quote! {::jj_lib::content_hash::ContentHash::hash(&#ix, state);});
for (ty, b) in typed_bindings.iter() {
hash_statements.push(quote_spanned! {b.span() =>
<#ty as ::jj_lib::content_hash::ContentHash>::hash(#b, state);
});
}

hash_statements
}