1- use rustc_data_structures:: vec_linked_list as vll;
21use rustc_index:: IndexVec ;
32use rustc_middle:: mir:: visit:: { PlaceContext , Visitor } ;
43use 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+
4345struct 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+
6082impl 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) ;
0 commit comments