Skip to content

Shorten the 'world lifetime returned from QueryLens::query(). #17694

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
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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(Foo(10_u32)).id();

let mut system_state = SystemState::<Query<&mut Foo>>::new(&mut world);
{
let mut query = system_state.get_mut(&mut world);
let mut lens = query.as_query_lens();
dbg!("hi");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to leave these in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the skeleton from https://github.com/bevyengine/bevy/blob/f2a65c2dd3a3ef75964a8aaed67e1f24618bf19f/crates/bevy_ecs/compile_fail/tests/ui/query_lifetime_safety.rs . So, I did mean to leave them there, but not for any particularly good reason :). I'm inclined to leave it like this for consistency with the other file, but I'm happy to change it if there are strong objections.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I must have got my search filtering wrong because I thought none of the other compile-fail tests had dbg in them, feel free to leave it then.

{
let mut data: Mut<Foo> = lens.query().get_inner(e).unwrap();
let mut data2: Mut<Foo> = lens.query().get_inner(e).unwrap();
//~^ E0499
assert_eq!(&mut *data, &mut *data2); // oops UB
}
dbg!("bye");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error[E0499]: cannot borrow `lens` as mutable more than once at a time
--> tests/ui\query_lens_lifetime_safety.rs:18:39
|
17 | let mut data: Mut<Foo> = lens.query().get_inner(e).unwrap();
| ---- first mutable borrow occurs here
20 changes: 17 additions & 3 deletions crates/bevy_ecs/src/system/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,21 @@ pub struct QueryLens<'w, Q: QueryData, F: QueryFilter = ()> {

impl<'w, Q: QueryData, F: QueryFilter> QueryLens<'w, Q, F> {
/// Create a [`Query`] from the underlying [`QueryState`].
pub fn query(&mut self) -> Query<'w, '_, Q, F> {
pub fn query(&mut self) -> Query<'_, '_, Q, F> {
Copy link
Contributor

@cBournhonesque cBournhonesque Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the new flow is

  1. query -> Query<'w, s'>
  2. lens = &'a query.as_query_lens() -> QueryLens<'a>
  3. lens.query() -> Query<'a, 'a>
  4. get_inner() -> QueryItem<'a>

and the old one is

  1. lens.query() -> Query<'w, 'a>
  2. get_inner() -> QueryItem<'w>

So in the old case, the item we get only depends on the 'w lifetime, so the compiler doesn't stop us from creating a second object with the 'a lifetime via lens.query().

I think the change makes sense; out of curoisity, would this PR #15396 also work to fix your compile-fail test?
Since the QueryItem we would get would have been QueryItem<'w, 'a>, we might not be able to call lens.query() a second time because the 'a lifetime is present in the return value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did't completely follow your notation, but your conclusion sounds right! We had an &'a mut QueryLens<'w>, and currently that gives a Query<'w, 'a> which we can use with get_inner() to get a QueryItem<'w>, but with this change it gives a Query<'a, 'a> that we can only use to get a QueryItem<'a>.

I believe the compile-fail test would still compile with #15396, because the compiler would see that the concrete type of QueryItem<'w, 'a> is Mut<'w, Foo>, which doesn't involve the 'a lifetime.

Query {
world: self.world,
state: &self.state,
last_run: self.last_run,
this_run: self.this_run,
}
}
}

impl<'w, Q: ReadOnlyQueryData, F: QueryFilter> QueryLens<'w, Q, F> {
/// Create a [`Query`] from the underlying [`QueryState`].
/// This returns results with the actual "inner" world lifetime,
/// so it may only be used with read-only queries to prevent mutable aliasing.
pub fn query_inner(&self) -> Query<'w, '_, Q, F> {
Query {
world: self.world,
state: &self.state,
Expand All @@ -2108,9 +2122,9 @@ impl<'w, Q: QueryData, F: QueryFilter> QueryLens<'w, Q, F> {
}

impl<'w, 's, Q: QueryData, F: QueryFilter> From<&'s mut QueryLens<'w, Q, F>>
for Query<'w, 's, Q, F>
for Query<'s, 's, Q, F>
{
fn from(value: &'s mut QueryLens<'w, Q, F>) -> Query<'w, 's, Q, F> {
fn from(value: &'s mut QueryLens<'w, Q, F>) -> Query<'s, 's, Q, F> {
value.query()
}
}
Expand Down