@@ -12,7 +12,7 @@ use hir_def::{
1212 } ,
1313} ;
1414use hir_expand:: name:: Name ;
15- use intern:: Symbol ;
15+ use intern:: { Symbol , sym } ;
1616use la_arena:: Arena ;
1717use rustc_type_ir:: inherent:: Ty as _;
1818use triomphe:: Arc ;
@@ -24,52 +24,39 @@ use super::{Const, EarlyParamRegion, ErrorGuaranteed, ParamConst, Region, Solver
2424use super :: { DbInterner , GenericArg } ;
2525
2626pub ( crate ) fn generics ( db : & dyn HirDatabase , def : SolverDefId ) -> Generics {
27- let mk_lt = |( index, ( _ , lt ) ) : ( usize , ( _ , & LifetimeParamData ) ) | {
27+ let mk_lt = |index, lt : & LifetimeParamData | {
2828 let name = lt. name . symbol ( ) . clone ( ) ;
29- let index = index as u32 ;
3029 let kind = GenericParamDefKind :: Lifetime ;
3130 GenericParamDef { name, index, kind }
3231 } ;
33- let mk_ty = |len_lt, ( index, p) : ( usize , & TypeOrConstParamData ) | {
34- let name = p
35- . name ( )
36- . map ( |n| n. symbol ( ) . clone ( ) )
37- . unwrap_or_else ( || Name :: missing ( ) . symbol ( ) . clone ( ) ) ;
38- let index = ( len_lt + index) as u32 ;
32+ let mk_ty = |index, p : & TypeOrConstParamData | {
33+ let name = p. name ( ) . map ( |n| n. symbol ( ) . clone ( ) ) . unwrap_or_else ( || sym:: MISSING_NAME ) ;
3934 let kind = match p {
4035 TypeOrConstParamData :: TypeParamData ( _) => GenericParamDefKind :: Type ,
4136 TypeOrConstParamData :: ConstParamData ( _) => GenericParamDefKind :: Const ,
4237 } ;
4338 GenericParamDef { name, index, kind }
4439 } ;
4540 let own_params_for_generic_params = |params : & GenericParams | {
46- if params. trait_self_param ( ) . is_some ( ) {
47- let len_lt = params. len_lifetimes ( ) + 1 ;
48- params
49- . iter_type_or_consts ( )
50- . take ( 1 )
51- . enumerate ( )
52- . map ( |t| mk_ty ( 0 , ( t. 0 , t. 1 . 1 ) ) )
53- . chain ( params. iter_lt ( ) . enumerate ( ) . map ( mk_lt) )
54- . chain (
55- params
56- . iter_type_or_consts ( )
57- . skip ( 1 )
58- . enumerate ( )
59- . map ( |t| mk_ty ( len_lt, ( t. 0 , t. 1 . 1 ) ) ) ,
60- )
61- . collect ( )
62- } else {
63- let len_lt = params. len_lifetimes ( ) ;
64- params
65- . iter_lt ( )
66- . enumerate ( )
67- . map ( mk_lt)
68- . chain (
69- params. iter_type_or_consts ( ) . enumerate ( ) . map ( |t| mk_ty ( len_lt, ( t. 0 , t. 1 . 1 ) ) ) ,
70- )
71- . collect ( )
41+ let mut result = Vec :: with_capacity ( params. len ( ) ) ;
42+ let mut type_and_consts = params. iter_type_or_consts ( ) ;
43+ let mut index = 0 ;
44+ if let Some ( self_param) = params. trait_self_param ( ) {
45+ result. push ( mk_ty ( 0 , & params[ self_param] ) ) ;
46+ type_and_consts. next ( ) ;
47+ index += 1 ;
7248 }
49+ result. extend ( params. iter_lt ( ) . map ( |( _, data) | {
50+ let lt = mk_lt ( index, data) ;
51+ index += 1 ;
52+ lt
53+ } ) ) ;
54+ result. extend ( type_and_consts. map ( |( _, data) | {
55+ let ty = mk_ty ( index, data) ;
56+ index += 1 ;
57+ ty
58+ } ) ) ;
59+ result
7360 } ;
7461
7562 let ( parent, own_params) = match ( def. try_into ( ) , def) {
@@ -82,20 +69,17 @@ pub(crate) fn generics(db: &dyn HirDatabase, def: SolverDefId) -> Generics {
8269 // The opaque type itself does not have generics - only the parent function
8370 ( Some ( GenericDefId :: FunctionId ( function_id) ) , vec ! [ ] )
8471 }
85- crate :: ImplTraitId :: TypeAliasImplTrait ( type_alias_id, _) => (
86- None ,
87- own_params_for_generic_params (
88- & db. generic_params ( GenericDefId :: TypeAliasId ( type_alias_id) ) ,
89- ) ,
90- ) ,
72+ crate :: ImplTraitId :: TypeAliasImplTrait ( type_alias_id, _) => {
73+ ( Some ( type_alias_id. into ( ) ) , Vec :: new ( ) )
74+ }
9175 crate :: ImplTraitId :: AsyncBlockTypeImplTrait ( def, _) => {
9276 let param = TypeOrConstParamData :: TypeParamData ( TypeParamData {
9377 name : None ,
9478 default : None ,
9579 provenance : TypeParamProvenance :: TypeParamList ,
9680 } ) ;
9781 // Yes, there is a parent but we don't include it in the generics
98- ( None , vec ! [ mk_ty( 0 , ( 0 , & param) ) ] )
82+ ( None , vec ! [ mk_ty( 0 , & param) ] )
9983 }
10084 }
10185 }
0 commit comments