Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ecoskey
Copy link
Contributor

@ecoskey ecoskey commented Feb 2, 2025

Objective

  • Currently, the error span for get_struct_field when encountering an enum or union points to the macro invocation, rather than the enum or union token. It also doesn't mention which macro reported the error.

Solution

  • Report the correct error span
  • Add parameter for passing in the name of the macro invocation

Testing

Bevy compiles fine with this change

Migration Guide

// before
let fields = get_struct_fields(&ast.data);

// after
let fields = get_struct_fields(&ast.data, "derive(Bundle)");

@ecoskey ecoskey added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Utils Utility functions and types S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Macros Code that generates Rust code D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Feb 2, 2025
/// `meta` should be the name of the macro calling this function.
pub fn get_struct_fields<'a>(
data: &'a Data,
meta: &str,
Copy link
Member

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}

Copy link
Contributor Author

@ecoskey ecoskey Feb 2, 2025

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

Copy link
Contributor

@chescock chescock left a 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 {
   | ^^^^^

Comment on lines +18 to +21
Fields::Unit => Err(Error::new(
data_struct.fields.span(),
format!("#[{meta}] only supports structs with more than zero fields"),
)),
Copy link
Contributor

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.)

if field.is_empty() {

Suggested change
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() }),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Utils Utility functions and types C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Macros Code that generates Rust code D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants