Skip to content

Invalid lifetime when deriving SystemParam #7447

@BeastLe9enD

Description

@BeastLe9enD

Bevy Version

The latest version on github (a441939)

I am getting a problem when using #[derive(SystemParam)]. My structure is the following :

#[derive(SystemParam)]
pub struct InputResources<'w, 's> {
    pub keyboard_input: Res<'w, Input<KeyCode>>,
    pub egui_input: ResMut<'w, EguiRenderInputContainer>,
    #[system_param(ignore)]
    _marker: PhantomData<&'s ()>,
}

In my opinion this is valid, but the compiler complains:

error: lifetime may not live long enough
  --> crates\kamel-egui\src\systems.rs:56:10
   |
56 | #[derive(SystemParam)]
   |          ^^^^^^^^^^^
   |          |
   |          lifetime `'s` defined here
   |          lifetime `'s2` defined here
   |          associated function was supposed to return data with lifetime `'s2` but it is returning data with lifetime `'s`
   |
   = help: consider adding the following bound: `'s: 's2`
   = note: this error originates in the derive macro `SystemParam` (in Nightly builds, run with -Z macro-backtrace for more info)

I expanded the macro using cargo expand.

...
unsafe fn get_param<'w2, 's2>(
            state: &'s2 mut Self::State,
            system_meta: &bevy_ecs::system::SystemMeta,
            world: &'w2 bevy_ecs::world::World,
            change_tick: u32,
        ) -> Self::Item<'w2, 's2> {
            let (f0, f1) = <(
                Res<'w, Input<KeyCode>>,
                ResMut<'w, EguiRenderInputContainer>,
            ) as bevy_ecs::system::SystemParam>::get_param(
                &mut state.state,
                system_meta,
                world,
                change_tick,
            );
            InputResources {
                keyboard_input: f0,
                egui_input: f1,
                _marker: <PhantomData<&'s ()>>::default(),
            }
        }
...

As the error already says, a structure with the lifetime 's is returned, but the function has a return type with the lifetime 's2.
I don't know if the error only occurs because I'm using something incorrectly, but even if I am, I don't really find the error message accurate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorC-DocsAn addition or correction to our documentation

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions