Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix access checks for DeferredWorld as SystemParam. #17616

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions crates/bevy_ecs/src/query/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,16 @@ impl<T: SparseSetIndex> Access<T> {
self.writes_all_resources || !self.resource_writes.is_clear()
}

/// Returns `true` if this accesses any resources or components.
pub fn has_any_read(&self) -> bool {
self.has_any_component_read() || self.has_any_resource_read()
}

/// Returns `true` if this accesses any resources or components mutably.
pub fn has_any_write(&self) -> bool {
self.has_any_component_write() || self.has_any_resource_write()
}

/// Returns true if this has an archetypal (indirect) access to the component given by `index`.
///
/// This is a component whose value is not accessed (and thus will never cause conflicts),
Expand Down Expand Up @@ -1319,12 +1329,16 @@ impl<T: SparseSetIndex> FilteredAccessSet<T> {

/// Marks the set as reading all possible indices of type T.
pub fn read_all(&mut self) {
self.combined_access.read_all();
let mut filter = FilteredAccess::matches_everything();
filter.read_all();
self.add(filter);
}

/// Marks the set as writing all T.
pub fn write_all(&mut self) {
self.combined_access.write_all();
let mut filter = FilteredAccess::matches_everything();
filter.write_all();
self.add(filter);
}

/// Removes all accesses stored in this set.
Expand Down
32 changes: 31 additions & 1 deletion crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ mod tests {
Commands, In, IntoSystem, Local, NonSend, NonSendMut, ParamSet, Query, Res, ResMut,
Single, StaticSystemParam, System, SystemState,
},
world::{EntityMut, FromWorld, World},
world::{DeferredWorld, EntityMut, FromWorld, World},
};

#[derive(Resource, PartialEq, Debug)]
Expand Down Expand Up @@ -1624,6 +1624,36 @@ mod tests {
super::assert_system_does_not_conflict(system);
}

#[test]
#[should_panic(
expected = "error[B0001]: Query<EntityRef, ()> in system bevy_ecs::system::tests::assert_deferred_world_and_entity_ref_system_does_conflict_first::system accesses component(s) in a way that conflicts with a previous system parameter. Consider using `Without<T>` to create disjoint Queries or merging conflicting Queries into a `ParamSet`. See: https://bevyengine.org/learn/errors/b0001"
)]
fn assert_deferred_world_and_entity_ref_system_does_conflict_first() {
fn system(_world: DeferredWorld, _query: Query<EntityRef>) {}
super::assert_system_does_not_conflict(system);
}

#[test]
#[should_panic(
expected = "DeferredWorld in system bevy_ecs::system::tests::assert_deferred_world_and_entity_ref_system_does_conflict_second::system conflicts with a previous access."
)]
fn assert_deferred_world_and_entity_ref_system_does_conflict_second() {
fn system(_query: Query<EntityRef>, _world: DeferredWorld) {}
super::assert_system_does_not_conflict(system);
}

#[test]
fn assert_deferred_world_and_empty_query_does_not_conflict_first() {
fn system(_world: DeferredWorld, _query: Query<Entity>) {}
super::assert_system_does_not_conflict(system);
}

#[test]
fn assert_deferred_world_and_empty_query_does_not_conflict_second() {
fn system(_query: Query<Entity>, _world: DeferredWorld) {}
super::assert_system_does_not_conflict(system);
}

#[test]
#[should_panic]
fn panic_inside_system() {
Expand Down
11 changes: 9 additions & 2 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,9 +1072,16 @@ unsafe impl<'w> SystemParam for DeferredWorld<'w> {
type Item<'world, 'state> = DeferredWorld<'world>;

fn init_state(_world: &mut World, system_meta: &mut SystemMeta) -> Self::State {
system_meta.component_access_set.read_all();
assert!(
!system_meta
.component_access_set
.combined_access()
.has_any_read(),
"DeferredWorld in system {} conflicts with a previous access.",
system_meta.name,
);
system_meta.component_access_set.write_all();
system_meta.set_has_deferred();
system_meta.archetype_component_access.write_all();
}

unsafe fn get_param<'world, 'state>(
Expand Down