Skip to content

Commit 4c4195f

Browse files
Fix def paths creation for items inlined from external crates.
Avoid duplicating the last element of the def path which led to paths like "std::slice::into_vec::into_vec".
1 parent 42c3ef8 commit 4c4195f

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

src/librustc/front/map/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -839,11 +839,10 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest) -> Map<'ast> {
839839
}
840840

841841
/// Used for items loaded from external crate that are being inlined into this
842-
/// crate. The `path` should be the path to the item but should not include
843-
/// the item itself.
842+
/// crate.
844843
pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
845-
path: Vec<PathElem>,
846-
def_path: DefPath,
844+
parent_path: Vec<PathElem>,
845+
parent_def_path: DefPath,
847846
ii: InlinedItem,
848847
fold_ops: F)
849848
-> &'ast InlinedItem {
@@ -862,7 +861,7 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
862861
};
863862

864863
let ii_parent = map.forest.inlined_items.alloc(InlinedParent {
865-
path: path,
864+
path: parent_path,
866865
ii: ii
867866
});
868867

@@ -872,7 +871,7 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
872871
map.krate(),
873872
ii_parent,
874873
ii_parent_id,
875-
def_path,
874+
parent_def_path,
876875
mem::replace(&mut *map.map.borrow_mut(), vec![]),
877876
mem::replace(&mut *map.definitions.borrow_mut(), Definitions::new()));
878877
ii_parent.ii.visit(&mut collector);

src/librustc_metadata/astencode.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,20 @@ impl<'a, 'b, 'c, 'tcx> ast_map::FoldOps for &'a DecodeContext<'b, 'c, 'tcx> {
124124
/// ast-map.
125125
pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
126126
tcx: &ty::ctxt<'tcx>,
127-
path: Vec<ast_map::PathElem>,
128-
def_path: ast_map::DefPath,
127+
parent_path: Vec<ast_map::PathElem>,
128+
parent_def_path: ast_map::DefPath,
129129
par_doc: rbml::Doc,
130130
orig_did: DefId)
131131
-> Result<&'tcx InlinedItem, (Vec<ast_map::PathElem>,
132132
ast_map::DefPath)> {
133133
match par_doc.opt_child(c::tag_ast) {
134-
None => Err((path, def_path)),
134+
None => Err((parent_path, parent_def_path)),
135135
Some(ast_doc) => {
136136
let mut path_as_str = None;
137137
debug!("> Decoding inlined fn: {:?}::?",
138138
{
139139
// Do an Option dance to use the path after it is moved below.
140-
let s = ast_map::path_to_string(path.iter().cloned());
140+
let s = ast_map::path_to_string(parent_path.iter().cloned());
141141
path_as_str = Some(s);
142142
path_as_str.as_ref().map(|x| &x[..])
143143
});
@@ -152,8 +152,11 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
152152
last_filemap_index: Cell::new(0)
153153
};
154154
let raw_ii = decode_ast(ast_doc);
155-
let ii = ast_map::map_decoded_item(&dcx.tcx.map, path, def_path, raw_ii, dcx);
156-
155+
let ii = ast_map::map_decoded_item(&dcx.tcx.map,
156+
parent_path,
157+
parent_def_path,
158+
raw_ii,
159+
dcx);
157160
let name = match *ii {
158161
InlinedItem::Item(ref i) => i.name,
159162
InlinedItem::Foreign(ref i) => i.name,

src/librustc_metadata/decoder.rs

+36-11
Original file line numberDiff line numberDiff line change
@@ -763,29 +763,54 @@ pub fn get_item_name(intr: &IdentInterner, cdata: Cmd, id: DefIndex) -> ast::Nam
763763
pub type DecodeInlinedItem<'a> =
764764
Box<for<'tcx> FnMut(Cmd,
765765
&ty::ctxt<'tcx>,
766-
Vec<hir_map::PathElem>,
767-
hir_map::DefPath,
766+
Vec<hir_map::PathElem>, // parent_path
767+
hir_map::DefPath, // parent_def_path
768768
rbml::Doc,
769769
DefId)
770770
-> Result<&'tcx InlinedItem, (Vec<hir_map::PathElem>,
771771
hir_map::DefPath)> + 'a>;
772772

773-
pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: DefIndex,
773+
pub fn maybe_get_item_ast<'tcx>(cdata: Cmd,
774+
tcx: &ty::ctxt<'tcx>,
775+
id: DefIndex,
774776
mut decode_inlined_item: DecodeInlinedItem)
775777
-> FoundAst<'tcx> {
776778
debug!("Looking up item: {:?}", id);
777779
let item_doc = cdata.lookup_item(id);
778780
let item_did = item_def_id(item_doc, cdata);
779-
let path = item_path(item_doc).split_last().unwrap().1.to_vec();
780-
let def_path = def_path(cdata, id);
781-
match decode_inlined_item(cdata, tcx, path, def_path, item_doc, item_did) {
781+
let parent_path = {
782+
let mut path = item_path(item_doc);
783+
path.pop();
784+
path
785+
};
786+
let parent_def_path = {
787+
let mut def_path = def_path(cdata, id);
788+
def_path.pop();
789+
def_path
790+
};
791+
match decode_inlined_item(cdata,
792+
tcx,
793+
parent_path,
794+
parent_def_path,
795+
item_doc,
796+
item_did) {
782797
Ok(ii) => FoundAst::Found(ii),
783-
Err((path, def_path)) => {
798+
Err((mut parent_path, mut parent_def_path)) => {
784799
match item_parent_item(cdata, item_doc) {
785-
Some(did) => {
786-
let parent_item = cdata.lookup_item(did.index);
787-
match decode_inlined_item(cdata, tcx, path, def_path, parent_item, did) {
788-
Ok(ii) => FoundAst::FoundParent(did, ii),
800+
Some(parent_did) => {
801+
// Remove the last element from the paths, since we are now
802+
// trying to inline the parent.
803+
parent_path.pop();
804+
parent_def_path.pop();
805+
806+
let parent_item = cdata.lookup_item(parent_did.index);
807+
match decode_inlined_item(cdata,
808+
tcx,
809+
parent_path,
810+
parent_def_path,
811+
parent_item,
812+
parent_did) {
813+
Ok(ii) => FoundAst::FoundParent(parent_did, ii),
789814
Err(_) => FoundAst::NotFound
790815
}
791816
}

0 commit comments

Comments
 (0)