@@ -26,7 +26,7 @@ use errors::{DiagnosticBuilder, DiagnosticId};
2626use rustc:: hir:: intravisit:: { self , Visitor , NestedVisitorMap } ;
2727use rustc:: hir;
2828
29- pub struct CheckTypeWellFormedVisitor < ' a , ' tcx : ' a > {
29+ pub struct CheckTypeWellFormed < ' a , ' tcx : ' a > {
3030 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
3131}
3232
@@ -43,14 +43,14 @@ struct CheckWfFcxBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
4343impl < ' a , ' gcx , ' tcx > CheckWfFcxBuilder < ' a , ' gcx , ' tcx > {
4444 fn with_fcx < F > ( & ' tcx mut self , f : F ) where
4545 F : for < ' b > FnOnce ( & FnCtxt < ' b , ' gcx , ' tcx > ,
46- & mut CheckTypeWellFormedVisitor < ' b , ' gcx > ) -> Vec < Ty < ' tcx > >
46+ & mut CheckTypeWellFormed < ' b , ' gcx > ) -> Vec < Ty < ' tcx > >
4747 {
4848 let id = self . id ;
4949 let span = self . span ;
5050 let param_env = self . param_env ;
5151 self . inherited . enter ( |inh| {
5252 let fcx = FnCtxt :: new ( & inh, param_env, id) ;
53- let wf_tys = f ( & fcx, & mut CheckTypeWellFormedVisitor {
53+ let wf_tys = f ( & fcx, & mut CheckTypeWellFormed {
5454 tcx : fcx. tcx . global_tcx ( ) ,
5555 } ) ;
5656 fcx. select_all_obligations_or_error ( ) ;
@@ -59,10 +59,10 @@ impl<'a, 'gcx, 'tcx> CheckWfFcxBuilder<'a, 'gcx, 'tcx> {
5959 }
6060}
6161
62- impl < ' a , ' gcx > CheckTypeWellFormedVisitor < ' a , ' gcx > {
62+ impl < ' a , ' gcx > CheckTypeWellFormed < ' a , ' gcx > {
6363 pub fn new ( tcx : TyCtxt < ' a , ' gcx , ' gcx > )
64- -> CheckTypeWellFormedVisitor < ' a , ' gcx > {
65- CheckTypeWellFormedVisitor {
64+ -> CheckTypeWellFormed < ' a , ' gcx > {
65+ CheckTypeWellFormed {
6666 tcx,
6767 }
6868 }
@@ -78,11 +78,14 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
7878 /// We do this check as a pre-pass before checking fn bodies because if these constraints are
7979 /// not included it frequently leads to confusing errors in fn bodies. So it's better to check
8080 /// the types first.
81- fn check_item_well_formed ( & mut self , item : & hir :: Item ) {
81+ pub fn check_item_well_formed ( & mut self , def_id : DefId ) {
8282 let tcx = self . tcx ;
83+ let node_id = tcx. hir . as_local_node_id ( def_id) . unwrap ( ) ;
84+ let item = tcx. hir . expect_item ( node_id) ;
85+
8386 debug ! ( "check_item_well_formed(it.id={}, it.name={})" ,
8487 item. id,
85- tcx. item_path_str( tcx . hir . local_def_id ( item . id ) ) ) ;
88+ tcx. item_path_str( def_id ) ) ;
8689
8790 match item. node {
8891 // Right now we check that every default trait implementation
@@ -259,7 +262,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
259262
260263 // All field types must be well-formed.
261264 for field in & variant. fields {
262- fcx. register_wf_obligation ( field. ty , field. span , ObligationCauseCode :: MiscObligation )
265+ fcx. register_wf_obligation ( field. ty , field. span ,
266+ ObligationCauseCode :: MiscObligation )
263267 }
264268 }
265269
@@ -333,7 +337,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
333337 None => {
334338 let self_ty = fcx. tcx . type_of ( item_def_id) ;
335339 let self_ty = fcx. normalize_associated_types_in ( item. span , & self_ty) ;
336- fcx. register_wf_obligation ( self_ty, ast_self_ty. span , ObligationCauseCode :: MiscObligation ) ;
340+ fcx. register_wf_obligation ( self_ty, ast_self_ty. span ,
341+ ObligationCauseCode :: MiscObligation ) ;
337342 }
338343 }
339344
@@ -368,7 +373,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
368373 // parameter includes another (e.g., <T, U = T>). In those cases, we can't
369374 // be sure if it will error or not as user might always specify the other.
370375 if !ty. needs_subst ( ) {
371- fcx. register_wf_obligation ( ty, fcx. tcx . def_span ( d) , ObligationCauseCode :: MiscObligation ) ;
376+ fcx. register_wf_obligation ( ty, fcx. tcx . def_span ( d) ,
377+ ObligationCauseCode :: MiscObligation ) ;
372378 }
373379 }
374380
@@ -642,14 +648,28 @@ fn reject_shadowing_type_parameters(tcx: TyCtxt, def_id: DefId) {
642648 }
643649}
644650
651+ pub struct CheckTypeWellFormedVisitor < ' a , ' tcx : ' a > {
652+ tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
653+ }
654+
655+ impl < ' a , ' gcx > CheckTypeWellFormedVisitor < ' a , ' gcx > {
656+ pub fn new ( tcx : TyCtxt < ' a , ' gcx , ' gcx > )
657+ -> CheckTypeWellFormedVisitor < ' a , ' gcx > {
658+ CheckTypeWellFormedVisitor {
659+ tcx,
660+ }
661+ }
662+ }
663+
645664impl < ' a , ' tcx , ' v > Visitor < ' v > for CheckTypeWellFormedVisitor < ' a , ' tcx > {
646665 fn nested_visit_map < ' this > ( & ' this mut self ) -> NestedVisitorMap < ' this , ' v > {
647666 NestedVisitorMap :: None
648667 }
649668
650669 fn visit_item ( & mut self , i : & hir:: Item ) {
651670 debug ! ( "visit_item: {:?}" , i) ;
652- self . check_item_well_formed ( i) ;
671+ let def_id = self . tcx . hir . local_def_id ( i. id ) ;
672+ ty:: maps:: queries:: check_item_well_formed:: ensure ( self . tcx , def_id) ;
653673 intravisit:: walk_item ( self , i) ;
654674 }
655675
@@ -659,7 +679,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'a, 'tcx> {
659679 hir:: TraitItemKind :: Method ( ref sig, _) => Some ( sig) ,
660680 _ => None
661681 } ;
662- self . check_associated_item ( trait_item. id , trait_item. span , method_sig) ;
682+ CheckTypeWellFormed :: new ( self . tcx )
683+ . check_associated_item ( trait_item. id , trait_item. span , method_sig) ;
663684 intravisit:: walk_trait_item ( self , trait_item)
664685 }
665686
@@ -669,7 +690,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'a, 'tcx> {
669690 hir:: ImplItemKind :: Method ( ref sig, _) => Some ( sig) ,
670691 _ => None
671692 } ;
672- self . check_associated_item ( impl_item. id , impl_item. span , method_sig) ;
693+ CheckTypeWellFormed :: new ( self . tcx )
694+ . check_associated_item ( impl_item. id , impl_item. span , method_sig) ;
673695 intravisit:: walk_impl_item ( self , impl_item)
674696 }
675697}
0 commit comments