Skip to content

Commit

Permalink
Add a note to the on_unimplemented message for QueryData recommendi…
Browse files Browse the repository at this point in the history
…ng `&T` and `&mut T`. (#16449)

# Objective

A new user is likely to try `Query<Component>` instead of
`Query<&Component>`. The error message should guide them to the right
solution.

## Solution

Add a note to the on_unimplemented message for `QueryData` recommending
`&T` and `&mut T`.

The full error message now looks like: 

```
error[E0277]: `A` is not valid to request as data in a `Query`
   --> crates\bevy_ecs\src\query\world_query.rs:260:18
    |
260 | fn system(query: Query<A>) {}
    |                  ^^^^^^^^ invalid `Query` data
    |
    = help: the trait `fetch::QueryData` is not implemented for `A`
    = note: if `A` is a component type, try using `&A` or `&mut A`
    = help: the following other types implement trait `fetch::QueryData`:
              &'__w mut T
              &Archetype
              &T
              ()
              (F,)
              (F0, F1)
              (F0, F1, F2)
              (F0, F1, F2, F3)
            and 41 others
note: required by a bound in `system::query::Query`
   --> crates\bevy_ecs\src\system\query.rs:362:37
    |
362 | pub struct Query<'world, 'state, D: QueryData, F: QueryFilter = ()> {
    |                                     ^^^^^^^^^ required by this bound in `Query`
```
  • Loading branch information
chescock authored Nov 21, 2024
1 parent deda3f2 commit 4362b52
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ use smallvec::SmallVec;
/// [`ReadOnly`]: Self::ReadOnly
#[diagnostic::on_unimplemented(
message = "`{Self}` is not valid to request as data in a `Query`",
label = "invalid `Query` data"
label = "invalid `Query` data",
note = "if `{Self}` is a component type, try using `&{Self}` or `&mut {Self}`"
)]
pub unsafe trait QueryData: WorldQuery {
/// The read-only variant of this [`QueryData`], which satisfies the [`ReadOnlyQueryData`] trait.
Expand Down

0 comments on commit 4362b52

Please sign in to comment.