Skip to content

Commit 3cb8034

Browse files
committed
Auto merge of #42598 - cramertj:track-more-metadata, r=nikomatsakis
Track more crate metadata Part of #41417 r? @nikomatsakis
2 parents fe7227f + e6dd869 commit 3cb8034

File tree

16 files changed

+122
-93
lines changed

16 files changed

+122
-93
lines changed

src/librustc/dep_graph/dep_node.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ macro_rules! define_dep_nodes {
8181
($(
8282
$variant:ident $(( $($tuple_arg:tt),* ))*
8383
$({ $($struct_arg_name:ident : $struct_arg_ty:ty),* })*
84-
),*
84+
,)*
8585
) => (
8686
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,
8787
RustcEncodable, RustcDecodable)]
@@ -394,6 +394,7 @@ define_dep_nodes!(
394394
ItemSignature(DefId),
395395
ItemVarianceConstraints(DefId),
396396
ItemVariances(DefId),
397+
IsConstFn(DefId),
397398
IsForeignItem(DefId),
398399
TypeParamPredicates { item_id: DefId, param_id: DefId },
399400
SizedConstraint(DefId),
@@ -475,7 +476,11 @@ define_dep_nodes!(
475476
IsExportedSymbol(DefId),
476477
IsMirAvailable(DefId),
477478
ItemAttrs(DefId),
478-
FnArgNames(DefId)
479+
FnArgNames(DefId),
480+
DylibDepFormats(DefId),
481+
IsAllocator(DefId),
482+
IsPanicRuntime(DefId),
483+
ExternCrate(DefId),
479484
);
480485

481486
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> {

src/librustc/hir/def_id.rs

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ impl CrateNum {
5858
pub fn as_u32(&self) -> u32 {
5959
self.0
6060
}
61+
62+
pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
6163
}
6264

6365
impl fmt::Display for CrateNum {

src/librustc/middle/cstore.rs

-13
Original file line numberDiff line numberDiff line change
@@ -243,24 +243,18 @@ pub trait CrateStore {
243243
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem;
244244

245245
// flags
246-
fn is_const_fn(&self, did: DefId) -> bool;
247246
fn is_dllimport_foreign_item(&self, def: DefId) -> bool;
248247
fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool;
249248

250249
// crate metadata
251-
fn dylib_dependency_formats(&self, cnum: CrateNum)
252-
-> Vec<(CrateNum, LinkagePreference)>;
253250
fn dep_kind(&self, cnum: CrateNum) -> DepKind;
254251
fn export_macros(&self, cnum: CrateNum);
255252
fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>;
256253
fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>;
257-
fn is_allocator(&self, cnum: CrateNum) -> bool;
258-
fn is_panic_runtime(&self, cnum: CrateNum) -> bool;
259254
fn is_compiler_builtins(&self, cnum: CrateNum) -> bool;
260255
fn is_sanitizer_runtime(&self, cnum: CrateNum) -> bool;
261256
fn is_profiler_runtime(&self, cnum: CrateNum) -> bool;
262257
fn panic_strategy(&self, cnum: CrateNum) -> PanicStrategy;
263-
fn extern_crate(&self, cnum: CrateNum) -> Option<ExternCrate>;
264258
/// The name of the crate as it is referred to in source code of the current
265259
/// crate.
266260
fn crate_name(&self, cnum: CrateNum) -> Symbol;
@@ -365,29 +359,22 @@ impl CrateStore for DummyCrateStore {
365359
{ bug!("associated_item_cloned") }
366360

367361
// flags
368-
fn is_const_fn(&self, did: DefId) -> bool { bug!("is_const_fn") }
369362
fn is_dllimport_foreign_item(&self, id: DefId) -> bool { false }
370363
fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool { false }
371364

372365
// crate metadata
373-
fn dylib_dependency_formats(&self, cnum: CrateNum)
374-
-> Vec<(CrateNum, LinkagePreference)>
375-
{ bug!("dylib_dependency_formats") }
376366
fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>
377367
{ bug!("lang_items") }
378368
fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>
379369
{ bug!("missing_lang_items") }
380370
fn dep_kind(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
381371
fn export_macros(&self, cnum: CrateNum) { bug!("export_macros") }
382-
fn is_allocator(&self, cnum: CrateNum) -> bool { bug!("is_allocator") }
383-
fn is_panic_runtime(&self, cnum: CrateNum) -> bool { bug!("is_panic_runtime") }
384372
fn is_compiler_builtins(&self, cnum: CrateNum) -> bool { bug!("is_compiler_builtins") }
385373
fn is_profiler_runtime(&self, cnum: CrateNum) -> bool { bug!("is_profiler_runtime") }
386374
fn is_sanitizer_runtime(&self, cnum: CrateNum) -> bool { bug!("is_sanitizer_runtime") }
387375
fn panic_strategy(&self, cnum: CrateNum) -> PanicStrategy {
388376
bug!("panic_strategy")
389377
}
390-
fn extern_crate(&self, cnum: CrateNum) -> Option<ExternCrate> { bug!("extern_crate") }
391378
fn crate_name(&self, cnum: CrateNum) -> Symbol { bug!("crate_name") }
392379
fn original_crate_name(&self, cnum: CrateNum) -> Symbol {
393380
bug!("original_crate_name")

src/librustc/middle/dependency_format.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use hir::def_id::CrateNum;
6565

6666
use session;
6767
use session::config;
68+
use ty::TyCtxt;
6869
use middle::cstore::DepKind;
6970
use middle::cstore::LinkagePreference::{self, RequireStatic, RequireDynamic};
7071
use util::nodemap::FxHashMap;
@@ -91,18 +92,22 @@ pub enum Linkage {
9192
Dynamic,
9293
}
9394

94-
pub fn calculate(sess: &session::Session) {
95+
pub fn calculate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
96+
let sess = &tcx.sess;
9597
let mut fmts = sess.dependency_formats.borrow_mut();
9698
for &ty in sess.crate_types.borrow().iter() {
97-
let linkage = calculate_type(sess, ty);
98-
verify_ok(sess, &linkage);
99+
let linkage = calculate_type(tcx, ty);
100+
verify_ok(tcx, &linkage);
99101
fmts.insert(ty, linkage);
100102
}
101103
sess.abort_if_errors();
102104
}
103105

104-
fn calculate_type(sess: &session::Session,
105-
ty: config::CrateType) -> DependencyList {
106+
fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
107+
ty: config::CrateType) -> DependencyList {
108+
109+
let sess = &tcx.sess;
110+
106111
if !sess.opts.output_types.should_trans() {
107112
return Vec::new();
108113
}
@@ -111,7 +116,7 @@ fn calculate_type(sess: &session::Session,
111116
// If the global prefer_dynamic switch is turned off, first attempt
112117
// static linkage (this can fail).
113118
config::CrateTypeExecutable if !sess.opts.cg.prefer_dynamic => {
114-
if let Some(v) = attempt_static(sess) {
119+
if let Some(v) = attempt_static(tcx) {
115120
return v;
116121
}
117122
}
@@ -124,7 +129,7 @@ fn calculate_type(sess: &session::Session,
124129
// to be found, we generate some nice pretty errors.
125130
config::CrateTypeStaticlib |
126131
config::CrateTypeCdylib => {
127-
if let Some(v) = attempt_static(sess) {
132+
if let Some(v) = attempt_static(tcx) {
128133
return v;
129134
}
130135
for cnum in sess.cstore.crates() {
@@ -141,7 +146,7 @@ fn calculate_type(sess: &session::Session,
141146
// to try to eagerly statically link all dependencies. This is normally
142147
// done for end-product dylibs, not intermediate products.
143148
config::CrateTypeDylib if !sess.opts.cg.prefer_dynamic => {
144-
if let Some(v) = attempt_static(sess) {
149+
if let Some(v) = attempt_static(tcx) {
145150
return v;
146151
}
147152
}
@@ -167,8 +172,8 @@ fn calculate_type(sess: &session::Session,
167172
if src.dylib.is_some() {
168173
info!("adding dylib: {}", name);
169174
add_library(sess, cnum, RequireDynamic, &mut formats);
170-
let deps = sess.cstore.dylib_dependency_formats(cnum);
171-
for &(depnum, style) in &deps {
175+
let deps = tcx.dylib_dependency_formats(cnum.as_def_id());
176+
for &(depnum, style) in deps.iter() {
172177
info!("adding {:?}: {}", style,
173178
sess.cstore.crate_name(depnum));
174179
add_library(sess, depnum, style, &mut formats);
@@ -210,9 +215,9 @@ fn calculate_type(sess: &session::Session,
210215
// Things like allocators and panic runtimes may not have been activated
211216
// quite yet, so do so here.
212217
activate_injected_dep(sess.injected_allocator.get(), &mut ret,
213-
&|cnum| sess.cstore.is_allocator(cnum));
218+
&|cnum| tcx.is_allocator(cnum.as_def_id()));
214219
activate_injected_dep(sess.injected_panic_runtime.get(), &mut ret,
215-
&|cnum| sess.cstore.is_panic_runtime(cnum));
220+
&|cnum| tcx.is_panic_runtime(cnum.as_def_id()));
216221

217222
// When dylib B links to dylib A, then when using B we must also link to A.
218223
// It could be the case, however, that the rlib for A is present (hence we
@@ -269,7 +274,8 @@ fn add_library(sess: &session::Session,
269274
}
270275
}
271276

272-
fn attempt_static(sess: &session::Session) -> Option<DependencyList> {
277+
fn attempt_static<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<DependencyList> {
278+
let sess = &tcx.sess;
273279
let crates = sess.cstore.used_crates(RequireStatic);
274280
if !crates.iter().by_ref().all(|&(_, ref p)| p.is_some()) {
275281
return None
@@ -290,9 +296,9 @@ fn attempt_static(sess: &session::Session) -> Option<DependencyList> {
290296
// explicitly linked, which is the case for any injected dependency. Handle
291297
// that here and activate them.
292298
activate_injected_dep(sess.injected_allocator.get(), &mut ret,
293-
&|cnum| sess.cstore.is_allocator(cnum));
299+
&|cnum| tcx.is_allocator(cnum.as_def_id()));
294300
activate_injected_dep(sess.injected_panic_runtime.get(), &mut ret,
295-
&|cnum| sess.cstore.is_panic_runtime(cnum));
301+
&|cnum| tcx.is_panic_runtime(cnum.as_def_id()));
296302

297303
Some(ret)
298304
}
@@ -327,7 +333,8 @@ fn activate_injected_dep(injected: Option<CrateNum>,
327333

328334
// After the linkage for a crate has been determined we need to verify that
329335
// there's only going to be one allocator in the output.
330-
fn verify_ok(sess: &session::Session, list: &[Linkage]) {
336+
fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
337+
let sess = &tcx.sess;
331338
if list.len() == 0 {
332339
return
333340
}
@@ -338,7 +345,7 @@ fn verify_ok(sess: &session::Session, list: &[Linkage]) {
338345
continue
339346
}
340347
let cnum = CrateNum::new(i + 1);
341-
if sess.cstore.is_allocator(cnum) {
348+
if tcx.is_allocator(cnum.as_def_id()) {
342349
if let Some(prev) = allocator {
343350
let prev_name = sess.cstore.crate_name(prev);
344351
let cur_name = sess.cstore.crate_name(cnum);
@@ -349,7 +356,7 @@ fn verify_ok(sess: &session::Session, list: &[Linkage]) {
349356
allocator = Some(cnum);
350357
}
351358

352-
if sess.cstore.is_panic_runtime(cnum) {
359+
if tcx.is_panic_runtime(cnum.as_def_id()) {
353360
if let Some((prev, _)) = panic_runtime {
354361
let prev_name = sess.cstore.crate_name(prev);
355362
let cur_name = sess.cstore.crate_name(cnum);

src/librustc/ty/item_path.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
100100
//
101101
// Returns `None` for the local crate.
102102
if cnum != LOCAL_CRATE {
103-
let opt_extern_crate = self.sess.cstore.extern_crate(cnum);
103+
let opt_extern_crate = self.extern_crate(cnum.as_def_id());
104104
let opt_extern_crate = opt_extern_crate.and_then(|extern_crate| {
105105
if extern_crate.direct {
106106
Some(extern_crate.def_id)
@@ -136,8 +136,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
136136
// If `cur_def` is a direct or injected extern crate, push the path to the crate
137137
// followed by the path to the item within the crate and return.
138138
if cur_def.index == CRATE_DEF_INDEX {
139-
match self.sess.cstore.extern_crate(cur_def.krate) {
140-
Some(extern_crate) if extern_crate.direct => {
139+
match *self.extern_crate(cur_def) {
140+
Some(ref extern_crate) if extern_crate.direct => {
141141
self.push_item_path(buffer, extern_crate.def_id);
142142
cur_path.iter().rev().map(|segment| buffer.push(&segment.as_str())).count();
143143
return true;

src/librustc/ty/maps.rs

+42
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, LOCAL_CRATE};
1313
use hir::def::Def;
1414
use hir;
1515
use middle::const_val;
16+
use middle::cstore::{ExternCrate, LinkagePreference};
1617
use middle::privacy::AccessLevels;
1718
use middle::region::RegionMaps;
1819
use mir;
@@ -476,6 +477,36 @@ impl<'tcx> QueryDescription for queries::is_object_safe<'tcx> {
476477
}
477478
}
478479

480+
impl<'tcx> QueryDescription for queries::is_const_fn<'tcx> {
481+
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
482+
format!("checking if item is const fn: `{}`", tcx.item_path_str(def_id))
483+
}
484+
}
485+
486+
impl<'tcx> QueryDescription for queries::dylib_dependency_formats<'tcx> {
487+
fn describe(_: TyCtxt, _: DefId) -> String {
488+
"dylib dependency formats of crate".to_string()
489+
}
490+
}
491+
492+
impl<'tcx> QueryDescription for queries::is_allocator<'tcx> {
493+
fn describe(_: TyCtxt, _: DefId) -> String {
494+
"checking if the crate is_allocator".to_string()
495+
}
496+
}
497+
498+
impl<'tcx> QueryDescription for queries::is_panic_runtime<'tcx> {
499+
fn describe(_: TyCtxt, _: DefId) -> String {
500+
"checking if the crate is_panic_runtime".to_string()
501+
}
502+
}
503+
504+
impl<'tcx> QueryDescription for queries::extern_crate<'tcx> {
505+
fn describe(_: TyCtxt, _: DefId) -> String {
506+
"getting crate's ExternCrateData".to_string()
507+
}
508+
}
509+
479510
macro_rules! define_maps {
480511
(<$tcx:tt>
481512
$($(#[$attr:meta])*
@@ -791,6 +822,9 @@ define_maps! { <'tcx>
791822
[] adt_sized_constraint: SizedConstraint(DefId) -> &'tcx [Ty<'tcx>],
792823
[] adt_dtorck_constraint: DtorckConstraint(DefId) -> ty::DtorckConstraint<'tcx>,
793824

825+
/// True if this is a const fn
826+
[] is_const_fn: IsConstFn(DefId) -> bool,
827+
794828
/// True if this is a foreign item (i.e., linked via `extern { ... }`).
795829
[] is_foreign_item: IsForeignItem(DefId) -> bool,
796830

@@ -929,6 +963,14 @@ define_maps! { <'tcx>
929963
[] needs_drop_raw: needs_drop_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
930964
[] layout_raw: layout_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
931965
-> Result<&'tcx Layout, LayoutError<'tcx>>,
966+
967+
[] dylib_dependency_formats: DylibDepFormats(DefId)
968+
-> Rc<Vec<(CrateNum, LinkagePreference)>>,
969+
970+
[] is_allocator: IsAllocator(DefId) -> bool,
971+
[] is_panic_runtime: IsPanicRuntime(DefId) -> bool,
972+
973+
[] extern_crate: ExternCrate(DefId) -> Rc<Option<ExternCrate>>,
932974
}
933975

934976
fn type_param_predicates((item_id, param_id): (DefId, DefId)) -> DepConstructor {

src/librustc_const_eval/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ fn eval_const_expr_partial<'a, 'tcx>(cx: &ConstContext<'a, 'tcx>,
351351
signal!(e, TypeckError)
352352
}
353353
} else {
354-
if tcx.sess.cstore.is_const_fn(def_id) {
354+
if tcx.is_const_fn(def_id) {
355355
tcx.sess.cstore.item_body(tcx, def_id)
356356
} else {
357357
signal!(e, TypeckError)

src/librustc_driver/driver.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
900900
reachable::provide(&mut local_providers);
901901
rustc_const_eval::provide(&mut local_providers);
902902
middle::region::provide(&mut local_providers);
903+
cstore::provide_local(&mut local_providers);
903904

904905
let mut extern_providers = ty::maps::Providers::default();
905906
cstore::provide(&mut extern_providers);
@@ -1050,7 +1051,7 @@ pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10501051

10511052
time(time_passes,
10521053
"resolving dependency formats",
1053-
|| dependency_format::calculate(&tcx.sess));
1054+
|| dependency_format::calculate(tcx));
10541055

10551056
let translation =
10561057
time(time_passes,

src/librustc_metadata/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub use rustc::middle::cstore::{NativeLibrary, NativeLibraryKind, LinkagePrefere
3434
pub use rustc::middle::cstore::NativeLibraryKind::*;
3535
pub use rustc::middle::cstore::{CrateSource, LinkMeta, LibSource};
3636

37-
pub use cstore_impl::provide;
37+
pub use cstore_impl::{provide, provide_local};
3838

3939
// A map from external crate numbers (as decoded from some crate file) to
4040
// local crate numbers (as generated during this session). Each external

0 commit comments

Comments
 (0)