bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.73k stars 3.61k forks source link

Plugin implementations with generic parameters can't call `app.add_plugins` on other plugins with generic parameters #11550

Closed NoahShomette closed 10 months ago

NoahShomette commented 10 months ago

Bevy version

0.12.1

What you did

Plugin implementations with generic parameters can't call app.add_plugins on other plugins with generic parameters. This worked in 0.11 however it no longer works as of 0.12.1

Note that if you implement a trait with generic bounds and attempt to implement that trait on App then you will run into the same issue.

Minimum reproduction

Plugin version


trait SubPluginTrait: Sync + Send + 'static {}

struct MainPlugin<A: SubPluginTrait> {
    a: PhantomData<A>,
}

impl<A: SubPluginTrait + Default> Plugin for MainPlugin<A> {
    fn build(&self, app: &mut bevy::prelude::App) {
        app.add_plugins(SubPlugin::<A>::default()); // Error appears here
    }
}

#[derive(Default)]
struct SubPlugin<A: SubPluginTrait + Default> {
    a: PhantomData<A>,
}

Trait version

trait ImplementedOnApp<A: SubPluginTrait + Default>{
    fn do_something_with_app(&mut self);
}

impl<A: SubPluginTrait + Default> ImplementedOnApp<A> for App{
    fn do_something_with_app(&mut self) {
        self.add_plugins(GenericPlugin2::<A>::default()); // Error appears here
    }
}

The error that is returned is:


error[E0277]: the trait bound `SubPlugin<A>: bevy_app::plugin::sealed::Plugins<_>` is not satisfied
   --> crates/client/client_binary/src/controls/testing.rs:13:25
    |
13  |         app.add_plugins(SubPlugin::<A>::default());
    |             ----------- ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `bevy_app::plugin::sealed::Plugins<_>` is not implemented for `SubPlugin<A>`
    |             |
    |             required by a bound introduced by this call
    |
    = help: the following other types implement trait `bevy_app::plugin::sealed::Plugins<Marker>`:
              <() as bevy_app::plugin::sealed::Plugins<(bevy_app::plugin::sealed::PluginsTupleMarker,)>>
              <(S0,) as bevy_app::plugin::sealed::Plugins<(bevy_app::plugin::sealed::PluginsTupleMarker, P0)>>
              <(S0, S1) as bevy_app::plugin::sealed::Plugins<(bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1)>>
              <(S0, S1, S2) as bevy_app::plugin::sealed::Plugins<(bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2)>>
              <(S0, S1, S2, S3) as bevy_app::plugin::sealed::Plugins<(bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2, P3)>>
              <(S0, S1, S2, S3, S4) as bevy_app::plugin::sealed::Plugins<(bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2, P3, P4)>>
              <(S0, S1, S2, S3, S4, S5) as bevy_app::plugin::sealed::Plugins<(bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2, P3, P4, P5)>>
              <(S0, S1, S2, S3, S4, S5, S6) as bevy_app::plugin::sealed::Plugins<(bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2, P3, P4, P5, P6)>>
            and 8 others
    = note: required for `SubPlugin<A>` to implement `Plugins<_>`
note: required by a bound in `App::add_plugins`
   --> /home/noahs/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_app-0.12.1/src/app.rs:721:52
    |
721 |     pub fn add_plugins<M>(&mut self, plugins: impl Plugins<M>) -> &mut Self {
    |                                                    ^^^^^^^^^^ required by this bound in `App::add_plugins`
alice-i-cecile commented 10 months ago

Can you reproduce it on main? What if you use the new style on the https://github.com/bevyengine/bevy/pull/11080 branch?

NoahShomette commented 10 months ago

Can you reproduce it on main? What if you use the new style on the #11080 branch?

Yes to both of these. Its still broken in main and on the branch

alice-i-cecile commented 10 months ago

Great. That's about what what I expected, but it's very helpful to have confirmation.

NoahShomette commented 10 months ago

Great. That's about what what I expected, but it's very helpful to have confirmation.

Hmm super weird but if I try it now on the 0.11 version of Bevy it no longer works. It 100% worked on my project back then though, I definitely used it lol. Rust issue...?

NoahShomette commented 10 months ago

Nevermind. I messed up on this one. Reproduction is wrong but same error. Error was originally that Leafwing added new TypePath bounds to their generic umbrella implementation of plugin for their struct and I copy pasted old code that didn't satisfy the new bounds and it led me on a rabbit hole. Plugin works great still :)