Skip to content

Commit 099386f

Browse files
committed
Fix unsound lifetime annotation on Query::get_component (#2964)
#2605 changed the lifetime annotations on `get_component` introducing unsoundness as you could keep the returned borrow even after using the query. Example unsoundness: ```rust use bevy::prelude::*; fn main() { App::new() .add_startup_system(startup) .add_system(unsound) .run(); } #[derive(Debug, Component, PartialEq, Eq)] struct Foo(Vec<u32>); fn startup(mut c: Commands) { let e = c.spawn().insert(Foo(vec![10])).id(); c.insert_resource(e); } fn unsound(mut q: Query<&mut Foo>, res: Res<Entity>) { let foo = q.get_component::<Foo>(*res).unwrap(); let mut foo2 = q.iter_mut().next().unwrap(); let first_elem = &foo.0[0]; for _ in 0..16 { foo2.0.push(12); } dbg!(*first_elem); } ``` output: `[src/main.rs:26] *first_elem = 0`
1 parent f4776f2 commit 099386f

File tree

1 file changed

+1
-4
lines changed

1 file changed

+1
-4
lines changed

crates/bevy_ecs/src/system/query.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,7 @@ where
699699
/// # print_selected_character_name_system.system();
700700
/// ```
701701
#[inline]
702-
pub fn get_component<T: Component>(
703-
&self,
704-
entity: Entity,
705-
) -> Result<&'w T, QueryComponentError> {
702+
pub fn get_component<T: Component>(&self, entity: Entity) -> Result<&T, QueryComponentError> {
706703
let world = self.world;
707704
let entity_ref = world
708705
.get_entity(entity)

0 commit comments

Comments
 (0)