-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Shorten the 'world lifetime returned from QueryLens::query()
.
#17694
base: main
Are you sure you want to change the base?
Conversation
Add a new `QueryLens::query_inner()` to handle cases that were relying on the longer lifetime.
@Victoronz @chescock @13ros27 take a look at this please :) |
I'll be honest the way |
Yes, definitely! I have never written a compile-fail test, though. Can anyone point me in the right direction to get started? |
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.
As I mentioned I can't fully review the lifetime changes but the compile-fail test seems good and compiles successfully on main (where it shouldn't of course).
{ | ||
let mut query = system_state.get_mut(&mut world); | ||
let mut lens = query.as_query_lens(); | ||
dbg!("hi"); |
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?
Objective
Fix unsoundness introduced by #15858.
QueryLens::query()
would hand out aQuery
with the full'w
lifetime, and the new_inner
methods would let the results outlive theQuery
. This could be used to create aliasing mutable references, likeFixes #17693
Solution
Restrict the
'world
lifetime in theQuery
returned byQueryLens::query()
to'_
, the lifetime of the borrow of theQueryLens
.The model here is that
Query<'w, 's, D, F>
andQueryLens<'w, D, F>
have permission to access their components for the lifetime'w
. So going from&'a mut QueryLens<'w>
toQuery<'w, 'a>
would borrow the permission only for the'a
lifetime, but incorrectly give it out for the full'w
lifetime.To handle any cases where users were calling
get_inner()
oriter_inner()
on theQuery
and expecting the full'w
lifetime, we introduce a newQueryLens::query_inner()
method. This is only valid forReadOnlyQueryData
, so it may safely hand out a copy of the permission for the full'w
lifetime. Sinceget_inner()
anditer_inner()
were only valid onReadOnlyQueryData
prior to #15858, that should cover any uses that relied on the longer lifetime.Migration Guide
Users of
QueryLens::query()
who were callingget_inner()
oriter_inner()
will need to replace the call withQueryLens::query_inner()
.