SystemParam Derive fixes (#2838)

# Objective

A user on Discord couldn't derive SystemParam for this Struct:

```rs
#[derive(SystemParam)]
pub struct SpatialQuery<'w, 's, Q: WorldQuery + Send + Sync + 'static, F: WorldQuery + Send + Sync + 'static = ()>
where
    F::Fetch: FilterFetch,
{
    query: Query<'w, 's, (C, &'static Transform), F>,
}
```

## Solution

1. The `where`-clause is now also copied to the `SystemParamFetch` impl Block.
2. The `SystemParamState` impl Block no longer gets any defaults for generics


Co-authored-by: MinerSebas <66798382+MinerSebas@users.noreply.github.com>
This commit is contained in:
MinerSebas 2022-02-03 03:32:02 +00:00
parent b506c30cd3
commit 69e9a47d92
2 changed files with 34 additions and 4 deletions

View File

@ -11,7 +11,7 @@ use syn::{
parse_macro_input,
punctuated::Punctuated,
token::Comma,
DeriveInput, Field, GenericParam, Ident, Index, LitInt, Result, Token,
DeriveInput, Field, GenericParam, Ident, Index, LitInt, Result, Token, TypeParam,
};
struct AllTuples {
@ -357,12 +357,18 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
.collect();
let mut punctuated_generics = Punctuated::<_, Token![,]>::new();
punctuated_generics.extend(lifetimeless_generics.iter());
punctuated_generics.extend(lifetimeless_generics.iter().map(|g| match g {
GenericParam::Type(g) => GenericParam::Type(TypeParam {
default: None,
..g.clone()
}),
_ => unreachable!(),
}));
let mut punctuated_generic_idents = Punctuated::<_, Token![,]>::new();
punctuated_generic_idents.extend(lifetimeless_generics.iter().map(|g| match g {
GenericParam::Type(g) => &g.ident,
_ => panic!(),
_ => unreachable!(),
}));
let struct_name = &ast.ident;
@ -402,7 +408,7 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
}
}
impl #impl_generics #path::system::SystemParamFetch<'w, 's> for #fetch_struct_name <(#(<#field_types as #path::system::SystemParam>::Fetch,)*), #punctuated_generic_idents> {
impl #impl_generics #path::system::SystemParamFetch<'w, 's> for #fetch_struct_name <(#(<#field_types as #path::system::SystemParam>::Fetch,)*), #punctuated_generic_idents> #where_clause {
type Item = #struct_name #ty_generics;
unsafe fn get_param(
state: &'s mut Self,

View File

@ -1253,3 +1253,27 @@ pub mod lifetimeless {
pub type SResMut<T> = super::ResMut<'static, T>;
pub type SCommands = crate::system::Commands<'static, 'static>;
}
#[cfg(test)]
mod tests {
use super::SystemParam;
use crate::{
self as bevy_ecs, // Necessary for the `SystemParam` Derive when used inside `bevy_ecs`.
query::{FilterFetch, WorldQuery},
system::Query,
};
// Compile test for #2838
#[derive(SystemParam)]
pub struct SpecialQuery<
'w,
's,
Q: WorldQuery + Send + Sync + 'static,
F: WorldQuery + Send + Sync + 'static = (),
>
where
F::Fetch: FilterFetch,
{
_query: Query<'w, 's, Q, F>,
}
}