From a71f48aadaf76f83421c46b68c99bb565b23b230 Mon Sep 17 00:00:00 2001 From: MrGunflame Date: Sun, 25 Jun 2023 19:45:58 +0200 Subject: [PATCH 1/3] Correctly apply `track_caller` to panicking entity accessors methods --- crates/bevy_ecs/src/system/commands/mod.rs | 14 ++++++++--- crates/bevy_ecs/src/world/mod.rs | 28 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 0a2f36170eb41..fac21c7c0ffc2 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -307,11 +307,19 @@ impl<'w, 's> Commands<'w, 's> { #[inline] #[track_caller] pub fn entity<'a>(&'a mut self, entity: Entity) -> EntityCommands<'w, 's, 'a> { - self.get_entity(entity).unwrap_or_else(|| { + #[inline(never)] + #[cold] + #[track_caller] + fn panic_no_entity(entity: Entity) -> ! { panic!( "Attempting to create an EntityCommands for entity {entity:?}, which doesn't exist.", - ) - }) + ); + } + + match self.get_entity(entity) { + Some(entity) => entity, + None => panic_no_entity(entity), + } } /// Returns the [`EntityCommands`] for the requested [`Entity`], if it exists. diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 0a2b4011ab3de..66595cb2a54ce 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -241,10 +241,19 @@ impl World { /// assert_eq!(position.x, 0.0); /// ``` #[inline] + #[track_caller] pub fn entity(&self, entity: Entity) -> EntityRef { - // Lazily evaluate panic!() via unwrap_or_else() to avoid allocation unless failure - self.get_entity(entity) - .unwrap_or_else(|| panic!("Entity {entity:?} does not exist")) + #[inline(never)] + #[cold] + #[track_caller] + fn panic_no_entity(entity: Entity) -> ! { + panic!("Entity {entity:?} does not exist"); + } + + match self.get_entity(entity) { + Some(entity) => entity, + None => panic_no_entity(entity), + } } /// Retrieves an [`EntityMut`] that exposes read and write operations for the given `entity`. @@ -268,9 +277,18 @@ impl World { /// ``` #[inline] pub fn entity_mut(&mut self, entity: Entity) -> EntityMut { + #[inline(never)] + #[cold] + #[track_caller] + fn panic_no_entity(entity: Entity) -> ! { + panic!("Entity {entity:?} does not exist"); + } + // Lazily evaluate panic!() via unwrap_or_else() to avoid allocation unless failure - self.get_entity_mut(entity) - .unwrap_or_else(|| panic!("Entity {entity:?} does not exist")) + match self.get_entity_mut(entity) { + Some(entity) => entity, + None => panic_no_entity(entity), + } } /// Returns the components of an [`Entity`](crate::entity::Entity) through [`ComponentInfo`](crate::component::ComponentInfo). From baaaf1f2b02e3db8d5c25d6be52cb3258c003c8f Mon Sep 17 00:00:00 2001 From: MrGunflame Date: Sun, 25 Jun 2023 19:59:15 +0200 Subject: [PATCH 2/3] Apply `track_caller` to `World::entity_mut` --- crates/bevy_ecs/src/world/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 66595cb2a54ce..5a0a4e5cc56df 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -276,6 +276,7 @@ impl World { /// position.x = 1.0; /// ``` #[inline] + #[track_caller] pub fn entity_mut(&mut self, entity: Entity) -> EntityMut { #[inline(never)] #[cold] From f88b9676adf465af200a2ee803a18b6734fc158c Mon Sep 17 00:00:00 2001 From: 0xc0001a2040 Date: Mon, 26 Jun 2023 17:21:46 +0200 Subject: [PATCH 3/3] Update crates/bevy_ecs/src/world/mod.rs Co-authored-by: Giacomo Stevanato --- crates/bevy_ecs/src/world/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 5a0a4e5cc56df..a5d1e5fb14e7b 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -285,7 +285,6 @@ impl World { panic!("Entity {entity:?} does not exist"); } - // Lazily evaluate panic!() via unwrap_or_else() to avoid allocation unless failure match self.get_entity_mut(entity) { Some(entity) => entity, None => panic_no_entity(entity),