Skip to content

Commit 43e0266

Browse files
committed
Store partial resolution and full resolution separately
1 parent 1c11ea3 commit 43e0266

File tree

16 files changed

+80
-46
lines changed

16 files changed

+80
-46
lines changed

src/librustc/hir/def.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ impl PathResolution {
101101
}
102102

103103
// Definition mapping
104-
pub type DefMap = NodeMap<PathResolution>;
104+
pub type DefMap = NodeMap<Def>;
105+
pub type AssocMap = NodeMap<PathResolution>;
105106
// This is the replacement export map. It maps a module to all of the exports
106107
// within.
107108
pub type ExportMap = NodeMap<Vec<Export>>;

src/librustc/hir/lowering.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use hir;
4444
use hir::map::Definitions;
4545
use hir::map::definitions::DefPathData;
4646
use hir::def_id::{DefIndex, DefId};
47-
use hir::def::{Def, PathResolution};
47+
use hir::def::Def;
4848
use session::Session;
4949

5050
use std::collections::BTreeMap;
@@ -74,7 +74,7 @@ pub trait Resolver {
7474
fn resolve_generated_global_path(&mut self, path: &hir::Path, is_value: bool) -> Def;
7575

7676
// Obtain the resolution for a node id
77-
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
77+
fn get_resolution(&mut self, id: NodeId) -> Option<Def>;
7878

7979
// Record the resolution of a path or binding generated by the lowerer when expanding.
8080
fn record_resolution(&mut self, id: NodeId, def: Def);
@@ -873,7 +873,7 @@ impl<'a> LoweringContext<'a> {
873873
PatKind::Wild => hir::PatKind::Wild,
874874
PatKind::Ident(ref binding_mode, pth1, ref sub) => {
875875
self.with_parent_def(p.id, |this| {
876-
match this.resolver.get_resolution(p.id).map(|d| d.base_def) {
876+
match this.resolver.get_resolution(p.id) {
877877
// `None` can occur in body-less function signatures
878878
None | Some(Def::Local(..)) => {
879879
hir::PatKind::Binding(this.lower_binding_mode(binding_mode),

src/librustc/hir/pat_util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
5757
PatKind::TupleStruct(..) |
5858
PatKind::Path(..) |
5959
PatKind::Struct(..) => {
60-
match dm.get(&pat.id).map(|d| d.full_def()) {
60+
match dm.get(&pat.id).cloned() {
6161
Some(Def::Variant(..)) | Some(Def::VariantCtor(..)) => true,
6262
_ => false
6363
}
@@ -70,7 +70,7 @@ pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
7070
pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
7171
match pat.node {
7272
PatKind::Path(..) => {
73-
match dm.get(&pat.id).map(|d| d.full_def()) {
73+
match dm.get(&pat.id).cloned() {
7474
Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => true,
7575
_ => false
7676
}
@@ -173,7 +173,7 @@ pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
173173
PatKind::TupleStruct(..) |
174174
PatKind::Path(..) |
175175
PatKind::Struct(..) => {
176-
match dm.get(&p.id).map(|d| d.full_def()) {
176+
match dm.get(&p.id).cloned() {
177177
Some(Def::Variant(id)) |
178178
Some(Def::VariantCtor(id, ..)) => variants.push(id),
179179
_ => ()

src/librustc/middle/resolve_lifetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
247247
hir::TyPath(None, ref path) => {
248248
// if this path references a trait, then this will resolve to
249249
// a trait ref, which introduces a binding scope.
250-
match self.def_map.get(&ty.id).map(|d| (d.base_def, d.depth)) {
251-
Some((Def::Trait(..), 0)) => {
250+
match self.def_map.get(&ty.id).cloned() {
251+
Some(Def::Trait(..)) => {
252252
self.with(LateScope(&[], self.scope), |_, this| {
253253
this.visit_path(path, ty.id);
254254
});

src/librustc/ty/context.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use dep_graph::{DepGraph, DepTrackingMap};
1414
use session::Session;
1515
use middle;
1616
use hir::TraitMap;
17-
use hir::def::DefMap;
17+
use hir::def::{AssocMap, DefMap};
1818
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
1919
use hir::map as ast_map;
2020
use hir::map::{DefKey, DefPathData, DisambiguatedDefPathData};
@@ -379,11 +379,14 @@ pub struct GlobalCtxt<'tcx> {
379379

380380
pub sess: &'tcx Session,
381381

382-
/// Map from path id to the results from resolve; generated
383-
/// initially by resolve and updated during typeck in some cases
384-
/// (e.g., UFCS paths)
382+
/// Map from path id to definition; generated
383+
/// initially by resolve and updated during typeck for
384+
/// associated items
385385
pub def_map: RefCell<DefMap>,
386386

387+
/// Map from path id to partial resolution for associated items
388+
pub assoc_map: AssocMap,
389+
387390
/// Map indicating what traits are in scope for places where this
388391
/// is relevant; generated by resolve.
389392
pub trait_map: TraitMap,
@@ -769,6 +772,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
769772
pub fn create_and_enter<F, R>(s: &'tcx Session,
770773
arenas: &'tcx CtxtArenas<'tcx>,
771774
def_map: DefMap,
775+
assoc_map: AssocMap,
772776
trait_map: TraitMap,
773777
named_region_map: resolve_lifetime::NamedRegionMap,
774778
map: ast_map::Map<'tcx>,
@@ -798,6 +802,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
798802
variance_computed: Cell::new(false),
799803
sess: s,
800804
def_map: RefCell::new(def_map),
805+
assoc_map: assoc_map,
801806
trait_map: trait_map,
802807
tables: RefCell::new(Tables::empty()),
803808
impl_trait_refs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),

src/librustc/ty/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,18 +2259,24 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
22592259

22602260
/// Returns a path resolution for node id if it exists, panics otherwise.
22612261
pub fn expect_resolution(self, id: NodeId) -> PathResolution {
2262-
*self.def_map.borrow().get(&id).expect("no def-map entry for node id")
2262+
if let Some(def) = self.def_map.borrow().get(&id) {
2263+
return PathResolution::new(*def);
2264+
}
2265+
if let Some(resolution) = self.assoc_map.get(&id) {
2266+
return *resolution;
2267+
}
2268+
panic!("no def-map entry for node id");
22632269
}
22642270

22652271
/// Returns a fully resolved definition for node id if it exists, panics otherwise.
22662272
pub fn expect_def(self, id: NodeId) -> Def {
2267-
self.expect_resolution(id).full_def()
2273+
*self.def_map.borrow().get(&id).expect("no def-map entry for node id")
22682274
}
22692275

22702276
/// Returns a fully resolved definition for node id if it exists, or none if no
2271-
/// definition exists, panics on partial resolutions to catch errors.
2277+
/// definition exists or only a partial resolution exists.
22722278
pub fn expect_def_or_none(self, id: NodeId) -> Option<Def> {
2273-
self.def_map.borrow().get(&id).map(|resolution| resolution.full_def())
2279+
self.def_map.borrow().get(&id).cloned()
22742280
}
22752281

22762282
// Returns `ty::VariantDef` if `def` refers to a struct,

src/librustc_const_eval/eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc::hir::map as ast_map;
1919
use rustc::hir::map::blocks::FnLikeNode;
2020
use rustc::middle::cstore::InlinedItem;
2121
use rustc::traits;
22-
use rustc::hir::def::{Def, CtorKind, PathResolution};
22+
use rustc::hir::def::{Def, CtorKind};
2323
use rustc::hir::def_id::DefId;
2424
use rustc::hir::pat_util::def_to_path;
2525
use rustc::ty::{self, Ty, TyCtxt};
@@ -284,7 +284,7 @@ pub fn const_expr_to_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
284284
hir::ExprCall(ref callee, ref args) => {
285285
let def = tcx.expect_def(callee.id);
286286
if let Vacant(entry) = tcx.def_map.borrow_mut().entry(expr.id) {
287-
entry.insert(PathResolution::new(def));
287+
entry.insert(def);
288288
}
289289
let path = match def {
290290
Def::StructCtor(def_id, CtorKind::Fn) |

src/librustc_driver/driver.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use rustc::hir;
1212
use rustc::hir::{map as hir_map, FreevarMap, TraitMap};
13-
use rustc::hir::def::DefMap;
13+
use rustc::hir::def::{AssocMap, DefMap};
1414
use rustc::hir::lowering::lower_crate;
1515
use rustc_data_structures::blake2b::Blake2bHasher;
1616
use rustc_data_structures::fmt_wrap::FmtWrap;
@@ -64,6 +64,7 @@ use derive_registrar;
6464
#[derive(Clone)]
6565
pub struct Resolutions {
6666
pub def_map: DefMap,
67+
pub assoc_map: AssocMap,
6768
pub freevars: FreevarMap,
6869
pub trait_map: TraitMap,
6970
pub maybe_unused_trait_imports: NodeSet,
@@ -790,6 +791,7 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
790791
},
791792
resolutions: Resolutions {
792793
def_map: resolver.def_map,
794+
assoc_map: resolver.assoc_map,
793795
freevars: resolver.freevars,
794796
trait_map: resolver.trait_map,
795797
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
@@ -866,6 +868,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
866868
TyCtxt::create_and_enter(sess,
867869
arenas,
868870
resolutions.def_map,
871+
resolutions.assoc_map,
869872
resolutions.trait_map,
870873
named_region_map,
871874
hir_map,

src/librustc_incremental/calculate_svh/svh_visitor.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,12 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
802802

803803
if let Some(def) = self.tcx.def_map.borrow().get(&id) {
804804
debug!("hash_resolve: id={:?} def={:?} st={:?}", id, def, self.st);
805-
self.hash_partial_def(def);
805+
self.hash_def(*def);
806+
}
807+
808+
if let Some(partial_def) = self.tcx.assoc_map.get(&id) {
809+
debug!("hash_resolve: id={:?} partial_def={:?} st={:?}", id, partial_def, self.st);
810+
self.hash_partial_def(partial_def);
806811
}
807812

808813
if let Some(traits) = self.tcx.trait_map.get(&id) {

src/librustc_metadata/astencode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use schema::*;
1818

1919
use rustc::middle::cstore::{InlinedItem, InlinedItemRef};
2020
use rustc::middle::const_qualif::ConstQualif;
21-
use rustc::hir::def::{self, Def};
21+
use rustc::hir::def::Def;
2222
use rustc::hir::def_id::DefId;
2323
use rustc::ty::{self, TyCtxt, Ty};
2424

@@ -141,7 +141,7 @@ pub fn decode_inlined_item<'a, 'tcx>(cdata: &CrateMetadata,
141141
for (id, entry) in ast.side_tables.decode((cdata, tcx, id_ranges)) {
142142
match entry {
143143
TableEntry::Def(def) => {
144-
tcx.def_map.borrow_mut().insert(id, def::PathResolution::new(def));
144+
tcx.def_map.borrow_mut().insert(id, def);
145145
}
146146
TableEntry::NodeType(ty) => {
147147
tcx.tables.borrow_mut().node_types.insert(id, ty);

src/librustc_passes/static_recursion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> {
251251
fn visit_expr(&mut self, e: &'ast hir::Expr) {
252252
match e.node {
253253
hir::ExprPath(..) => {
254-
match self.def_map.get(&e.id).map(|d| d.base_def) {
254+
match self.def_map.get(&e.id).cloned() {
255255
Some(Def::Static(def_id, _)) |
256256
Some(Def::AssociatedConst(def_id)) |
257257
Some(Def::Const(def_id)) => {

src/librustc_resolve/lib.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ pub struct Resolver<'a> {
10891089
primitive_type_table: PrimitiveTypeTable,
10901090

10911091
pub def_map: DefMap,
1092+
pub assoc_map: AssocMap,
10921093
pub freevars: FreevarMap,
10931094
freevars_seen: NodeMap<NodeMap<usize>>,
10941095
pub export_map: ExportMap,
@@ -1223,12 +1224,12 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
12231224
}
12241225
}
12251226

1226-
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution> {
1227+
fn get_resolution(&mut self, id: NodeId) -> Option<Def> {
12271228
self.def_map.get(&id).cloned()
12281229
}
12291230

12301231
fn record_resolution(&mut self, id: NodeId, def: Def) {
1231-
self.def_map.insert(id, PathResolution::new(def));
1232+
self.def_map.insert(id, def);
12321233
}
12331234

12341235
fn definitions(&mut self) -> &mut Definitions {
@@ -1306,6 +1307,7 @@ impl<'a> Resolver<'a> {
13061307
primitive_type_table: PrimitiveTypeTable::new(),
13071308

13081309
def_map: NodeMap(),
1310+
assoc_map: NodeMap(),
13091311
freevars: NodeMap(),
13101312
freevars_seen: NodeMap(),
13111313
export_map: NodeMap(),
@@ -2141,7 +2143,7 @@ impl<'a> Resolver<'a> {
21412143
pat.walk(&mut |pat| {
21422144
if let PatKind::Ident(binding_mode, ident, ref sub_pat) = pat.node {
21432145
if sub_pat.is_some() || match self.def_map.get(&pat.id) {
2144-
Some(&PathResolution { base_def: Def::Local(..), .. }) => true,
2146+
Some(&Def::Local(..)) => true,
21452147
_ => false,
21462148
} {
21472149
let binding_info = BindingInfo { span: ident.span, binding_mode: binding_mode };
@@ -2849,9 +2851,9 @@ impl<'a> Resolver<'a> {
28492851

28502852
if let Some(node_id) = self.current_self_type.as_ref().and_then(extract_node_id) {
28512853
// Look for a field with the same name in the current self_type.
2852-
if let Some(resolution) = self.def_map.get(&node_id) {
2853-
match resolution.base_def {
2854-
Def::Struct(did) | Def::Union(did) if resolution.depth == 0 => {
2854+
if let Some(def) = self.def_map.get(&node_id) {
2855+
match *def {
2856+
Def::Struct(did) | Def::Union(did) => {
28552857
if let Some(field_names) = self.field_names.get(&did) {
28562858
if field_names.iter().any(|&field_name| name == field_name) {
28572859
return Field;
@@ -3327,8 +3329,20 @@ impl<'a> Resolver<'a> {
33273329

33283330
fn record_def(&mut self, node_id: NodeId, resolution: PathResolution) {
33293331
debug!("(recording def) recording {:?} for {}", resolution, node_id);
3330-
if let Some(prev_res) = self.def_map.insert(node_id, resolution) {
3331-
panic!("path resolved multiple times ({:?} before, {:?} now)", prev_res, resolution);
3332+
3333+
if resolution.depth != 0 {
3334+
// partial resolution
3335+
if let Some(prev_res) = self.assoc_map.insert(node_id, resolution) {
3336+
panic!("path resolved multiple times ({:?} before, {:?} now)",
3337+
prev_res,
3338+
resolution);
3339+
}
3340+
return;
3341+
}
3342+
3343+
let def = resolution.base_def;
3344+
if let Some(prev_def) = self.def_map.insert(node_id, def) {
3345+
panic!("path resolved multiple times ({:?} before, {:?} now)", prev_def, def);
33323346
}
33333347
}
33343348

@@ -3343,10 +3357,10 @@ impl<'a> Resolver<'a> {
33433357
};
33443358

33453359
let segments: Vec<_> = path.segments.iter().map(|seg| seg.identifier).collect();
3346-
let mut path_resolution = err_path_resolution();
3360+
let mut def = Def::Err;
33473361
let vis = match self.resolve_module_path(&segments, DontUseLexicalScope, Some(path.span)) {
33483362
Success(module) => {
3349-
path_resolution = PathResolution::new(module.def().unwrap());
3363+
def = module.def().unwrap();
33503364
ty::Visibility::Restricted(module.normal_ancestor_id.unwrap())
33513365
}
33523366
Indeterminate => unreachable!(),
@@ -3357,7 +3371,7 @@ impl<'a> Resolver<'a> {
33573371
ty::Visibility::Public
33583372
}
33593373
};
3360-
self.def_map.insert(id, path_resolution);
3374+
self.def_map.insert(id, def);
33613375
if !self.is_accessible(vis) {
33623376
let msg = format!("visibilities can only be restricted to ancestor modules");
33633377
self.session.span_err(path.span, &msg);

src/librustc_resolve/resolve_imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
678678
// this may resolve to either a value or a type, but for documentation
679679
// purposes it's good enough to just favor one over the other.
680680
self.per_ns(|this, ns| if let Some(binding) = result[ns].get().ok() {
681-
this.def_map.entry(directive.id).or_insert(PathResolution::new(binding.def()));
681+
this.def_map.entry(directive.id).or_insert(binding.def());
682682
});
683683

684684
debug!("(resolving single import) successfully resolved import");
@@ -716,8 +716,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
716716

717717
// Record the destination of this import
718718
if let Some(did) = module.def_id() {
719-
let resolution = PathResolution::new(Def::Mod(did));
720-
self.def_map.insert(directive.id, resolution);
719+
let def = Def::Mod(did);
720+
self.def_map.insert(directive.id, def);
721721
}
722722
}
723723

src/librustc_typeck/astconv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
5151
use rustc_const_eval::eval_length;
5252
use hir::{self, SelfKind};
53-
use hir::def::{Def, PathResolution};
53+
use hir::def::Def;
5454
use hir::def_id::DefId;
5555
use hir::print as pprust;
5656
use middle::resolve_lifetime as rl;
@@ -1759,7 +1759,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
17591759

17601760
// Write back the new resolution.
17611761
if path_res.depth != 0 {
1762-
tcx.def_map.borrow_mut().insert(ast_ty.id, PathResolution::new(def));
1762+
tcx.def_map.borrow_mut().insert(ast_ty.id, def);
17631763
}
17641764

17651765
ty

src/librustc_typeck/check/callee.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
219219

220220
if let hir::ExprCall(ref expr, _) = call_expr.node {
221221
let tcx = self.tcx;
222-
if let Some(pr) = tcx.def_map.borrow().get(&expr.id) {
223-
if pr.depth == 0 && pr.base_def != Def::Err {
224-
if let Some(span) = tcx.map.span_if_local(pr.base_def.def_id()) {
222+
if let Some(def) = tcx.def_map.borrow().get(&expr.id).cloned() {
223+
if def != Def::Err {
224+
if let Some(span) = tcx.map.span_if_local(def.def_id()) {
225225
err.span_note(span, "defined here");
226226
}
227227
}

0 commit comments

Comments
 (0)