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

Improve codegen for world validation #9464

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 10 additions & 5 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,17 @@ impl<Q: WorldQuery, F: ReadOnlyWorldQuery> QueryState<Q, F> {
/// 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::<Self>(),
);
#[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:?}.");
}
JoJoJet marked this conversation as resolved.
Show resolved Hide resolved
if self.world_id != world_id {
panic_mismatched(self.world_id, world_id);
}
}

/// Update the current [`QueryState`] with information from the provided [`Archetype`]
Expand Down
11 changes: 10 additions & 1 deletion crates/bevy_ecs/src/system/function_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,17 @@ impl<Param: SystemParam> SystemState<Param> {

/// 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:?}.");
}
JoJoJet marked this conversation as resolved.
Show resolved Hide resolved
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,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down