Skip to content

Commit a274617

Browse files
committed
Auto merge of #38278 - jseyfried:improve_proc_macro_def_ids, r=michaelwoerister
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
2 parents 2ac7121 + 5200a11 commit a274617

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>,
@@ -515,7 +515,12 @@ impl<'tcx> EntryKind<'tcx> {
515515
}
516516

517517
impl<'a, 'tcx> CrateMetadata {
518+
fn is_proc_macro(&self, id: DefIndex) -> bool {
519+
self.proc_macros.is_some() && id != CRATE_DEF_INDEX
520+
}
521+
518522
fn maybe_entry(&self, item_id: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
523+
assert!(!self.is_proc_macro(item_id));
519524
self.root.index.lookup(self.blob.raw_bytes(), item_id)
520525
}
521526

@@ -548,18 +553,17 @@ impl<'a, 'tcx> CrateMetadata {
548553
}
549554

550555
pub fn get_def(&self, index: DefIndex) -> Option<Def> {
551-
if self.proc_macros.is_some() {
552-
Some(match index {
553-
CRATE_DEF_INDEX => Def::Mod(self.local_def_id(index)),
554-
_ => Def::Macro(self.local_def_id(index)),
555-
})
556-
} else {
557-
self.entry(index).kind.to_def(self.local_def_id(index))
556+
match self.is_proc_macro(index) {
557+
true => Some(Def::Macro(self.local_def_id(index))),
558+
false => self.entry(index).kind.to_def(self.local_def_id(index)),
558559
}
559560
}
560561

561562
pub fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
562-
self.entry(index).span.decode((self, sess))
563+
match self.is_proc_macro(index) {
564+
true => DUMMY_SP,
565+
false => self.entry(index).span.decode((self, sess)),
566+
}
563567
}
564568

565569
pub fn get_trait_def(&self,
@@ -670,23 +674,23 @@ impl<'a, 'tcx> CrateMetadata {
670674
}
671675

672676
pub fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
673-
match self.proc_macros {
674-
Some(_) if id != CRATE_DEF_INDEX => None,
675-
_ => self.entry(id).stability.map(|stab| stab.decode(self)),
677+
match self.is_proc_macro(id) {
678+
true => None,
679+
false => self.entry(id).stability.map(|stab| stab.decode(self)),
676680
}
677681
}
678682

679683
pub fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
680-
match self.proc_macros {
681-
Some(_) if id != CRATE_DEF_INDEX => None,
682-
_ => self.entry(id).deprecation.map(|depr| depr.decode(self)),
684+
match self.is_proc_macro(id) {
685+
true => None,
686+
false => self.entry(id).deprecation.map(|depr| depr.decode(self)),
683687
}
684688
}
685689

686690
pub fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
687-
match self.proc_macros {
688-
Some(_) => ty::Visibility::Public,
689-
_ => self.entry(id).visibility,
691+
match self.is_proc_macro(id) {
692+
true => ty::Visibility::Public,
693+
false => self.entry(id).visibility,
690694
}
691695
}
692696

@@ -832,6 +836,7 @@ impl<'a, 'tcx> CrateMetadata {
832836
id: DefIndex)
833837
-> Option<&'tcx InlinedItem> {
834838
debug!("Looking up item: {:?}", id);
839+
if self.is_proc_macro(id) { return None; }
835840
let item_doc = self.entry(id);
836841
let item_did = self.local_def_id(id);
837842
let parent_def_id = self.local_def_id(self.def_key(id).parent.unwrap());
@@ -844,6 +849,7 @@ impl<'a, 'tcx> CrateMetadata {
844849
}
845850

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

@@ -874,7 +880,10 @@ impl<'a, 'tcx> CrateMetadata {
874880
tcx: TyCtxt<'a, 'tcx, 'tcx>,
875881
id: DefIndex)
876882
-> Option<Mir<'tcx>> {
877-
self.entry(id).mir.map(|mir| mir.decode((self, tcx)))
883+
match self.is_proc_macro(id) {
884+
true => None,
885+
false => self.entry(id).mir.map(|mir| mir.decode((self, tcx))),
886+
}
878887
}
879888

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

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

11321141
pub fn def_key(&self, id: DefIndex) -> hir_map::DefKey {
11331142
debug!("def_key: id={:?}", id);
1134-
self.entry(id).def_key.decode(self)
1143+
if self.is_proc_macro(id) {
1144+
let name = self.proc_macros.as_ref().unwrap()[id.as_usize() - 1].0;
1145+
hir_map::DefKey {
1146+
parent: Some(CRATE_DEF_INDEX),
1147+
disambiguated_data: hir_map::DisambiguatedDefPathData {
1148+
data: hir_map::DefPathData::MacroDef(name.as_str()),
1149+
disambiguator: 0,
1150+
},
1151+
}
1152+
} else {
1153+
self.entry(id).def_key.decode(self)
1154+
}
11351155
}
11361156

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

0 commit comments

Comments
 (0)