Skip to content

Commit 73b43aa

Browse files
cBournhonesquemockersf
authored andcommitted
Fix error in AnyOf (#14027)
# Objective - Fixes a correctness error introduced in #14013 ... ## Solution I've been playing around a lot of with the access code and I realized that I introduced a soundness error when trying to simplify the code. When we have a `Or<(With<A>, With<B>)>` filter, we cannot call ``` let mut intermediate = FilteredAccess::default(); $name::update_component_access($name, &mut intermediate); _new_access.append_or(&intermediate); ``` because that's just equivalent to adding the new components as `Or` clauses. For example if the existing `filter_sets` was `vec![With<C>]`, we would then get `vec![With<C>, With<A>, With<B>]` which translates to `A or B or C`. Instead what we want is `(A and B) or (A and C)`, so we need to have each new OR clause compose with the existing access like so: ``` let mut intermediate = _access.clone(); // if we previously had a With<C> in the filter_set, this will become `With<C> AND With<A>` $name::update_component_access($name, &mut intermediate); _new_access.append_or(&intermediate); ``` ## Testing - Added a unit test that is broken in main, but passes in this PR
1 parent 20638f3 commit 73b43aa

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

crates/bevy_ecs/src/query/fetch.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,16 +1864,13 @@ macro_rules! impl_anytuple_fetch {
18641864
fn update_component_access(state: &Self::State, _access: &mut FilteredAccess<ComponentId>) {
18651865
let mut _new_access = _access.clone();
18661866

1867-
// update the access (add the read/writes)
1868-
<($(Option<$name>,)*)>::update_component_access(state, _access);
1869-
18701867
// update the filters (Or<(With<$name>,)>)
18711868
let ($($name,)*) = state;
18721869
let mut _not_first = false;
18731870
$(
18741871
if _not_first {
1875-
// we use an intermediate access because we only want to update the filters, not the access
1876-
let mut intermediate = FilteredAccess::default();
1872+
// we use an intermediate access because we only want to update the filter_sets, not the access
1873+
let mut intermediate = _access.clone();
18771874
$name::update_component_access($name, &mut intermediate);
18781875
_new_access.append_or(&intermediate);
18791876
} else {
@@ -1884,6 +1881,11 @@ macro_rules! impl_anytuple_fetch {
18841881
)*
18851882

18861883
_access.filter_sets = _new_access.filter_sets;
1884+
1885+
// update the access (add the read/writes)
1886+
// Option<T> updates the access but not the filter_sets
1887+
<($(Option<$name>,)*)>::update_component_access(state, _access);
1888+
18871889
}
18881890
#[allow(unused_variables)]
18891891
fn init_state(world: &mut World) -> Self::State {

crates/bevy_ecs/src/system/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,13 @@ mod tests {
563563
run_system(&mut world, sys);
564564
}
565565

566+
#[test]
567+
fn any_of_with_and_without_common() {
568+
fn sys(_: Query<(&mut D, &C, AnyOf<(&A, &B)>)>, _: Query<&mut D, Without<C>>) {}
569+
let mut world = World::default();
570+
run_system(&mut world, sys);
571+
}
572+
566573
#[test]
567574
#[should_panic = "&bevy_ecs::system::tests::A conflicts with a previous access in this query."]
568575
fn any_of_with_mut_and_ref() {

0 commit comments

Comments
 (0)