forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
constrain WorldQuery::get_state to only use &Components (bevyengine#1…
…3343) # 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](https://github.com/JoJoJet/bevy-trait-query/blob/0c0e7dd646b4a77fa3496ef5e9686107d17fdd1e/src/lib.rs#L445) inside of [`get_state`](https://github.com/JoJoJet/bevy-trait-query/blob/0c0e7dd646b4a77fa3496ef5e9686107d17fdd1e/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.
- Loading branch information
Showing
6 changed files
with
89 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.