Skip to content

fix: issue #24334#24357

Closed
tevans-3 wants to merge 1 commit into
bevyengine:mainfrom
tevans-3:main
Closed

fix: issue #24334#24357
tevans-3 wants to merge 1 commit into
bevyengine:mainfrom
tevans-3:main

Conversation

@tevans-3
Copy link
Copy Markdown

@tevans-3 tevans-3 commented May 20, 2026

Objective

Fixes #24334

Solution

I added a bool flag to ComponentDescriptor and a check helper to that struct's impl. During component registration, i.e. in register_component_inner, new checks determine (A) if the component is a resource and (B) if its required_by set is non-empty. If (A) and (B) are true, it warns. I also updated all files referencing new_with_layout.

Testing

Yes. I ran cargo test -p bevy_ecs component::.

cargo build compiled successfully.

@github-actions
Copy link
Copy Markdown
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide, as well as our policy regarding AI usage, and we look forward to reviewing your pull request shortly ✨

@mockersf
Copy link
Copy Markdown
Member

I tried to trigger this warning but didn't get it, where should it appear?

I think it would make more sense to compile fail if we want to disable this use case.

Also could you change the PR title to reflect what you're changing? The PR title will appear in the commit log of Bevy once merged.

@SpecificProtagonist
Copy link
Copy Markdown
Contributor

Hm, do we have a way to write tests that check for warnings?

Two reasons no warning is emitted:

  • the resource derive macro never sets the resource flag to true
  • required_by starts empty when the component is being registered and gets populated in Components::register_required_by; the check should be there

I think it would make more sense to compile fail if we want to disable this use case.

I initially thought it would be annoying to implement well for not much benefit (esp. since I want to be careful around adding too much code generation to each component and affecting compile times), but urben1680 found a way that should work well:

trait Component {
    const ASSERT_NOT_RESOURCE: ();
    fn register_required();}

struct MyComponent;
struct MyResource;

impl Component for MyComponent {
    const ASSERT_NOT_RESOURCE: () = ();
    fn register_required() {
        // #[require(MyResource)] is used, so proc macro adds following line
        // (varies depending on wheather a function call or type was specified)
        let () = MyResource::ASSERT_NOT_RESOURCE; // this panics during compilation
        
        // actual requiring code follows here
    }
}

impl Component for MyResource {
    const ASSERT_NOT_RESOURCE: () = panic!("is resource"); // only panics if evaluated somewhere}

mutable: bool,
clone_behavior: ComponentCloneBehavior,
relationship_accessor: Option<RelationshipAccessorInitializer>,
resource: bool,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens to be used in a benchmark (why CI is failing): https://github.com/bevyengine/bevy/blob/main/benches/benches/bevy_ecs/resources.rs#L20

@tevans-3
Copy link
Copy Markdown
Author

Hm, do we have a way to write tests that check for warnings?

Two reasons no warning is emitted:

* the resource derive macro never sets the `resource` flag to true

* `required_by` starts empty when the component is being registered and gets populated in `Components::register_required_by`; the check should be there

Is compile fail the preferred solution, then? Should I take a stab at implementing that instead of fixing this up?

@cart
Copy link
Copy Markdown
Member

cart commented May 20, 2026

Using a Resource as a required component isn't inherently broken, it is just likely to be. Additionally, given that we already log an error when there are multiple "resource entities", I think I'd prefer to not add additional per-component-registration checks / inlined data to cover this case.

@cart cart closed this May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Required components should not be resources

5 participants