Skip to content

Cycles in required components produce inscrutable error messages #16645

Closed
@rparrett

Description

@rparrett

Bevy version

0.15, main

What you did

use bevy::prelude::*;

#[derive(Component, Default)]
#[require(A)]
struct A;

#[derive(Component, Default)]
#[require(C)]
struct B;
#[derive(Component, Default)]
#[require(B)]
struct C;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(A);
    //commands.spawn(B);
}

What went wrong

thread 'main' panicked at examples/cycles.rs:9:10:
attempt to add with overflow

Digging a bit deeper, we get some hints, and it's probably fairly clear what's going on.

thread 'main' panicked at examples/cycles.rs:9:10:
attempt to add with overflow
stack backtrace:
   0: rust_begin_unwind
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:74:14
   2: core::panicking::panic_const::panic_const_add_overflow
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:181:21
   3: <cycles::B as bevy_ecs::component::Component>::register_required_components
             at ./examples/cycles.rs:9:10
   4: <cycles::C as bevy_ecs::component::Component>::register_required_components
             at ./examples/cycles.rs:12:10

But here's a case from "the wild" where I accidentally introduced a case like A above, and it wasn't so clear.

#[derive(Component, Default)]
#[require(Sprite, HitPoints, TilePos, MovingProgress, Speed, WorkCooldown, Worker)]
pub struct Worker;
Expand large backtrace
thread 'main' panicked at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_transform-0.15.0/src/components/transform.rs:43:12:
attempt to add with overflow
stack backtrace:
   0: rust_begin_unwind
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:74:14
   2: core::panicking::panic_const::panic_const_add_overflow
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:181:21
   3: <bevy_transform::components::transform::Transform as bevy_ecs::component::Component>::register_required_components
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_transform-0.15.0/src/components/transform.rs:43:12
   4: <bevy_sprite::sprite::Sprite as bevy_ecs::component::Component>::register_required_components
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_sprite-0.15.0/src/sprite.rs:13:10
   5: <entytd::worker::Worker as bevy_ecs::component::Component>::register_required_components
             at ./src/worker.rs:39:10
   6: bevy_ecs::component::Components::register_component
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/component.rs:917:13
   7: bevy_ecs::world::World::register_component
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/world/mod.rs:272:9
   8: <bevy_ecs::query::filter::With<T> as bevy_ecs::query::world_query::WorldQuery>::init_state
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/query/filter.rs:194:15
   9: <(F0,F1,F2) as bevy_ecs::query::world_query::WorldQuery>::init_state
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/query/world_query.rs:228:20
  10: bevy_ecs::query::state::QueryState<D,F>::new_uninitialized
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/query/state.rs:188:28
  11: bevy_ecs::query::state::QueryState<D,F>::new_with_access
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/query/state.rs:168:25
  12: <bevy_ecs::system::query::Query<D,F> as bevy_ecs::system::system_param::SystemParam>::init_state
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/system/system_param.rs:298:21
  13: <(P0,P1,P2,P3) as bevy_ecs::system::system_param::SystemParam>::init_state
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/system/system_param.rs:1913:21
  14: <bevy_ecs::system::function_system::FunctionSystem<Marker,F> as bevy_ecs::system::system::System>::initialize
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/system/function_system.rs:779:37
  15: bevy_ecs::schedule::schedule::ScheduleGraph::initialize
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/schedule/schedule.rs:1057:21
  16: bevy_ecs::schedule::schedule::Schedule::initialize
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/schedule/schedule.rs:406:13
  17: bevy_ecs::schedule::schedule::Schedule::run
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/schedule/schedule.rs:382:9
  18: bevy_ecs::world::World::try_run_schedule::{{closure}}
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/world/mod.rs:3720:55
  19: bevy_ecs::world::World::try_schedule_scope
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/world/mod.rs:3653:21
  20: bevy_ecs::world::World::try_run_schedule
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/world/mod.rs:3720:9
  21: bevy_app::main_schedule::Main::run_main::{{closure}}
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_app-0.15.0/src/main_schedule.rs:279:31
  22: bevy_ecs::world::World::resource_scope
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/world/mod.rs:2824:22
  23: bevy_app::main_schedule::Main::run_main
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_app-0.15.0/src/main_schedule.rs:277:9
  24: core::ops::function::FnMut::call_mut
             at /Users/me/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:166:5
  25: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
             at /Users/me/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:294:13
  26: <Func as bevy_ecs::system::exclusive_function_system::ExclusiveSystemParamFunction<fn(F0) .> Out>>::run::call_inner
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/system/exclusive_function_system.rs:245:21
  27: <Func as bevy_ecs::system::exclusive_function_system::ExclusiveSystemParamFunction<fn(F0) .> Out>>::run
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/system/exclusive_function_system.rs:248:17
  28: <bevy_ecs::system::exclusive_function_system::ExclusiveFunctionSystem<Marker,F> as bevy_ecs::system::system::System>::run::{{closure}}
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/system/exclusive_function_system.rs:129:23
  29: bevy_ecs::world::World::last_change_tick_scope
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/world/mod.rs:3187:9
  30: <bevy_ecs::system::exclusive_function_system::ExclusiveFunctionSystem<Marker,F> as bevy_ecs::system::system::System>::run
             at /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.0/src/system/exclusive_function_system.rs:121:9
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!

Additional information

I haven't looked at the required components code, but it feels like case A should be detectable. And it might be possible to improve case B by introducing a smaller recursion limit and a prettier error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorD-MacrosCode that generates Rust code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions