@@ -12,7 +12,7 @@ use hir_def::{
12
12
} ,
13
13
} ;
14
14
use hir_expand:: name:: Name ;
15
- use intern:: Symbol ;
15
+ use intern:: { Symbol , sym } ;
16
16
use la_arena:: Arena ;
17
17
use rustc_type_ir:: inherent:: Ty as _;
18
18
use triomphe:: Arc ;
@@ -24,52 +24,39 @@ use super::{Const, EarlyParamRegion, ErrorGuaranteed, ParamConst, Region, Solver
24
24
use super :: { DbInterner , GenericArg } ;
25
25
26
26
pub ( crate ) fn generics ( db : & dyn HirDatabase , def : SolverDefId ) -> Generics {
27
- let mk_lt = |( index, ( _ , lt ) ) : ( usize , ( _ , & LifetimeParamData ) ) | {
27
+ let mk_lt = |index, lt : & LifetimeParamData | {
28
28
let name = lt. name . symbol ( ) . clone ( ) ;
29
- let index = index as u32 ;
30
29
let kind = GenericParamDefKind :: Lifetime ;
31
30
GenericParamDef { name, index, kind }
32
31
} ;
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 ) ;
39
34
let kind = match p {
40
35
TypeOrConstParamData :: TypeParamData ( _) => GenericParamDefKind :: Type ,
41
36
TypeOrConstParamData :: ConstParamData ( _) => GenericParamDefKind :: Const ,
42
37
} ;
43
38
GenericParamDef { name, index, kind }
44
39
} ;
45
40
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 ;
72
48
}
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
73
60
} ;
74
61
75
62
let ( parent, own_params) = match ( def. try_into ( ) , def) {
@@ -82,20 +69,17 @@ pub(crate) fn generics(db: &dyn HirDatabase, def: SolverDefId) -> Generics {
82
69
// The opaque type itself does not have generics - only the parent function
83
70
( Some ( GenericDefId :: FunctionId ( function_id) ) , vec ! [ ] )
84
71
}
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
+ }
91
75
crate :: ImplTraitId :: AsyncBlockTypeImplTrait ( def, _) => {
92
76
let param = TypeOrConstParamData :: TypeParamData ( TypeParamData {
93
77
name : None ,
94
78
default : None ,
95
79
provenance : TypeParamProvenance :: TypeParamList ,
96
80
} ) ;
97
81
// 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) ] )
99
83
}
100
84
}
101
85
}
0 commit comments