From c98ca953b07c2afb8dd7ceab1215249e94c3025d Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Wed, 14 Jun 2017 22:49:07 -0700 Subject: [PATCH] Switch CrateNum queries to DefId --- src/librustc/dep_graph/dep_node.rs | 1 - src/librustc/hir/def_id.rs | 2 + src/librustc/middle/dependency_format.rs | 14 +- src/librustc/ty/item_path.rs | 4 +- src/librustc/ty/maps.rs | 16 +-- src/librustc_metadata/cstore_impl.rs | 169 ++++++++++------------- src/librustc_save_analysis/lib.rs | 2 +- src/librustc_trans/back/symbol_export.rs | 4 +- 8 files changed, 96 insertions(+), 116 deletions(-) diff --git a/src/librustc/dep_graph/dep_node.rs b/src/librustc/dep_graph/dep_node.rs index 6c4c90585b3c..c987a37c420d 100644 --- a/src/librustc/dep_graph/dep_node.rs +++ b/src/librustc/dep_graph/dep_node.rs @@ -305,7 +305,6 @@ define_dep_nodes!( // Represents the metadata for a given HIR node, typically found // in an extern crate. MetaData(DefId), - MetaDataByCrateNum(CrateNum), // Represents some artifact that we save to disk. Note that these // do not have a def-id as part of their identifier. diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs index ce2baa738975..95a27f065999 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -58,6 +58,8 @@ impl CrateNum { pub fn as_u32(&self) -> u32 { self.0 } + + pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } } } impl fmt::Display for CrateNum { diff --git a/src/librustc/middle/dependency_format.rs b/src/librustc/middle/dependency_format.rs index da217d7a1733..4e1f06cca06c 100644 --- a/src/librustc/middle/dependency_format.rs +++ b/src/librustc/middle/dependency_format.rs @@ -172,7 +172,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, if src.dylib.is_some() { info!("adding dylib: {}", name); add_library(sess, cnum, RequireDynamic, &mut formats); - let deps = tcx.dylib_dependency_formats(cnum); + let deps = tcx.dylib_dependency_formats(cnum.as_def_id()); for &(depnum, style) in deps.iter() { info!("adding {:?}: {}", style, sess.cstore.crate_name(depnum)); @@ -215,9 +215,9 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, // Things like allocators and panic runtimes may not have been activated // quite yet, so do so here. activate_injected_dep(sess.injected_allocator.get(), &mut ret, - &|cnum| tcx.is_allocator(cnum)); + &|cnum| tcx.is_allocator(cnum.as_def_id())); activate_injected_dep(sess.injected_panic_runtime.get(), &mut ret, - &|cnum| tcx.is_panic_runtime(cnum)); + &|cnum| tcx.is_panic_runtime(cnum.as_def_id())); // When dylib B links to dylib A, then when using B we must also link to A. // It could be the case, however, that the rlib for A is present (hence we @@ -296,9 +296,9 @@ fn attempt_static<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) { continue } let cnum = CrateNum::new(i + 1); - if tcx.is_allocator(cnum) { + if tcx.is_allocator(cnum.as_def_id()) { if let Some(prev) = allocator { let prev_name = sess.cstore.crate_name(prev); let cur_name = sess.cstore.crate_name(cnum); @@ -356,7 +356,7 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) { allocator = Some(cnum); } - if tcx.is_panic_runtime(cnum) { + if tcx.is_panic_runtime(cnum.as_def_id()) { if let Some((prev, _)) = panic_runtime { let prev_name = sess.cstore.crate_name(prev); let cur_name = sess.cstore.crate_name(cnum); diff --git a/src/librustc/ty/item_path.rs b/src/librustc/ty/item_path.rs index e63fe42877fe..09a3bcd06138 100644 --- a/src/librustc/ty/item_path.rs +++ b/src/librustc/ty/item_path.rs @@ -100,7 +100,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { // // Returns `None` for the local crate. if cnum != LOCAL_CRATE { - let opt_extern_crate = self.extern_crate(cnum); + let opt_extern_crate = self.extern_crate(cnum.as_def_id()); let opt_extern_crate = opt_extern_crate.and_then(|extern_crate| { if extern_crate.direct { Some(extern_crate.def_id) @@ -136,7 +136,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { // If `cur_def` is a direct or injected extern crate, push the path to the crate // followed by the path to the item within the crate and return. if cur_def.index == CRATE_DEF_INDEX { - match *self.extern_crate(cur_def.krate) { + match *self.extern_crate(cur_def) { Some(ref extern_crate) if extern_crate.direct => { self.push_item_path(buffer, extern_crate.def_id); cur_path.iter().rev().map(|segment| buffer.push(&segment.as_str())).count(); diff --git a/src/librustc/ty/maps.rs b/src/librustc/ty/maps.rs index 27713f728ac5..dccc0a8283a9 100644 --- a/src/librustc/ty/maps.rs +++ b/src/librustc/ty/maps.rs @@ -484,25 +484,25 @@ impl<'tcx> QueryDescription for queries::is_const_fn<'tcx> { } impl<'tcx> QueryDescription for queries::dylib_dependency_formats<'tcx> { - fn describe(_: TyCtxt, _: CrateNum) -> String { + fn describe(_: TyCtxt, _: DefId) -> String { "dylib dependency formats of crate".to_string() } } impl<'tcx> QueryDescription for queries::is_allocator<'tcx> { - fn describe(_: TyCtxt, _: CrateNum) -> String { + fn describe(_: TyCtxt, _: DefId) -> String { "checking if the crate is_allocator".to_string() } } impl<'tcx> QueryDescription for queries::is_panic_runtime<'tcx> { - fn describe(_: TyCtxt, _: CrateNum) -> String { + fn describe(_: TyCtxt, _: DefId) -> String { "checking if the crate is_panic_runtime".to_string() } } impl<'tcx> QueryDescription for queries::extern_crate<'tcx> { - fn describe(_: TyCtxt, _: CrateNum) -> String { + fn describe(_: TyCtxt, _: DefId) -> String { "getting crate's ExternCrateData".to_string() } } @@ -964,13 +964,13 @@ define_maps! { <'tcx> [] layout_raw: layout_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> Result<&'tcx Layout, LayoutError<'tcx>>, - [] dylib_dependency_formats: MetaDataByCrateNum(CrateNum) + [] dylib_dependency_formats: MetaData(DefId) -> Rc>, - [] is_allocator: MetaDataByCrateNum(CrateNum) -> bool, - [] is_panic_runtime: MetaDataByCrateNum(CrateNum) -> bool, + [] is_allocator: MetaData(DefId) -> bool, + [] is_panic_runtime: MetaData(DefId) -> bool, - [] extern_crate: MetaDataByCrateNum(CrateNum) -> Rc>, + [] extern_crate: MetaData(DefId) -> Rc>, } fn type_param_predicates((item_id, param_id): (DefId, DefId)) -> DepConstructor { diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index 9522d9625c94..7e85c2779799 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -40,17 +40,10 @@ use rustc::hir::svh::Svh; use rustc::hir; macro_rules! provide { - (<$lt:tt> $tcx:ident, $def_id:ident, $cdata:ident, $cnum:ident, - ByDefId { - $($cdata_fn_name:ident => $cdata_fn_compute:block)* - } - ByCrateNum { - $($cnum_fn_name:ident => $cnum_fn_compute:block)* - } - )=> { + (<$lt:tt> $tcx:ident, $def_id:ident, $cdata:ident, $($name:ident => $compute:block)*) => { pub fn provide<$lt>(providers: &mut Providers<$lt>) { - $(fn $cdata_fn_name<'a, $lt:$lt>($tcx: TyCtxt<'a, $lt, $lt>, $def_id: DefId) - -> as + $(fn $name<'a, $lt:$lt>($tcx: TyCtxt<'a, $lt, $lt>, $def_id: DefId) + -> as DepTrackingMapConfig>::Value { assert!(!$def_id.is_local()); @@ -62,102 +55,88 @@ macro_rules! provide { let $cdata = $tcx.sess.cstore.crate_data_as_rc_any($def_id.krate); let $cdata = $cdata.downcast_ref::() .expect("CrateStore crated ata is not a CrateMetadata"); - $cdata_fn_compute - })* - - $(fn $cnum_fn_name<'a, $lt:$lt>($tcx: TyCtxt<'a, $lt, $lt>, $cnum: CrateNum) - -> as - DepTrackingMapConfig>::Value { - let $cdata = $tcx.sess.cstore.crate_data_as_rc_any($cnum); - let $cdata = $cdata.downcast_ref::() - .expect("CrateStore crated ata is not a CrateMetadata"); - $cnum_fn_compute + $compute })* *providers = Providers { - $($cdata_fn_name,)* - $($cnum_fn_name,)* + $($name,)* ..*providers }; } } } -provide! { <'tcx> tcx, def_id, cdata, cnum, - ByDefId { - type_of => { cdata.get_type(def_id.index, tcx) } - generics_of => { tcx.alloc_generics(cdata.get_generics(def_id.index)) } - predicates_of => { cdata.get_predicates(def_id.index, tcx) } - super_predicates_of => { cdata.get_super_predicates(def_id.index, tcx) } - trait_def => { - tcx.alloc_trait_def(cdata.get_trait_def(def_id.index)) - } - adt_def => { cdata.get_adt_def(def_id.index, tcx) } - adt_destructor => { - let _ = cdata; - tcx.calculate_dtor(def_id, &mut |_,_| Ok(())) - } - variances_of => { Rc::new(cdata.get_item_variances(def_id.index)) } - associated_item_def_ids => { - let mut result = vec![]; - cdata.each_child_of_item(def_id.index, - |child| result.push(child.def.def_id()), tcx.sess); - Rc::new(result) - } - associated_item => { cdata.get_associated_item(def_id.index) } - impl_trait_ref => { cdata.get_impl_trait(def_id.index, tcx) } - impl_polarity => { cdata.get_impl_polarity(def_id.index) } - coerce_unsized_info => { - cdata.get_coerce_unsized_info(def_id.index).unwrap_or_else(|| { - bug!("coerce_unsized_info: `{:?}` is missing its info", def_id); - }) - } - optimized_mir => { - let mir = cdata.maybe_get_optimized_mir(tcx, def_id.index).unwrap_or_else(|| { - bug!("get_optimized_mir: missing MIR for `{:?}`", def_id) - }); - - let mir = tcx.alloc_mir(mir); - - mir - } - mir_const_qualif => { cdata.mir_const_qualif(def_id.index) } - typeck_tables_of => { cdata.item_body_tables(def_id.index, tcx) } - closure_kind => { cdata.closure_kind(def_id.index) } - closure_type => { cdata.closure_ty(def_id.index, tcx) } - inherent_impls => { Rc::new(cdata.get_inherent_implementations_for_type(def_id.index)) } - is_const_fn => { cdata.is_const_fn(def_id.index) } - is_foreign_item => { cdata.is_foreign_item(def_id.index) } - is_default_impl => { cdata.is_default_impl(def_id.index) } - describe_def => { cdata.get_def(def_id.index) } - def_span => { cdata.get_span(def_id.index, &tcx.sess) } - stability => { cdata.get_stability(def_id.index) } - deprecation => { cdata.get_deprecation(def_id.index) } - item_attrs => { cdata.get_item_attrs(def_id.index, &tcx.dep_graph) } - // FIXME(#38501) We've skipped a `read` on the `HirBody` of - // a `fn` when encoding, so the dep-tracking wouldn't work. - // This is only used by rustdoc anyway, which shouldn't have - // incremental recompilation ever enabled. - fn_arg_names => { cdata.get_fn_arg_names(def_id.index) } - impl_parent => { cdata.get_parent_impl(def_id.index) } - trait_of_item => { cdata.get_trait_of_item(def_id.index) } - is_exported_symbol => { - let dep_node = cdata.metadata_dep_node(GlobalMetaDataKind::ExportedSymbols); - cdata.exported_symbols.get(&tcx.dep_graph, dep_node).contains(&def_id.index) - } - item_body_nested_bodies => { Rc::new(cdata.item_body_nested_bodies(def_id.index)) } - const_is_rvalue_promotable_to_static => { - cdata.const_is_rvalue_promotable_to_static(def_id.index) - } - is_mir_available => { cdata.is_item_mir_available(def_id.index) } +provide! { <'tcx> tcx, def_id, cdata, + type_of => { cdata.get_type(def_id.index, tcx) } + generics_of => { tcx.alloc_generics(cdata.get_generics(def_id.index)) } + predicates_of => { cdata.get_predicates(def_id.index, tcx) } + super_predicates_of => { cdata.get_super_predicates(def_id.index, tcx) } + trait_def => { + tcx.alloc_trait_def(cdata.get_trait_def(def_id.index)) } - - ByCrateNum { - dylib_dependency_formats => { Rc::new(cdata.get_dylib_dependency_formats(&tcx.dep_graph)) } - is_allocator => { cdata.is_allocator(&tcx.dep_graph) } - is_panic_runtime => { cdata.is_panic_runtime(&tcx.dep_graph) } - extern_crate => { Rc::new(cdata.extern_crate.get()) } + adt_def => { cdata.get_adt_def(def_id.index, tcx) } + adt_destructor => { + let _ = cdata; + tcx.calculate_dtor(def_id, &mut |_,_| Ok(())) } + variances_of => { Rc::new(cdata.get_item_variances(def_id.index)) } + associated_item_def_ids => { + let mut result = vec![]; + cdata.each_child_of_item(def_id.index, + |child| result.push(child.def.def_id()), tcx.sess); + Rc::new(result) + } + associated_item => { cdata.get_associated_item(def_id.index) } + impl_trait_ref => { cdata.get_impl_trait(def_id.index, tcx) } + impl_polarity => { cdata.get_impl_polarity(def_id.index) } + coerce_unsized_info => { + cdata.get_coerce_unsized_info(def_id.index).unwrap_or_else(|| { + bug!("coerce_unsized_info: `{:?}` is missing its info", def_id); + }) + } + optimized_mir => { + let mir = cdata.maybe_get_optimized_mir(tcx, def_id.index).unwrap_or_else(|| { + bug!("get_optimized_mir: missing MIR for `{:?}`", def_id) + }); + + let mir = tcx.alloc_mir(mir); + + mir + } + mir_const_qualif => { cdata.mir_const_qualif(def_id.index) } + typeck_tables_of => { cdata.item_body_tables(def_id.index, tcx) } + closure_kind => { cdata.closure_kind(def_id.index) } + closure_type => { cdata.closure_ty(def_id.index, tcx) } + inherent_impls => { Rc::new(cdata.get_inherent_implementations_for_type(def_id.index)) } + is_const_fn => { cdata.is_const_fn(def_id.index) } + is_foreign_item => { cdata.is_foreign_item(def_id.index) } + is_default_impl => { cdata.is_default_impl(def_id.index) } + describe_def => { cdata.get_def(def_id.index) } + def_span => { cdata.get_span(def_id.index, &tcx.sess) } + stability => { cdata.get_stability(def_id.index) } + deprecation => { cdata.get_deprecation(def_id.index) } + item_attrs => { cdata.get_item_attrs(def_id.index, &tcx.dep_graph) } + // FIXME(#38501) We've skipped a `read` on the `HirBody` of + // a `fn` when encoding, so the dep-tracking wouldn't work. + // This is only used by rustdoc anyway, which shouldn't have + // incremental recompilation ever enabled. + fn_arg_names => { cdata.get_fn_arg_names(def_id.index) } + impl_parent => { cdata.get_parent_impl(def_id.index) } + trait_of_item => { cdata.get_trait_of_item(def_id.index) } + is_exported_symbol => { + let dep_node = cdata.metadata_dep_node(GlobalMetaDataKind::ExportedSymbols); + cdata.exported_symbols.get(&tcx.dep_graph, dep_node).contains(&def_id.index) + } + item_body_nested_bodies => { Rc::new(cdata.item_body_nested_bodies(def_id.index)) } + const_is_rvalue_promotable_to_static => { + cdata.const_is_rvalue_promotable_to_static(def_id.index) + } + is_mir_available => { cdata.is_item_mir_available(def_id.index) } + + dylib_dependency_formats => { Rc::new(cdata.get_dylib_dependency_formats(&tcx.dep_graph)) } + is_allocator => { cdata.is_allocator(&tcx.dep_graph) } + is_panic_runtime => { cdata.is_panic_runtime(&tcx.dep_graph) } + extern_crate => { Rc::new(cdata.extern_crate.get()) } } pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) { diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index 814a45ef636b..4ab2c1aa63c5 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -107,7 +107,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { let mut result = Vec::new(); for n in self.tcx.sess.cstore.crates() { - let span = match *self.tcx.extern_crate(n) { + let span = match *self.tcx.extern_crate(n.as_def_id()) { Some(ref c) => c.span, None => { debug!("Skipping crate {}, no data", n); diff --git a/src/librustc_trans/back/symbol_export.rs b/src/librustc_trans/back/symbol_export.rs index 8eb7ccd48220..b38dc1883892 100644 --- a/src/librustc_trans/back/symbol_export.rs +++ b/src/librustc_trans/back/symbol_export.rs @@ -92,8 +92,8 @@ impl ExportedSymbols { // Down below we'll hardwire all of the symbols to the `Rust` export // level instead. let special_runtime_crate = - scx.tcx().is_allocator(cnum) || - scx.tcx().is_panic_runtime(cnum) || + scx.tcx().is_allocator(cnum.as_def_id()) || + scx.tcx().is_panic_runtime(cnum.as_def_id()) || scx.sess().cstore.is_compiler_builtins(cnum); let crate_exports = scx