@@ -4,24 +4,21 @@ use crate::constrained_generic_params::{identify_constrained_generic_params, Par
4
4
5
5
use rustc_ast as ast;
6
6
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
7
+ use rustc_data_structures:: sync:: par_for_each_in;
7
8
use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder , ErrorGuaranteed } ;
8
9
use rustc_hir as hir;
9
10
use rustc_hir:: def_id:: { DefId , LocalDefId } ;
10
- use rustc_hir:: intravisit as hir_visit;
11
- use rustc_hir:: intravisit:: Visitor ;
12
- use rustc_hir:: itemlikevisit:: ParItemLikeVisitor ;
13
11
use rustc_hir:: lang_items:: LangItem ;
14
12
use rustc_hir:: ItemKind ;
15
13
use rustc_infer:: infer:: outlives:: env:: OutlivesEnvironment ;
16
14
use rustc_infer:: infer:: outlives:: obligations:: TypeOutlives ;
17
15
use rustc_infer:: infer:: region_constraints:: GenericKind ;
18
16
use rustc_infer:: infer:: { self , InferCtxt , TyCtxtInferExt } ;
19
- use rustc_middle:: hir:: nested_filter;
20
17
use rustc_middle:: ty:: subst:: { GenericArgKind , InternalSubsts , Subst } ;
21
18
use rustc_middle:: ty:: trait_def:: TraitSpecializationKind ;
22
19
use rustc_middle:: ty:: {
23
- self , AdtKind , EarlyBinder , GenericParamDefKind , ToPredicate , Ty , TyCtxt , TypeFoldable ,
24
- TypeSuperFoldable , TypeVisitor ,
20
+ self , AdtKind , DefIdTree , EarlyBinder , GenericParamDefKind , ToPredicate , Ty , TyCtxt ,
21
+ TypeFoldable , TypeSuperFoldable , TypeVisitor ,
25
22
} ;
26
23
use rustc_session:: parse:: feature_err;
27
24
use rustc_span:: symbol:: { sym, Ident , Symbol } ;
@@ -70,6 +67,23 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> {
70
67
}
71
68
}
72
69
70
+ pub ( crate ) fn check_well_formed ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) {
71
+ let node = tcx. hir ( ) . expect_owner ( def_id) ;
72
+ match node {
73
+ hir:: OwnerNode :: Crate ( _) => { }
74
+ hir:: OwnerNode :: Item ( item) => check_item ( tcx, item) ,
75
+ hir:: OwnerNode :: TraitItem ( item) => check_trait_item ( tcx, item) ,
76
+ hir:: OwnerNode :: ImplItem ( item) => check_impl_item ( tcx, item) ,
77
+ hir:: OwnerNode :: ForeignItem ( item) => check_foreign_item ( tcx, item) ,
78
+ }
79
+
80
+ if let Some ( generics) = node. generics ( ) {
81
+ for param in generics. params {
82
+ check_param_wf ( tcx, param)
83
+ }
84
+ }
85
+ }
86
+
73
87
/// Checks that the field types (in a struct def'n) or argument types (in an enum def'n) are
74
88
/// well-formed, meaning that they do not require any constraints not declared in the struct
75
89
/// definition itself. For example, this definition would be illegal:
@@ -84,8 +98,8 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> {
84
98
/// not included it frequently leads to confusing errors in fn bodies. So it's better to check
85
99
/// the types first.
86
100
#[ instrument( skip( tcx) , level = "debug" ) ]
87
- pub fn check_item_well_formed ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) {
88
- let item = tcx . hir ( ) . expect_item ( def_id) ;
101
+ fn check_item < ' tcx > ( tcx : TyCtxt < ' tcx > , item : & ' tcx hir :: Item < ' tcx > ) {
102
+ let def_id = item . def_id ;
89
103
90
104
debug ! (
91
105
?item. def_id,
@@ -156,20 +170,6 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
156
170
hir:: ItemKind :: Const ( ty, ..) => {
157
171
check_item_type ( tcx, item. def_id , ty. span , false ) ;
158
172
}
159
- hir:: ItemKind :: ForeignMod { items, .. } => {
160
- for it in items. iter ( ) {
161
- let it = tcx. hir ( ) . foreign_item ( it. id ) ;
162
- match it. kind {
163
- hir:: ForeignItemKind :: Fn ( decl, ..) => {
164
- check_item_fn ( tcx, it. def_id , it. ident , it. span , decl)
165
- }
166
- hir:: ForeignItemKind :: Static ( ty, ..) => {
167
- check_item_type ( tcx, it. def_id , ty. span , true )
168
- }
169
- hir:: ForeignItemKind :: Type => ( ) ,
170
- }
171
- }
172
- }
173
173
hir:: ItemKind :: Struct ( ref struct_def, ref ast_generics) => {
174
174
check_type_defn ( tcx, item, false , |fcx| vec ! [ fcx. non_enum_variant( struct_def) ] ) ;
175
175
@@ -191,13 +191,31 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
191
191
hir:: ItemKind :: TraitAlias ( ..) => {
192
192
check_trait ( tcx, item) ;
193
193
}
194
+ // `ForeignItem`s are handled separately.
195
+ hir:: ItemKind :: ForeignMod { .. } => { }
194
196
_ => { }
195
197
}
196
198
}
197
199
198
- pub fn check_trait_item ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) {
199
- let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
200
- let trait_item = tcx. hir ( ) . expect_trait_item ( def_id) ;
200
+ fn check_foreign_item ( tcx : TyCtxt < ' _ > , item : & hir:: ForeignItem < ' _ > ) {
201
+ let def_id = item. def_id ;
202
+
203
+ debug ! (
204
+ ?item. def_id,
205
+ item. name = ? tcx. def_path_str( def_id. to_def_id( ) )
206
+ ) ;
207
+
208
+ match item. kind {
209
+ hir:: ForeignItemKind :: Fn ( decl, ..) => {
210
+ check_item_fn ( tcx, item. def_id , item. ident , item. span , decl)
211
+ }
212
+ hir:: ForeignItemKind :: Static ( ty, ..) => check_item_type ( tcx, item. def_id , ty. span , true ) ,
213
+ hir:: ForeignItemKind :: Type => ( ) ,
214
+ }
215
+ }
216
+
217
+ fn check_trait_item ( tcx : TyCtxt < ' _ > , trait_item : & hir:: TraitItem < ' _ > ) {
218
+ let def_id = trait_item. def_id ;
201
219
202
220
let ( method_sig, span) = match trait_item. kind {
203
221
hir:: TraitItemKind :: Fn ( ref sig, _) => ( Some ( sig) , trait_item. span ) ,
@@ -207,7 +225,7 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
207
225
check_object_unsafe_self_trait_by_name ( tcx, trait_item) ;
208
226
check_associated_item ( tcx, trait_item. def_id , span, method_sig) ;
209
227
210
- let encl_trait_def_id = tcx. hir ( ) . get_parent_item ( hir_id ) ;
228
+ let encl_trait_def_id = tcx. local_parent ( def_id ) ;
211
229
let encl_trait = tcx. hir ( ) . expect_item ( encl_trait_def_id) ;
212
230
let encl_trait_def_id = encl_trait. def_id . to_def_id ( ) ;
213
231
let fn_lang_item_name = if Some ( encl_trait_def_id) == tcx. lang_items ( ) . fn_trait ( ) {
@@ -783,8 +801,8 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem
783
801
}
784
802
}
785
803
786
- pub fn check_impl_item ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) {
787
- let impl_item = tcx . hir ( ) . expect_impl_item ( def_id) ;
804
+ fn check_impl_item ( tcx : TyCtxt < ' _ > , impl_item : & hir :: ImplItem < ' _ > ) {
805
+ let def_id = impl_item . def_id ;
788
806
789
807
let ( method_sig, span) = match impl_item. kind {
790
808
hir:: ImplItemKind :: Fn ( ref sig, _) => ( Some ( sig) , impl_item. span ) ,
@@ -793,7 +811,7 @@ pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
793
811
_ => ( None , impl_item. span ) ,
794
812
} ;
795
813
796
- check_associated_item ( tcx, impl_item . def_id , span, method_sig) ;
814
+ check_associated_item ( tcx, def_id, span, method_sig) ;
797
815
}
798
816
799
817
fn check_param_wf ( tcx : TyCtxt < ' _ > , param : & hir:: GenericParam < ' _ > ) {
@@ -1840,67 +1858,12 @@ fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, mut span: Span, id: hir::HirI
1840
1858
fcx. select_all_obligations_or_error ( ) ;
1841
1859
}
1842
1860
1843
- #[ derive( Clone , Copy ) ]
1844
- pub struct CheckTypeWellFormedVisitor < ' tcx > {
1845
- tcx : TyCtxt < ' tcx > ,
1846
- }
1847
-
1848
- impl < ' tcx > CheckTypeWellFormedVisitor < ' tcx > {
1849
- pub fn new ( tcx : TyCtxt < ' tcx > ) -> CheckTypeWellFormedVisitor < ' tcx > {
1850
- CheckTypeWellFormedVisitor { tcx }
1851
- }
1852
- }
1853
-
1854
- impl < ' tcx > ParItemLikeVisitor < ' tcx > for CheckTypeWellFormedVisitor < ' tcx > {
1855
- fn visit_item ( & self , i : & ' tcx hir:: Item < ' tcx > ) {
1856
- Visitor :: visit_item ( & mut self . clone ( ) , i) ;
1857
- }
1858
-
1859
- fn visit_trait_item ( & self , trait_item : & ' tcx hir:: TraitItem < ' tcx > ) {
1860
- Visitor :: visit_trait_item ( & mut self . clone ( ) , trait_item) ;
1861
- }
1862
-
1863
- fn visit_impl_item ( & self , impl_item : & ' tcx hir:: ImplItem < ' tcx > ) {
1864
- Visitor :: visit_impl_item ( & mut self . clone ( ) , impl_item) ;
1865
- }
1866
-
1867
- fn visit_foreign_item ( & self , foreign_item : & ' tcx hir:: ForeignItem < ' tcx > ) {
1868
- Visitor :: visit_foreign_item ( & mut self . clone ( ) , foreign_item)
1869
- }
1870
- }
1871
-
1872
- impl < ' tcx > Visitor < ' tcx > for CheckTypeWellFormedVisitor < ' tcx > {
1873
- type NestedFilter = nested_filter:: OnlyBodies ;
1874
-
1875
- fn nested_visit_map ( & mut self ) -> Self :: Map {
1876
- self . tcx . hir ( )
1877
- }
1878
-
1879
- #[ instrument( skip( self , i) , level = "debug" ) ]
1880
- fn visit_item ( & mut self , i : & ' tcx hir:: Item < ' tcx > ) {
1881
- trace ! ( ?i) ;
1882
- self . tcx . ensure ( ) . check_item_well_formed ( i. def_id ) ;
1883
- hir_visit:: walk_item ( self , i) ;
1884
- }
1885
-
1886
- #[ instrument( skip( self , trait_item) , level = "debug" ) ]
1887
- fn visit_trait_item ( & mut self , trait_item : & ' tcx hir:: TraitItem < ' tcx > ) {
1888
- trace ! ( ?trait_item) ;
1889
- self . tcx . ensure ( ) . check_trait_item_well_formed ( trait_item. def_id ) ;
1890
- hir_visit:: walk_trait_item ( self , trait_item) ;
1891
- }
1892
-
1893
- #[ instrument( skip( self , impl_item) , level = "debug" ) ]
1894
- fn visit_impl_item ( & mut self , impl_item : & ' tcx hir:: ImplItem < ' tcx > ) {
1895
- trace ! ( ?impl_item) ;
1896
- self . tcx . ensure ( ) . check_impl_item_well_formed ( impl_item. def_id ) ;
1897
- hir_visit:: walk_impl_item ( self , impl_item) ;
1898
- }
1899
-
1900
- fn visit_generic_param ( & mut self , p : & ' tcx hir:: GenericParam < ' tcx > ) {
1901
- check_param_wf ( self . tcx , p) ;
1902
- hir_visit:: walk_generic_param ( self , p) ;
1903
- }
1861
+ pub ( crate ) fn check_wf_new ( tcx : TyCtxt < ' _ > ) {
1862
+ let items = tcx. hir_crate_items ( ( ) ) ;
1863
+ par_for_each_in ( items. items ( ) , |item| tcx. ensure ( ) . check_well_formed ( item. def_id ) ) ;
1864
+ par_for_each_in ( items. impl_items ( ) , |item| tcx. ensure ( ) . check_well_formed ( item. def_id ) ) ;
1865
+ par_for_each_in ( items. trait_items ( ) , |item| tcx. ensure ( ) . check_well_formed ( item. def_id ) ) ;
1866
+ par_for_each_in ( items. foreign_items ( ) , |item| tcx. ensure ( ) . check_well_formed ( item. def_id ) ) ;
1904
1867
}
1905
1868
1906
1869
///////////////////////////////////////////////////////////////////////////
0 commit comments