forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yeet unsound lifetime annotations on
Query
methods (bevyengine#4243)
# Objective Continuation of bevyengine#2964 (I really should have checked other methods when I made that PR) yeet unsound lifetime annotations on `Query` methods. Example unsoundness: ```rust use bevy::prelude::*; fn main() { App::new().add_startup_system(bar).add_system(foo).run(); } pub fn bar(mut cmds: Commands) { let e = cmds.spawn().insert(Foo { a: 10 }).id(); cmds.insert_resource(e); } #[derive(Component, Debug, PartialEq, Eq)] pub struct Foo { a: u32, } pub fn foo(mut query: Query<&mut Foo>, e: Res<Entity>) { dbg!("hi"); { let data: &Foo = query.get(*e).unwrap(); let data2: Mut<Foo> = query.get_mut(*e).unwrap(); assert_eq!(data, &*data2); // oops UB } { let data: &Foo = query.single(); let data2: Mut<Foo> = query.single_mut(); assert_eq!(data, &*data2); // oops UB } { let data: &Foo = query.get_single().unwrap(); let data2: Mut<Foo> = query.get_single_mut().unwrap(); assert_eq!(data, &*data2); // oops UB } { let data: &Foo = query.iter().next().unwrap(); let data2: Mut<Foo> = query.iter_mut().next().unwrap(); assert_eq!(data, &*data2); // oops UB } { let mut opt_data: Option<&Foo> = None; let mut opt_data_2: Option<Mut<Foo>> = None; query.for_each(|data| opt_data = Some(data)); query.for_each_mut(|data| opt_data_2 = Some(data)); assert_eq!(opt_data.unwrap(), &*opt_data_2.unwrap()); // oops UB } dbg!("bye"); } ``` ## Solution yeet unsound lifetime annotations on `Query` methods Co-authored-by: Carter Anderson <[email protected]>
- Loading branch information
Showing
7 changed files
with
324 additions
and
24 deletions.
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
92 changes: 92 additions & 0 deletions
92
crates/bevy_ecs_compile_fail_tests/tests/ui/query_lifetime_safety.rs
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use bevy_ecs::prelude::*; | ||
use bevy_ecs::system::SystemState; | ||
|
||
#[derive(Component, Eq, PartialEq, Debug)] | ||
struct Foo(u32); | ||
|
||
fn main() { | ||
let mut world = World::default(); | ||
let e = world.spawn().insert(Foo(10_u32)).id(); | ||
|
||
let mut system_state = SystemState::<Query<&mut Foo>>::new(&mut world); | ||
{ | ||
let mut query = system_state.get_mut(&mut world); | ||
dbg!("hi"); | ||
{ | ||
let data: &Foo = query.get(e).unwrap(); | ||
let mut data2: Mut<Foo> = query.get_mut(e).unwrap(); | ||
assert_eq!(data, &mut *data2); // oops UB | ||
} | ||
|
||
{ | ||
let mut data2: Mut<Foo> = query.get_mut(e).unwrap(); | ||
let data: &Foo = query.get(e).unwrap(); | ||
assert_eq!(data, &mut *data2); // oops UB | ||
} | ||
|
||
{ | ||
let data: &Foo = query.get_component::<Foo>(e).unwrap(); | ||
let mut data2: Mut<Foo> = query.get_component_mut(e).unwrap(); | ||
assert_eq!(data, &mut *data2); // oops UB | ||
} | ||
|
||
{ | ||
let mut data2: Mut<Foo> = query.get_component_mut(e).unwrap(); | ||
let data: &Foo = query.get_component::<Foo>(e).unwrap(); | ||
assert_eq!(data, &mut *data2); // oops UB | ||
} | ||
|
||
{ | ||
let data: &Foo = query.single(); | ||
let mut data2: Mut<Foo> = query.single_mut(); | ||
assert_eq!(data, &mut *data2); // oops UB | ||
} | ||
|
||
{ | ||
let mut data2: Mut<Foo> = query.single_mut(); | ||
let data: &Foo = query.single(); | ||
assert_eq!(data, &mut *data2); // oops UB | ||
} | ||
|
||
{ | ||
let data: &Foo = query.get_single().unwrap(); | ||
let mut data2: Mut<Foo> = query.get_single_mut().unwrap(); | ||
assert_eq!(data, &mut *data2); // oops UB | ||
} | ||
|
||
{ | ||
let mut data2: Mut<Foo> = query.get_single_mut().unwrap(); | ||
let data: &Foo = query.get_single().unwrap(); | ||
assert_eq!(data, &mut *data2); // oops UB | ||
} | ||
|
||
{ | ||
let data: &Foo = query.iter().next().unwrap(); | ||
let mut data2: Mut<Foo> = query.iter_mut().next().unwrap(); | ||
assert_eq!(data, &mut *data2); // oops UB | ||
} | ||
|
||
{ | ||
let mut data2: Mut<Foo> = query.iter_mut().next().unwrap(); | ||
let data: &Foo = query.iter().next().unwrap(); | ||
assert_eq!(data, &mut *data2); // oops UB | ||
} | ||
|
||
{ | ||
let mut opt_data: Option<&Foo> = None; | ||
let mut opt_data_2: Option<Mut<Foo>> = None; | ||
query.for_each(|data| opt_data = Some(data)); | ||
query.for_each_mut(|data| opt_data_2 = Some(data)); | ||
assert_eq!(opt_data.unwrap(), &mut *opt_data_2.unwrap()); // oops UB | ||
} | ||
|
||
{ | ||
let mut opt_data_2: Option<Mut<Foo>> = None; | ||
let mut opt_data: Option<&Foo> = None; | ||
query.for_each_mut(|data| opt_data_2 = Some(data)); | ||
query.for_each(|data| opt_data = Some(data)); | ||
assert_eq!(opt_data.unwrap(), &mut *opt_data_2.unwrap()); // oops UB | ||
} | ||
dbg!("bye"); | ||
} | ||
} |
Oops, something went wrong.