-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Merged by Bors] - Fix unsoundnes in insert
remove
and despawn
#7805
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -369,8 +369,19 @@ impl<'w> EntityMut<'w> { | |||||||||||||
) { | ||||||||||||||
let old_archetype = &mut archetypes[old_archetype_id]; | ||||||||||||||
let remove_result = old_archetype.swap_remove(old_location.archetype_row); | ||||||||||||||
// if an entity was moved into this entity's archetype row, update its archetype row | ||||||||||||||
if let Some(swapped_entity) = remove_result.swapped_entity { | ||||||||||||||
entities.set(swapped_entity.index(), old_location); | ||||||||||||||
let swapped_location = entities.get(swapped_entity).unwrap(); | ||||||||||||||
|
||||||||||||||
entities.set( | ||||||||||||||
swapped_entity.index(), | ||||||||||||||
EntityLocation { | ||||||||||||||
archetype_id: swapped_location.archetype_id, | ||||||||||||||
archetype_row: old_location.archetype_row, | ||||||||||||||
table_id: swapped_location.table_id, | ||||||||||||||
table_row: swapped_location.table_row, | ||||||||||||||
}, | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
let old_table_row = remove_result.table_row; | ||||||||||||||
let old_table_id = old_archetype.table_id(); | ||||||||||||||
|
@@ -393,9 +404,19 @@ impl<'w> EntityMut<'w> { | |||||||||||||
// SAFETY: move_result.new_row is a valid position in new_archetype's table | ||||||||||||||
let new_location = new_archetype.allocate(entity, move_result.new_row); | ||||||||||||||
|
||||||||||||||
// if an entity was moved into this entity's table spot, update its table row | ||||||||||||||
// if an entity was moved into this entity's table row, update its table row | ||||||||||||||
if let Some(swapped_entity) = move_result.swapped_entity { | ||||||||||||||
let swapped_location = entities.get(swapped_entity).unwrap(); | ||||||||||||||
|
||||||||||||||
entities.set( | ||||||||||||||
swapped_entity.index(), | ||||||||||||||
EntityLocation { | ||||||||||||||
archetype_id: swapped_location.archetype_id, | ||||||||||||||
archetype_row: swapped_location.archetype_row, | ||||||||||||||
table_id: swapped_location.table_id, | ||||||||||||||
table_row: old_location.table_row, | ||||||||||||||
Comment on lines
+414
to
+417
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Alternatively just mutate the table_row on swapped_location. |
||||||||||||||
}, | ||||||||||||||
); | ||||||||||||||
archetypes[swapped_location.archetype_id] | ||||||||||||||
.set_entity_table_row(swapped_location.archetype_row, old_table_row); | ||||||||||||||
} | ||||||||||||||
|
@@ -489,10 +510,19 @@ impl<'w> EntityMut<'w> { | |||||||||||||
} | ||||||||||||||
let remove_result = archetype.swap_remove(location.archetype_row); | ||||||||||||||
if let Some(swapped_entity) = remove_result.swapped_entity { | ||||||||||||||
let swapped_location = world.entities.get(swapped_entity).unwrap(); | ||||||||||||||
// SAFETY: swapped_entity is valid and the swapped entity's components are | ||||||||||||||
// moved to the new location immediately after. | ||||||||||||||
unsafe { | ||||||||||||||
world.entities.set(swapped_entity.index(), location); | ||||||||||||||
world.entities.set( | ||||||||||||||
swapped_entity.index(), | ||||||||||||||
EntityLocation { | ||||||||||||||
archetype_id: swapped_location.archetype_id, | ||||||||||||||
archetype_row: location.archetype_row, | ||||||||||||||
table_id: swapped_location.table_id, | ||||||||||||||
table_row: swapped_location.table_row, | ||||||||||||||
}, | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
table_row = remove_result.table_row; | ||||||||||||||
|
@@ -509,6 +539,19 @@ impl<'w> EntityMut<'w> { | |||||||||||||
|
||||||||||||||
if let Some(moved_entity) = moved_entity { | ||||||||||||||
let moved_location = world.entities.get(moved_entity).unwrap(); | ||||||||||||||
// SAFETY: `moved_entity` is valid and the provided `EntityLocation` accurately reflects | ||||||||||||||
// the current location of the entity and its component data. | ||||||||||||||
unsafe { | ||||||||||||||
world.entities.set( | ||||||||||||||
moved_entity.index(), | ||||||||||||||
EntityLocation { | ||||||||||||||
archetype_id: moved_location.archetype_id, | ||||||||||||||
archetype_row: moved_location.archetype_row, | ||||||||||||||
table_id: moved_location.table_id, | ||||||||||||||
table_row, | ||||||||||||||
}, | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
world.archetypes[moved_location.archetype_id] | ||||||||||||||
.set_entity_table_row(moved_location.archetype_row, table_row); | ||||||||||||||
} | ||||||||||||||
|
@@ -905,4 +948,159 @@ mod tests { | |||||||||||||
// Ensure that the location has been properly updated. | ||||||||||||||
assert!(entity.location() != old_location); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// regression test for https://github.com/bevyengine/bevy/pull/7805 | ||||||||||||||
#[test] | ||||||||||||||
BoxyUwU marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
fn removing_sparse_updates_archetype_row() { | ||||||||||||||
#[derive(Component, PartialEq, Debug)] | ||||||||||||||
struct Dense(u8); | ||||||||||||||
|
||||||||||||||
#[derive(Component)] | ||||||||||||||
#[component(storage = "SparseSet")] | ||||||||||||||
struct Sparse; | ||||||||||||||
|
||||||||||||||
let mut world = World::new(); | ||||||||||||||
let e1 = world.spawn((Dense(0), Sparse)).id(); | ||||||||||||||
let e2 = world.spawn((Dense(1), Sparse)).id(); | ||||||||||||||
|
||||||||||||||
world.entity_mut(e1).remove::<Sparse>(); | ||||||||||||||
assert_eq!(world.entity(e2).get::<Dense>().unwrap(), &Dense(1)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// regression test for https://github.com/bevyengine/bevy/pull/7805 | ||||||||||||||
#[test] | ||||||||||||||
BoxyUwU marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
fn removing_dense_updates_table_row() { | ||||||||||||||
#[derive(Component, PartialEq, Debug)] | ||||||||||||||
struct Dense(u8); | ||||||||||||||
|
||||||||||||||
#[derive(Component)] | ||||||||||||||
#[component(storage = "SparseSet")] | ||||||||||||||
struct Sparse; | ||||||||||||||
|
||||||||||||||
let mut world = World::new(); | ||||||||||||||
let e1 = world.spawn((Dense(0), Sparse)).id(); | ||||||||||||||
let e2 = world.spawn((Dense(1), Sparse)).id(); | ||||||||||||||
|
||||||||||||||
world.entity_mut(e1).remove::<Dense>(); | ||||||||||||||
assert_eq!(world.entity(e2).get::<Dense>().unwrap(), &Dense(1)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// regression test for https://github.com/bevyengine/bevy/pull/7805 | ||||||||||||||
#[test] | ||||||||||||||
fn inserting_sparse_updates_archetype_row() { | ||||||||||||||
#[derive(Component, PartialEq, Debug)] | ||||||||||||||
struct Dense(u8); | ||||||||||||||
|
||||||||||||||
#[derive(Component)] | ||||||||||||||
#[component(storage = "SparseSet")] | ||||||||||||||
struct Sparse; | ||||||||||||||
|
||||||||||||||
let mut world = World::new(); | ||||||||||||||
let e1 = world.spawn(Dense(0)).id(); | ||||||||||||||
let e2 = world.spawn(Dense(1)).id(); | ||||||||||||||
|
||||||||||||||
world.entity_mut(e1).insert(Sparse); | ||||||||||||||
assert_eq!(world.entity(e2).get::<Dense>().unwrap(), &Dense(1)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// regression test for https://github.com/bevyengine/bevy/pull/7805 | ||||||||||||||
#[test] | ||||||||||||||
fn inserting_dense_updates_archetype_row() { | ||||||||||||||
#[derive(Component, PartialEq, Debug)] | ||||||||||||||
struct Dense(u8); | ||||||||||||||
|
||||||||||||||
#[derive(Component)] | ||||||||||||||
struct Dense2; | ||||||||||||||
|
||||||||||||||
#[derive(Component)] | ||||||||||||||
#[component(storage = "SparseSet")] | ||||||||||||||
struct Sparse; | ||||||||||||||
|
||||||||||||||
let mut world = World::new(); | ||||||||||||||
let e1 = world.spawn(Dense(0)).id(); | ||||||||||||||
let e2 = world.spawn(Dense(1)).id(); | ||||||||||||||
|
||||||||||||||
world.entity_mut(e1).insert(Sparse).remove::<Sparse>(); | ||||||||||||||
|
||||||||||||||
// archetype with [e2, e1] | ||||||||||||||
// table with [e1, e2] | ||||||||||||||
|
||||||||||||||
world.entity_mut(e2).insert(Dense2); | ||||||||||||||
|
||||||||||||||
assert_eq!(world.entity(e1).get::<Dense>().unwrap(), &Dense(0)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
fn inserting_dense_updates_table_row() { | ||||||||||||||
#[derive(Component, PartialEq, Debug)] | ||||||||||||||
struct Dense(u8); | ||||||||||||||
|
||||||||||||||
#[derive(Component)] | ||||||||||||||
struct Dense2; | ||||||||||||||
|
||||||||||||||
#[derive(Component)] | ||||||||||||||
#[component(storage = "SparseSet")] | ||||||||||||||
struct Sparse; | ||||||||||||||
|
||||||||||||||
let mut world = World::new(); | ||||||||||||||
let e1 = world.spawn(Dense(0)).id(); | ||||||||||||||
let e2 = world.spawn(Dense(1)).id(); | ||||||||||||||
|
||||||||||||||
world.entity_mut(e1).insert(Sparse).remove::<Sparse>(); | ||||||||||||||
|
||||||||||||||
// archetype with [e2, e1] | ||||||||||||||
// table with [e1, e2] | ||||||||||||||
|
||||||||||||||
world.entity_mut(e1).insert(Dense2); | ||||||||||||||
|
||||||||||||||
assert_eq!(world.entity(e2).get::<Dense>().unwrap(), &Dense(1)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// regression test for https://github.com/bevyengine/bevy/pull/7805 | ||||||||||||||
#[test] | ||||||||||||||
fn despawning_entity_updates_archetype_row() { | ||||||||||||||
#[derive(Component, PartialEq, Debug)] | ||||||||||||||
struct Dense(u8); | ||||||||||||||
|
||||||||||||||
#[derive(Component)] | ||||||||||||||
#[component(storage = "SparseSet")] | ||||||||||||||
struct Sparse; | ||||||||||||||
|
||||||||||||||
let mut world = World::new(); | ||||||||||||||
let e1 = world.spawn(Dense(0)).id(); | ||||||||||||||
let e2 = world.spawn(Dense(1)).id(); | ||||||||||||||
|
||||||||||||||
world.entity_mut(e1).insert(Sparse).remove::<Sparse>(); | ||||||||||||||
|
||||||||||||||
// archetype with [e2, e1] | ||||||||||||||
// table with [e1, e2] | ||||||||||||||
|
||||||||||||||
world.entity_mut(e2).despawn(); | ||||||||||||||
|
||||||||||||||
assert_eq!(world.entity(e1).get::<Dense>().unwrap(), &Dense(0)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// regression test for https://github.com/bevyengine/bevy/pull/7805 | ||||||||||||||
#[test] | ||||||||||||||
fn despawning_entity_updates_table_row() { | ||||||||||||||
#[derive(Component, PartialEq, Debug)] | ||||||||||||||
struct Dense(u8); | ||||||||||||||
|
||||||||||||||
#[derive(Component)] | ||||||||||||||
#[component(storage = "SparseSet")] | ||||||||||||||
struct Sparse; | ||||||||||||||
|
||||||||||||||
let mut world = World::new(); | ||||||||||||||
let e1 = world.spawn(Dense(0)).id(); | ||||||||||||||
let e2 = world.spawn(Dense(1)).id(); | ||||||||||||||
|
||||||||||||||
world.entity_mut(e1).insert(Sparse).remove::<Sparse>(); | ||||||||||||||
|
||||||||||||||
// archetype with [e2, e1] | ||||||||||||||
// table with [e1, e2] | ||||||||||||||
|
||||||||||||||
world.entity_mut(e1).despawn(); | ||||||||||||||
|
||||||||||||||
assert_eq!(world.entity(e2).get::<Dense>().unwrap(), &Dense(1)); | ||||||||||||||
} | ||||||||||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we should consider exposing a pub(crate)
get_mut
fromEntities
or just mutate swapped_location with the archetype row.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I specifically chose to construct a whole new struct here so that if we change the fields we'll get a compile error rather than it continuing to compile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense to me. Not sure if the compiler will optimize that down though. Probably worth investigating separately.
Not sure if this is worth leaving a comment so others don't try to make that change.