Skip to content

Commit

Permalink
Fix nested generics in Reflect derive (bevyengine#10791)
Browse files Browse the repository at this point in the history
# Objective

> Issue raised on
[Discord](https://discord.com/channels/691052431525675048/1002362493634629796/1179182488787103776)

Currently the following code fails due to a missing `TypePath` bound:

```rust
#[derive(Reflect)] struct Foo<T>(T);
#[derive(Reflect)] struct Bar<T>(Foo<T>);
#[derive(Reflect)] struct Baz<T>(Bar<Foo<T>>);
```

## Solution

Add `TypePath` to the per-field bounds instead of _just_ the generic
type parameter bounds.

### Related Work

It should be noted that bevyengine#9046 would help make these kinds of issues
easier to work around and/or avoid entirely.

---

## Changelog

- Fixes missing `TypePath` requirement when deriving `Reflect` on nested
generics
  • Loading branch information
MrGVSV authored and james7132 committed Dec 1, 2023
1 parent 7c302da commit 804973e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ impl WhereClauseOptions {
let custom_bounds = active_bounds(field).map(|bounds| quote!(+ #bounds));

let bounds = if is_from_reflect {
quote!(#bevy_reflect_path::FromReflect #custom_bounds)
quote!(#bevy_reflect_path::FromReflect + #bevy_reflect_path::TypePath #custom_bounds)
} else {
quote!(#bevy_reflect_path::Reflect #custom_bounds)
quote!(#bevy_reflect_path::Reflect + #bevy_reflect_path::TypePath #custom_bounds)
};

(ty, bounds)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use bevy_reflect::Reflect;

mod nested_generics {
use super::*;

#[derive(Reflect)]
struct Foo<T>(T);

#[derive(Reflect)]
struct Bar<T>(Foo<T>);

#[derive(Reflect)]
struct Baz<T>(Bar<Foo<T>>);
}

fn main() {}

0 comments on commit 804973e

Please sign in to comment.