From 6179686e99214ed9f9fa5261d9b0a814c72058b8 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Tue, 21 Mar 2023 19:30:53 -0400 Subject: [PATCH] check for conflicting accesses in `assert_is_system` --- crates/bevy_ecs/src/system/mod.rs | 62 +++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/crates/bevy_ecs/src/system/mod.rs b/crates/bevy_ecs/src/system/mod.rs index 19a68b17137cc..1ce4bfc17f915 100644 --- a/crates/bevy_ecs/src/system/mod.rs +++ b/crates/bevy_ecs/src/system/mod.rs @@ -123,17 +123,40 @@ pub use system::*; pub use system_param::*; pub use system_piping::*; +use crate::world::World; + /// Ensure that a given function is a [system](System). /// /// This should be used when writing doc examples, /// to confirm that systems used in an example are /// valid systems. -pub fn assert_is_system>(sys: S) { - if false { - // Check it can be converted into a system - // TODO: This should ensure that the system has no conflicting system params - IntoSystem::into_system(sys); - } +/// +/// # Examples +/// +/// The following example will panic when run since the +/// system's parameters mutably access the same component +/// multiple times. +/// +/// ```should_panic +/// # use bevy_ecs::{prelude::*, system::assert_is_system}; +/// # +/// # #[derive(Component)] +/// # struct Transform; +/// # +/// fn my_system(query1: Query<&mut Transform>, query2: Query<&mut Transform>) { +/// // ... +/// } +/// +/// assert_is_system(my_system); +/// ``` +pub fn assert_is_system( + system: impl IntoSystem, +) { + let mut system = IntoSystem::into_system(system); + + // Initialize the system, which will panic if the system has access conflicts. + let mut world = World::new(); + system.initialize(&mut world); } /// Ensure that a given function is a [read-only system](ReadOnlySystem). @@ -141,15 +164,30 @@ pub fn assert_is_system>(sys: S) /// This should be used when writing doc examples, /// to confirm that systems used in an example are /// valid systems. -pub fn assert_is_read_only_system>(sys: S) +/// +/// # Examples +/// +/// The following example will fail to compile +/// since the system accesses a component mutably. +/// +/// ```compile_fail +/// # use bevy_ecs::{prelude::*, system::assert_is_read_only_system}; +/// # +/// # #[derive(Component)] +/// # struct Transform; +/// # +/// fn my_system(query: Query<&mut Transform>) { +/// // ... +/// } +/// +/// assert_is_read_only_system(my_system); +/// ``` +pub fn assert_is_read_only_system(system: S) where + S: IntoSystem, S::System: ReadOnlySystem, { - if false { - // Check it can be converted into a system - // TODO: This should ensure that the system has no conflicting system params - IntoSystem::into_system(sys); - } + assert_is_system(system); } #[cfg(test)]