Skip to content

Commit

Permalink
Make TypeckTables::type_dependent_defs use ItemLocalId instead of Nod…
Browse files Browse the repository at this point in the history
…eId.
  • Loading branch information
michaelwoerister committed Aug 11, 2017
1 parent 9868352 commit 783ccc4
Show file tree
Hide file tree
Showing 31 changed files with 386 additions and 151 deletions.
7 changes: 7 additions & 0 deletions src/librustc/hir/def_id.rs
Expand Up @@ -201,4 +201,11 @@ impl DefId {
pub fn is_local(&self) -> bool {
self.krate == LOCAL_CRATE
}

pub fn invalid() -> DefId {
DefId {
krate: INVALID_CRATE,
index: CRATE_DEF_INDEX,
}
}
}
175 changes: 113 additions & 62 deletions src/librustc/hir/lowering.rs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/librustc/hir/map/definitions.rs
Expand Up @@ -465,6 +465,10 @@ impl Definitions {
self.node_to_hir_id[node_id]
}

pub fn find_node_for_hir_id(&self, hir_id: hir::HirId) -> ast::NodeId {
self.node_to_hir_id.binary_search(&hir_id).unwrap()
}

/// Add a definition with a parent definition.
pub fn create_root_def(&mut self,
crate_name: &str,
Expand Down
9 changes: 9 additions & 0 deletions src/librustc/hir/map/mod.rs
Expand Up @@ -357,6 +357,7 @@ impl<'hir> Map<'hir> {
}
}

#[inline]
pub fn definitions(&self) -> &Definitions {
&self.definitions
}
Expand All @@ -377,21 +378,29 @@ impl<'hir> Map<'hir> {
self.definitions.def_path(def_id.index)
}

#[inline]
pub fn local_def_id(&self, node: NodeId) -> DefId {
self.opt_local_def_id(node).unwrap_or_else(|| {
bug!("local_def_id: no entry for `{}`, which has a map of `{:?}`",
node, self.find_entry(node))
})
}

#[inline]
pub fn opt_local_def_id(&self, node: NodeId) -> Option<DefId> {
self.definitions.opt_local_def_id(node)
}

#[inline]
pub fn as_local_node_id(&self, def_id: DefId) -> Option<NodeId> {
self.definitions.as_local_node_id(def_id)
}

#[inline]
pub fn node_to_hir_id(&self, node_id: NodeId) -> HirId {
self.definitions.node_to_hir_id(node_id)
}

fn entry_count(&self) -> usize {
self.map.len()
}
Expand Down
8 changes: 7 additions & 1 deletion src/librustc/hir/mod.rs
Expand Up @@ -129,9 +129,11 @@ pub const CRATE_HIR_ID: HirId = HirId {

pub const DUMMY_HIR_ID: HirId = HirId {
owner: CRATE_DEF_INDEX,
local_id: ItemLocalId(!0)
local_id: DUMMY_ITEM_LOCAL_ID,
};

pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId(!0);

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
pub struct Lifetime {
pub id: NodeId,
Expand Down Expand Up @@ -547,6 +549,7 @@ pub struct Block {
/// without a semicolon, if any
pub expr: Option<P<Expr>>,
pub id: NodeId,
pub hir_id: HirId,
/// Distinguishes between `unsafe { ... }` and `{ ... }`
pub rules: BlockCheckMode,
pub span: Span,
Expand All @@ -560,6 +563,7 @@ pub struct Block {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub struct Pat {
pub id: NodeId,
pub hir_id: HirId,
pub node: PatKind,
pub span: Span,
}
Expand Down Expand Up @@ -986,6 +990,7 @@ pub struct Expr {
pub span: Span,
pub node: Expr_,
pub attrs: ThinVec<Attribute>,
pub hir_id: HirId,
}

impl fmt::Debug for Expr {
Expand Down Expand Up @@ -1423,6 +1428,7 @@ pub struct InlineAsm {
pub struct Arg {
pub pat: P<Pat>,
pub id: NodeId,
pub hir_id: HirId,
}

/// Represents the header (not the body) of a function declaration
Expand Down
6 changes: 5 additions & 1 deletion src/librustc/ich/impls_hir.rs
Expand Up @@ -359,6 +359,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::B
ref stmts,
ref expr,
id,
hir_id: _,
rules,
span,
targeted_by_break,
Expand Down Expand Up @@ -423,6 +424,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::P

let hir::Pat {
id,
hir_id: _,
ref node,
ref span
} = *self;
Expand Down Expand Up @@ -551,6 +553,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::E
hcx.while_hashing_hir_bodies(true, |hcx| {
let hir::Expr {
id,
hir_id: _,
ref span,
ref node,
ref attrs
Expand Down Expand Up @@ -1021,7 +1024,8 @@ impl_stable_hash_for!(enum hir::Stmt_ {

impl_stable_hash_for!(struct hir::Arg {
pat,
id
id,
hir_id
});

impl_stable_hash_for!(struct hir::Body {
Expand Down
5 changes: 4 additions & 1 deletion src/librustc/ich/impls_ty.rs
Expand Up @@ -618,6 +618,7 @@ for ty::TypeckTables<'gcx> {
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>) {
let ty::TypeckTables {
local_id_root: _,
ref type_dependent_defs,
ref node_types,
ref node_substs,
Expand All @@ -637,7 +638,9 @@ for ty::TypeckTables<'gcx> {
} = *self;

hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
ich::hash_stable_nodemap(hcx, hasher, type_dependent_defs);
ich::hash_stable_hashmap(hcx, hasher, type_dependent_defs, |_, item_local_id| {
*item_local_id
});
ich::hash_stable_nodemap(hcx, hasher, node_types);
ich::hash_stable_nodemap(hcx, hasher, node_substs);
ich::hash_stable_nodemap(hcx, hasher, adjustments);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/mod.rs
Expand Up @@ -358,8 +358,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'gcx> {
impl<'a, 'gcx, 'tcx> InferCtxtBuilder<'a, 'gcx, 'tcx> {
/// Used only by `rustc_typeck` during body type-checking/inference,
/// will initialize `in_progress_tables` with fresh `TypeckTables`.
pub fn with_fresh_in_progress_tables(mut self) -> Self {
self.fresh_tables = Some(RefCell::new(ty::TypeckTables::empty()));
pub fn with_fresh_in_progress_tables(mut self, table_owner: DefId) -> Self {
self.fresh_tables = Some(RefCell::new(ty::TypeckTables::empty(table_owner)));
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lint/context.rs
Expand Up @@ -43,7 +43,7 @@ use syntax::ast;
use syntax_pos::{MultiSpan, Span};
use errors::DiagnosticBuilder;
use hir;
use hir::def_id::LOCAL_CRATE;
use hir::def_id::{DefId, LOCAL_CRATE};
use hir::intravisit as hir_visit;
use syntax::visit as ast_visit;

Expand Down Expand Up @@ -986,7 +986,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {

let mut cx = LateContext {
tcx,
tables: &ty::TypeckTables::empty(),
tables: &ty::TypeckTables::empty(DefId::invalid()),
param_env: ty::ParamEnv::empty(Reveal::UserFacing),
access_levels,
lint_sess: LintSession::new(&tcx.sess.lint_store),
Expand Down
12 changes: 7 additions & 5 deletions src/librustc/middle/dead.rs
Expand Up @@ -94,7 +94,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
}
}

fn lookup_and_handle_method(&mut self, id: ast::NodeId) {
fn lookup_and_handle_method(&mut self, id: hir::ItemLocalId) {
self.check_def_id(self.tables.type_dependent_defs[&id].def_id());
}

Expand All @@ -119,6 +119,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {

fn handle_field_pattern_match(&mut self, lhs: &hir::Pat, def: Def,
pats: &[codemap::Spanned<hir::FieldPat>]) {


let variant = match self.tables.node_id_to_type(lhs.id).sty {
ty::TyAdt(adt, _) => adt.variant_of_def(def),
_ => span_bug!(lhs.span, "non-ADT in struct pattern")
Expand Down Expand Up @@ -235,11 +237,11 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
match expr.node {
hir::ExprPath(ref qpath @ hir::QPath::TypeRelative(..)) => {
let def = self.tables.qpath_def(qpath, expr.id);
let def = self.tables.qpath_def(qpath, expr.hir_id);
self.handle_definition(def);
}
hir::ExprMethodCall(..) => {
self.lookup_and_handle_method(expr.id);
self.lookup_and_handle_method(expr.hir_id.local_id);
}
hir::ExprField(ref lhs, ref name) => {
self.handle_field_access(&lhs, name.node);
Expand Down Expand Up @@ -282,7 +284,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
self.handle_field_pattern_match(pat, path.def, fields);
}
PatKind::Path(ref qpath @ hir::QPath::TypeRelative(..)) => {
let def = self.tables.qpath_def(qpath, pat.id);
let def = self.tables.qpath_def(qpath, pat.hir_id);
self.handle_definition(def);
}
_ => ()
Expand Down Expand Up @@ -425,7 +427,7 @@ fn find_live<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let mut symbol_visitor = MarkSymbolVisitor {
worklist,
tcx,
tables: &ty::TypeckTables::empty(),
tables: &ty::TypeckTables::empty(DefId::invalid()),
live_symbols: box FxHashSet(),
struct_has_extern_repr: false,
ignore_non_const_paths: false,
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/middle/effect.rs
Expand Up @@ -19,6 +19,7 @@ use syntax::ast;
use syntax_pos::Span;
use hir::{self, PatKind};
use hir::def::Def;
use hir::def_id::DefId;
use hir::intravisit::{self, FnKind, Visitor, NestedVisitorMap};

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -165,7 +166,7 @@ impl<'a, 'tcx> Visitor<'tcx> for EffectCheckVisitor<'a, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
match expr.node {
hir::ExprMethodCall(..) => {
let def_id = self.tables.type_dependent_defs[&expr.id].def_id();
let def_id = self.tables.type_dependent_defs[&expr.hir_id.local_id].def_id();
let sig = self.tcx.fn_sig(def_id);
debug!("effect: method call case, signature is {:?}",
sig);
Expand Down Expand Up @@ -262,7 +263,7 @@ impl<'a, 'tcx> Visitor<'tcx> for EffectCheckVisitor<'a, 'tcx> {
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
let mut visitor = EffectCheckVisitor {
tcx,
tables: &ty::TypeckTables::empty(),
tables: &ty::TypeckTables::empty(DefId::invalid()),
body_id: hir::BodyId { node_id: ast::CRATE_NODE_ID },
unsafe_context: UnsafeContext::new(SafeContext),
};
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/expr_use_visitor.rs
Expand Up @@ -537,7 +537,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
}
ty::TyError => { }
_ => {
let def_id = self.mc.tables.type_dependent_defs[&call.id].def_id();
let def_id = self.mc.tables.type_dependent_defs[&call.hir_id.local_id].def_id();
match OverloadedCallType::from_method_id(self.tcx(), def_id) {
FnMutOverloadedCall => {
let call_scope_r = self.tcx().node_scope_region(call.id);
Expand Down Expand Up @@ -863,7 +863,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
PatKind::Struct(ref qpath, ..) => qpath,
_ => return
};
let def = mc.tables.qpath_def(qpath, pat.id);
let def = mc.tables.qpath_def(qpath, pat.hir_id);
match def {
Def::Variant(variant_did) |
Def::VariantCtor(variant_did, ..) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/intrinsicck.rs
Expand Up @@ -146,7 +146,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {

fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
let def = if let hir::ExprPath(ref qpath) = expr.node {
self.tables.qpath_def(qpath, expr.id)
self.tables.qpath_def(qpath, expr.hir_id)
} else {
Def::Err
};
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/mem_categorization.rs
Expand Up @@ -604,7 +604,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
}

hir::ExprPath(ref qpath) => {
let def = self.tables.qpath_def(qpath, expr.id);
let def = self.tables.qpath_def(qpath, expr.hir_id);
self.cat_def(expr.id, expr.span, expr_ty, def)
}

Expand Down Expand Up @@ -1124,7 +1124,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {

match pat.node {
PatKind::TupleStruct(ref qpath, ref subpats, ddpos) => {
let def = self.tables.qpath_def(qpath, pat.id);
let def = self.tables.qpath_def(qpath, pat.hir_id);
let (cmt, expected_len) = match def {
Def::Err => {
debug!("access to unresolvable pattern {:?}", pat);
Expand Down Expand Up @@ -1161,7 +1161,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {

PatKind::Struct(ref qpath, ref field_pats, _) => {
// {f1: p1, ..., fN: pN}
let def = self.tables.qpath_def(qpath, pat.id);
let def = self.tables.qpath_def(qpath, pat.hir_id);
let cmt = match def {
Def::Err => {
debug!("access to unresolvable pattern {:?}", pat);
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/reachable.rs
Expand Up @@ -107,10 +107,10 @@ impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
let def = match expr.node {
hir::ExprPath(ref qpath) => {
Some(self.tables.qpath_def(qpath, expr.id))
Some(self.tables.qpath_def(qpath, expr.hir_id))
}
hir::ExprMethodCall(..) => {
Some(self.tables.type_dependent_defs[&expr.id])
Some(self.tables.type_dependent_defs[&expr.hir_id.local_id])
}
_ => None
};
Expand Down Expand Up @@ -375,7 +375,7 @@ fn reachable_set<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) ->
});
let mut reachable_context = ReachableContext {
tcx,
tables: &ty::TypeckTables::empty(),
tables: &ty::TypeckTables::empty(DefId::invalid()),
reachable_symbols: NodeSet(),
worklist: Vec::new(),
any_library,
Expand Down

0 comments on commit 783ccc4

Please sign in to comment.