Skip to content

Commit 5200a11

Browse files
committed
Improve proc-macro def ids.
1 parent 7b06438 commit 5200a11

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

src/librustc_metadata/decoder.rs

+42-22
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
4444
use syntax::attr;
4545
use syntax::ast::{self, NodeId};
4646
use syntax::codemap;
47-
use syntax_pos::{self, Span, BytePos, Pos};
47+
use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP};
4848

4949
pub struct DecodeContext<'a, 'tcx: 'a> {
5050
opaque: opaque::Decoder<'a>,
@@ -507,7 +507,12 @@ impl<'tcx> EntryKind<'tcx> {
507507
}
508508

509509
impl<'a, 'tcx> CrateMetadata {
510+
fn is_proc_macro(&self, id: DefIndex) -> bool {
511+
self.proc_macros.is_some() && id != CRATE_DEF_INDEX
512+
}
513+
510514
fn maybe_entry(&self, item_id: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
515+
assert!(!self.is_proc_macro(item_id));
511516
self.root.index.lookup(self.blob.raw_bytes(), item_id)
512517
}
513518

@@ -540,18 +545,17 @@ impl<'a, 'tcx> CrateMetadata {
540545
}
541546

542547
pub fn get_def(&self, index: DefIndex) -> Option<Def> {
543-
if self.proc_macros.is_some() {
544-
Some(match index {
545-
CRATE_DEF_INDEX => Def::Mod(self.local_def_id(index)),
546-
_ => Def::Macro(self.local_def_id(index)),
547-
})
548-
} else {
549-
self.entry(index).kind.to_def(self.local_def_id(index))
548+
match self.is_proc_macro(index) {
549+
true => Some(Def::Macro(self.local_def_id(index))),
550+
false => self.entry(index).kind.to_def(self.local_def_id(index)),
550551
}
551552
}
552553

553554
pub fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
554-
self.entry(index).span.decode((self, sess))
555+
match self.is_proc_macro(index) {
556+
true => DUMMY_SP,
557+
false => self.entry(index).span.decode((self, sess)),
558+
}
555559
}
556560

557561
pub fn get_trait_def(&self,
@@ -662,23 +666,23 @@ impl<'a, 'tcx> CrateMetadata {
662666
}
663667

664668
pub fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
665-
match self.proc_macros {
666-
Some(_) if id != CRATE_DEF_INDEX => None,
667-
_ => self.entry(id).stability.map(|stab| stab.decode(self)),
669+
match self.is_proc_macro(id) {
670+
true => None,
671+
false => self.entry(id).stability.map(|stab| stab.decode(self)),
668672
}
669673
}
670674

671675
pub fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
672-
match self.proc_macros {
673-
Some(_) if id != CRATE_DEF_INDEX => None,
674-
_ => self.entry(id).deprecation.map(|depr| depr.decode(self)),
676+
match self.is_proc_macro(id) {
677+
true => None,
678+
false => self.entry(id).deprecation.map(|depr| depr.decode(self)),
675679
}
676680
}
677681

678682
pub fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
679-
match self.proc_macros {
680-
Some(_) => ty::Visibility::Public,
681-
_ => self.entry(id).visibility,
683+
match self.is_proc_macro(id) {
684+
true => ty::Visibility::Public,
685+
false => self.entry(id).visibility,
682686
}
683687
}
684688

@@ -824,6 +828,7 @@ impl<'a, 'tcx> CrateMetadata {
824828
id: DefIndex)
825829
-> Option<&'tcx InlinedItem> {
826830
debug!("Looking up item: {:?}", id);
831+
if self.is_proc_macro(id) { return None; }
827832
let item_doc = self.entry(id);
828833
let item_did = self.local_def_id(id);
829834
let parent_def_id = self.local_def_id(self.def_key(id).parent.unwrap());
@@ -836,14 +841,18 @@ impl<'a, 'tcx> CrateMetadata {
836841
}
837842

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

842848
pub fn maybe_get_item_mir(&self,
843849
tcx: TyCtxt<'a, 'tcx, 'tcx>,
844850
id: DefIndex)
845851
-> Option<Mir<'tcx>> {
846-
self.entry(id).mir.map(|mir| mir.decode((self, tcx)))
852+
match self.is_proc_macro(id) {
853+
true => None,
854+
false => self.entry(id).mir.map(|mir| mir.decode((self, tcx))),
855+
}
847856
}
848857

849858
pub fn get_associated_item(&self, id: DefIndex) -> Option<ty::AssociatedItem> {
@@ -919,7 +928,7 @@ impl<'a, 'tcx> CrateMetadata {
919928
}
920929

921930
pub fn get_item_attrs(&self, node_id: DefIndex) -> Vec<ast::Attribute> {
922-
if self.proc_macros.is_some() && node_id != CRATE_DEF_INDEX {
931+
if self.is_proc_macro(node_id) {
923932
return Vec::new();
924933
}
925934
// The attributes for a tuple struct are attached to the definition, not the ctor;
@@ -1105,15 +1114,26 @@ impl<'a, 'tcx> CrateMetadata {
11051114

11061115
pub fn def_key(&self, id: DefIndex) -> hir_map::DefKey {
11071116
debug!("def_key: id={:?}", id);
1108-
self.entry(id).def_key.decode(self)
1117+
if self.is_proc_macro(id) {
1118+
let name = self.proc_macros.as_ref().unwrap()[id.as_usize() - 1].0;
1119+
hir_map::DefKey {
1120+
parent: Some(CRATE_DEF_INDEX),
1121+
disambiguated_data: hir_map::DisambiguatedDefPathData {
1122+
data: hir_map::DefPathData::MacroDef(name.as_str()),
1123+
disambiguator: 0,
1124+
},
1125+
}
1126+
} else {
1127+
self.entry(id).def_key.decode(self)
1128+
}
11091129
}
11101130

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

0 commit comments

Comments
 (0)