Auto merge of #44435 - alexcrichton:in-scope, r=michaelwoerister

rustc: Remove HirId from queries

This'll allow us to reconstruct query parameters purely from the `DepNode`
they're associated with.

Closes #44414
This commit is contained in:
bors 2017-09-11 15:35:35 +00:00
commit efa3ec67e2
16 changed files with 142 additions and 121 deletions

View file

@ -60,7 +60,7 @@
//! user of the `DepNode` API of having to know how to compute the expected
//! fingerprint for a given set of node parameters.
use hir::def_id::{CrateNum, DefId};
use hir::def_id::{CrateNum, DefId, DefIndex};
use hir::map::DefPathHash;
use hir::{HirId, ItemLocalId};
@ -528,8 +528,8 @@ define_dep_nodes!( <'tcx>
[] ExternCrate(DefId),
[] LintLevels,
[] Specializes { impl1: DefId, impl2: DefId },
[] InScopeTraits(HirId),
[] ModuleExports(HirId),
[] InScopeTraits(DefIndex),
[] ModuleExports(DefId),
[] IsSanitizerRuntime(CrateNum),
[] IsProfilerRuntime(CrateNum),
[] GetPanicStrategy(CrateNum),
@ -551,15 +551,15 @@ define_dep_nodes!( <'tcx>
[] NativeLibraryKind(DefId),
[] LinkArgs,
[] NamedRegion(HirId),
[] IsLateBound(HirId),
[] ObjectLifetimeDefaults(HirId),
[] NamedRegion(DefIndex),
[] IsLateBound(DefIndex),
[] ObjectLifetimeDefaults(DefIndex),
[] Visibility(DefId),
[] DepKind(CrateNum),
[] CrateName(CrateNum),
[] ItemChildren(DefId),
[] ExternModStmtCnum(HirId),
[] ExternModStmtCnum(DefId),
[] GetLangItems,
[] DefinedLangItems(CrateNum),
[] MissingLangItems(CrateNum),
@ -570,8 +570,8 @@ define_dep_nodes!( <'tcx>
[] UsedCrateSource(CrateNum),
[] PostorderCnums,
[] Freevars(HirId),
[] MaybeUnusedTraitImport(HirId),
[] Freevars(DefId),
[] MaybeUnusedTraitImport(DefId),
[] MaybeUnusedExternCrates,
[] StabilityIndex,
[] AllCrateNums,

View file

@ -9,7 +9,7 @@
// except according to those terms.
use hir::def_id::DefId;
use util::nodemap::NodeMap;
use util::nodemap::{NodeMap, DefIdMap};
use syntax::ast;
use syntax::ext::base::MacroKind;
use syntax_pos::Span;
@ -115,7 +115,7 @@ pub type DefMap = NodeMap<PathResolution>;
/// This is the replacement export map. It maps a module to all of the exports
/// within.
pub type ExportMap = NodeMap<Vec<Export>>;
pub type ExportMap = DefIdMap<Vec<Export>>;
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct Export {

View file

@ -199,20 +199,19 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ast::N
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>) {
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
match hcx.node_id_hashing_mode {
NodeIdHashingMode::Ignore => {
// Most NodeIds in the HIR can be ignored, but if there is a
// corresponding entry in the `trait_map` we need to hash that.
// Make sure we don't ignore too much by checking that there is
// no entry in a debug_assert!().
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
debug_assert!(hcx.tcx.in_scope_traits(hir_id).is_none());
}
NodeIdHashingMode::HashDefPath => {
hcx.tcx.hir.definitions().node_to_hir_id(*self).hash_stable(hcx, hasher);
hir_id.hash_stable(hcx, hasher);
}
NodeIdHashingMode::HashTraitsInScope => {
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
if let Some(traits) = hcx.tcx.in_scope_traits(hir_id) {
// The ordering of the candidates is not fixed. So we hash
// the def-ids and then sort them and hash the collection.

View file

@ -612,8 +612,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 hir_id = self.tcx.hir.node_to_hir_id(item.id);
let cnum = match self.tcx.extern_mod_stmt_cnum(hir_id) {
let def_id = self.tcx.hir.local_def_id(item.id);
let cnum = match self.tcx.extern_mod_stmt_cnum(def_id) {
Some(cnum) => cnum,
None => return,
};

View file

@ -14,9 +14,9 @@ use dep_graph::DepGraph;
use errors::DiagnosticBuilder;
use session::Session;
use middle;
use hir::{TraitCandidate, HirId};
use hir::{TraitCandidate, HirId, ItemLocalId};
use hir::def::{Def, Export};
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
use hir::map as hir_map;
use hir::map::DefPathHash;
use lint::{self, Lint};
@ -817,10 +817,10 @@ pub struct GlobalCtxt<'tcx> {
/// Map indicating what traits are in scope for places where this
/// is relevant; generated by resolve.
trait_map: FxHashMap<HirId, Rc<Vec<TraitCandidate>>>,
trait_map: FxHashMap<DefIndex, Rc<FxHashMap<ItemLocalId, Rc<Vec<TraitCandidate>>>>>,
/// Export map produced by name resolution.
export_map: FxHashMap<HirId, Rc<Vec<Export>>>,
export_map: FxHashMap<DefId, Rc<Vec<Export>>>,
named_region_map: NamedRegionMap,
@ -837,11 +837,11 @@ pub struct GlobalCtxt<'tcx> {
// Records the free variables refrenced by every closure
// expression. Do not track deps for this, just recompute it from
// scratch every time.
freevars: FxHashMap<HirId, Rc<Vec<hir::Freevar>>>,
freevars: FxHashMap<DefId, Rc<Vec<hir::Freevar>>>,
maybe_unused_trait_imports: FxHashSet<HirId>,
maybe_unused_trait_imports: FxHashSet<DefId>,
maybe_unused_extern_crates: Vec<(HirId, Span)>,
maybe_unused_extern_crates: Vec<(DefId, Span)>,
// Internal cache for metadata decoding. No need to track deps on this.
pub rcache: RefCell<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
@ -1032,6 +1032,35 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
None
};
let mut trait_map = FxHashMap();
for (k, v) in resolutions.trait_map {
let hir_id = hir.node_to_hir_id(k);
let map = trait_map.entry(hir_id.owner)
.or_insert_with(|| Rc::new(FxHashMap()));
Rc::get_mut(map).unwrap().insert(hir_id.local_id, Rc::new(v));
}
let mut defs = FxHashMap();
for (k, v) in named_region_map.defs {
let hir_id = hir.node_to_hir_id(k);
let map = defs.entry(hir_id.owner)
.or_insert_with(|| Rc::new(FxHashMap()));
Rc::get_mut(map).unwrap().insert(hir_id.local_id, v);
}
let mut late_bound = FxHashMap();
for k in named_region_map.late_bound {
let hir_id = hir.node_to_hir_id(k);
let map = late_bound.entry(hir_id.owner)
.or_insert_with(|| Rc::new(FxHashSet()));
Rc::get_mut(map).unwrap().insert(hir_id.local_id);
}
let mut object_lifetime_defaults = FxHashMap();
for (k, v) in named_region_map.object_lifetime_defaults {
let hir_id = hir.node_to_hir_id(k);
let map = object_lifetime_defaults.entry(hir_id.owner)
.or_insert_with(|| Rc::new(FxHashMap()));
Rc::get_mut(map).unwrap().insert(hir_id.local_id, Rc::new(v));
}
tls::enter_global(GlobalCtxt {
sess: s,
trans_trait_caches: traits::trans::TransTraitCaches::new(dep_graph.clone()),
@ -1040,40 +1069,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
dep_graph: dep_graph.clone(),
types: common_types,
named_region_map: NamedRegionMap {
defs:
named_region_map.defs
.into_iter()
.map(|(k, v)| (hir.node_to_hir_id(k), v))
.collect(),
late_bound:
named_region_map.late_bound
.into_iter()
.map(|k| hir.node_to_hir_id(k))
.collect(),
object_lifetime_defaults:
named_region_map.object_lifetime_defaults
.into_iter()
.map(|(k, v)| (hir.node_to_hir_id(k), Rc::new(v)))
.collect(),
defs,
late_bound,
object_lifetime_defaults,
},
trait_map: resolutions.trait_map.into_iter().map(|(k, v)| {
(hir.node_to_hir_id(k), Rc::new(v))
}).collect(),
trait_map,
export_map: resolutions.export_map.into_iter().map(|(k, v)| {
(hir.node_to_hir_id(k), Rc::new(v))
(k, Rc::new(v))
}).collect(),
freevars: resolutions.freevars.into_iter().map(|(k, v)| {
(hir.node_to_hir_id(k), Rc::new(v))
(hir.local_def_id(k), Rc::new(v))
}).collect(),
maybe_unused_trait_imports:
resolutions.maybe_unused_trait_imports
.into_iter()
.map(|id| hir.node_to_hir_id(id))
.map(|id| hir.local_def_id(id))
.collect(),
maybe_unused_extern_crates:
resolutions.maybe_unused_extern_crates
.into_iter()
.map(|(id, sp)| (hir.node_to_hir_id(id), sp))
.map(|(id, sp)| (hir.local_def_id(id), sp))
.collect(),
hir,
def_path_hash_to_def_id,
@ -1967,6 +1982,29 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let (level, src) = self.lint_level_at_node(lint, id);
lint::struct_lint_level(self.sess, lint, level, src, None, msg)
}
pub fn in_scope_traits(self, id: HirId) -> Option<Rc<Vec<TraitCandidate>>> {
self.in_scope_traits_map(id.owner)
.and_then(|map| map.get(&id.local_id).cloned())
}
pub fn named_region(self, id: HirId) -> Option<resolve_lifetime::Region> {
self.named_region_map(id.owner)
.and_then(|map| map.get(&id.local_id).cloned())
}
pub fn is_late_bound(self, id: HirId) -> bool {
self.is_late_bound_map(id.owner)
.map(|set| set.contains(&id.local_id))
.unwrap_or(false)
}
pub fn object_lifetime_defaults(self, id: HirId)
-> Option<Rc<Vec<ObjectLifetimeDefault>>>
{
self.object_lifetime_defaults_map(id.owner)
.and_then(|map| map.get(&id.local_id).cloned())
}
}
pub trait InternAs<T: ?Sized, R> {
@ -2014,20 +2052,24 @@ impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
}
struct NamedRegionMap {
defs: FxHashMap<HirId, resolve_lifetime::Region>,
late_bound: FxHashSet<HirId>,
object_lifetime_defaults: FxHashMap<HirId, Rc<Vec<ObjectLifetimeDefault>>>,
defs: FxHashMap<DefIndex, Rc<FxHashMap<ItemLocalId, resolve_lifetime::Region>>>,
late_bound: FxHashMap<DefIndex, Rc<FxHashSet<ItemLocalId>>>,
object_lifetime_defaults:
FxHashMap<
DefIndex,
Rc<FxHashMap<ItemLocalId, Rc<Vec<ObjectLifetimeDefault>>>>,
>,
}
pub fn provide(providers: &mut ty::maps::Providers) {
// FIXME(#44234) - almost all of these queries have no sub-queries and
// therefore no actual inputs, they're just reading tables calculated in
// resolve! Does this work? Unsure! That's what the issue is about
providers.in_scope_traits = |tcx, id| tcx.gcx.trait_map.get(&id).cloned();
providers.in_scope_traits_map = |tcx, id| tcx.gcx.trait_map.get(&id).cloned();
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).cloned();
providers.named_region = |tcx, id| tcx.gcx.named_region_map.defs.get(&id).cloned();
providers.is_late_bound = |tcx, id| tcx.gcx.named_region_map.late_bound.contains(&id);
providers.object_lifetime_defaults = |tcx, id| {
providers.named_region_map = |tcx, id| tcx.gcx.named_region_map.defs.get(&id).cloned();
providers.is_late_bound_map = |tcx, id| tcx.gcx.named_region_map.late_bound.get(&id).cloned();
providers.object_lifetime_defaults_map = |tcx, id| {
tcx.gcx.named_region_map.object_lifetime_defaults.get(&id).cloned()
};
providers.crate_name = |tcx, id| {

View file

@ -12,7 +12,7 @@ use dep_graph::{DepConstructor, DepNode, DepNodeIndex};
use errors::{Diagnostic, DiagnosticBuilder};
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE, DefIndex};
use hir::def::{Def, Export};
use hir::{self, TraitCandidate, HirId};
use hir::{self, TraitCandidate, ItemLocalId};
use hir::svh::Svh;
use lint;
use middle::const_val;
@ -39,7 +39,7 @@ use util::common::{profq_msg, ProfileQueriesMsg};
use rustc_data_structures::indexed_set::IdxSetBuf;
use rustc_back::PanicStrategy;
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use std::cell::{RefCell, RefMut, Cell};
use std::fmt::Debug;
use std::hash::Hash;
@ -87,7 +87,7 @@ impl Key for CrateNum {
}
}
impl Key for HirId {
impl Key for DefIndex {
fn map_crate(&self) -> CrateNum {
LOCAL_CRATE
}
@ -556,15 +556,9 @@ impl<'tcx> QueryDescription for queries::specializes<'tcx> {
}
}
impl<'tcx> QueryDescription for queries::in_scope_traits<'tcx> {
fn describe(_tcx: TyCtxt, _: HirId) -> String {
format!("fetching the traits in scope at a particular ast node")
}
}
impl<'tcx> QueryDescription for queries::module_exports<'tcx> {
fn describe(_tcx: TyCtxt, _: HirId) -> String {
format!("fetching the exported items for a module")
impl<'tcx> QueryDescription for queries::in_scope_traits_map<'tcx> {
fn describe(_tcx: TyCtxt, _: DefIndex) -> String {
format!("traits in scope at a block")
}
}
@ -652,21 +646,21 @@ impl<'tcx> QueryDescription for queries::link_args<'tcx> {
}
}
impl<'tcx> QueryDescription for queries::named_region<'tcx> {
fn describe(_tcx: TyCtxt, _: HirId) -> String {
format!("fetching info about a named region")
impl<'tcx> QueryDescription for queries::named_region_map<'tcx> {
fn describe(_tcx: TyCtxt, _: DefIndex) -> String {
format!("looking up a named region")
}
}
impl<'tcx> QueryDescription for queries::is_late_bound<'tcx> {
fn describe(_tcx: TyCtxt, _: HirId) -> String {
format!("testing whether a lifetime is late bound")
impl<'tcx> QueryDescription for queries::is_late_bound_map<'tcx> {
fn describe(_tcx: TyCtxt, _: DefIndex) -> String {
format!("testing if a region is late boudn")
}
}
impl<'tcx> QueryDescription for queries::object_lifetime_defaults<'tcx> {
fn describe(_tcx: TyCtxt, _: HirId) -> String {
format!("fetching a list of ObjectLifetimeDefault for a lifetime")
impl<'tcx> QueryDescription for queries::object_lifetime_defaults_map<'tcx> {
fn describe(_tcx: TyCtxt, _: DefIndex) -> String {
format!("looking up lifetime defaults for a region")
}
}
@ -682,12 +676,6 @@ impl<'tcx> QueryDescription for queries::crate_name<'tcx> {
}
}
impl<'tcx> QueryDescription for queries::extern_mod_stmt_cnum<'tcx> {
fn describe(_tcx: TyCtxt, _: HirId) -> String {
format!("looking up the CrateNum for an `extern mod` statement")
}
}
impl<'tcx> QueryDescription for queries::get_lang_items<'tcx> {
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
format!("calculating the lang items map")
@ -730,18 +718,6 @@ impl<'tcx> QueryDescription for queries::postorder_cnums<'tcx> {
}
}
impl<'tcx> QueryDescription for queries::freevars<'tcx> {
fn describe(_tcx: TyCtxt, _: HirId) -> String {
format!("looking up free variables for a node")
}
}
impl<'tcx> QueryDescription for queries::maybe_unused_trait_import<'tcx> {
fn describe(_tcx: TyCtxt, _: HirId) -> String {
format!("testing if a trait import is unused")
}
}
impl<'tcx> QueryDescription for queries::maybe_unused_extern_crates<'tcx> {
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
format!("looking up all possibly unused extern crates")
@ -1331,8 +1307,9 @@ define_maps! { <'tcx>
[] fn extern_crate: ExternCrate(DefId) -> Rc<Option<ExternCrate>>,
[] fn specializes: specializes_node((DefId, DefId)) -> bool,
[] fn in_scope_traits: InScopeTraits(HirId) -> Option<Rc<Vec<TraitCandidate>>>,
[] fn module_exports: ModuleExports(HirId) -> Option<Rc<Vec<Export>>>,
[] fn in_scope_traits_map: InScopeTraits(DefIndex)
-> Option<Rc<FxHashMap<ItemLocalId, Rc<Vec<TraitCandidate>>>>>,
[] fn module_exports: ModuleExports(DefId) -> Option<Rc<Vec<Export>>>,
[] fn lint_levels: lint_levels_node(CrateNum) -> Rc<lint::LintLevelMap>,
[] fn impl_defaultness: ImplDefaultness(DefId) -> hir::Defaultness,
@ -1355,16 +1332,18 @@ define_maps! { <'tcx>
-> Option<NativeLibraryKind>,
[] fn link_args: link_args_node(CrateNum) -> Rc<Vec<String>>,
[] fn named_region: NamedRegion(HirId) -> Option<Region>,
[] fn is_late_bound: IsLateBound(HirId) -> bool,
[] fn object_lifetime_defaults: ObjectLifetimeDefaults(HirId)
-> Option<Rc<Vec<ObjectLifetimeDefault>>>,
[] fn named_region_map: NamedRegion(DefIndex) ->
Option<Rc<FxHashMap<ItemLocalId, Region>>>,
[] fn is_late_bound_map: IsLateBound(DefIndex) ->
Option<Rc<FxHashSet<ItemLocalId>>>,
[] fn object_lifetime_defaults_map: ObjectLifetimeDefaults(DefIndex)
-> Option<Rc<FxHashMap<ItemLocalId, Rc<Vec<ObjectLifetimeDefault>>>>>,
[] fn visibility: Visibility(DefId) -> ty::Visibility,
[] fn dep_kind: DepKind(CrateNum) -> DepKind,
[] fn crate_name: CrateName(CrateNum) -> Symbol,
[] fn item_children: ItemChildren(DefId) -> Rc<Vec<Export>>,
[] fn extern_mod_stmt_cnum: ExternModStmtCnum(HirId) -> Option<CrateNum>,
[] fn extern_mod_stmt_cnum: ExternModStmtCnum(DefId) -> Option<CrateNum>,
[] fn get_lang_items: get_lang_items_node(CrateNum) -> Rc<LanguageItems>,
[] fn defined_lang_items: DefinedLangItems(CrateNum) -> Rc<Vec<(DefIndex, usize)>>,
@ -1376,10 +1355,10 @@ define_maps! { <'tcx>
[] fn used_crate_source: UsedCrateSource(CrateNum) -> Rc<CrateSource>,
[] fn postorder_cnums: postorder_cnums_node(CrateNum) -> Rc<Vec<CrateNum>>,
[] fn freevars: Freevars(HirId) -> Option<Rc<Vec<hir::Freevar>>>,
[] fn maybe_unused_trait_import: MaybeUnusedTraitImport(HirId) -> bool,
[] fn freevars: Freevars(DefId) -> Option<Rc<Vec<hir::Freevar>>>,
[] fn maybe_unused_trait_import: MaybeUnusedTraitImport(DefId) -> bool,
[] fn maybe_unused_extern_crates: maybe_unused_extern_crates_node(CrateNum)
-> Rc<Vec<(HirId, Span)>>,
-> Rc<Vec<(DefId, Span)>>,
[] fn stability_index: stability_index_node(CrateNum) -> Rc<stability::Index<'tcx>>,
[] fn all_crate_nums: all_crate_nums_node(CrateNum) -> Rc<Vec<CrateNum>>,

View file

@ -2325,8 +2325,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn with_freevars<T, F>(self, fid: NodeId, f: F) -> T where
F: FnOnce(&[hir::Freevar]) -> T,
{
let hir_id = self.hir.node_to_hir_id(fid);
match self.freevars(hir_id) {
let def_id = self.hir.local_def_id(fid);
match self.freevars(def_id) {
None => f(&[]),
Some(d) => f(&d),
}