Add a note to the on_unimplemented message for QueryData recommending &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`
```
This commit is contained in:
Chris Russell 2024-11-20 22:52:10 -05:00 committed by GitHub
parent deda3f2522
commit 4362b52a01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -272,7 +272,8 @@ use smallvec::SmallVec;
/// [`ReadOnly`]: Self::ReadOnly /// [`ReadOnly`]: Self::ReadOnly
#[diagnostic::on_unimplemented( #[diagnostic::on_unimplemented(
message = "`{Self}` is not valid to request as data in a `Query`", 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 { pub unsafe trait QueryData: WorldQuery {
/// The read-only variant of this [`QueryData`], which satisfies the [`ReadOnlyQueryData`] trait. /// The read-only variant of this [`QueryData`], which satisfies the [`ReadOnlyQueryData`] trait.