metadata: Constrain FoundAst::FoundParent to an Item.
This commit is contained in:
parent
b918e37eb3
commit
062a05dde8
4 changed files with 69 additions and 100 deletions
|
|
@ -122,7 +122,7 @@ pub struct ChildItem {
|
|||
|
||||
pub enum FoundAst<'ast> {
|
||||
Found(&'ast InlinedItem),
|
||||
FoundParent(DefId, &'ast InlinedItem),
|
||||
FoundParent(DefId, &'ast hir::Item),
|
||||
NotFound,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -125,61 +125,51 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
|
|||
tcx: &TyCtxt<'tcx>,
|
||||
parent_path: Vec<ast_map::PathElem>,
|
||||
parent_def_path: ast_map::DefPath,
|
||||
par_doc: rbml::Doc,
|
||||
ast_doc: rbml::Doc,
|
||||
orig_did: DefId)
|
||||
-> Result<&'tcx InlinedItem, (Vec<ast_map::PathElem>,
|
||||
ast_map::DefPath)> {
|
||||
match par_doc.opt_child(c::tag_ast) {
|
||||
None => Err((parent_path, parent_def_path)),
|
||||
Some(ast_doc) => {
|
||||
let mut path_as_str = None;
|
||||
debug!("> Decoding inlined fn: {:?}::?",
|
||||
{
|
||||
// Do an Option dance to use the path after it is moved below.
|
||||
let s = ast_map::path_to_string(parent_path.iter().cloned());
|
||||
path_as_str = Some(s);
|
||||
path_as_str.as_ref().map(|x| &x[..])
|
||||
});
|
||||
let mut ast_dsr = reader::Decoder::new(ast_doc);
|
||||
let from_id_range = Decodable::decode(&mut ast_dsr).unwrap();
|
||||
let to_id_range = reserve_id_range(&tcx.sess, from_id_range);
|
||||
let dcx = &DecodeContext {
|
||||
cdata: cdata,
|
||||
tcx: tcx,
|
||||
from_id_range: from_id_range,
|
||||
to_id_range: to_id_range,
|
||||
last_filemap_index: Cell::new(0)
|
||||
};
|
||||
let raw_ii = decode_ast(ast_doc);
|
||||
let ii = ast_map::map_decoded_item(&dcx.tcx.map,
|
||||
parent_path,
|
||||
parent_def_path,
|
||||
raw_ii,
|
||||
dcx);
|
||||
let name = match *ii {
|
||||
InlinedItem::Item(ref i) => i.name,
|
||||
InlinedItem::Foreign(ref i) => i.name,
|
||||
InlinedItem::TraitItem(_, ref ti) => ti.name,
|
||||
InlinedItem::ImplItem(_, ref ii) => ii.name
|
||||
};
|
||||
debug!("Fn named: {}", name);
|
||||
debug!("< Decoded inlined fn: {}::{}",
|
||||
path_as_str.unwrap(),
|
||||
name);
|
||||
region::resolve_inlined_item(&tcx.sess, &tcx.region_maps, ii);
|
||||
decode_side_tables(dcx, ast_doc);
|
||||
copy_item_types(dcx, ii, orig_did);
|
||||
match *ii {
|
||||
InlinedItem::Item(ref i) => {
|
||||
debug!(">>> DECODED ITEM >>>\n{}\n<<< DECODED ITEM <<<",
|
||||
::rustc_front::print::pprust::item_to_string(&i));
|
||||
}
|
||||
_ => { }
|
||||
}
|
||||
|
||||
Ok(ii)
|
||||
}
|
||||
-> &'tcx InlinedItem {
|
||||
let mut path_as_str = None;
|
||||
debug!("> Decoding inlined fn: {:?}::?",
|
||||
{
|
||||
// Do an Option dance to use the path after it is moved below.
|
||||
let s = ast_map::path_to_string(parent_path.iter().cloned());
|
||||
path_as_str = Some(s);
|
||||
path_as_str.as_ref().map(|x| &x[..])
|
||||
});
|
||||
let mut ast_dsr = reader::Decoder::new(ast_doc);
|
||||
let from_id_range = Decodable::decode(&mut ast_dsr).unwrap();
|
||||
let to_id_range = reserve_id_range(&tcx.sess, from_id_range);
|
||||
let dcx = &DecodeContext {
|
||||
cdata: cdata,
|
||||
tcx: tcx,
|
||||
from_id_range: from_id_range,
|
||||
to_id_range: to_id_range,
|
||||
last_filemap_index: Cell::new(0)
|
||||
};
|
||||
let ii = ast_map::map_decoded_item(&dcx.tcx.map,
|
||||
parent_path,
|
||||
parent_def_path,
|
||||
decode_ast(ast_doc),
|
||||
dcx);
|
||||
let name = match *ii {
|
||||
InlinedItem::Item(ref i) => i.name,
|
||||
InlinedItem::Foreign(ref i) => i.name,
|
||||
InlinedItem::TraitItem(_, ref ti) => ti.name,
|
||||
InlinedItem::ImplItem(_, ref ii) => ii.name
|
||||
};
|
||||
debug!("Fn named: {}", name);
|
||||
debug!("< Decoded inlined fn: {}::{}",
|
||||
path_as_str.unwrap(),
|
||||
name);
|
||||
region::resolve_inlined_item(&tcx.sess, &tcx.region_maps, ii);
|
||||
decode_side_tables(dcx, ast_doc);
|
||||
copy_item_types(dcx, ii, orig_did);
|
||||
if let InlinedItem::Item(ref i) = *ii {
|
||||
debug!(">>> DECODED ITEM >>>\n{}\n<<< DECODED ITEM <<<",
|
||||
::rustc_front::print::pprust::item_to_string(&i));
|
||||
}
|
||||
|
||||
ii
|
||||
}
|
||||
|
||||
// ______________________________________________________________________
|
||||
|
|
|
|||
|
|
@ -798,53 +798,36 @@ pub fn get_item_name(intr: &IdentInterner, cdata: Cmd, id: DefIndex) -> ast::Nam
|
|||
item_name(intr, cdata.lookup_item(id))
|
||||
}
|
||||
|
||||
pub fn maybe_get_item_ast<'tcx>(cdata: Cmd,
|
||||
tcx: &TyCtxt<'tcx>,
|
||||
id: DefIndex,
|
||||
pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &TyCtxt<'tcx>, id: DefIndex)
|
||||
-> FoundAst<'tcx> {
|
||||
debug!("Looking up item: {:?}", id);
|
||||
let item_doc = cdata.lookup_item(id);
|
||||
let item_did = item_def_id(item_doc, cdata);
|
||||
let parent_path = {
|
||||
let mut path = item_path(item_doc);
|
||||
path.pop();
|
||||
path
|
||||
};
|
||||
let parent_def_path = {
|
||||
let mut def_path = def_path(cdata, id);
|
||||
def_path.pop();
|
||||
def_path
|
||||
};
|
||||
match decode_inlined_item(cdata,
|
||||
tcx,
|
||||
parent_path,
|
||||
parent_def_path,
|
||||
item_doc,
|
||||
item_did) {
|
||||
Ok(ii) => FoundAst::Found(ii),
|
||||
Err((mut parent_path, mut parent_def_path)) => {
|
||||
match item_parent_item(cdata, item_doc) {
|
||||
Some(parent_did) => {
|
||||
// Remove the last element from the paths, since we are now
|
||||
// trying to inline the parent.
|
||||
parent_path.pop();
|
||||
parent_def_path.pop();
|
||||
|
||||
let parent_item = cdata.lookup_item(parent_did.index);
|
||||
match decode_inlined_item(cdata,
|
||||
tcx,
|
||||
parent_path,
|
||||
parent_def_path,
|
||||
parent_item,
|
||||
parent_did) {
|
||||
Ok(ii) => FoundAst::FoundParent(parent_did, ii),
|
||||
Err(_) => FoundAst::NotFound
|
||||
}
|
||||
}
|
||||
None => FoundAst::NotFound
|
||||
let mut parent_path = item_path(item_doc);
|
||||
parent_path.pop();
|
||||
let mut parent_def_path = def_path(cdata, id);
|
||||
parent_def_path.pop();
|
||||
if let Some(ast_doc) = reader::maybe_get_doc(item_doc, tag_ast as usize) {
|
||||
let ii = decode_inlined_item(cdata, tcx, parent_path,
|
||||
parent_def_path,
|
||||
ast_doc, item_did);
|
||||
return FoundAst::Found(ii);
|
||||
} else if let Some(parent_did) = item_parent_item(cdata, item_doc) {
|
||||
// Remove the last element from the paths, since we are now
|
||||
// trying to inline the parent.
|
||||
parent_path.pop();
|
||||
parent_def_path.pop();
|
||||
let parent_doc = cdata.lookup_item(parent_did.index);
|
||||
if let Some(ast_doc) = reader::maybe_get_doc(parent_doc, tag_ast as usize) {
|
||||
let ii = decode_inlined_item(cdata, tcx, parent_path,
|
||||
parent_def_path,
|
||||
ast_doc, parent_did);
|
||||
if let &InlinedItem::Item(ref i) = ii {
|
||||
return FoundAst::FoundParent(parent_did, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
FoundAst::NotFound
|
||||
}
|
||||
|
||||
pub fn is_item_mir_available<'tcx>(cdata: Cmd, id: DefIndex) -> bool {
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: DefId) -> Option<DefId> {
|
|||
ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
|
||||
item.id
|
||||
}
|
||||
FoundAst::FoundParent(parent_id, &InlinedItem::Item(ref item)) => {
|
||||
FoundAst::FoundParent(parent_id, item) => {
|
||||
ccx.external().borrow_mut().insert(parent_id, Some(item.id));
|
||||
ccx.external_srcs().borrow_mut().insert(item.id, parent_id);
|
||||
|
||||
|
|
@ -101,7 +101,7 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: DefId) -> Option<DefId> {
|
|||
match item.node {
|
||||
hir::ItemEnum(ref ast_def, _) => {
|
||||
let ast_vs = &ast_def.variants;
|
||||
let ty_vs = &ccx.tcx().lookup_adt_def(parent_id).variants;
|
||||
let ty_vs = &tcx.lookup_adt_def(parent_id).variants;
|
||||
assert_eq!(ast_vs.len(), ty_vs.len());
|
||||
for (ast_v, ty_v) in ast_vs.iter().zip(ty_vs.iter()) {
|
||||
if ty_v.did == fn_id { my_id = ast_v.node.data.id(); }
|
||||
|
|
@ -123,10 +123,6 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: DefId) -> Option<DefId> {
|
|||
trans_item(ccx, &item);
|
||||
my_id
|
||||
}
|
||||
FoundAst::FoundParent(_, _) => {
|
||||
ccx.sess().bug("maybe_get_item_ast returned a FoundParent \
|
||||
with a non-item parent");
|
||||
}
|
||||
FoundAst::Found(&InlinedItem::TraitItem(_, ref trait_item)) => {
|
||||
ccx.external().borrow_mut().insert(fn_id, Some(trait_item.id));
|
||||
ccx.external_srcs().borrow_mut().insert(trait_item.id, fn_id);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue