Skip to content

Commit 06a7783

Browse files
committed
temporary system safety tests (to prove aliased muts arent possible)
1 parent 390c719 commit 06a7783

File tree

1 file changed

+151
-0
lines changed
  • crates/bevy_ecs/src/system

1 file changed

+151
-0
lines changed

crates/bevy_ecs/src/system/mod.rs

+151
Original file line numberDiff line numberDiff line change
@@ -624,4 +624,155 @@ mod tests {
624624
);
625625
}
626626
}
627+
628+
#[test]
629+
fn system_safety_test() {
630+
struct A(usize);
631+
struct B(usize);
632+
633+
fn system(mut query: Query<&mut A>, e: Res<Entity>) {
634+
{
635+
let mut iter = query.iter_mut();
636+
let a = &mut *iter.next().unwrap();
637+
638+
let mut iter2 = query.iter_mut();
639+
let b = &mut *iter2.next().unwrap();
640+
641+
// this should fail to compile
642+
println!("{}", a.0);
643+
}
644+
645+
{
646+
let mut a1 = query.get_mut(*e).unwrap();
647+
648+
let mut a2 = query.get_mut(*e).unwrap();
649+
650+
// this should fail to compile
651+
println!("{} {}", a1.0, a2.0);
652+
}
653+
}
654+
655+
fn system_set(mut queries: QuerySet<(Query<&mut A>,Query<&A>)>, e: Res<Entity>) {
656+
{
657+
658+
let q2 = queries.q0_mut();
659+
let mut iter2 = q2.iter_mut();
660+
let mut b = iter2.next().unwrap();
661+
662+
let q1 = queries.q1();
663+
let mut iter = q1.iter();
664+
let a = &*iter.next().unwrap();
665+
666+
// this should fail to compile
667+
b.0 = a.0
668+
}
669+
670+
{
671+
672+
let q1 = queries.q1();
673+
let mut iter = q1.iter();
674+
let a = &*iter.next().unwrap();
675+
676+
let q2 = queries.q0_mut();
677+
let mut iter2 = q2.iter_mut();
678+
let mut b = iter2.next().unwrap();
679+
680+
// this should fail to compile (but currently doesn't)
681+
b.0 = a.0;
682+
}
683+
{
684+
685+
let q2 = queries.q0_mut();
686+
let mut b = q2.get_mut(*e).unwrap();
687+
688+
let q1 = queries.q1();
689+
let a = q1.get(*e).unwrap();
690+
691+
// this should fail to compile
692+
b.0 = a.0
693+
}
694+
695+
{
696+
let q1 = queries.q1();
697+
let a = q1.get(*e).unwrap();
698+
699+
let q2 = queries.q0_mut();
700+
let mut b = q2.get_mut(*e).unwrap();
701+
// this should fail to compile (but currently doesn't)
702+
b.0 = a.0
703+
}
704+
}
705+
706+
let mut world = World::default();
707+
world.spawn().insert_bundle((A(1), B(1)));
708+
run_system(&mut world, system);
709+
}
710+
711+
/// this test exists to show that read-only world-only queries can return data that lives as long as 'world
712+
#[test]
713+
fn long_life_test() {
714+
struct Holder<'w> {
715+
value: &'w A,
716+
}
717+
718+
struct State {
719+
state: SystemState<Res<'static, A>>,
720+
state_q: SystemState<Query<'static, 'static, &'static A>>,
721+
}
722+
723+
impl State {
724+
fn hold_res<'w>(&mut self, world: &'w World) -> Holder<'w> {
725+
let a = self.state.get(&world);
726+
Holder {
727+
value: a.into_inner(),
728+
}
729+
}
730+
fn hold_component<'w>(&mut self, world: &'w World, entity: Entity) -> Holder<'w> {
731+
let q = self.state_q.get(&world);
732+
let a = q.get(entity).unwrap();
733+
Holder { value: a }
734+
}
735+
fn hold_components<'w>(&mut self, world: &'w World) -> Vec<Holder<'w>> {
736+
let mut components = Vec::new();
737+
let q = self.state_q.get(&world);
738+
for a in q.iter() {
739+
components.push(Holder { value: a });
740+
}
741+
components
742+
}
743+
}
744+
}
745+
746+
#[test]
747+
fn system_state_safety_test() {
748+
struct A(usize);
749+
struct B(usize);
750+
751+
struct State {
752+
state_r: SystemState<Query<'static, 'static, &'static A>>,
753+
state_w: SystemState<Query<'static, 'static, &'static mut A>>,
754+
}
755+
756+
impl State {
757+
fn get_component<'w>(&mut self, world: &'w mut World, entity: Entity) {
758+
let q1 = self.state_r.get(&world);
759+
let a1 = q1.get(entity).unwrap();
760+
761+
let mut q2 = self.state_w.get_mut(world);
762+
let a2 = q2.get_mut(entity).unwrap();
763+
// this should fail to compile
764+
println!("{}", a1.0);
765+
}
766+
767+
fn get_components<'w>(&mut self, world: &'w mut World) {
768+
let q1 = self.state_r.get(&world);
769+
let a1 = q1.iter().next().unwrap();
770+
771+
let mut q2 = self.state_w.get_mut(world);
772+
let a2 = q2.iter_mut().next().unwrap();
773+
// this should fail to compile
774+
println!("{}", a1.0);
775+
}
776+
}
777+
}
627778
}

0 commit comments

Comments
 (0)