@@ -50,13 +50,17 @@ impl Generics {
5050 }
5151
5252 pub ( crate ) fn iter_id ( & self ) -> impl Iterator < Item = GenericParamId > + ' _ {
53- self . iter ( ) . map ( | ( id , _ ) | id )
53+ self . iter_self_id ( ) . chain ( self . iter_parent_id ( ) )
5454 }
5555
5656 pub ( crate ) fn iter_self_id ( & self ) -> impl Iterator < Item = GenericParamId > + ' _ {
5757 self . iter_self ( ) . map ( |( id, _) | id)
5858 }
5959
60+ fn iter_parent_id ( & self ) -> impl Iterator < Item = GenericParamId > + ' _ {
61+ self . iter_parent ( ) . map ( |( id, _) | id)
62+ }
63+
6064 pub ( crate ) fn iter_self_type_or_consts (
6165 & self ,
6266 ) -> impl DoubleEndedIterator < Item = ( LocalTypeOrConstParamId , & TypeOrConstParamData ) > {
@@ -81,7 +85,7 @@ impl Generics {
8185 }
8286
8387 /// Iterator over types and const params of parent.
84- pub ( crate ) fn iter_parent (
88+ fn iter_parent (
8589 & self ,
8690 ) -> impl DoubleEndedIterator < Item = ( GenericParamId , GenericParamDataRef < ' _ > ) > + ' _ {
8791 self . parent_generics ( ) . into_iter ( ) . flat_map ( |it| {
@@ -108,7 +112,6 @@ impl Generics {
108112 let mut type_params = 0 ;
109113 let mut impl_trait_params = 0 ;
110114 let mut const_params = 0 ;
111- let mut lifetime_params = 0 ;
112115 self . params . iter_type_or_consts ( ) . for_each ( |( _, data) | match data {
113116 TypeOrConstParamData :: TypeParamData ( p) => match p. provenance {
114117 TypeParamProvenance :: TypeParamList => type_params += 1 ,
@@ -118,52 +121,44 @@ impl Generics {
118121 TypeOrConstParamData :: ConstParamData ( _) => const_params += 1 ,
119122 } ) ;
120123
121- self . params . iter_lt ( ) . for_each ( | ( _ , _ ) | lifetime_params += 1 ) ;
124+ let lifetime_params = self . params . iter_lt ( ) . count ( ) ;
122125
123126 let parent_len = self . parent_generics ( ) . map_or ( 0 , Generics :: len) ;
124127 ( parent_len, self_param, type_params, const_params, impl_trait_params, lifetime_params)
125128 }
126129
127130 pub ( crate ) fn type_or_const_param_idx ( & self , param : TypeOrConstParamId ) -> Option < usize > {
128- Some ( self . find_type_or_const_param ( param) ? . 0 )
131+ self . find_type_or_const_param ( param)
129132 }
130133
131- fn find_type_or_const_param (
132- & self ,
133- param : TypeOrConstParamId ,
134- ) -> Option < ( usize , & TypeOrConstParamData ) > {
134+ fn find_type_or_const_param ( & self , param : TypeOrConstParamId ) -> Option < usize > {
135135 if param. parent == self . def {
136136 let idx = param. local_id . into_raw ( ) . into_u32 ( ) as usize ;
137- if idx >= self . params . type_or_consts . len ( ) {
138- return None ;
139- }
140- Some ( ( idx, & self . params . type_or_consts [ param. local_id ] ) )
137+ debug_assert ! ( idx <= self . params. type_or_consts. len( ) ) ;
138+ Some ( idx)
141139 } else {
140+ debug_assert_eq ! ( self . parent_generics( ) . map( |it| it. def) , Some ( param. parent) ) ;
142141 self . parent_generics ( )
143142 . and_then ( |g| g. find_type_or_const_param ( param) )
144143 // Remember that parent parameters come after parameters for self.
145- . map ( |( idx, data ) | ( self . len_self ( ) + idx, data ) )
144+ . map ( |idx| self . len_self ( ) + idx)
146145 }
147146 }
148147
149148 pub ( crate ) fn lifetime_idx ( & self , lifetime : LifetimeParamId ) -> Option < usize > {
150- Some ( self . find_lifetime ( lifetime) ? . 0 )
149+ self . find_lifetime ( lifetime)
151150 }
152151
153- fn find_lifetime ( & self , lifetime : LifetimeParamId ) -> Option < ( usize , & LifetimeParamData ) > {
152+ fn find_lifetime ( & self , lifetime : LifetimeParamId ) -> Option < usize > {
154153 if lifetime. parent == self . def {
155154 let idx = lifetime. local_id . into_raw ( ) . into_u32 ( ) as usize ;
156- if idx >= self . params . lifetimes . len ( ) {
157- return None ;
158- }
159- Some ( (
160- self . params . type_or_consts . len ( ) + idx,
161- & self . params . lifetimes [ lifetime. local_id ] ,
162- ) )
155+ debug_assert ! ( idx <= self . params. lifetimes. len( ) ) ;
156+ Some ( self . params . type_or_consts . len ( ) + idx)
163157 } else {
158+ debug_assert_eq ! ( self . parent_generics( ) . map( |it| it. def) , Some ( lifetime. parent) ) ;
164159 self . parent_generics ( )
165160 . and_then ( |g| g. find_lifetime ( lifetime) )
166- . map ( |( idx, data ) | ( self . len_self ( ) + idx, data ) )
161+ . map ( |idx| self . len_self ( ) + idx)
167162 }
168163 }
169164
0 commit comments