1010// a variable.
1111
1212use rustc_arena:: DroplessArena ;
13- use rustc_hir as hir;
1413use rustc_hir:: def:: DefKind ;
15- use rustc_hir:: HirIdMap ;
14+ use rustc_hir:: def_id :: { LocalDefId , LocalDefIdMap } ;
1615use rustc_middle:: ty:: { self , TyCtxt } ;
1716use std:: fmt;
1817
@@ -52,11 +51,11 @@ pub struct TermsContext<'a, 'tcx> {
5251 // For marker types, UnsafeCell, and other lang items where
5352 // variance is hardcoded, records the item-id and the hardcoded
5453 // variance.
55- pub lang_items : Vec < ( hir :: HirId , Vec < ty:: Variance > ) > ,
54+ pub lang_items : Vec < ( LocalDefId , Vec < ty:: Variance > ) > ,
5655
5756 // Maps from the node id of an item to the first inferred index
5857 // used for its type & region parameters.
59- pub inferred_starts : HirIdMap < InferredIndex > ,
58+ pub inferred_starts : LocalDefIdMap < InferredIndex > ,
6059
6160 // Maps from an InferredIndex to the term for that variable.
6261 pub inferred_terms : Vec < VarianceTermPtr < ' a > > ,
@@ -81,51 +80,48 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>(
8180 // - https://rustc-dev-guide.rust-lang.org/variance.html
8281 let crate_items = tcx. hir_crate_items ( ( ) ) ;
8382
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) ;
8785
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) ;
9387
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) ;
9991
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+ _ => { }
103101 }
104102 }
105103
106104 terms_cx
107105}
108106
109- fn lang_items ( tcx : TyCtxt < ' _ > ) -> Vec < ( hir :: HirId , Vec < ty:: Variance > ) > {
107+ fn lang_items ( tcx : TyCtxt < ' _ > ) -> Vec < ( LocalDefId , Vec < ty:: Variance > ) > {
110108 let lang_items = tcx. lang_items ( ) ;
111109 let all = [
112110 ( lang_items. phantom_data ( ) , vec ! [ ty:: Covariant ] ) ,
113111 ( lang_items. unsafe_cell_type ( ) , vec ! [ ty:: Invariant ] ) ,
114112 ] ;
115113
116114 all. into_iter ( ) // iterating over (Option<DefId>, Variance)
117- . filter ( |& ( ref d, _) | d. is_some ( ) )
118- . map ( |( d, v) | ( d. unwrap ( ) , v) ) // (DefId, Variance)
119115 . 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+ } )
122119 . collect ( )
123120}
124121
125122impl < ' 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 ) {
127124 let tcx = self . tcx ;
128- let def_id = tcx. hir ( ) . local_def_id ( id) ;
129125 let count = tcx. generics_of ( def_id) . count ( ) ;
130126
131127 if count == 0 {
@@ -134,7 +130,7 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> {
134130
135131 // Record the start of this item's inferreds.
136132 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 ( ) ;
138134 assert ! ( newly_added) ;
139135
140136 // N.B., in the code below for writing the results back into the
@@ -146,42 +142,4 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> {
146142 ( start..( start + count) ) . map ( |i| & * arena. alloc ( InferredTerm ( InferredIndex ( i) ) ) ) ,
147143 ) ;
148144 }
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- }
187145}
0 commit comments