Skip to content

Commit

Permalink
Add a test for direct recursion in required components. (bevyengine#1…
Browse files Browse the repository at this point in the history
…7626)

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 <[email protected]>
  • Loading branch information
ElliottjPierce and alice-i-cecile authored Feb 2, 2025
1 parent 33c5e0b commit 361397f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2189,7 +2189,7 @@ pub fn enforce_no_required_components_recursion(
.join(" → "),
if direct_recursion {
format!(
"Remove require({})",
"Remove require({}).",
ShortName(components.get_name(requiree).unwrap())
)
} else {
Expand Down Expand Up @@ -2225,7 +2225,7 @@ pub fn component_clone_via_clone<C: Clone + Component>(
/// - Component has [`ReflectFromPtr`](bevy_reflect::ReflectFromPtr) registered
/// - Component has one of the following registered: [`ReflectFromReflect`](bevy_reflect::ReflectFromReflect),
/// [`ReflectDefault`](bevy_reflect::std_traits::ReflectDefault), [`ReflectFromWorld`](crate::reflect::ReflectFromWorld)
///
///
/// If any of the conditions is not satisfied, the component will be skipped.
///
/// See [`EntityCloneBuilder`](crate::entity::EntityCloneBuilder) for details.
Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2662,6 +2662,16 @@ mod tests {
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(
dead_code,
reason = "This struct is used as a compilation test to test the derive macros, and as such is intentionally never constructed."
Expand Down

0 comments on commit 361397f

Please sign in to comment.