From 7e3926d8de19f66ec110a0d12add11b6d903f25d Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Wed, 16 Aug 2023 21:40:40 -0400 Subject: [PATCH 1/4] factor out panics into separate fns --- crates/bevy_ecs/src/query/state.rs | 15 ++++++++++----- crates/bevy_ecs/src/system/function_system.rs | 11 ++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index ef01805937ad8..e088307b02304 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -189,12 +189,17 @@ impl QueryState { /// Many unsafe query methods require the world to match for soundness. This function is the easiest /// way of ensuring that it matches. #[inline] + #[track_caller] pub fn validate_world(&self, world_id: WorldId) { - assert!( - world_id == self.world_id, - "Attempted to use {} with a mismatched World. QueryStates can only be used with the World they were created from.", - std::any::type_name::(), - ); + #[inline(never)] + #[track_caller] + #[cold] + fn panic_mismatched(this: WorldId, other: WorldId) -> ! { + panic!("Encountered a mismatched World. This QueryState was created from {this:?}, but a method was called using {other:?}."); + } + if self.world_id != world_id { + panic_mismatched(self.world_id, world_id); + } } /// Update the current [`QueryState`] with information from the provided [`Archetype`] diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index e245f93716d78..5c22395bedaa7 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -216,8 +216,17 @@ impl SystemState { /// Asserts that the [`SystemState`] matches the provided world. #[inline] + #[track_caller] fn validate_world(&self, world_id: WorldId) { - assert!(self.matches_world(world_id), "Encountered a mismatched World. A SystemState cannot be used with Worlds other than the one it was created with."); + #[inline(never)] + #[track_caller] + #[cold] + fn panic_mismatched(this: WorldId, other: WorldId) -> ! { + panic!("Encountered a mismatched World. This SystemState was created from {this:?}, but a method was called using {other:?}."); + } + if !self.matches_world(world_id) { + panic_mismatched(self.world_id, world_id); + } } /// Updates the state's internal view of the [`World`]'s archetypes. If this is not called before fetching the parameters, From 2eb591cbca39037a87d9898a230c7a8c4b950d1c Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Wed, 16 Aug 2023 21:46:45 -0400 Subject: [PATCH 2/4] update an expected panic message --- crates/bevy_ecs/src/system/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/mod.rs b/crates/bevy_ecs/src/system/mod.rs index 3900dd68ac6ba..2fa5c3d5e6f15 100644 --- a/crates/bevy_ecs/src/system/mod.rs +++ b/crates/bevy_ecs/src/system/mod.rs @@ -1733,7 +1733,7 @@ mod tests { } #[test] - #[should_panic = "Attempted to use bevy_ecs::query::state::QueryState<()> with a mismatched World."] + #[should_panic = "Encountered a mismatched World."] fn query_validates_world_id() { let mut world1 = World::new(); let world2 = World::new(); From e87cfb6d522eeb435b341129d10f58a4c2674bdf Mon Sep 17 00:00:00 2001 From: Joseph <21144246+JoJoJet@users.noreply.github.com> Date: Thu, 21 Sep 2023 00:00:34 -0700 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: James Liu --- crates/bevy_ecs/src/query/state.rs | 1 + crates/bevy_ecs/src/system/function_system.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index e088307b02304..20d0fb2e75c09 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -197,6 +197,7 @@ impl QueryState { fn panic_mismatched(this: WorldId, other: WorldId) -> ! { panic!("Encountered a mismatched World. This QueryState was created from {this:?}, but a method was called using {other:?}."); } + if self.world_id != world_id { panic_mismatched(self.world_id, world_id); } diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index 5c22395bedaa7..c1d7f9535c7ca 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -224,6 +224,7 @@ impl SystemState { fn panic_mismatched(this: WorldId, other: WorldId) -> ! { panic!("Encountered a mismatched World. This SystemState was created from {this:?}, but a method was called using {other:?}."); } + if !self.matches_world(world_id) { panic_mismatched(self.world_id, world_id); } From 1c61f3023ee6435d04b78cb8aa3118df52a0133a Mon Sep 17 00:00:00 2001 From: Joseph Giordano <21144246+JoJoJet@users.noreply.github.com> Date: Thu, 21 Sep 2023 09:43:03 -0700 Subject: [PATCH 4/4] cargo fmt --- crates/bevy_ecs/src/query/state.rs | 2 +- crates/bevy_ecs/src/system/function_system.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 3b76955c1bae4..e64c257ec04b5 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -232,7 +232,7 @@ impl QueryState { fn panic_mismatched(this: WorldId, other: WorldId) -> ! { panic!("Encountered a mismatched World. This QueryState was created from {this:?}, but a method was called using {other:?}."); } - + if self.world_id != world_id { panic_mismatched(self.world_id, world_id); } diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index 9e735ce6fd315..9e5fd21513dc9 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -240,7 +240,7 @@ impl SystemState { fn panic_mismatched(this: WorldId, other: WorldId) -> ! { panic!("Encountered a mismatched World. This SystemState was created from {this:?}, but a method was called using {other:?}."); } - + if !self.matches_world(world_id) { panic_mismatched(self.world_id, world_id); }