]> git.lizzy.rs Git - rust.git/commitdiff
Queryify check_item_well_formed
authorWesley Wiser <wwiser@gmail.com>
Sat, 10 Mar 2018 03:35:15 +0000 (22:35 -0500)
committerWesley Wiser <wwiser@gmail.com>
Fri, 16 Mar 2018 03:11:09 +0000 (23:11 -0400)
Fixes #46753

src/librustc/dep_graph/dep_node.rs
src/librustc/ty/maps/mod.rs
src/librustc/ty/maps/plumbing.rs
src/librustc_typeck/check/mod.rs
src/librustc_typeck/check/wfcheck.rs

index 8d7fef90b754ef676f8118e4845620ac3cd98940..20c3fb501572c84d7c05908133254a3ec6af6b2f 100644 (file)
@@ -579,6 +579,7 @@ pub fn fingerprint_needed_for_crate_hash(self) -> bool {
     [] GetPanicStrategy(CrateNum),
     [] IsNoBuiltins(CrateNum),
     [] ImplDefaultness(DefId),
+    [] CheckItemWellFormed(DefId),
     [] ReachableNonGenerics(CrateNum),
     [] NativeLibraries(CrateNum),
     [] PluginRegistrarFn(CrateNum),
index c1783654effefc868efaeb0881952500c419f1c9..8f4100ad5f703e327f171c70308f102c4bdb5654 100644 (file)
 
     [] fn impl_defaultness: ImplDefaultness(DefId) -> hir::Defaultness,
 
+    [] fn check_item_well_formed: CheckItemWellFormed(DefId) -> (),
+
     // The DefIds of all non-generic functions and statics in the given crate
     // that can be reached from outside the crate.
     //
index bc7186f781a82498053208d8ca352be178276306..732c91ea98aa6e45b28234e61b141f9c0e3e3e62 100644 (file)
@@ -871,6 +871,7 @@ macro_rules! force {
         DepKind::GetPanicStrategy => { force!(panic_strategy, krate!()); }
         DepKind::IsNoBuiltins => { force!(is_no_builtins, krate!()); }
         DepKind::ImplDefaultness => { force!(impl_defaultness, def_id!()); }
+        DepKind::CheckItemWellFormed => { force!(check_item_well_formed, def_id!()); }
         DepKind::ReachableNonGenerics => { force!(reachable_non_generics, krate!()); }
         DepKind::NativeLibraries => { force!(native_libraries, krate!()); }
         DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); }
index dc9455487ede708e45edc734b8b4d9c1e5eae476..a6b307b841490d2cece5ea9d7af509ee8f525c3f 100644 (file)
@@ -718,6 +718,10 @@ fn typeck_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum
     })?)
 }
 
+fn check_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
+    wfcheck::CheckTypeWellFormed::new(tcx).check_item_well_formed(def_id);
+}
+
 pub fn provide(providers: &mut Providers) {
     *providers = Providers {
         typeck_item_bodies,
@@ -725,6 +729,7 @@ pub fn provide(providers: &mut Providers) {
         has_typeck_tables,
         adt_destructor,
         used_trait_imports,
+        check_item_well_formed,
         ..*providers
     };
 }
index edb4ebb6284e955da6179356ffc19f61dbf92f43..d665e55898d4a6eda658851b15517a1cdf04506d 100644 (file)
@@ -26,7 +26,7 @@
 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
 use rustc::hir;
 
-pub struct CheckTypeWellFormedVisitor<'a, 'tcx:'a> {
+pub struct CheckTypeWellFormed<'a, 'tcx:'a> {
     tcx: TyCtxt<'a, 'tcx, 'tcx>,
 }
 
@@ -43,14 +43,14 @@ struct CheckWfFcxBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
 impl<'a, 'gcx, 'tcx> CheckWfFcxBuilder<'a, 'gcx, 'tcx> {
     fn with_fcx<F>(&'tcx mut self, f: F) where
         F: for<'b> FnOnce(&FnCtxt<'b, 'gcx, 'tcx>,
-                          &mut CheckTypeWellFormedVisitor<'b, 'gcx>) -> Vec<Ty<'tcx>>
+                          &mut CheckTypeWellFormed<'b, 'gcx>) -> Vec<Ty<'tcx>>
     {
         let id = self.id;
         let span = self.span;
         let param_env = self.param_env;
         self.inherited.enter(|inh| {
             let fcx = FnCtxt::new(&inh, param_env, id);
-            let wf_tys = f(&fcx, &mut CheckTypeWellFormedVisitor {
+            let wf_tys = f(&fcx, &mut CheckTypeWellFormed {
                 tcx: fcx.tcx.global_tcx(),
             });
             fcx.select_all_obligations_or_error();
@@ -59,10 +59,10 @@ fn with_fcx<F>(&'tcx mut self, f: F) where
     }
 }
 
-impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
+impl<'a, 'gcx> CheckTypeWellFormed<'a, 'gcx> {
     pub fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>)
-               -> CheckTypeWellFormedVisitor<'a, 'gcx> {
-        CheckTypeWellFormedVisitor {
+               -> CheckTypeWellFormed<'a, 'gcx> {
+        CheckTypeWellFormed {
             tcx,
         }
     }
@@ -78,11 +78,14 @@ pub fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>)
     /// We do this check as a pre-pass before checking fn bodies because if these constraints are
     /// not included it frequently leads to confusing errors in fn bodies. So it's better to check
     /// the types first.
-    fn check_item_well_formed(&mut self, item: &hir::Item) {
+    pub fn check_item_well_formed(&mut self, def_id: DefId) {
         let tcx = self.tcx;
+        let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
+        let item = tcx.hir.expect_item(node_id);
+
         debug!("check_item_well_formed(it.id={}, it.name={})",
                item.id,
-               tcx.item_path_str(tcx.hir.local_def_id(item.id)));
+               tcx.item_path_str(def_id));
 
         match item.node {
             // Right now we check that every default trait implementation
@@ -259,7 +262,8 @@ fn check_type_defn<F>(&mut self, item: &hir::Item, all_sized: bool, mut lookup_f
 
                 // All field types must be well-formed.
                 for field in &variant.fields {
-                    fcx.register_wf_obligation(field.ty, field.span, ObligationCauseCode::MiscObligation)
+                    fcx.register_wf_obligation(field.ty, field.span,
+                        ObligationCauseCode::MiscObligation)
                 }
             }
 
@@ -333,7 +337,8 @@ fn check_impl(&mut self,
                 None => {
                     let self_ty = fcx.tcx.type_of(item_def_id);
                     let self_ty = fcx.normalize_associated_types_in(item.span, &self_ty);
-                    fcx.register_wf_obligation(self_ty, ast_self_ty.span, ObligationCauseCode::MiscObligation);
+                    fcx.register_wf_obligation(self_ty, ast_self_ty.span,
+                        ObligationCauseCode::MiscObligation);
                 }
             }
 
@@ -368,7 +373,8 @@ fn check_where_clauses<'fcx, 'tcx>(&mut self,
             // parameter includes another (e.g., <T, U = T>). In those cases, we can't
             // be sure if it will error or not as user might always specify the other.
             if !ty.needs_subst() {
-                fcx.register_wf_obligation(ty, fcx.tcx.def_span(d), ObligationCauseCode::MiscObligation);
+                fcx.register_wf_obligation(ty, fcx.tcx.def_span(d),
+                    ObligationCauseCode::MiscObligation);
             }
         }
 
@@ -642,6 +648,19 @@ fn reject_shadowing_type_parameters(tcx: TyCtxt, def_id: DefId) {
     }
 }
 
+pub struct CheckTypeWellFormedVisitor<'a, 'tcx: 'a> {
+    tcx: TyCtxt<'a, 'tcx, 'tcx>,
+}
+
+impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
+    pub fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>)
+               -> CheckTypeWellFormedVisitor<'a, 'gcx> {
+        CheckTypeWellFormedVisitor {
+            tcx,
+        }
+    }
+}
+
 impl<'a, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'a, 'tcx> {
     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
         NestedVisitorMap::None
@@ -649,7 +668,8 @@ fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
 
     fn visit_item(&mut self, i: &hir::Item) {
         debug!("visit_item: {:?}", i);
-        self.check_item_well_formed(i);
+        let def_id = self.tcx.hir.local_def_id(i.id);
+        ty::maps::queries::check_item_well_formed::ensure(self.tcx, def_id);
         intravisit::walk_item(self, i);
     }
 
@@ -659,7 +679,8 @@ fn visit_trait_item(&mut self, trait_item: &'v hir::TraitItem) {
             hir::TraitItemKind::Method(ref sig, _) => Some(sig),
             _ => None
         };
-        self.check_associated_item(trait_item.id, trait_item.span, method_sig);
+        CheckTypeWellFormed::new(self.tcx)
+            .check_associated_item(trait_item.id, trait_item.span, method_sig);
         intravisit::walk_trait_item(self, trait_item)
     }
 
@@ -669,7 +690,8 @@ fn visit_impl_item(&mut self, impl_item: &'v hir::ImplItem) {
             hir::ImplItemKind::Method(ref sig, _) => Some(sig),
             _ => None
         };
-        self.check_associated_item(impl_item.id, impl_item.span, method_sig);
+        CheckTypeWellFormed::new(self.tcx)
+            .check_associated_item(impl_item.id, impl_item.span, method_sig);
         intravisit::walk_impl_item(self, impl_item)
     }
 }