-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
{ | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the new flow is
and the old one is
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 I think the change makes sense; out of curoisity, would this PR #15396 also work to fix your compile-fail test? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I believe the compile-fail test would still compile with #15396, because the compiler would see that the concrete type of |
||
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, | ||
|
@@ -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() | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.