bevy/crates/bevy_ecs_compile_fail_tests/tests/ui
Boxy 407c080e59 Replace ReadOnlyFetch with ReadOnlyWorldQuery (#4626)
# Objective

- Fix a type inference regression introduced by #3001
- Make read only bounds on world queries more user friendly

ptrification required you to write `Q::Fetch: ReadOnlyFetch` as `for<'w> QueryFetch<'w, Q>: ReadOnlyFetch` which has the same type inference problem as `for<'w> QueryFetch<'w, Q>: FilterFetch<'w>` had, i.e. the following code would error:
```rust
#[derive(Component)]
struct Foo;

fn bar(a: Query<(&Foo, Without<Foo>)>) {
    foo(a);
}

fn foo<Q: WorldQuery>(a: Query<Q, ()>)
where
    for<'w> QueryFetch<'w, Q>: ReadOnlyFetch,
{
}
```
`for<..>` bounds are also rather user unfriendly..

## Solution

Remove the `ReadOnlyFetch` trait in favour of a `ReadOnlyWorldQuery` trait, and remove `WorldQueryGats::ReadOnlyFetch` in favor of `WorldQuery::ReadOnly` allowing the previous code snippet to be written as:
```rust
#[derive(Component)]
struct Foo;

fn bar(a: Query<(&Foo, Without<Foo>)>) {
    foo(a);
}

fn foo<Q: ReadOnlyWorldQuery>(a: Query<Q, ()>) {}
``` 
This avoids the `for<...>` bound which makes the code simpler and also fixes the type inference issue.

The reason for moving the two functions out of `FetchState` and into `WorldQuery` is to allow the world query `&mut T` to share a `State` with the `&T` world query so that it can have `type ReadOnly = &T`. Presumably it would be possible to instead have a `ReadOnlyRefMut<T>` world query and then do `type ReadOnly = ReadOnlyRefMut<T>` much like how (before this PR) we had a `ReadOnlyWriteFetch<T>`. A side benefit of the current solution in this PR is that it will likely make it easier in the future to support an API such as `Query<&mut T> -> Query<&T>`. The primary benefit IMO is just that `ReadOnlyRefMut<T>` and its associated fetch would have to reimplement all of the logic that the `&T` world query impl does but this solution avoids that :)

---

## Changelog/Migration Guide

The trait `ReadOnlyFetch` has been replaced with `ReadOnlyWorldQuery` along with the `WorldQueryGats::ReadOnlyFetch` assoc type which has been replaced with `<WorldQuery::ReadOnly as WorldQueryGats>::Fetch`
- Any where clauses such as `QueryFetch<Q>: ReadOnlyFetch` should be replaced with `Q: ReadOnlyWorldQuery`.
- Any custom world query impls should implement `ReadOnlyWorldQuery` insead of `ReadOnlyFetch`

Functions `update_component_access` and `update_archetype_component_access` have been moved from the `FetchState` trait to `WorldQuery`
- Any callers should now call `Q::update_component_access(state` instead of `state.update_component_access` (and `update_archetype_component_access` respectively)
- Any custom world query impls should move the functions from the `FetchState` impl to `WorldQuery` impl

`WorldQuery` has been made an `unsafe trait`, `FetchState` has been made a safe `trait`. (I think this is how it should have always been, but regardless this is _definitely_ necessary now that the two functions have been moved to `WorldQuery`)
- If you have a custom `FetchState` impl make it a normal `impl` instead of `unsafe impl`
- If you have a custom `WorldQuery` impl make it an `unsafe impl`, if your code was sound before it is going to still be sound
2022-06-13 23:35:54 +00:00
..
entity_ref_mut_lifetime_safety.rs REMOVE unsound lifetime annotations on EntityMut (#4096) 2022-04-04 21:33:33 +00:00
entity_ref_mut_lifetime_safety.stderr REMOVE unsound lifetime annotations on EntityMut (#4096) 2022-04-04 21:33:33 +00:00
query_lifetime_safety.rs yeet unsound lifetime annotations on Query methods (#4243) 2022-03-22 02:49:41 +00:00
query_lifetime_safety.stderr yeet unsound lifetime annotations on Query methods (#4243) 2022-03-22 02:49:41 +00:00
system_param_derive_readonly.rs Make derived SystemParam readonly if possible (#4650) 2022-05-09 16:09:33 +00:00
system_param_derive_readonly.stderr Replace ReadOnlyFetch with ReadOnlyWorldQuery (#4626) 2022-06-13 23:35:54 +00:00
system_query_get_lifetime_safety.rs Assert compiler errors for compile_fail tests (#3067) 2021-11-13 22:43:19 +00:00
system_query_get_lifetime_safety.stderr Fix clippy lints for 1.57 (#3238) 2021-12-02 23:40:37 +00:00
system_query_get_many_lifetime_safety.rs Rename get_multiple APIs to get_many (#4384) 2022-03-31 20:59:26 +00:00
system_query_get_many_lifetime_safety.stderr Rename get_multiple APIs to get_many (#4384) 2022-03-31 20:59:26 +00:00
system_query_get_many_mut_lifetime_safety.rs Rename get_multiple APIs to get_many (#4384) 2022-03-31 20:59:26 +00:00
system_query_get_many_mut_lifetime_safety.stderr Rename get_multiple APIs to get_many (#4384) 2022-03-31 20:59:26 +00:00
system_query_iter_lifetime_safety.rs Assert compiler errors for compile_fail tests (#3067) 2021-11-13 22:43:19 +00:00
system_query_iter_lifetime_safety.stderr Fix clippy lints for 1.57 (#3238) 2021-12-02 23:40:37 +00:00
system_query_many_for_each_mut_lifetime_safety.rs Add methods for querying lists of entities. (#4879) 2022-06-06 16:09:16 +00:00
system_query_many_for_each_mut_lifetime_safety.stderr Add methods for querying lists of entities. (#4879) 2022-06-06 16:09:16 +00:00
system_query_set_get_lifetime_safety.rs ParamSet for conflicting SystemParam:s (#2765) 2022-03-29 23:39:38 +00:00
system_query_set_get_lifetime_safety.stderr ParamSet for conflicting SystemParam:s (#2765) 2022-03-29 23:39:38 +00:00
system_query_set_iter_lifetime_safety.rs ParamSet for conflicting SystemParam:s (#2765) 2022-03-29 23:39:38 +00:00
system_query_set_iter_lifetime_safety.stderr ParamSet for conflicting SystemParam:s (#2765) 2022-03-29 23:39:38 +00:00
system_state_get_lifetime_safety.rs Assert compiler errors for compile_fail tests (#3067) 2021-11-13 22:43:19 +00:00
system_state_get_lifetime_safety.stderr Assert compiler errors for compile_fail tests (#3067) 2021-11-13 22:43:19 +00:00
system_state_iter_lifetime_safety.rs Assert compiler errors for compile_fail tests (#3067) 2021-11-13 22:43:19 +00:00
system_state_iter_lifetime_safety.stderr Assert compiler errors for compile_fail tests (#3067) 2021-11-13 22:43:19 +00:00
system_state_iter_mut_overlap_safety.rs Implement iter() for mutable Queries (#2305) 2021-12-01 23:28:10 +00:00
system_state_iter_mut_overlap_safety.stderr Fix clippy lints for 1.57 (#3238) 2021-12-02 23:40:37 +00:00
world_query_derive.rs Introduce tests for derive(WorldQuery) (#4625) 2022-04-28 21:06:20 +00:00
world_query_derive.stderr Replace ReadOnlyFetch with ReadOnlyWorldQuery (#4626) 2022-06-13 23:35:54 +00:00