Skip to content

Commit

Permalink
Auto merge of #38278 - jseyfried:improve_proc_macro_def_ids, r=michae…
Browse files Browse the repository at this point in the history
…lwoerister

Improve proc-macro def ids

Support `cstore.relative_def_path(id)` and `cstore.def_key(id)` with proc-macro def ids.
Fixes #38207.
r? @nikomatsakis
  • Loading branch information
bors committed Dec 13, 2016
2 parents 2ac7121 + 5200a11 commit a274617
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
use syntax::attr;
use syntax::ast::{self, NodeId};
use syntax::codemap;
use syntax_pos::{self, Span, BytePos, Pos};
use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP};

pub struct DecodeContext<'a, 'tcx: 'a> {
opaque: opaque::Decoder<'a>,
Expand Down Expand Up @@ -515,7 +515,12 @@ impl<'tcx> EntryKind<'tcx> {
}

impl<'a, 'tcx> CrateMetadata {
fn is_proc_macro(&self, id: DefIndex) -> bool {
self.proc_macros.is_some() && id != CRATE_DEF_INDEX
}

fn maybe_entry(&self, item_id: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
assert!(!self.is_proc_macro(item_id));
self.root.index.lookup(self.blob.raw_bytes(), item_id)
}

Expand Down Expand Up @@ -548,18 +553,17 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn get_def(&self, index: DefIndex) -> Option<Def> {
if self.proc_macros.is_some() {
Some(match index {
CRATE_DEF_INDEX => Def::Mod(self.local_def_id(index)),
_ => Def::Macro(self.local_def_id(index)),
})
} else {
self.entry(index).kind.to_def(self.local_def_id(index))
match self.is_proc_macro(index) {
true => Some(Def::Macro(self.local_def_id(index))),
false => self.entry(index).kind.to_def(self.local_def_id(index)),
}
}

pub fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
self.entry(index).span.decode((self, sess))
match self.is_proc_macro(index) {
true => DUMMY_SP,
false => self.entry(index).span.decode((self, sess)),
}
}

pub fn get_trait_def(&self,
Expand Down Expand Up @@ -670,23 +674,23 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
match self.proc_macros {
Some(_) if id != CRATE_DEF_INDEX => None,
_ => self.entry(id).stability.map(|stab| stab.decode(self)),
match self.is_proc_macro(id) {
true => None,
false => self.entry(id).stability.map(|stab| stab.decode(self)),
}
}

pub fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
match self.proc_macros {
Some(_) if id != CRATE_DEF_INDEX => None,
_ => self.entry(id).deprecation.map(|depr| depr.decode(self)),
match self.is_proc_macro(id) {
true => None,
false => self.entry(id).deprecation.map(|depr| depr.decode(self)),
}
}

pub fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
match self.proc_macros {
Some(_) => ty::Visibility::Public,
_ => self.entry(id).visibility,
match self.is_proc_macro(id) {
true => ty::Visibility::Public,
false => self.entry(id).visibility,
}
}

Expand Down Expand Up @@ -832,6 +836,7 @@ impl<'a, 'tcx> CrateMetadata {
id: DefIndex)
-> Option<&'tcx InlinedItem> {
debug!("Looking up item: {:?}", id);
if self.is_proc_macro(id) { return None; }
let item_doc = self.entry(id);
let item_did = self.local_def_id(id);
let parent_def_id = self.local_def_id(self.def_key(id).parent.unwrap());
Expand All @@ -844,6 +849,7 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn is_item_mir_available(&self, id: DefIndex) -> bool {
!self.is_proc_macro(id) &&
self.maybe_entry(id).and_then(|item| item.decode(self).mir).is_some()
}

Expand Down Expand Up @@ -874,7 +880,10 @@ impl<'a, 'tcx> CrateMetadata {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: DefIndex)
-> Option<Mir<'tcx>> {
self.entry(id).mir.map(|mir| mir.decode((self, tcx)))
match self.is_proc_macro(id) {
true => None,
false => self.entry(id).mir.map(|mir| mir.decode((self, tcx))),
}
}

pub fn get_associated_item(&self, id: DefIndex) -> Option<ty::AssociatedItem> {
Expand Down Expand Up @@ -950,7 +959,7 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn get_item_attrs(&self, node_id: DefIndex) -> Vec<ast::Attribute> {
if self.proc_macros.is_some() && node_id != CRATE_DEF_INDEX {
if self.is_proc_macro(node_id) {
return Vec::new();
}
// The attributes for a tuple struct are attached to the definition, not the ctor;
Expand Down Expand Up @@ -1131,15 +1140,26 @@ impl<'a, 'tcx> CrateMetadata {

pub fn def_key(&self, id: DefIndex) -> hir_map::DefKey {
debug!("def_key: id={:?}", id);
self.entry(id).def_key.decode(self)
if self.is_proc_macro(id) {
let name = self.proc_macros.as_ref().unwrap()[id.as_usize() - 1].0;
hir_map::DefKey {
parent: Some(CRATE_DEF_INDEX),
disambiguated_data: hir_map::DisambiguatedDefPathData {
data: hir_map::DefPathData::MacroDef(name.as_str()),
disambiguator: 0,
},
}
} else {
self.entry(id).def_key.decode(self)
}
}

// Returns the path leading to the thing with this `id`. Note that
// some def-ids don't wind up in the metadata, so `def_path` sometimes
// returns `None`
pub fn def_path(&self, id: DefIndex) -> Option<hir_map::DefPath> {
debug!("def_path(id={:?})", id);
if self.maybe_entry(id).is_some() {
if self.is_proc_macro(id) || self.maybe_entry(id).is_some() {
Some(hir_map::DefPath::make(self.cnum, id, |parent| self.def_key(parent)))
} else {
None
Expand Down

0 comments on commit a274617

Please sign in to comment.