Skip to content

Commit 49d3636

Browse files
committed
track def-id for inlined items
1 parent 3341533 commit 49d3636

File tree

5 files changed

+72
-16
lines changed

5 files changed

+72
-16
lines changed

src/librustc/front/map/collector.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::MapEntry::*;
1414
use rustc_front::hir::*;
1515
use rustc_front::util;
1616
use rustc_front::intravisit::{self, Visitor};
17-
use middle::def_id::{CRATE_DEF_INDEX, DefIndex};
17+
use middle::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};
1818
use std::iter::repeat;
1919
use syntax::ast::{NodeId, CRATE_NODE_ID, DUMMY_NODE_ID};
2020
use syntax::codemap::Span;
@@ -50,6 +50,7 @@ impl<'ast> NodeCollector<'ast> {
5050
parent: &'ast InlinedParent,
5151
parent_node: NodeId,
5252
parent_def_path: DefPath,
53+
parent_def_id: DefId,
5354
map: Vec<MapEntry<'ast>>,
5455
definitions: Definitions)
5556
-> NodeCollector<'ast> {
@@ -60,8 +61,14 @@ impl<'ast> NodeCollector<'ast> {
6061
definitions: definitions,
6162
};
6263

64+
assert_eq!(parent_def_path.krate, parent_def_id.krate);
65+
let root_path = Box::new(InlinedRootPath {
66+
data: parent_def_path.data,
67+
def_id: parent_def_id,
68+
});
69+
6370
collector.insert_entry(parent_node, RootInlinedParent(parent));
64-
collector.create_def(parent_node, DefPathData::InlinedRoot(parent_def_path));
71+
collector.create_def(parent_node, DefPathData::InlinedRoot(root_path));
6572

6673
collector
6774
}

src/librustc/front/map/definitions.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,38 @@ pub struct DefData {
6060
}
6161

6262
pub type DefPath = Vec<DisambiguatedDefPathData>;
63+
/// Root of an inlined item. We track the `DefPath` of the item within
64+
/// the original crate but also its def-id. This is kind of an
65+
/// augmented version of a `DefPath` that includes a `DefId`. This is
66+
/// all sort of ugly but the hope is that inlined items will be going
67+
/// away soon anyway.
68+
///
69+
/// Some of the constraints that led to the current approach:
70+
///
71+
/// - I don't want to have a `DefId` in the main `DefPath` because
72+
/// that gets serialized for incr. comp., and when reloaded the
73+
/// `DefId` is no longer valid. I'd rather maintain the invariant
74+
/// that every `DefId` is valid, and a potentially outdated `DefId` is
75+
/// represented as a `DefPath`.
76+
/// - (We don't serialize def-paths from inlined items, so it's ok to have one here.)
77+
/// - We need to be able to extract the def-id from inline items to
78+
/// make the symbol name. In theory we could retrace it from the
79+
/// data, but the metadata doesn't have the required indices, and I
80+
/// don't want to write the code to create one just for this.
81+
/// - It may be that we don't actually need `data` at all. We'll have
82+
/// to see about that.
83+
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
84+
pub struct InlinedRootPath {
85+
pub data: Vec<DisambiguatedDefPathData>,
86+
pub def_id: DefId,
87+
}
6388

6489
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
6590
pub enum DefPathData {
6691
// Root: these should only be used for the root nodes, because
6792
// they are treated specially by the `def_path` function.
6893
CrateRoot,
69-
InlinedRoot(DefPath),
94+
InlinedRoot(Box<InlinedRootPath>),
7095

7196
// Catch-all for random DefId things like DUMMY_NODE_ID
7297
Misc,

src/librustc/front/map/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ pub use self::Node::*;
1212
pub use self::PathElem::*;
1313
use self::MapEntry::*;
1414
use self::collector::NodeCollector;
15-
pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData, DisambiguatedDefPathData};
15+
pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
16+
DisambiguatedDefPathData, InlinedRootPath};
1617

1718
use dep_graph::{DepGraph, DepNode};
1819

@@ -322,7 +323,8 @@ impl<'ast> Map<'ast> {
322323
id = p,
323324

324325
RootCrate |
325-
RootInlinedParent(_) => // FIXME(#2369) clarify story about cross-crate dep tracking
326+
RootInlinedParent(_) =>
327+
// FIXME(#32015) clarify story about cross-crate dep tracking
326328
return DepNode::Krate,
327329

328330
NotPresent =>
@@ -958,6 +960,7 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest) -> Map<'ast> {
958960
pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
959961
parent_path: Vec<PathElem>,
960962
parent_def_path: DefPath,
963+
parent_def_id: DefId,
961964
ii: InlinedItem,
962965
fold_ops: F)
963966
-> &'ast InlinedItem {
@@ -987,6 +990,7 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
987990
ii_parent,
988991
ii_parent_id,
989992
parent_def_path,
993+
parent_def_id,
990994
mem::replace(&mut *map.map.borrow_mut(), vec![]),
991995
mem::replace(&mut *map.definitions.borrow_mut(), Definitions::new()));
992996
ii_parent.ii.visit(&mut collector);

src/librustc_metadata/astencode.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
125125
tcx: &TyCtxt<'tcx>,
126126
parent_path: Vec<ast_map::PathElem>,
127127
parent_def_path: ast_map::DefPath,
128+
parent_did: DefId,
128129
ast_doc: rbml::Doc,
129130
orig_did: DefId)
130131
-> &'tcx InlinedItem {
@@ -149,6 +150,7 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
149150
let ii = ast_map::map_decoded_item(&dcx.tcx.map,
150151
parent_path,
151152
parent_def_path,
153+
parent_did,
152154
decode_ast(ast_doc),
153155
dcx);
154156
let name = match *ii {
@@ -349,8 +351,8 @@ fn simplify_ast(ii: InlinedItemRef) -> InlinedItem {
349351
}
350352
}
351353

352-
fn decode_ast(par_doc: rbml::Doc) -> InlinedItem {
353-
let chi_doc = par_doc.get(c::tag_tree as usize);
354+
fn decode_ast(item_doc: rbml::Doc) -> InlinedItem {
355+
let chi_doc = item_doc.get(c::tag_tree as usize);
354356
let mut rbml_r = reader::Decoder::new(chi_doc);
355357
rbml_r.read_opaque(|decoder, _| Decodable::decode(decoder)).unwrap()
356358
}
@@ -1280,8 +1282,8 @@ fn encode_item_ast(rbml_w: &mut Encoder, item: &hir::Item) {
12801282
}
12811283

12821284
#[cfg(test)]
1283-
fn decode_item_ast(par_doc: rbml::Doc) -> hir::Item {
1284-
let chi_doc = par_doc.get(c::tag_tree as usize);
1285+
fn decode_item_ast(item_doc: rbml::Doc) -> hir::Item {
1286+
let chi_doc = item_doc.get(c::tag_tree as usize);
12851287
let mut d = reader::Decoder::new(chi_doc);
12861288
Decodable::decode(&mut d).unwrap()
12871289
}

src/librustc_metadata/decoder.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -803,25 +803,43 @@ pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &TyCtxt<'tcx>, id: DefIndex)
803803
debug!("Looking up item: {:?}", id);
804804
let item_doc = cdata.lookup_item(id);
805805
let item_did = item_def_id(item_doc, cdata);
806+
let parent_def_id = DefId {
807+
krate: cdata.cnum,
808+
index: def_key(cdata, id).parent.unwrap()
809+
};
806810
let mut parent_path = item_path(item_doc);
807811
parent_path.pop();
808812
let mut parent_def_path = def_path(cdata, id);
809813
parent_def_path.pop();
810814
if let Some(ast_doc) = reader::maybe_get_doc(item_doc, tag_ast as usize) {
811-
let ii = decode_inlined_item(cdata, tcx, parent_path,
815+
let ii = decode_inlined_item(cdata,
816+
tcx,
817+
parent_path,
812818
parent_def_path,
813-
ast_doc, item_did);
819+
parent_def_id,
820+
ast_doc,
821+
item_did);
814822
return FoundAst::Found(ii);
815823
} else if let Some(parent_did) = item_parent_item(cdata, item_doc) {
816824
// Remove the last element from the paths, since we are now
817825
// trying to inline the parent.
818-
parent_path.pop();
819-
parent_def_path.pop();
826+
let grandparent_def_id = DefId {
827+
krate: cdata.cnum,
828+
index: def_key(cdata, parent_def_id.index).parent.unwrap()
829+
};
830+
let mut grandparent_path = parent_path;
831+
grandparent_path.pop();
832+
let mut grandparent_def_path = parent_def_path;
833+
grandparent_def_path.pop();
820834
let parent_doc = cdata.lookup_item(parent_did.index);
821835
if let Some(ast_doc) = reader::maybe_get_doc(parent_doc, tag_ast as usize) {
822-
let ii = decode_inlined_item(cdata, tcx, parent_path,
823-
parent_def_path,
824-
ast_doc, parent_did);
836+
let ii = decode_inlined_item(cdata,
837+
tcx,
838+
grandparent_path,
839+
grandparent_def_path,
840+
grandparent_def_id,
841+
ast_doc,
842+
parent_did);
825843
if let &InlinedItem::Item(ref i) = ii {
826844
return FoundAst::FoundParent(parent_did, i);
827845
}

0 commit comments

Comments
 (0)