10
10
// a variable.
11
11
12
12
use rustc_arena:: DroplessArena ;
13
- use rustc_hir as hir;
14
13
use rustc_hir:: def:: DefKind ;
15
- use rustc_hir:: HirIdMap ;
14
+ use rustc_hir:: def_id :: { LocalDefId , LocalDefIdMap } ;
16
15
use rustc_middle:: ty:: { self , TyCtxt } ;
17
16
use std:: fmt;
18
17
@@ -52,11 +51,11 @@ pub struct TermsContext<'a, 'tcx> {
52
51
// For marker types, UnsafeCell, and other lang items where
53
52
// variance is hardcoded, records the item-id and the hardcoded
54
53
// variance.
55
- pub lang_items : Vec < ( hir :: HirId , Vec < ty:: Variance > ) > ,
54
+ pub lang_items : Vec < ( LocalDefId , Vec < ty:: Variance > ) > ,
56
55
57
56
// Maps from the node id of an item to the first inferred index
58
57
// used for its type & region parameters.
59
- pub inferred_starts : HirIdMap < InferredIndex > ,
58
+ pub inferred_starts : LocalDefIdMap < InferredIndex > ,
60
59
61
60
// Maps from an InferredIndex to the term for that variable.
62
61
pub inferred_terms : Vec < VarianceTermPtr < ' a > > ,
@@ -81,51 +80,48 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>(
81
80
// - https://rustc-dev-guide.rust-lang.org/variance.html
82
81
let crate_items = tcx. hir_crate_items ( ( ) ) ;
83
82
84
- for id in crate_items. items ( ) {
85
- terms_cx. check_item ( id) ;
86
- }
83
+ for def_id in crate_items. definitions ( ) {
84
+ debug ! ( "add_inferreds for item {:?}" , def_id) ;
87
85
88
- for id in crate_items. trait_items ( ) {
89
- if let DefKind :: AssocFn = tcx. def_kind ( id. def_id ) {
90
- terms_cx. add_inferreds_for_item ( id. hir_id ( ) ) ;
91
- }
92
- }
86
+ let def_kind = tcx. def_kind ( def_id) ;
93
87
94
- for id in crate_items. impl_items ( ) {
95
- if let DefKind :: AssocFn = tcx. def_kind ( id. def_id ) {
96
- terms_cx. add_inferreds_for_item ( id. hir_id ( ) ) ;
97
- }
98
- }
88
+ match def_kind {
89
+ DefKind :: Struct | DefKind :: Union | DefKind :: Enum => {
90
+ terms_cx. add_inferreds_for_item ( def_id) ;
99
91
100
- for id in crate_items. foreign_items ( ) {
101
- if let DefKind :: Fn = tcx. def_kind ( id. def_id ) {
102
- terms_cx. add_inferreds_for_item ( id. hir_id ( ) ) ;
92
+ let adt = tcx. adt_def ( def_id) ;
93
+ for variant in adt. variants ( ) {
94
+ if let Some ( ctor) = variant. ctor_def_id {
95
+ terms_cx. add_inferreds_for_item ( ctor. expect_local ( ) ) ;
96
+ }
97
+ }
98
+ }
99
+ DefKind :: Fn | DefKind :: AssocFn => terms_cx. add_inferreds_for_item ( def_id) ,
100
+ _ => { }
103
101
}
104
102
}
105
103
106
104
terms_cx
107
105
}
108
106
109
- fn lang_items ( tcx : TyCtxt < ' _ > ) -> Vec < ( hir :: HirId , Vec < ty:: Variance > ) > {
107
+ fn lang_items ( tcx : TyCtxt < ' _ > ) -> Vec < ( LocalDefId , Vec < ty:: Variance > ) > {
110
108
let lang_items = tcx. lang_items ( ) ;
111
109
let all = [
112
110
( lang_items. phantom_data ( ) , vec ! [ ty:: Covariant ] ) ,
113
111
( lang_items. unsafe_cell_type ( ) , vec ! [ ty:: Invariant ] ) ,
114
112
] ;
115
113
116
114
all. into_iter ( ) // iterating over (Option<DefId>, Variance)
117
- . filter ( |& ( ref d, _) | d. is_some ( ) )
118
- . map ( |( d, v) | ( d. unwrap ( ) , v) ) // (DefId, Variance)
119
115
. filter_map ( |( d, v) | {
120
- d. as_local ( ) . map ( |d| tcx. hir ( ) . local_def_id_to_hir_id ( d) ) . map ( |n| ( n, v) )
121
- } ) // (HirId, Variance)
116
+ let def_id = d?. as_local ( ) ?; // LocalDefId
117
+ Some ( ( def_id, v) )
118
+ } )
122
119
. collect ( )
123
120
}
124
121
125
122
impl < ' a , ' tcx > TermsContext < ' a , ' tcx > {
126
- fn add_inferreds_for_item ( & mut self , id : hir :: HirId ) {
123
+ fn add_inferreds_for_item ( & mut self , def_id : LocalDefId ) {
127
124
let tcx = self . tcx ;
128
- let def_id = tcx. hir ( ) . local_def_id ( id) ;
129
125
let count = tcx. generics_of ( def_id) . count ( ) ;
130
126
131
127
if count == 0 {
@@ -134,7 +130,7 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> {
134
130
135
131
// Record the start of this item's inferreds.
136
132
let start = self . inferred_terms . len ( ) ;
137
- let newly_added = self . inferred_starts . insert ( id , InferredIndex ( start) ) . is_none ( ) ;
133
+ let newly_added = self . inferred_starts . insert ( def_id , InferredIndex ( start) ) . is_none ( ) ;
138
134
assert ! ( newly_added) ;
139
135
140
136
// N.B., in the code below for writing the results back into the
@@ -146,42 +142,4 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> {
146
142
( start..( start + count) ) . map ( |i| & * arena. alloc ( InferredTerm ( InferredIndex ( i) ) ) ) ,
147
143
) ;
148
144
}
149
-
150
- fn check_item ( & mut self , id : hir:: ItemId ) {
151
- debug ! ( "add_inferreds for item {}" , self . tcx. hir( ) . node_to_string( id. hir_id( ) ) ) ;
152
-
153
- let def_kind = self . tcx . def_kind ( id. def_id ) ;
154
- match def_kind {
155
- DefKind :: Struct | DefKind :: Union => {
156
- let item = self . tcx . hir ( ) . item ( id) ;
157
-
158
- if let hir:: ItemKind :: Struct ( ref struct_def, _)
159
- | hir:: ItemKind :: Union ( ref struct_def, _) = item. kind
160
- {
161
- self . add_inferreds_for_item ( item. hir_id ( ) ) ;
162
-
163
- if let hir:: VariantData :: Tuple ( ..) = * struct_def {
164
- self . add_inferreds_for_item ( struct_def. ctor_hir_id ( ) . unwrap ( ) ) ;
165
- }
166
- }
167
- }
168
- DefKind :: Enum => {
169
- let item = self . tcx . hir ( ) . item ( id) ;
170
-
171
- if let hir:: ItemKind :: Enum ( ref enum_def, _) = item. kind {
172
- self . add_inferreds_for_item ( item. hir_id ( ) ) ;
173
-
174
- for variant in enum_def. variants {
175
- if let hir:: VariantData :: Tuple ( ..) = variant. data {
176
- self . add_inferreds_for_item ( variant. data . ctor_hir_id ( ) . unwrap ( ) ) ;
177
- }
178
- }
179
- }
180
- }
181
- DefKind :: Fn => {
182
- self . add_inferreds_for_item ( id. hir_id ( ) ) ;
183
- }
184
- _ => { }
185
- }
186
- }
187
145
}
0 commit comments