Skip to content
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

rustc_borrowck cleanups #132250

Merged
merged 10 commits into from
Nov 4, 2024
Prev Previous commit
Next Next commit
Simplify LocalUseMapBuild.
It has four different `insert` methods, with some duplication. This
commit finds the commonality and removes them all.
nnethercote committed Nov 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit ce2f0b4ce9bb34ca22251317ce66c71c1a2df26a
62 changes: 14 additions & 48 deletions compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs
Original file line number Diff line number Diff line change
@@ -137,56 +137,22 @@ struct LocalUseMapBuild<'me> {
locals_with_use_data: IndexVec<Local, bool>,
}

impl LocalUseMapBuild<'_> {
fn insert_def(&mut self, local: Local, location: Location) {
Self::insert(
self.elements,
&mut self.local_use_map.first_def_at[local],
&mut self.local_use_map.appearances,
location,
);
}

fn insert_use(&mut self, local: Local, location: Location) {
Self::insert(
self.elements,
&mut self.local_use_map.first_use_at[local],
&mut self.local_use_map.appearances,
location,
);
}

fn insert_drop(&mut self, local: Local, location: Location) {
Self::insert(
self.elements,
&mut self.local_use_map.first_drop_at[local],
&mut self.local_use_map.appearances,
location,
);
}

fn insert(
elements: &DenseLocationMap,
first_appearance: &mut Option<AppearanceIndex>,
appearances: &mut Appearances,
location: Location,
) {
let point_index = elements.point_from_location(location);
let appearance_index =
appearances.push(Appearance { point_index, next: *first_appearance });
*first_appearance = Some(appearance_index);
}
}

impl Visitor<'_> for LocalUseMapBuild<'_> {
fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
if self.locals_with_use_data[local] {
match def_use::categorize(context) {
Some(DefUse::Def) => self.insert_def(local, location),
Some(DefUse::Use) => self.insert_use(local, location),
Some(DefUse::Drop) => self.insert_drop(local, location),
_ => (),
}
if self.locals_with_use_data[local]
&& let Some(def_use) = def_use::categorize(context)
{
let first_appearance = match def_use {
DefUse::Def => &mut self.local_use_map.first_def_at[local],
DefUse::Use => &mut self.local_use_map.first_use_at[local],
DefUse::Drop => &mut self.local_use_map.first_drop_at[local],
};
let point_index = self.elements.point_from_location(location);
let appearance_index = self
.local_use_map
.appearances
.push(Appearance { point_index, next: *first_appearance });
*first_appearance = Some(appearance_index);
}
}
}