diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index c477e01597e86..3178c8a1fc128 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -1218,10 +1218,15 @@ unsafe impl WorldQuery for Option { } fn update_component_access(state: &T::State, access: &mut FilteredAccess) { - // We don't want to add the `with`/`without` of `T` as `Option` will match things regardless of - // `T`'s filters. for example `Query<(Option<&U>, &mut V)>` will match every entity with a `V` component - // regardless of whether it has a `U` component. If we don't do this the query will not conflict with - // `Query<&mut V, Without>` which would be unsound. + // FilteredAccess::add_[write,read] adds the component to the `with` filter. + // Those methods are called on `access` in `T::update_component_access`. + // But in `Option`, we specifically don't filter on `T`, + // since `(Option, &OtherComponent)` should be a valid item, even + // if `Option` is `None`. + // + // We pass a clone of the `FilteredAccess` to `T`, and only update the `Access` + // using `extend_access` so that we can apply `T`'s component_access + // without updating the `with` filters of `access`. let mut intermediate = access.clone(); T::update_component_access(state, &mut intermediate); access.extend_access(&intermediate);