@@ -4,24 +4,21 @@ use crate::constrained_generic_params::{identify_constrained_generic_params, Par
44
55use rustc_ast as ast;
66use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
7+ use rustc_data_structures:: sync:: par_for_each_in;
78use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder , ErrorGuaranteed } ;
89use rustc_hir as hir;
910use 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 ;
1311use rustc_hir:: lang_items:: LangItem ;
1412use rustc_hir:: ItemKind ;
1513use rustc_infer:: infer:: outlives:: env:: OutlivesEnvironment ;
1614use rustc_infer:: infer:: outlives:: obligations:: TypeOutlives ;
1715use rustc_infer:: infer:: region_constraints:: GenericKind ;
1816use rustc_infer:: infer:: { self , InferCtxt , TyCtxtInferExt } ;
19- use rustc_middle:: hir:: nested_filter;
2017use rustc_middle:: ty:: subst:: { GenericArgKind , InternalSubsts , Subst } ;
2118use rustc_middle:: ty:: trait_def:: TraitSpecializationKind ;
2219use 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 ,
2522} ;
2623use rustc_session:: parse:: feature_err;
2724use rustc_span:: symbol:: { sym, Ident , Symbol } ;
@@ -70,6 +67,23 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> {
7067 }
7168}
7269
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+
7387/// Checks that the field types (in a struct def'n) or argument types (in an enum def'n) are
7488/// well-formed, meaning that they do not require any constraints not declared in the struct
7589/// definition itself. For example, this definition would be illegal:
@@ -84,8 +98,8 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> {
8498/// not included it frequently leads to confusing errors in fn bodies. So it's better to check
8599/// the types first.
86100#[ 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 ;
89103
90104 debug ! (
91105 ?item. def_id,
@@ -156,20 +170,6 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
156170 hir:: ItemKind :: Const ( ty, ..) => {
157171 check_item_type ( tcx, item. def_id , ty. span , false ) ;
158172 }
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- }
173173 hir:: ItemKind :: Struct ( ref struct_def, ref ast_generics) => {
174174 check_type_defn ( tcx, item, false , |fcx| vec ! [ fcx. non_enum_variant( struct_def) ] ) ;
175175
@@ -191,13 +191,31 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
191191 hir:: ItemKind :: TraitAlias ( ..) => {
192192 check_trait ( tcx, item) ;
193193 }
194+ // `ForeignItem`s are handled separately.
195+ hir:: ItemKind :: ForeignMod { .. } => { }
194196 _ => { }
195197 }
196198}
197199
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 ;
201219
202220 let ( method_sig, span) = match trait_item. kind {
203221 hir:: TraitItemKind :: Fn ( ref sig, _) => ( Some ( sig) , trait_item. span ) ,
@@ -207,7 +225,7 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
207225 check_object_unsafe_self_trait_by_name ( tcx, trait_item) ;
208226 check_associated_item ( tcx, trait_item. def_id , span, method_sig) ;
209227
210- let encl_trait_def_id = tcx. hir ( ) . get_parent_item ( hir_id ) ;
228+ let encl_trait_def_id = tcx. local_parent ( def_id ) ;
211229 let encl_trait = tcx. hir ( ) . expect_item ( encl_trait_def_id) ;
212230 let encl_trait_def_id = encl_trait. def_id . to_def_id ( ) ;
213231 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
783801 }
784802}
785803
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 ;
788806
789807 let ( method_sig, span) = match impl_item. kind {
790808 hir:: ImplItemKind :: Fn ( ref sig, _) => ( Some ( sig) , impl_item. span ) ,
@@ -793,7 +811,7 @@ pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
793811 _ => ( None , impl_item. span ) ,
794812 } ;
795813
796- check_associated_item ( tcx, impl_item . def_id , span, method_sig) ;
814+ check_associated_item ( tcx, def_id, span, method_sig) ;
797815}
798816
799817fn 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
18401858 fcx. select_all_obligations_or_error ( ) ;
18411859}
18421860
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 ) ) ;
19041867}
19051868
19061869///////////////////////////////////////////////////////////////////////////
0 commit comments