Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc: Make the trait_map of TyCtxt private #44162

Merged
merged 2 commits into from
Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

use hir::def_id::{CrateNum, DefId};
use hir::map::DefPathHash;
use hir::HirId;

use ich::Fingerprint;
use ty::{TyCtxt, Instance, InstanceDef};
Expand Down Expand Up @@ -527,6 +528,8 @@ define_dep_nodes!( <'tcx>
[] HasGlobalAllocator(DefId),
[] ExternCrate(DefId),
[] LintLevels,
[] InScopeTraits(HirId),
[] ModuleExports(HirId),
);

trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {
Expand Down
6 changes: 4 additions & 2 deletions src/librustc/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,15 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ast::N
// 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!().
debug_assert!(hcx.tcx.trait_map.get(self).is_none());
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);
}
NodeIdHashingMode::HashTraitsInScope => {
if let Some(traits) = hcx.tcx.trait_map.get(self) {
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.
let mut candidates: AccumulateVec<[_; 8]> =
Expand Down
33 changes: 27 additions & 6 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use dep_graph::DepGraph;
use errors::DiagnosticBuilder;
use session::Session;
use middle;
use hir::{TraitMap};
use hir::def::{Def, ExportMap};
use hir::{TraitCandidate, HirId};
use hir::def::{Def, Export};
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use hir::map as hir_map;
use hir::map::DefPathHash;
Expand Down Expand Up @@ -819,10 +819,10 @@ pub struct GlobalCtxt<'tcx> {

/// Map indicating what traits are in scope for places where this
/// is relevant; generated by resolve.
pub trait_map: TraitMap,
trait_map: FxHashMap<HirId, Rc<Vec<TraitCandidate>>>,

/// Export map produced by name resolution.
pub export_map: ExportMap,
export_map: FxHashMap<HirId, Rc<Vec<Export>>>,

pub named_region_map: resolve_lifetime::NamedRegionMap,

Expand Down Expand Up @@ -1078,8 +1078,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
dep_graph: dep_graph.clone(),
types: common_types,
named_region_map,
trait_map: resolutions.trait_map,
export_map: resolutions.export_map,
trait_map: resolutions.trait_map.into_iter().map(|(k, v)| {
(hir.node_to_hir_id(k), Rc::new(v))
}).collect(),
export_map: resolutions.export_map.into_iter().map(|(k, v)| {
(hir.node_to_hir_id(k), Rc::new(v))
}).collect(),
hir,
def_path_hash_to_def_id,
maps: maps::Maps::new(providers),
Expand Down Expand Up @@ -1997,3 +2001,20 @@ impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
Ok(f(&iter.collect::<Result<AccumulateVec<[_; 8]>, _>>()?))
}
}

fn in_scope_traits<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: HirId)
-> Option<Rc<Vec<TraitCandidate>>>
{
tcx.gcx.trait_map.get(&id).cloned()
}

fn module_exports<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: HirId)
-> Option<Rc<Vec<Export>>>
{
tcx.gcx.export_map.get(&id).cloned()
}

pub fn provide(providers: &mut ty::maps::Providers) {
providers.in_scope_traits = in_scope_traits;
providers.module_exports = module_exports;
}
28 changes: 26 additions & 2 deletions src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use dep_graph::{DepConstructor, DepNode, DepNodeIndex};
use errors::{Diagnostic, DiagnosticBuilder};
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use hir::def::Def;
use hir;
use hir::def::{Def, Export};
use hir::{self, TraitCandidate, HirId};
use lint;
use middle::const_val;
use middle::cstore::{ExternCrate, LinkagePreference};
Expand Down Expand Up @@ -80,6 +80,15 @@ impl Key for CrateNum {
}
}

impl Key for HirId {
fn map_crate(&self) -> CrateNum {
LOCAL_CRATE
}
fn default_span(&self, _tcx: TyCtxt) -> Span {
DUMMY_SP
}
}

impl Key for DefId {
fn map_crate(&self) -> CrateNum {
self.krate
Expand Down Expand Up @@ -540,6 +549,18 @@ impl<'tcx> QueryDescription for queries::lint_levels<'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")
}
}

// If enabled, send a message to the profile-queries thread
macro_rules! profq_msg {
($tcx:expr, $msg:expr) => {
Expand Down Expand Up @@ -1108,6 +1129,9 @@ define_maps! { <'tcx>
[] extern_crate: ExternCrate(DefId) -> Rc<Option<ExternCrate>>,

[] lint_levels: lint_levels(CrateNum) -> Rc<lint::LintLevelMap>,

[] in_scope_traits: InScopeTraits(HirId) -> Option<Rc<Vec<TraitCandidate>>>,
[] module_exports: ModuleExports(HirId) -> Option<Rc<Vec<Export>>>,
}

fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,7 @@ fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

pub fn provide(providers: &mut ty::maps::Providers) {
util::provide(providers);
context::provide(providers);
*providers = ty::maps::Providers {
associated_item,
associated_item_def_ids,
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,13 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
&hir::Visibility)>)
-> Entry<'tcx> {
let tcx = self.tcx;
let hir_id = tcx.hir.node_to_hir_id(id);
let def_id = tcx.hir.local_def_id(id);
debug!("IsolatedEncoder::encode_info_for_mod({:?})", def_id);

let data = ModData {
reexports: match tcx.export_map.get(&id) {
Some(exports) if *vis == hir::Public => {
reexports: match tcx.module_exports(hir_id) {
Some(ref exports) if *vis == hir::Public => {
self.lazy_seq_from_slice(exports.as_slice())
}
_ => LazySeq::empty(),
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_privacy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,9 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
// This code is here instead of in visit_item so that the
// crate module gets processed as well.
if self.prev_level.is_some() {
if let Some(exports) = self.tcx.export_map.get(&id) {
for export in exports {
let hir_id = self.tcx.hir.node_to_hir_id(id);
if let Some(exports) = self.tcx.module_exports(hir_id) {
for export in exports.iter() {
if let Some(node_id) = self.tcx.hir.as_local_node_id(export.def.def_id()) {
self.update(node_id, Some(AccessLevel::Exported));
}
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,14 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
fn assemble_extension_candidates_for_traits_in_scope(&mut self,
expr_id: ast::NodeId)
-> Result<(), MethodError<'tcx>> {
if expr_id == ast::DUMMY_NODE_ID {
return Ok(())
}
let mut duplicates = FxHashSet();
let opt_applicable_traits = self.tcx.trait_map.get(&expr_id);
let expr_hir_id = self.tcx.hir.node_to_hir_id(expr_id);
let opt_applicable_traits = self.tcx.in_scope_traits(expr_hir_id);
if let Some(applicable_traits) = opt_applicable_traits {
for trait_candidate in applicable_traits {
for trait_candidate in applicable_traits.iter() {
let trait_did = trait_candidate.def_id;
if duplicates.insert(trait_did) {
let import_id = trait_candidate.import_id;
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
self.visit_item(item, None, &mut om);
}
self.inside_public_path = orig_inside_public_path;
if let Some(exports) = self.cx.tcx.export_map.get(&id) {
for export in exports {
let hir_id = self.cx.tcx.hir.node_to_hir_id(id);
if let Some(exports) = self.cx.tcx.module_exports(hir_id) {
for export in exports.iter() {
if let Def::Macro(def_id, ..) = export.def {
if def_id.krate == LOCAL_CRATE || self.reexported_macros.contains(&def_id) {
continue // These are `krate.exported_macros`, handled in `self.visit()`.
Expand Down