Skip to content

bevy_reflect: Fix TypePath string concatenation #18609

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_reflect/derive/src/string_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl StringExpr {
let owned = self.into_owned();
let borrowed = other.into_borrowed();
Self::Owned(quote! {
#owned + #borrowed
::core::ops::Add::<&str>::add(#owned, #borrowed)
})
}
}
Expand Down
35 changes: 35 additions & 0 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,41 @@ mod tests {
assert_eq!(values, vec![1]);
}

/// This test ensures that we are able to reflect generic types with one or more type parameters.
///
/// When there is an `Add` implementation for `String`, the compiler isn't able to infer the correct
/// type to deref to.
/// If we don't append the strings in the `TypePath` derive correctly (i.e. explicitly specifying the type),
/// we'll get a compilation error saying that "`&String` cannot be added to `String`".
///
/// So this test just ensures that we do do that correctly.
///
/// This problem is a known issue and is unexpectedly expected behavior:
/// - <https://github.com/rust-lang/rust/issues/77143>
/// - <https://github.com/bodil/smartstring/issues/7>
/// - <https://github.com/pola-rs/polars/issues/14666>
#[test]
fn should_reflect_generic() {
struct FakeString {}

// This implementation confuses the compiler when trying to add a `&String` to a `String`
impl core::ops::Add<FakeString> for String {
type Output = Self;
fn add(self, _rhs: FakeString) -> Self::Output {
unreachable!()
}
}

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

#[derive(Reflect)]
struct Bar<A, B>(A, B);

#[derive(Reflect)]
struct Baz<A, B, C>(A, B, C);
}

#[test]
fn should_reflect_clone() {
// Struct
Expand Down