Skip to content

Commit 43ae380

Browse files
committed
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.
1 parent fd61fa5 commit 43ae380

File tree

27 files changed

+152
-99
lines changed

27 files changed

+152
-99
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,12 @@ define_dep_nodes!( <'tcx>
554554
[] NamedRegion(HirId),
555555
[] IsLateBound(HirId),
556556
[] ObjectLifetimeDefaults(HirId),
557+
558+
[] Visibility(DefId),
559+
[] DepKind(CrateNum),
560+
[] CrateName(CrateNum),
561+
[] ItemChildren(DefId),
562+
[] ExternModStmtCnum(HirId),
557563
);
558564

559565
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
357357
// for imported and non-imported crates
358358
if exp_path == found_path
359359
|| exp_abs_path == found_abs_path {
360-
let crate_name = self.tcx.sess.cstore.crate_name(did1.krate);
360+
let crate_name = self.tcx.crate_name(did1.krate);
361361
err.span_note(sp, &format!("Perhaps two different versions \
362362
of crate `{}` are being used?",
363363
crate_name));

src/librustc/middle/cstore.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -222,37 +222,46 @@ pub trait MetadataLoader {
222222

223223
/// A store of Rust crates, through with their metadata
224224
/// can be accessed.
225+
///
226+
/// Note that this trait should probably not be expanding today. All new
227+
/// functionality should be driven through queries instead!
228+
///
229+
/// If you find a method on this trait named `{name}_untracked` it signifies
230+
/// that it's *not* tracked for dependency information throughout compilation
231+
/// (it'd break incremental compilation) and should only be called pre-HIR (e.g.
232+
/// during resolve)
225233
pub trait CrateStore {
226234
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>;
227235

228236
// access to the metadata loader
229237
fn metadata_loader(&self) -> &MetadataLoader;
230238

231239
// item info
232-
fn visibility(&self, def: DefId) -> ty::Visibility;
233240
fn visible_parent_map<'a>(&'a self, sess: &Session) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
234241
fn item_generics_cloned(&self, def: DefId) -> ty::Generics;
235242

236243
// trait/impl-item info
237244
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem;
238245

239246
// crate metadata
240-
fn dep_kind(&self, cnum: CrateNum) -> DepKind;
241-
fn export_macros(&self, cnum: CrateNum);
242247
fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>;
243248
fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>;
244-
/// The name of the crate as it is referred to in source code of the current
245-
/// crate.
246-
fn crate_name(&self, cnum: CrateNum) -> Symbol;
247249

248250
// resolve
249251
fn def_key(&self, def: DefId) -> DefKey;
250252
fn def_path(&self, def: DefId) -> hir_map::DefPath;
251253
fn def_path_hash(&self, def: DefId) -> hir_map::DefPathHash;
252254
fn def_path_table(&self, cnum: CrateNum) -> Rc<DefPathTable>;
253-
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
254-
fn item_children(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
255-
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro;
255+
256+
// "queries" used in resolve that aren't tracked for incremental compilation
257+
fn visibility_untracked(&self, def: DefId) -> ty::Visibility;
258+
fn export_macros_untracked(&self, cnum: CrateNum);
259+
fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind;
260+
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
261+
fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name>;
262+
fn item_children_untracked(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
263+
fn load_macro_untracked(&self, did: DefId, sess: &Session) -> LoadedMacro;
264+
fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
256265

257266
// misc. metadata
258267
fn item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
@@ -265,7 +274,6 @@ pub trait CrateStore {
265274
// utility functions
266275
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>;
267276
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource;
268-
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
269277
fn encode_metadata<'a, 'tcx>(&self,
270278
tcx: TyCtxt<'a, 'tcx, 'tcx>,
271279
link_meta: &LinkMeta,
@@ -310,7 +318,7 @@ impl CrateStore for DummyCrateStore {
310318
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>
311319
{ bug!("crate_data_as_rc_any") }
312320
// item info
313-
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
321+
fn visibility_untracked(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
314322
fn visible_parent_map<'a>(&'a self, session: &Session)
315323
-> ::std::cell::Ref<'a, DefIdMap<DefId>>
316324
{
@@ -328,9 +336,9 @@ impl CrateStore for DummyCrateStore {
328336
{ bug!("lang_items") }
329337
fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>
330338
{ bug!("missing_lang_items") }
331-
fn dep_kind(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
332-
fn export_macros(&self, cnum: CrateNum) { bug!("export_macros") }
333-
fn crate_name(&self, cnum: CrateNum) -> Symbol { bug!("crate_name") }
339+
fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
340+
fn export_macros_untracked(&self, cnum: CrateNum) { bug!("export_macros") }
341+
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol { bug!("crate_name") }
334342

335343
// resolve
336344
fn def_key(&self, def: DefId) -> DefKey { bug!("def_key") }
@@ -343,11 +351,13 @@ impl CrateStore for DummyCrateStore {
343351
fn def_path_table(&self, cnum: CrateNum) -> Rc<DefPathTable> {
344352
bug!("def_path_table")
345353
}
346-
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
347-
fn item_children(&self, did: DefId, sess: &Session) -> Vec<def::Export> {
354+
fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name> {
355+
bug!("struct_field_names")
356+
}
357+
fn item_children_untracked(&self, did: DefId, sess: &Session) -> Vec<def::Export> {
348358
bug!("item_children")
349359
}
350-
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
360+
fn load_macro_untracked(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
351361

352362
// misc. metadata
353363
fn item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
@@ -363,7 +373,7 @@ impl CrateStore for DummyCrateStore {
363373
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
364374
{ vec![] }
365375
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") }
366-
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
376+
fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
367377
fn encode_metadata<'a, 'tcx>(&self,
368378
tcx: TyCtxt<'a, 'tcx, 'tcx>,
369379
link_meta: &LinkMeta,

src/librustc/middle/dependency_format.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
133133
return v;
134134
}
135135
for cnum in sess.cstore.crates() {
136-
if sess.cstore.dep_kind(cnum).macros_only() { continue }
136+
if tcx.dep_kind(cnum).macros_only() { continue }
137137
let src = sess.cstore.used_crate_source(cnum);
138138
if src.rlib.is_some() { continue }
139139
sess.err(&format!("dependency `{}` not found in rlib format",
140-
sess.cstore.crate_name(cnum)));
140+
tcx.crate_name(cnum)));
141141
}
142142
return Vec::new();
143143
}
@@ -166,17 +166,16 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
166166
// dependencies, ensuring there are no conflicts. The only valid case for a
167167
// dependency to be relied upon twice is for both cases to rely on a dylib.
168168
for cnum in sess.cstore.crates() {
169-
if sess.cstore.dep_kind(cnum).macros_only() { continue }
170-
let name = sess.cstore.crate_name(cnum);
169+
if tcx.dep_kind(cnum).macros_only() { continue }
170+
let name = tcx.crate_name(cnum);
171171
let src = sess.cstore.used_crate_source(cnum);
172172
if src.dylib.is_some() {
173173
info!("adding dylib: {}", name);
174-
add_library(sess, cnum, RequireDynamic, &mut formats);
174+
add_library(tcx, cnum, RequireDynamic, &mut formats);
175175
let deps = tcx.dylib_dependency_formats(cnum);
176176
for &(depnum, style) in deps.iter() {
177-
info!("adding {:?}: {}", style,
178-
sess.cstore.crate_name(depnum));
179-
add_library(sess, depnum, style, &mut formats);
177+
info!("adding {:?}: {}", style, tcx.crate_name(depnum));
178+
add_library(tcx, depnum, style, &mut formats);
180179
}
181180
}
182181
}
@@ -200,10 +199,10 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
200199
let src = sess.cstore.used_crate_source(cnum);
201200
if src.dylib.is_none() &&
202201
!formats.contains_key(&cnum) &&
203-
sess.cstore.dep_kind(cnum) == DepKind::Explicit {
202+
tcx.dep_kind(cnum) == DepKind::Explicit {
204203
assert!(src.rlib.is_some() || src.rmeta.is_some());
205-
info!("adding staticlib: {}", sess.cstore.crate_name(cnum));
206-
add_library(sess, cnum, RequireStatic, &mut formats);
204+
info!("adding staticlib: {}", tcx.crate_name(cnum));
205+
add_library(tcx, cnum, RequireStatic, &mut formats);
207206
ret[cnum.as_usize() - 1] = Linkage::Static;
208207
}
209208
}
@@ -237,7 +236,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
237236
Linkage::Static => "rlib",
238237
_ => "dylib",
239238
};
240-
let name = sess.cstore.crate_name(cnum);
239+
let name = tcx.crate_name(cnum);
241240
sess.err(&format!("crate `{}` required to be available in {}, \
242241
but it was not available in this form",
243242
name, kind));
@@ -248,7 +247,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
248247
return ret;
249248
}
250249

251-
fn add_library(sess: &session::Session,
250+
fn add_library(tcx: TyCtxt,
252251
cnum: CrateNum,
253252
link: LinkagePreference,
254253
m: &mut FxHashMap<CrateNum, LinkagePreference>) {
@@ -262,8 +261,8 @@ fn add_library(sess: &session::Session,
262261
// This error is probably a little obscure, but I imagine that it
263262
// can be refined over time.
264263
if link2 != link || link == RequireStatic {
265-
sess.struct_err(&format!("cannot satisfy dependencies so `{}` only \
266-
shows up once", sess.cstore.crate_name(cnum)))
264+
tcx.sess.struct_err(&format!("cannot satisfy dependencies so `{}` only \
265+
shows up once", tcx.crate_name(cnum)))
267266
.help("having upstream crates all available in one format \
268267
will likely make this go away")
269268
.emit();
@@ -284,7 +283,7 @@ fn attempt_static<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<DependencyLis
284283
// everything in explicitly so long as it's actually required.
285284
let last_crate = sess.cstore.crates().len();
286285
let mut ret = (1..last_crate+1).map(|cnum| {
287-
if sess.cstore.dep_kind(CrateNum::new(cnum)) == DepKind::Explicit {
286+
if tcx.dep_kind(CrateNum::new(cnum)) == DepKind::Explicit {
288287
Linkage::Static
289288
} else {
290289
Linkage::NotLinked
@@ -357,8 +356,8 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
357356

358357
if tcx.is_panic_runtime(cnum) {
359358
if let Some((prev, _)) = panic_runtime {
360-
let prev_name = sess.cstore.crate_name(prev);
361-
let cur_name = sess.cstore.crate_name(cnum);
359+
let prev_name = tcx.crate_name(prev);
360+
let cur_name = tcx.crate_name(cnum);
362361
sess.err(&format!("cannot link together two \
363362
panic runtimes: {} and {}",
364363
prev_name, cur_name));
@@ -379,7 +378,7 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
379378
sess.err(&format!("the linked panic runtime `{}` is \
380379
not compiled with this crate's \
381380
panic strategy `{}`",
382-
sess.cstore.crate_name(cnum),
381+
tcx.crate_name(cnum),
383382
desired_strategy.desc()));
384383
}
385384

@@ -405,7 +404,7 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
405404
panic strategy `{}` which is \
406405
incompatible with this crate's \
407406
strategy of `{}`",
408-
sess.cstore.crate_name(cnum),
407+
tcx.crate_name(cnum),
409408
found_strategy.desc(),
410409
desired_strategy.desc()));
411410
}

src/librustc/middle/lang_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
179179
name),
180180
None => self.session.struct_err(&format!(
181181
"duplicate lang item in crate `{}`: `{}`.",
182-
cstore.crate_name(item_def_id.krate),
182+
cstore.crate_name_untracked(item_def_id.krate),
183183
name)),
184184
};
185185
if let Some(span) = self.hir_map.span_if_local(original_def_id) {
186186
span_note!(&mut err, span,
187187
"first defined here.");
188188
} else {
189189
err.note(&format!("first defined in crate `{}`.",
190-
cstore.crate_name(original_def_id.krate)));
190+
cstore.crate_name_untracked(original_def_id.krate)));
191191
}
192192
err.emit();
193193
}

src/librustc/middle/stability.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
476476
_ => {}
477477
}
478478

479-
let visibility = self.sess.cstore.visibility(def_id);
479+
let visibility = self.visibility(def_id);
480480

481481
match visibility {
482482
// must check stability for pub items.
@@ -610,7 +610,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
610610
// compiler-generated `extern crate` items have a dummy span.
611611
if item.span == DUMMY_SP { return }
612612

613-
let cnum = match self.tcx.sess.cstore.extern_mod_stmt_cnum(item.id) {
613+
let hir_id = self.tcx.hir.node_to_hir_id(item.id);
614+
let cnum = match self.tcx.extern_mod_stmt_cnum(hir_id) {
614615
Some(cnum) => cnum,
615616
None => return,
616617
};

src/librustc/ty/context.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -909,14 +909,6 @@ impl<'tcx> GlobalCtxt<'tcx> {
909909
}
910910

911911
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
912-
pub fn crate_name(self, cnum: CrateNum) -> Symbol {
913-
if cnum == LOCAL_CRATE {
914-
self.crate_name
915-
} else {
916-
self.sess.cstore.crate_name(cnum)
917-
}
918-
}
919-
920912
pub fn alloc_generics(self, generics: ty::Generics) -> &'gcx ty::Generics {
921913
self.global_arenas.generics.alloc(generics)
922914
}
@@ -2008,4 +2000,8 @@ pub fn provide(providers: &mut ty::maps::Providers) {
20082000
providers.object_lifetime_defaults = |tcx, id| {
20092001
tcx.gcx.named_region_map.object_lifetime_defaults.get(&id).cloned()
20102002
};
2003+
providers.crate_name = |tcx, id| {
2004+
assert_eq!(id, LOCAL_CRATE);
2005+
tcx.crate_name
2006+
};
20112007
}

src/librustc/ty/maps.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use hir::svh::Svh;
1717
use lint;
1818
use middle::const_val;
1919
use middle::cstore::{ExternCrate, LinkagePreference, NativeLibrary};
20-
use middle::cstore::NativeLibraryKind;
20+
use middle::cstore::{NativeLibraryKind, DepKind};
2121
use middle::privacy::AccessLevels;
2222
use middle::region;
2323
use middle::region::RegionMaps;
@@ -669,6 +669,24 @@ impl<'tcx> QueryDescription for queries::object_lifetime_defaults<'tcx> {
669669
}
670670
}
671671

672+
impl<'tcx> QueryDescription for queries::dep_kind<'tcx> {
673+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
674+
format!("fetching what a dependency looks like")
675+
}
676+
}
677+
678+
impl<'tcx> QueryDescription for queries::crate_name<'tcx> {
679+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
680+
format!("fetching what a crate is named")
681+
}
682+
}
683+
684+
impl<'tcx> QueryDescription for queries::extern_mod_stmt_cnum<'tcx> {
685+
fn describe(_tcx: TyCtxt, _: HirId) -> String {
686+
format!("looking up the CrateNum for an `extern mod` statement")
687+
}
688+
}
689+
672690
// If enabled, send a message to the profile-queries thread
673691
macro_rules! profq_msg {
674692
($tcx:expr, $msg:expr) => {
@@ -1268,6 +1286,12 @@ define_maps! { <'tcx>
12681286
[] is_late_bound: IsLateBound(HirId) -> bool,
12691287
[] object_lifetime_defaults: ObjectLifetimeDefaults(HirId)
12701288
-> Option<Rc<Vec<ObjectLifetimeDefault>>>,
1289+
1290+
[] visibility: Visibility(DefId) -> ty::Visibility,
1291+
[] dep_kind: DepKind(CrateNum) -> DepKind,
1292+
[] crate_name: CrateName(CrateNum) -> Symbol,
1293+
[] item_children: ItemChildren(DefId) -> Rc<Vec<Export>>,
1294+
[] extern_mod_stmt_cnum: ExternModStmtCnum(HirId) -> Option<CrateNum>,
12711295
}
12721296

12731297
fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2315,7 +2315,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23152315
let node_id = self.hir.as_local_node_id(impl_did).unwrap();
23162316
Ok(self.hir.span(node_id))
23172317
} else {
2318-
Err(self.sess.cstore.crate_name(impl_did.krate))
2318+
Err(self.crate_name(impl_did.krate))
23192319
}
23202320
}
23212321

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PluginAsLibrary {
10631063
_ => return,
10641064
};
10651065

1066-
let prfn = match cx.sess().cstore.extern_mod_stmt_cnum(it.id) {
1066+
let hir_id = cx.tcx.hir.node_to_hir_id(it.id);
1067+
let prfn = match cx.tcx.extern_mod_stmt_cnum(hir_id) {
10671068
Some(cnum) => cx.tcx.plugin_registrar_fn(cnum),
10681069
None => {
10691070
// Probably means we aren't linking the crate for some reason.

0 commit comments

Comments
 (0)