Skip to content

Refactor pick2_mut & pick3_mut to use get_disjoint_mut #138372

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
Mar 12, 2025
Merged
Changes from 1 commit
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
18 changes: 10 additions & 8 deletions compiler/rustc_index/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,12 @@ impl<I: Idx, T> IndexSlice<I, T> {
pub fn pick2_mut(&mut self, a: I, b: I) -> (&mut T, &mut T) {
let (ai, bi) = (a.index(), b.index());
assert!(ai != bi);
let len = self.raw.len();
assert!(ai < len && bi < len);
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, is it worth doing these checks when get_disjoint_mut is just going to do them again?

Maybe we could, instead, have the panic!s be more like

    Err(OverlappingIndices) => panic!("Indices {a:?} and {b:?} are not disjoint!"),

so that we can avoid writing out any of the assert!s in the code here.

Copy link
Contributor Author

@Eclips4 Eclips4 Mar 11, 2025

Choose a reason for hiding this comment

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

You're right. I overlooked that when I looked at the get_disjoint_mut implementation. Sorry!


if ai < bi {
let (c1, c2) = self.raw.split_at_mut(bi);
(&mut c1[ai], &mut c2[0])
} else {
let (c2, c1) = self.pick2_mut(b, a);
(c1, c2)
match self.raw.get_disjoint_mut([ai, bi]) {
Ok([a, b]) => (a, b),
Err(_) => panic!("Can't get mutable references"),
}
}

Expand All @@ -139,8 +138,11 @@ impl<I: Idx, T> IndexSlice<I, T> {
assert!(ai != bi && bi != ci && ci != ai);
let len = self.raw.len();
assert!(ai < len && bi < len && ci < len);
let ptr = self.raw.as_mut_ptr();
unsafe { (&mut *ptr.add(ai), &mut *ptr.add(bi), &mut *ptr.add(ci)) }

match self.raw.get_disjoint_mut([ai, bi, ci]) {
Ok([a, b, c]) => (a, b, c),
Err(_) => panic!("Can't get mutable references"),
}
}

#[inline]
Expand Down
Loading