Skip to content

Commit 76ceba3

Browse files
committed
Remove vec_linked_list.
It provides a way to effectively embed a linked list within an `IndexVec` and also iterate over that list. It's written in a very generic way, involving two traits `Links` and `LinkElem`. But the `Links` trait is only impl'd for `IndexVec` and `&IndexVec`, and the whole thing is only used in one module within `rustc_borrowck`. So I think it's over-engineered and hard to read. Plus it has no comments. This commit removes it, and adds a (non-generic) local iterator for the use within `rustc_borrowck`. Much simpler.
1 parent b2e6d9a commit 76ceba3

File tree

3 files changed

+32
-81
lines changed

3 files changed

+32
-81
lines changed

compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs

+32-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rustc_data_structures::vec_linked_list as vll;
21
use rustc_index::IndexVec;
32
use rustc_middle::mir::visit::{PlaceContext, Visitor};
43
use rustc_middle::mir::{Body, Local, Location};
@@ -37,9 +36,12 @@ pub(crate) struct LocalUseMap {
3736
/// we add for each local variable.
3837
first_drop_at: IndexVec<Local, Option<AppearanceIndex>>,
3938

40-
appearances: IndexVec<AppearanceIndex, Appearance>,
39+
appearances: Appearances,
4140
}
4241

42+
// The `Appearance::next` field effectively embeds a linked list within `Appearances`.
43+
type Appearances = IndexVec<AppearanceIndex, Appearance>;
44+
4345
struct Appearance {
4446
point_index: PointIndex,
4547
next: Option<AppearanceIndex>,
@@ -49,14 +51,34 @@ rustc_index::newtype_index! {
4951
pub struct AppearanceIndex {}
5052
}
5153

52-
impl vll::LinkElem for Appearance {
53-
type LinkIndex = AppearanceIndex;
54+
fn appearances_iter(
55+
first: Option<AppearanceIndex>,
56+
appearances: &Appearances,
57+
) -> impl Iterator<Item = AppearanceIndex> + '_ {
58+
AppearancesIter { appearances, current: first }
59+
}
60+
61+
// Iterates over `Appearances` by following `next` fields.
62+
struct AppearancesIter<'a> {
63+
appearances: &'a Appearances,
64+
current: Option<AppearanceIndex>,
65+
}
5466

55-
fn next(elem: &Self) -> Option<AppearanceIndex> {
56-
elem.next
67+
impl<'a> Iterator for AppearancesIter<'a> {
68+
type Item = AppearanceIndex;
69+
70+
fn next(&mut self) -> Option<AppearanceIndex> {
71+
if let Some(c) = self.current {
72+
self.current = self.appearances[c].next;
73+
Some(c)
74+
} else {
75+
None
76+
}
5777
}
5878
}
5979

80+
//-----------------------------------------------------------------------------
81+
6082
impl LocalUseMap {
6183
pub(crate) fn build(
6284
live_locals: &[Local],
@@ -86,17 +108,17 @@ impl LocalUseMap {
86108
}
87109

88110
pub(crate) fn defs(&self, local: Local) -> impl Iterator<Item = PointIndex> + '_ {
89-
vll::iter(self.first_def_at[local], &self.appearances)
111+
appearances_iter(self.first_def_at[local], &self.appearances)
90112
.map(move |aa| self.appearances[aa].point_index)
91113
}
92114

93115
pub(crate) fn uses(&self, local: Local) -> impl Iterator<Item = PointIndex> + '_ {
94-
vll::iter(self.first_use_at[local], &self.appearances)
116+
appearances_iter(self.first_use_at[local], &self.appearances)
95117
.map(move |aa| self.appearances[aa].point_index)
96118
}
97119

98120
pub(crate) fn drops(&self, local: Local) -> impl Iterator<Item = PointIndex> + '_ {
99-
vll::iter(self.first_drop_at[local], &self.appearances)
121+
appearances_iter(self.first_drop_at[local], &self.appearances)
100122
.map(move |aa| self.appearances[aa].point_index)
101123
}
102124
}
@@ -146,7 +168,7 @@ impl LocalUseMapBuild<'_> {
146168
fn insert(
147169
elements: &DenseLocationMap,
148170
first_appearance: &mut Option<AppearanceIndex>,
149-
appearances: &mut IndexVec<AppearanceIndex, Appearance>,
171+
appearances: &mut Appearances,
150172
location: Location,
151173
) {
152174
let point_index = elements.point_from_location(location);

compiler/rustc_data_structures/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ pub mod temp_dir;
8484
pub mod transitive_relation;
8585
pub mod unhash;
8686
pub mod unord;
87-
pub mod vec_linked_list;
8887
pub mod work_queue;
8988

9089
mod atomic_ref;

compiler/rustc_data_structures/src/vec_linked_list.rs

-70
This file was deleted.

0 commit comments

Comments
 (0)