about summary refs log tree commit diff
path: root/src/librustc/middle
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2017-08-31 08:07:39 -0700
committerAlex Crichton <alex@alexcrichton.com>2017-09-05 07:37:39 -0700
commit43ae38019167ef02e53cfc202df8b6be0f79c32d (patch)
treedf5a04c15b5911e2f71c2318946cb5e19ded157b /src/librustc/middle
parentfd61fa5aef171e27209f1fad6388f730a64d61a2 (diff)
downloadrust-43ae38019167ef02e53cfc202df8b6be0f79c32d.tar.gz
rust-43ae38019167ef02e53cfc202df8b6be0f79c32d.zip
rustc: Flag some CrateStore methods as "untracked"
The main use of `CrateStore` *before* the `TyCtxt` is created is during
resolution, but we want to be sure that any methods used before resolution are
not used after the `TyCtxt` is created. This commit starts moving the methods
used by resolve to all be named `{name}_untracked` where the rest of the
compiler uses just `{name}` as a query.

During this transition a number of new queries were added to account for
post-resolve usage of these methods.
Diffstat (limited to 'src/librustc/middle')
-rw-r--r--src/librustc/middle/cstore.rs46
-rw-r--r--src/librustc/middle/dependency_format.rs39
-rw-r--r--src/librustc/middle/lang_items.rs4
-rw-r--r--src/librustc/middle/stability.rs5
4 files changed, 52 insertions, 42 deletions
diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs
index 97b04747410..61a923a5954 100644
--- a/src/librustc/middle/cstore.rs
+++ b/src/librustc/middle/cstore.rs
@@ -222,6 +222,14 @@ pub trait MetadataLoader {
 
 /// A store of Rust crates, through with their metadata
 /// can be accessed.
+///
+/// Note that this trait should probably not be expanding today. All new
+/// functionality should be driven through queries instead!
+///
+/// If you find a method on this trait named `{name}_untracked` it signifies
+/// that it's *not* tracked for dependency information throughout compilation
+/// (it'd break incremental compilation) and should only be called pre-HIR (e.g.
+/// during resolve)
 pub trait CrateStore {
     fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>;
 
@@ -229,7 +237,6 @@ pub trait CrateStore {
     fn metadata_loader(&self) -> &MetadataLoader;
 
     // item info
-    fn visibility(&self, def: DefId) -> ty::Visibility;
     fn visible_parent_map<'a>(&'a self, sess: &Session) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
     fn item_generics_cloned(&self, def: DefId) -> ty::Generics;
 
@@ -237,22 +244,24 @@ pub trait CrateStore {
     fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem;
 
     // crate metadata
-    fn dep_kind(&self, cnum: CrateNum) -> DepKind;
-    fn export_macros(&self, cnum: CrateNum);
     fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>;
     fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>;
-    /// The name of the crate as it is referred to in source code of the current
-    /// crate.
-    fn crate_name(&self, cnum: CrateNum) -> Symbol;
 
     // resolve
     fn def_key(&self, def: DefId) -> DefKey;
     fn def_path(&self, def: DefId) -> hir_map::DefPath;
     fn def_path_hash(&self, def: DefId) -> hir_map::DefPathHash;
     fn def_path_table(&self, cnum: CrateNum) -> Rc<DefPathTable>;
-    fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
-    fn item_children(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
-    fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro;
+
+    // "queries" used in resolve that aren't tracked for incremental compilation
+    fn visibility_untracked(&self, def: DefId) -> ty::Visibility;
+    fn export_macros_untracked(&self, cnum: CrateNum);
+    fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind;
+    fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
+    fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name>;
+    fn item_children_untracked(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
+    fn load_macro_untracked(&self, did: DefId, sess: &Session) -> LoadedMacro;
+    fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
 
     // misc. metadata
     fn item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
@@ -265,7 +274,6 @@ pub trait CrateStore {
     // utility functions
     fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>;
     fn used_crate_source(&self, cnum: CrateNum) -> CrateSource;
-    fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
     fn encode_metadata<'a, 'tcx>(&self,
                                  tcx: TyCtxt<'a, 'tcx, 'tcx>,
                                  link_meta: &LinkMeta,
@@ -310,7 +318,7 @@ impl CrateStore for DummyCrateStore {
     fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>
         { bug!("crate_data_as_rc_any") }
     // item info
-    fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
+    fn visibility_untracked(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
     fn visible_parent_map<'a>(&'a self, session: &Session)
         -> ::std::cell::Ref<'a, DefIdMap<DefId>>
     {
@@ -328,9 +336,9 @@ impl CrateStore for DummyCrateStore {
         { bug!("lang_items") }
     fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>
         { bug!("missing_lang_items") }
-    fn dep_kind(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
-    fn export_macros(&self, cnum: CrateNum) { bug!("export_macros") }
-    fn crate_name(&self, cnum: CrateNum) -> Symbol { bug!("crate_name") }
+    fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
+    fn export_macros_untracked(&self, cnum: CrateNum) { bug!("export_macros") }
+    fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol { bug!("crate_name") }
 
     // resolve
     fn def_key(&self, def: DefId) -> DefKey { bug!("def_key") }
@@ -343,11 +351,13 @@ impl CrateStore for DummyCrateStore {
     fn def_path_table(&self, cnum: CrateNum) -> Rc<DefPathTable> {
         bug!("def_path_table")
     }
-    fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
-    fn item_children(&self, did: DefId, sess: &Session) -> Vec<def::Export> {
+    fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name> {
+        bug!("struct_field_names")
+    }
+    fn item_children_untracked(&self, did: DefId, sess: &Session) -> Vec<def::Export> {
         bug!("item_children")
     }
-    fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
+    fn load_macro_untracked(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
 
     // misc. metadata
     fn item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
@@ -363,7 +373,7 @@ impl CrateStore for DummyCrateStore {
     fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
         { vec![] }
     fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") }
-    fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
+    fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
     fn encode_metadata<'a, 'tcx>(&self,
                                  tcx: TyCtxt<'a, 'tcx, 'tcx>,
                                  link_meta: &LinkMeta,
diff --git a/src/librustc/middle/dependency_format.rs b/src/librustc/middle/dependency_format.rs
index 323c069ef0c..a34ef099344 100644
--- a/src/librustc/middle/dependency_format.rs
+++ b/src/librustc/middle/dependency_format.rs
@@ -133,11 +133,11 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                 return v;
             }
             for cnum in sess.cstore.crates() {
-                if sess.cstore.dep_kind(cnum).macros_only() { continue }
+                if tcx.dep_kind(cnum).macros_only() { continue }
                 let src = sess.cstore.used_crate_source(cnum);
                 if src.rlib.is_some() { continue }
                 sess.err(&format!("dependency `{}` not found in rlib format",
-                                  sess.cstore.crate_name(cnum)));
+                                  tcx.crate_name(cnum)));
             }
             return Vec::new();
         }
@@ -166,17 +166,16 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
     // dependencies, ensuring there are no conflicts. The only valid case for a
     // dependency to be relied upon twice is for both cases to rely on a dylib.
     for cnum in sess.cstore.crates() {
-        if sess.cstore.dep_kind(cnum).macros_only() { continue }
-        let name = sess.cstore.crate_name(cnum);
+        if tcx.dep_kind(cnum).macros_only() { continue }
+        let name = tcx.crate_name(cnum);
         let src = sess.cstore.used_crate_source(cnum);
         if src.dylib.is_some() {
             info!("adding dylib: {}", name);
-            add_library(sess, cnum, RequireDynamic, &mut formats);
+            add_library(tcx, cnum, RequireDynamic, &mut formats);
             let deps = tcx.dylib_dependency_formats(cnum);
             for &(depnum, style) in deps.iter() {
-                info!("adding {:?}: {}", style,
-                      sess.cstore.crate_name(depnum));
-                add_library(sess, depnum, style, &mut formats);
+                info!("adding {:?}: {}", style, tcx.crate_name(depnum));
+                add_library(tcx, depnum, style, &mut formats);
             }
         }
     }
@@ -200,10 +199,10 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
         let src = sess.cstore.used_crate_source(cnum);
         if src.dylib.is_none() &&
            !formats.contains_key(&cnum) &&
-           sess.cstore.dep_kind(cnum) == DepKind::Explicit {
+           tcx.dep_kind(cnum) == DepKind::Explicit {
             assert!(src.rlib.is_some() || src.rmeta.is_some());
-            info!("adding staticlib: {}", sess.cstore.crate_name(cnum));
-            add_library(sess, cnum, RequireStatic, &mut formats);
+            info!("adding staticlib: {}", tcx.crate_name(cnum));
+            add_library(tcx, cnum, RequireStatic, &mut formats);
             ret[cnum.as_usize() - 1] = Linkage::Static;
         }
     }
@@ -237,7 +236,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                     Linkage::Static => "rlib",
                     _ => "dylib",
                 };
-                let name = sess.cstore.crate_name(cnum);
+                let name = tcx.crate_name(cnum);
                 sess.err(&format!("crate `{}` required to be available in {}, \
                                   but it was not available in this form",
                                   name, kind));
@@ -248,7 +247,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
     return ret;
 }
 
-fn add_library(sess: &session::Session,
+fn add_library(tcx: TyCtxt,
                cnum: CrateNum,
                link: LinkagePreference,
                m: &mut FxHashMap<CrateNum, LinkagePreference>) {
@@ -262,8 +261,8 @@ fn add_library(sess: &session::Session,
             // This error is probably a little obscure, but I imagine that it
             // can be refined over time.
             if link2 != link || link == RequireStatic {
-                sess.struct_err(&format!("cannot satisfy dependencies so `{}` only \
-                                          shows up once", sess.cstore.crate_name(cnum)))
+                tcx.sess.struct_err(&format!("cannot satisfy dependencies so `{}` only \
+                                              shows up once", tcx.crate_name(cnum)))
                     .help("having upstream crates all available in one format \
                            will likely make this go away")
                     .emit();
@@ -284,7 +283,7 @@ fn attempt_static<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<DependencyLis
     // everything in explicitly so long as it's actually required.
     let last_crate = sess.cstore.crates().len();
     let mut ret = (1..last_crate+1).map(|cnum| {
-        if sess.cstore.dep_kind(CrateNum::new(cnum)) == DepKind::Explicit {
+        if tcx.dep_kind(CrateNum::new(cnum)) == DepKind::Explicit {
             Linkage::Static
         } else {
             Linkage::NotLinked
@@ -357,8 +356,8 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
 
         if tcx.is_panic_runtime(cnum) {
             if let Some((prev, _)) = panic_runtime {
-                let prev_name = sess.cstore.crate_name(prev);
-                let cur_name = sess.cstore.crate_name(cnum);
+                let prev_name = tcx.crate_name(prev);
+                let cur_name = tcx.crate_name(cnum);
                 sess.err(&format!("cannot link together two \
                                    panic runtimes: {} and {}",
                                   prev_name, cur_name));
@@ -379,7 +378,7 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
             sess.err(&format!("the linked panic runtime `{}` is \
                                not compiled with this crate's \
                                panic strategy `{}`",
-                              sess.cstore.crate_name(cnum),
+                              tcx.crate_name(cnum),
                               desired_strategy.desc()));
         }
 
@@ -405,7 +404,7 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
                                panic strategy `{}` which is \
                                incompatible with this crate's \
                                strategy of `{}`",
-                              sess.cstore.crate_name(cnum),
+                              tcx.crate_name(cnum),
                               found_strategy.desc(),
                               desired_strategy.desc()));
         }
diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs
index ae3e3a30f37..351daa198e8 100644
--- a/src/librustc/middle/lang_items.rs
+++ b/src/librustc/middle/lang_items.rs
@@ -179,7 +179,7 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
                         name),
                     None => self.session.struct_err(&format!(
                             "duplicate lang item in crate `{}`: `{}`.",
-                            cstore.crate_name(item_def_id.krate),
+                            cstore.crate_name_untracked(item_def_id.krate),
                             name)),
                 };
                 if let Some(span) = self.hir_map.span_if_local(original_def_id) {
@@ -187,7 +187,7 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
                                "first defined here.");
                 } else {
                     err.note(&format!("first defined in crate `{}`.",
-                                      cstore.crate_name(original_def_id.krate)));
+                                      cstore.crate_name_untracked(original_def_id.krate)));
                 }
                 err.emit();
             }
diff --git a/src/librustc/middle/stability.rs b/src/librustc/middle/stability.rs
index d2ed29a3a0f..6d7d028d286 100644
--- a/src/librustc/middle/stability.rs
+++ b/src/librustc/middle/stability.rs
@@ -476,7 +476,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
             _ => {}
         }
 
-        let visibility = self.sess.cstore.visibility(def_id);
+        let visibility = self.visibility(def_id);
 
         match visibility {
             // must check stability for pub items.
@@ -610,7 +610,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
                 // compiler-generated `extern crate` items have a dummy span.
                 if item.span == DUMMY_SP { return }
 
-                let cnum = match self.tcx.sess.cstore.extern_mod_stmt_cnum(item.id) {
+                let hir_id = self.tcx.hir.node_to_hir_id(item.id);
+                let cnum = match self.tcx.extern_mod_stmt_cnum(hir_id) {
                     Some(cnum) => cnum,
                     None => return,
                 };