Skip to content

Commit

Permalink
Add generic type hints to boxed hooks (#3633)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Ichmed and Michael Meyer committed Mar 14, 2024
1 parent 95c29cc commit e9739fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/yew-macro/src/hook/mod.rs
Expand Up @@ -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;

Expand Down
22 changes: 22 additions & 0 deletions packages/yew-macro/tests/hook_attr/hook-trait-item-pass.rs
@@ -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() {}

0 comments on commit e9739fc

Please sign in to comment.