Skip to content

Commit 00e6809

Browse files
committed
Avoid miri error in slice::sort under Stacked Borrows
See comment in code. Fixes: #131065
1 parent 94a0cd1 commit 00e6809

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

library/alloctests/tests/sort/tests.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,13 @@ fn sort_vs_sort_by_impl<S: Sort>() {
362362
assert_eq!(input_sort_by, expected);
363363
}
364364

365+
pub fn box_value_impl<S: Sort>() {
366+
for len in [3, 9, 35, 56, 132] {
367+
test_is_sorted::<Box<i32>, S>(len, Box::new, patterns::random);
368+
test_is_sorted::<Box<i32>, S>(len, Box::new, |len| patterns::random_sorted(len, 80.0));
369+
}
370+
}
371+
365372
gen_sort_test_fns_with_default_patterns!(
366373
correct_i32,
367374
|len, pattern_fn| test_is_sorted::<i32, S>(len, |val| val, pattern_fn),
@@ -967,6 +974,7 @@ define_instantiate_sort_tests!(
967974
[miri_yes, fixed_seed_rand_vec_prefix],
968975
[miri_yes, int_edge],
969976
[miri_yes, sort_vs_sort_by],
977+
[miri_yes, box_value],
970978
[miri_yes, correct_i32_random],
971979
[miri_yes, correct_i32_random_z1],
972980
[miri_yes, correct_i32_random_d2],

library/core/src/slice/sort/stable/quicksort.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ pub fn quicksort<T, F: FnMut(&T, &T) -> bool>(
4242
// self-modifications via `is_less` would not be observed and this would
4343
// be unsound. Our temporary copy does not escape this scope.
4444
let pivot_copy = unsafe { ManuallyDrop::new(ptr::read(&v[pivot_pos])) };
45-
let pivot_ref = (!has_direct_interior_mutability::<T>()).then_some(&*pivot_copy);
45+
// Stacked Borrows does not allow the access pattern for `pivot_copy`, but Tree Borrows
46+
// does. FIXME: Remove `!cfg!(miri)` once/if Tree Borrows becomes the standard.
47+
let pivot_ref =
48+
(!has_direct_interior_mutability::<T>() && !cfg!(miri)).then_some(&*pivot_copy);
4649

4750
// We choose a pivot, and check if this pivot is equal to our left
4851
// ancestor. If true, we do a partition putting equal elements on the

0 commit comments

Comments
 (0)