-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Better macro errors for get_struct_fields #17639
base: main
Are you sure you want to change the base?
Conversation
/// `meta` should be the name of the macro calling this function. | ||
pub fn get_struct_fields<'a>( | ||
data: &'a Data, | ||
meta: &str, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't change much, but meta
could be only the name of the trait being derived instead of derive{trait}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is meant to accomodate attribute macros, which don't fit the derive()
pattern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested it, and it works!
#[derive(VisitEntities, Bundle)]
struct UnitStruct;
#[derive(VisitEntities, Bundle)]
enum Enum {}
#[derive(VisitEntities, Bundle)]
union Union {
x: (),
}
fails with
error: #[derive(VisitEntities)] only supports structs with more than zero fields
--> crates\bevy_ecs\src\entity\visit_entities.rs:60:10
|
60 | #[derive(VisitEntities, Bundle)]
| ^^^^^^^^^^^^^
|
= note: this error originates in the derive macro `VisitEntities` (in Nightly builds, run with -Z macro-backtrace for more info)
error: #[derive(Bundle)] only supports structs with more than zero fields
--> crates\bevy_ecs\src\entity\visit_entities.rs:60:25
|
60 | #[derive(VisitEntities, Bundle)]
| ^^^^^^
|
= note: this error originates in the derive macro `Bundle` (in Nightly builds, run with -Z macro-backtrace for more info)
error: #[derive(VisitEntities)] only supports structs, not enums
--> crates\bevy_ecs\src\entity\visit_entities.rs:64:1
|
64 | enum Enum {}
| ^^^^
error: #[derive(Bundle)] only supports structs, not enums
--> crates\bevy_ecs\src\entity\visit_entities.rs:64:1
|
64 | enum Enum {}
| ^^^^
error: #[derive(VisitEntities)] only supports structs, not unions
--> crates\bevy_ecs\src\entity\visit_entities.rs:67:1
|
67 | union Union {
| ^^^^^
error: #[derive(Bundle)] only supports structs, not unions
--> crates\bevy_ecs\src\entity\visit_entities.rs:67:1
|
67 | union Union {
| ^^^^^
Fields::Unit => Err(Error::new( | ||
data_struct.fields.span(), | ||
format!("#[{meta}] only supports structs with more than zero fields"), | ||
)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize this is how the code worked before, but would it make sense to support these derives on unit structs by returning an empty list of fields? (Although derive_visit_entities_base
raises a separate error that we'd have to remove to fully support that.)
bevy/crates/bevy_ecs/macros/src/lib.rs
Line 231 in edba54a
if field.is_empty() { |
Fields::Unit => Err(Error::new( | |
data_struct.fields.span(), | |
format!("#[{meta}] only supports structs with more than zero fields"), | |
)), | |
Fields::Unit => Ok(const { &Punctuated::new() }), |
Objective
get_struct_field
when encountering an enum or union points to the macro invocation, rather than theenum
orunion
token. It also doesn't mention which macro reported the error.Solution
Testing
Bevy compiles fine with this change
Migration Guide