-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce type-uniqueness of Bundles #2387
Comments
Macros don't have access to type information. Typechecking requires the full crate to be expanded first. |
Oof :( That doesn't surprise me but is still sad. So the complex cases are approximately impossible to check for statically? That pushes me much further towards "inserting duplicate components should be an error by default". |
An alternative would be to issue a warning whenever duplicate components up in a bundle, and more importantly this should be possible to silence: I can see a use case being to put a large nested bundle at the top of a struct, and then listing components to override under it. |
You might be able to generate some code which checks the type info at compile time. |
Could you provide an example of this? |
Just a heads up that we do already ensure that there aren't duplicate components when bundles are inserted. This test passes. Static validation of bundle derives is definitely nicer though! #[test]
#[should_panic]
fn duplicate_components_panic() {
let mut world = World::new();
world.spawn().insert_bundle((1, 2));
} |
So the current impl I have working for a single layer of components, however, nested bundles are not yet supported because idk if it's possible across crate boundaries... |
Yeah, this is basically resolved. Compile-time checks would be great, but not happening without work on the language itself. Closing. |
What problem does this solve or what need does it fill?
Currently, bundles can have fields of duplicate types. For example:
compiles with no warning.
However, this behaviour is deeply wrong, and leads to silent, subtle bugs in end user code.
Each field in a bundle represents one component that should be inserted into the final entity, and entities can only have one unique component of each (Rust) type.
Any duplicate components will have their data silently erased by the last field of the same type; wasting work and causing subtle and serious logic bugs. In the demonstrated example, setting the value of
foo1
has no effect: it will always be overwritten byfoo2
.This problem is particularly rough when working with type aliases (where the problem is not immediately obvious) and nested bundles (where there can be a large number of elided types, and new errors can occur without any change to user code on a new release if more components are added).
What solution would you like?
The
#[derive(Bundle)
macro should check for duplicated types, and error (ideally at compile time) if they are found with an error message that displays both the field and type names.Be sure to test that this check works with:
What alternative(s) have you considered?
Command
error handling #2241. I would prefer to leave that as an additional layer of opt-in safety, and instead provide compile time checks for obvious mistakes.The text was updated successfully, but these errors were encountered: