Add a test for direct recursion in required components. (#17626)

I realized there wasn't a test for this yet and figured it would be
trivial to add. Why not? Unless there was a test for this, and I just
missed it?

I appreciate the unique error message it gives and wanted to make sure
it doesn't get broken at some point. Or worse, endlessly recurse.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
This commit is contained in:
ElliottjPierce 2025-02-02 01:47:10 -05:00 committed by GitHub
parent 33c5e0bc96
commit 361397fcac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View File

@ -2189,7 +2189,7 @@ pub fn enforce_no_required_components_recursion(
.join(""), .join(""),
if direct_recursion { if direct_recursion {
format!( format!(
"Remove require({})", "Remove require({}).",
ShortName(components.get_name(requiree).unwrap()) ShortName(components.get_name(requiree).unwrap())
) )
} else { } else {
@ -2225,7 +2225,7 @@ pub fn component_clone_via_clone<C: Clone + Component>(
/// - Component has [`ReflectFromPtr`](bevy_reflect::ReflectFromPtr) registered /// - Component has [`ReflectFromPtr`](bevy_reflect::ReflectFromPtr) registered
/// - Component has one of the following registered: [`ReflectFromReflect`](bevy_reflect::ReflectFromReflect), /// - Component has one of the following registered: [`ReflectFromReflect`](bevy_reflect::ReflectFromReflect),
/// [`ReflectDefault`](bevy_reflect::std_traits::ReflectDefault), [`ReflectFromWorld`](crate::reflect::ReflectFromWorld) /// [`ReflectDefault`](bevy_reflect::std_traits::ReflectDefault), [`ReflectFromWorld`](crate::reflect::ReflectFromWorld)
/// ///
/// If any of the conditions is not satisfied, the component will be skipped. /// If any of the conditions is not satisfied, the component will be skipped.
/// ///
/// See [`EntityCloneBuilder`](crate::entity::EntityCloneBuilder) for details. /// See [`EntityCloneBuilder`](crate::entity::EntityCloneBuilder) for details.

View File

@ -2662,6 +2662,16 @@ mod tests {
World::new().register_component::<A>(); World::new().register_component::<A>();
} }
#[test]
#[should_panic = "Recursive required components detected: A → A\nhelp: Remove require(A)."]
fn required_components_self_errors() {
#[derive(Component, Default)]
#[require(A)]
struct A;
World::new().register_component::<A>();
}
#[expect( #[expect(
dead_code, dead_code,
reason = "This struct is used as a compilation test to test the derive macros, and as such is intentionally never constructed." reason = "This struct is used as a compilation test to test the derive macros, and as such is intentionally never constructed."