Add generic type hints to boxed hooks (#3633)

When using complex type constructs in a boxed hook, the compiler could get confused and request a type hint on the call to the inner function.  This adds all generic types of the outer hook function as explicit arguments to the call of the inner function.

* Add generic types to boxed hooks
* Create failing test

---------

Co-authored-by: Michael Meyer <ichmed95@gmail.com>
This commit is contained in:
Michael Meyer 2024-03-14 12:24:20 +01:00 committed by GitHub
parent 95c29cc684
commit e9739fc9ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -144,11 +144,13 @@ pub fn hook_impl(hook: HookFn) -> syn::Result<TokenStream> {
let as_boxed_fn = with_output.then(|| quote! { as #boxed_fn_type });
let generic_types = generics.type_params().map(|t| &t.ident);
// We need boxing implementation for `impl Trait` arguments.
quote! {
let #boxed_inner_ident = ::std::boxed::Box::new(
move |#ctx_ident: &mut ::yew::functional::HookContext| #inner_fn_rt {
#inner_fn_ident (#ctx_ident, #(#input_args,)*)
#inner_fn_ident :: <#(#generic_types,)*> (#ctx_ident, #(#input_args,)*)
}
) #as_boxed_fn;

View File

@ -0,0 +1,22 @@
use std::marker::PhantomData;
pub struct QueryState<T> {
p: PhantomData<T>
}
pub trait MyTrait {
type Associated;
}
#[yew::hook]
pub fn use_query_state<Props>(
selector: impl yew::html::IntoPropValue<bool>,
) -> QueryState<Props::Associated>
where
Props: MyTrait,
{
QueryState::<Props::Associated> { p: PhantomData::<Props::Associated> }
}
fn main() {}