bevy/crates/bevy_ecs/src/system
Vic 0eb4bb6bab
constrain WorldQuery::get_state to only use &Components (#13343)
# Objective

Passing `&World` in the `WorldQuery::get_state` method is unnecessary,
as all implementations of this method in the engine either only access
`Components` in `&World`, or do nothing with it.
It can introduce UB by necessitating the creation of a `&World` from a
`UnsafeWorldCell`.
This currently happens in `Query::transmute_lens`, which obtains a
`&World` from the internal `UnsafeWorldCell` solely to pass to
`get_state`. `Query::join` suffers from the same issue.
Other cases of UB come from allowing implementors of `WorldQuery` to
freely access `&World`, like in the `bevy-trait-query` crate, where a
[reference to a resource is
obtained](0c0e7dd646/src/lib.rs (L445))
inside of
[`get_state`](0c0e7dd646/src/one.rs (L245)),
potentially aliasing with a `ResMut` parameter in the same system.

`WorldQuery::init_state` currently requires `&mut World`, which doesn't
suffer from these issues.
But that too can be changed to receive a wrapper around `&mut
Components` and `&mut Storages` for consistency in a follow-up PR.

## Solution

Replace the `&World` parameter in `get_state` with `&Components`.

## Changelog

 `WorldQuery::get_state` now takes `&Components` instead of `&World`.
The `transmute`, `transmute_filtered`, `join` and `join_filtered`
methods on `QueryState` now similarly take `&Components` instead of
`&World`.

## Migration Guide

Users of `WorldQuery::get_state` or `transmute`, `transmute_filtered`,
`join` and `join_filtered` methods on `QueryState` now need to pass
`&Components` instead of `&World`.
`&Components` can be trivially obtained from either `components` method
on `&World` or `UnsafeWorldCell`.
For implementors of `WorldQuery::get_state` that were accessing more
than the `Components` inside `&World` and its methods, this is no longer
allowed.
2024-05-13 21:00:01 +00:00
..
commands Better SystemId to Entity conversions (#13090) 2024-04-25 18:47:49 +00:00
adapter_system.rs bevy_ecs address trivial cases of unsafe_op_in_unsafe_fn (#11861) 2024-02-22 00:04:38 +00:00
combinator.rs bevy_ecs address trivial cases of unsafe_op_in_unsafe_fn (#11861) 2024-02-22 00:04:38 +00:00
exclusive_function_system.rs Component Lifecycle Hooks and a Deferred World (#10756) 2024-03-01 14:59:22 +00:00
exclusive_system_param.rs impl ExclusiveSystemParam for PhantomData (#11153) 2024-01-01 16:02:21 +00:00
function_system.rs Make SystemParam::new_archetype and QueryState::new_archetype unsafe functions (#13044) 2024-04-21 02:49:42 +00:00
mod.rs Fix Ci failing over dead code in tests (#12623) 2024-03-21 18:08:47 +00:00
query.rs constrain WorldQuery::get_state to only use &Components (#13343) 2024-05-13 21:00:01 +00:00
system_name.rs impl ExclusiveSystemParam for SystemName (#11163) 2024-01-01 17:08:29 +00:00
system_param.rs Make SystemParam::new_archetype and QueryState::new_archetype unsafe functions (#13044) 2024-04-21 02:49:42 +00:00
system_registry.rs Better SystemId to Entity conversions (#13090) 2024-04-25 18:47:49 +00:00
system.rs add schedule docs (#13174) 2024-05-02 18:31:32 +00:00