@@ -177,21 +177,19 @@ pub(crate) fn generics(db: &dyn DefDatabase, def: GenericDefId) -> Generics {
177177 let parent_generics = parent_generic_def ( db, def) . map ( |def| Box :: new ( generics ( db, def) ) ) ;
178178 if parent_generics. is_some ( ) && matches ! ( def, GenericDefId :: TypeAliasId ( _) ) {
179179 let params = db. generic_params ( def) ;
180- if params
181- . type_or_consts
182- . iter ( )
183- . any ( |( _, x) | matches ! ( x, TypeOrConstParamData :: ConstParamData ( _) ) )
184- {
180+ let has_consts =
181+ params. iter ( ) . any ( |( _, x) | matches ! ( x, TypeOrConstParamData :: ConstParamData ( _) ) ) ;
182+ return if has_consts {
185183 // XXX: treat const generic associated types as not existing to avoid crashes (#11769)
186184 //
187185 // Chalk expects the inner associated type's parameters to come
188186 // *before*, not after the trait's generics as we've always done it.
189187 // Adapting to this requires a larger refactoring
190188 cov_mark:: hit!( ignore_gats) ;
191- return Generics { def, params : Interned :: new ( Default :: default ( ) ) , parent_generics } ;
189+ Generics { def, params : Interned :: new ( Default :: default ( ) ) , parent_generics }
192190 } else {
193- return Generics { def, params, parent_generics } ;
194- }
191+ Generics { def, params, parent_generics }
192+ } ;
195193 }
196194 Generics { def, params : db. generic_params ( def) , parent_generics }
197195}
@@ -219,68 +217,46 @@ impl Generics {
219217 pub ( crate ) fn iter < ' a > (
220218 & ' a self ,
221219 ) -> impl DoubleEndedIterator < Item = ( TypeOrConstParamId , & ' a TypeOrConstParamData ) > + ' a {
222- self . parent_generics
223- . as_ref ( )
220+ let to_toc_id = |it : & ' a Generics | {
221+ move |( local_id, p) | ( TypeOrConstParamId { parent : it. def , local_id } , p)
222+ } ;
223+ self . parent_generics ( )
224224 . into_iter ( )
225- . flat_map ( |it| {
226- it. params
227- . iter ( )
228- . map ( move |( local_id, p) | ( TypeOrConstParamId { parent : it. def , local_id } , p) )
229- } )
230- . chain (
231- self . params . iter ( ) . map ( move |( local_id, p) | {
232- ( TypeOrConstParamId { parent : self . def , local_id } , p)
233- } ) ,
234- )
225+ . flat_map ( move |it| it. params . iter ( ) . map ( to_toc_id ( it) ) )
226+ . chain ( self . params . iter ( ) . map ( to_toc_id ( self ) ) )
235227 }
236228
237229 /// Iterator over types and const params of parent.
238230 pub ( crate ) fn iter_parent < ' a > (
239231 & ' a self ,
240232 ) -> impl Iterator < Item = ( TypeOrConstParamId , & ' a TypeOrConstParamData ) > + ' a {
241- self . parent_generics . as_ref ( ) . into_iter ( ) . flat_map ( |it| {
242- it. params
243- . type_or_consts
244- . iter ( )
245- . map ( move |( local_id, p) | ( TypeOrConstParamId { parent : it. def , local_id } , p) )
233+ self . parent_generics ( ) . into_iter ( ) . flat_map ( |it| {
234+ let to_toc_id =
235+ move |( local_id, p) | ( TypeOrConstParamId { parent : it. def , local_id } , p) ;
236+ it. params . iter ( ) . map ( to_toc_id)
246237 } )
247238 }
248239
249240 pub ( crate ) fn len ( & self ) -> usize {
250- self . len_split ( ) . 0
251- }
252-
253- /// (total, parents, child)
254- pub ( crate ) fn len_split ( & self ) -> ( usize , usize , usize ) {
255- let parent = self . parent_generics . as_ref ( ) . map_or ( 0 , |p| p. len ( ) ) ;
241+ let parent = self . parent_generics ( ) . map_or ( 0 , Generics :: len) ;
256242 let child = self . params . type_or_consts . len ( ) ;
257- ( parent + child, parent , child )
243+ parent + child
258244 }
259245
260246 /// (parent total, self param, type param list, const param list, impl trait)
261247 pub ( crate ) fn provenance_split ( & self ) -> ( usize , usize , usize , usize , usize ) {
262- let parent = self . parent_generics . as_ref ( ) . map_or ( 0 , |p| p. len ( ) ) ;
263- let self_params = self
264- . params
265- . iter ( )
266- . filter_map ( |x| x. 1 . type_param ( ) )
267- . filter ( |p| p. provenance == TypeParamProvenance :: TraitSelf )
268- . count ( ) ;
269- let type_params = self
270- . params
271- . type_or_consts
272- . iter ( )
273- . filter_map ( |x| x. 1 . type_param ( ) )
274- . filter ( |p| p. provenance == TypeParamProvenance :: TypeParamList )
275- . count ( ) ;
248+ let ty_iter = || self . params . iter ( ) . filter_map ( |x| x. 1 . type_param ( ) ) ;
249+
250+ let self_params =
251+ ty_iter ( ) . filter ( |p| p. provenance == TypeParamProvenance :: TraitSelf ) . count ( ) ;
252+ let type_params =
253+ ty_iter ( ) . filter ( |p| p. provenance == TypeParamProvenance :: TypeParamList ) . count ( ) ;
254+ let impl_trait_params =
255+ ty_iter ( ) . filter ( |p| p. provenance == TypeParamProvenance :: ArgumentImplTrait ) . count ( ) ;
276256 let const_params = self . params . iter ( ) . filter_map ( |x| x. 1 . const_param ( ) ) . count ( ) ;
277- let impl_trait_params = self
278- . params
279- . iter ( )
280- . filter_map ( |x| x. 1 . type_param ( ) )
281- . filter ( |p| p. provenance == TypeParamProvenance :: ArgumentImplTrait )
282- . count ( ) ;
283- ( parent, self_params, type_params, const_params, impl_trait_params)
257+
258+ let parent_len = self . parent_generics ( ) . map_or ( 0 , Generics :: len) ;
259+ ( parent_len, self_params, type_params, const_params, impl_trait_params)
284260 }
285261
286262 pub ( crate ) fn param_idx ( & self , param : TypeOrConstParamId ) -> Option < usize > {
@@ -291,18 +267,21 @@ impl Generics {
291267 if param. parent == self . def {
292268 let ( idx, ( _local_id, data) ) = self
293269 . params
294- . type_or_consts
295270 . iter ( )
296271 . enumerate ( )
297272 . find ( |( _, ( idx, _) ) | * idx == param. local_id )
298273 . unwrap ( ) ;
299- let ( _total , parent_len, _child ) = self . len_split ( ) ;
274+ let parent_len = self . parent_generics ( ) . map_or ( 0 , Generics :: len ) ;
300275 Some ( ( parent_len + idx, data) )
301276 } else {
302- self . parent_generics . as_ref ( ) . and_then ( |g| g. find_param ( param) )
277+ self . parent_generics ( ) . and_then ( |g| g. find_param ( param) )
303278 }
304279 }
305280
281+ fn parent_generics ( & self ) -> Option < & Generics > {
282+ self . parent_generics . as_ref ( ) . map ( |it| & * * it)
283+ }
284+
306285 /// Returns a Substitution that replaces each parameter by a bound variable.
307286 pub ( crate ) fn bound_vars_subst (
308287 & self ,
@@ -377,10 +356,10 @@ pub fn is_fn_unsafe_to_call(db: &dyn HirDatabase, func: FunctionId) -> bool {
377356 // Function in an `extern` block are always unsafe to call, except when it has
378357 // `"rust-intrinsic"` ABI there are a few exceptions.
379358 let id = block. lookup ( db. upcast ( ) ) . id ;
380- match id . item_tree ( db . upcast ( ) ) [ id . value ] . abi . as_deref ( ) {
381- Some ( "rust-intrinsic" ) => is_intrinsic_fn_unsafe ( & data . name ) ,
382- _ => true ,
383- }
359+ ! matches ! (
360+ id . item_tree ( db . upcast ( ) ) [ id . value ] . abi . as_deref ( ) ,
361+ Some ( "rust-intrinsic" ) if !is_intrinsic_fn_unsafe ( & data . name )
362+ )
384363 }
385364 _ => false ,
386365 }
0 commit comments