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

fix syntax error at #[derive(Props)] using const generics #1607

Conversation

syrflover
Copy link
Contributor

@syrflover syrflover commented Nov 4, 2023

Reproduce problem

https://github.com/syrflover/derive-props-with-const-generics

To trigger an error, change the version of dioxus to 0.4 in Cargo.toml

What is problem

image

let phantom_generics = self.generics.params.iter().map(|param| match param {
syn::GenericParam::Lifetime(lifetime) => {
let lifetime = &lifetime.lifetime;
quote!(::core::marker::PhantomData<&#lifetime ()>)
}
syn::GenericParam::Type(ty) => {
let ty = &ty.ident;
quote!(::core::marker::PhantomData<#ty>)
}
syn::GenericParam::Const(_cnst) => {
quote!()
}
});

#[must_use]
#builder_type_doc
#[allow(dead_code, non_camel_case_types, non_snake_case)]
#vis struct #builder_name #b_generics {
fields: #all_fields_param,
_phantom: (#( #phantom_generics ),*),
}

When it is GenericParam::Const, it doesn't return anything. However, if that item is included in an iterator, a syntax error occurs because it generates commas as many times as the length of the iterator when creating tokens.

How to fixed it

https://github.com/syrflover/dioxus/blob/4f8b60834af43fef879c7eb0ff46b74d23a6eabe/packages/core-macro/src/props/mod.rs#L554-L564

Changed to returns None, when it is GenericParam::Const using filter_map.

@syrflover syrflover changed the title fix syntax error at derive Props using const generics fix syntax error at #[derive(Props)] using const generics Nov 4, 2023
Copy link
Member

@ealmloff ealmloff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch and very clear explanation, thank you!

@ealmloff ealmloff merged commit be1decf into DioxusLabs:master Nov 4, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants