bevy/crates/bevy_ecs/compile_fail/tests/ui
Chris Russell 94e6fa168f
Fix unsoundness in QueryIter::sort_by (#17826)
# Objective

`QueryIter::sort_by()` is unsound. It passes the lens items with the
full `'w` lifetime, and a malicious user could smuggle them out of the
closure where they could alias with the query results.

## Solution

Make the sort closures generic in the lifetime parameter of the lens
item. This ensures the lens items cannot outlive the call to the
closure.

## Testing

Added a compile-fail test that demonstrates the unsound pattern.

## Migration Guide

The `sort` family of methods on `QueryIter` unsoundly gave access
`L::Item<'w>` with the full `'w` lifetime. It has been shortened to
`L::Item<'w>` so that items cannot escape the comparer. If you get
lifetime errors using these methods, you will need to make the comparer
generic in the new lifetime. Often this can be done by replacing named
`'w` with `'_`, or by replacing the use of a function item with a
closure.

```rust
// Before: Now fails with "error: implementation of `FnMut` is not general enough"
query.iter().sort_by::<&C>(Ord::cmp);
// After: Wrap in a closure
query.iter().sort_by::<&C>(|l, r| Ord::cmp(l, r));

query.iter().sort_by::<&C>(comparer);
// Before: Uses specific `'w` lifetime from some outer scope
// now fails with "error: implementation of `FnMut` is not general enough"
fn comparer(left: &&'w C, right: &&'w C) -> Ordering { /* ... */ }
// After: Accepts any lifetime using inferred lifetime parameter
fn comparer(left: &&C, right: &&C) -> Ordering { /* ... */ }
2025-02-26 20:36:37 +00:00
..
entity_ref_mut_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
entity_ref_mut_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_exact_sized_iterator_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_exact_sized_iterator_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_iter_combinations_mut_iterator_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_iter_combinations_mut_iterator_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_iter_many_mut_iterator_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_iter_many_mut_iterator_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_lens_lifetime_safety.rs Shorten the 'world lifetime returned from QueryLens::query(). (#17694) 2025-02-12 22:41:02 +00:00
query_lens_lifetime_safety.stderr Shorten the 'world lifetime returned from QueryLens::query(). (#17694) 2025-02-12 22:41:02 +00:00
query_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_to_readonly.rs Introduce methods on QueryState to obtain a Query (#15858) 2025-02-05 18:33:15 +00:00
query_to_readonly.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_transmute_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
query_transmute_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_param_derive_readonly.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_param_derive_readonly.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_get_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_get_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_get_many_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_get_many_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_get_many_mut_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_get_many_mut_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_iter_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_iter_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_iter_many_mut_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_iter_many_mut_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_iter_sort_lifetime_safety.rs Fix unsoundness in QueryIter::sort_by (#17826) 2025-02-26 20:36:37 +00:00
system_query_iter_sort_lifetime_safety.stderr Fix unsoundness in QueryIter::sort_by (#17826) 2025-02-26 20:36:37 +00:00
system_query_set_get_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_set_get_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_set_iter_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_query_set_iter_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_state_get_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_state_get_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_state_iter_lifetime_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_state_iter_lifetime_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_state_iter_mut_overlap_safety.rs Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
system_state_iter_mut_overlap_safety.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00
world_query_derive.rs bevy_ecs: Replace panics in QueryData derive compile errors (#15691) 2024-10-07 16:30:34 +00:00
world_query_derive.stderr Move compile fail tests (#13196) 2024-05-03 13:35:21 +00:00