@@ -177,21 +177,19 @@ pub(crate) fn generics(db: &dyn DefDatabase, def: GenericDefId) -> Generics {
177
177
let parent_generics = parent_generic_def ( db, def) . map ( |def| Box :: new ( generics ( db, def) ) ) ;
178
178
if parent_generics. is_some ( ) && matches ! ( def, GenericDefId :: TypeAliasId ( _) ) {
179
179
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 {
185
183
// XXX: treat const generic associated types as not existing to avoid crashes (#11769)
186
184
//
187
185
// Chalk expects the inner associated type's parameters to come
188
186
// *before*, not after the trait's generics as we've always done it.
189
187
// Adapting to this requires a larger refactoring
190
188
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 }
192
190
} else {
193
- return Generics { def, params, parent_generics } ;
194
- }
191
+ Generics { def, params, parent_generics }
192
+ } ;
195
193
}
196
194
Generics { def, params : db. generic_params ( def) , parent_generics }
197
195
}
@@ -219,68 +217,46 @@ impl Generics {
219
217
pub ( crate ) fn iter < ' a > (
220
218
& ' a self ,
221
219
) -> 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 ( )
224
224
. 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 ) ) )
235
227
}
236
228
237
229
/// Iterator over types and const params of parent.
238
230
pub ( crate ) fn iter_parent < ' a > (
239
231
& ' a self ,
240
232
) -> 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)
246
237
} )
247
238
}
248
239
249
240
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) ;
256
242
let child = self . params . type_or_consts . len ( ) ;
257
- ( parent + child, parent , child )
243
+ parent + child
258
244
}
259
245
260
246
/// (parent total, self param, type param list, const param list, impl trait)
261
247
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 ( ) ;
276
256
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)
284
260
}
285
261
286
262
pub ( crate ) fn param_idx ( & self , param : TypeOrConstParamId ) -> Option < usize > {
@@ -291,18 +267,21 @@ impl Generics {
291
267
if param. parent == self . def {
292
268
let ( idx, ( _local_id, data) ) = self
293
269
. params
294
- . type_or_consts
295
270
. iter ( )
296
271
. enumerate ( )
297
272
. find ( |( _, ( idx, _) ) | * idx == param. local_id )
298
273
. unwrap ( ) ;
299
- let ( _total , parent_len, _child ) = self . len_split ( ) ;
274
+ let parent_len = self . parent_generics ( ) . map_or ( 0 , Generics :: len ) ;
300
275
Some ( ( parent_len + idx, data) )
301
276
} 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) )
303
278
}
304
279
}
305
280
281
+ fn parent_generics ( & self ) -> Option < & Generics > {
282
+ self . parent_generics . as_ref ( ) . map ( |it| & * * it)
283
+ }
284
+
306
285
/// Returns a Substitution that replaces each parameter by a bound variable.
307
286
pub ( crate ) fn bound_vars_subst (
308
287
& self ,
@@ -377,10 +356,10 @@ pub fn is_fn_unsafe_to_call(db: &dyn HirDatabase, func: FunctionId) -> bool {
377
356
// Function in an `extern` block are always unsafe to call, except when it has
378
357
// `"rust-intrinsic"` ABI there are a few exceptions.
379
358
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
+ )
384
363
}
385
364
_ => false ,
386
365
}
0 commit comments