Skip to content

AnyOf can violate single mutable alias rule #13993

@Zeenobit

Description

@Zeenobit

Bevy version

0.13.2

What you did

I'm noticing some weird behavior with AnyOf.
Given two systems test1 and test2:

    #[derive(Component)]
    struct X(usize);

    // Runs on startup
    fn setup_test(mut commands: Commands) {
        commands.spawn(X(0));
    }

    fn test1(query: Query<(&mut X, &mut X)>) {}

    fn test2(mut query: Query<AnyOf<(&mut X, &mut X)>>) {
        let (a, b) = query.single_mut();
        a.unwrap().0 += 1;
        *b.unwrap() = X(3);
        let (a, _) = query.single();
        dbg!(a.unwrap().0);
    }

Obviously test1 fails to run, since Query<(&mut X, &mut X)> is invalid.
But somehow test2 is perfectly ok. I was expecting at least one of a or b to be None, but nope! Both a and b are valid, implying we can violate the single mutable alias rule with AnyOf.

Running an app with test2 prints "3", always, which seems like an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorD-UnsafeTouches with unsafe code in some wayP-UnsoundA bug that results in undefined compiler behaviorS-Needs-InvestigationThis issue requires detective work to figure out what's going wrong

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions