Skip to content

Commit b4c6a39

Browse files
committed
change depr_map to use DeprecationEntry
1 parent 75e2624 commit b4c6a39

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

src/librustc/middle/stability.rs

+48-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use session::Session;
1919
use lint;
2020
use middle::cstore::LOCAL_CRATE;
2121
use hir::def::Def;
22-
use hir::def_id::{CRATE_DEF_INDEX, DefId};
22+
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};
2323
use ty::{self, TyCtxt};
2424
use middle::privacy::AccessLevels;
2525
use syntax::parse::token::InternedString;
@@ -61,12 +61,46 @@ enum AnnotationKind {
6161
Container,
6262
}
6363

64+
/// An entry in the `depr_map`.
65+
#[derive(Clone)]
66+
pub struct DeprecationEntry {
67+
/// The metadata of the attribute associated with this entry.
68+
pub attr: Deprecation,
69+
/// The def id where the attr was originally attached. `None` for non-local
70+
/// `DefId`'s.
71+
origin: Option<DefIndex>,
72+
}
73+
74+
impl DeprecationEntry {
75+
fn local(attr: Deprecation, id: DefId) -> DeprecationEntry {
76+
assert!(id.is_local());
77+
DeprecationEntry {
78+
attr: attr,
79+
origin: Some(id.index),
80+
}
81+
}
82+
83+
fn external(attr: Deprecation) -> DeprecationEntry {
84+
DeprecationEntry {
85+
attr: attr,
86+
origin: None,
87+
}
88+
}
89+
90+
pub fn same_origin(&self, other: &DeprecationEntry) -> bool {
91+
match (self.origin, other.origin) {
92+
(Some(o1), Some(o2)) => o1 == o2,
93+
_ => false
94+
}
95+
}
96+
}
97+
6498
/// A stability index, giving the stability level for items and methods.
6599
pub struct Index<'tcx> {
66100
/// This is mostly a cache, except the stabilities of local items
67101
/// are filled by the annotator.
68102
stab_map: DefIdMap<Option<&'tcx Stability>>,
69-
depr_map: DefIdMap<Option<Deprecation>>,
103+
depr_map: DefIdMap<Option<DeprecationEntry>>,
70104

71105
/// Maps for each crate whether it is part of the staged API.
72106
staged_api: FnvHashMap<ast::CrateNum, bool>
@@ -77,7 +111,7 @@ struct Annotator<'a, 'tcx: 'a> {
77111
tcx: TyCtxt<'a, 'tcx, 'tcx>,
78112
index: &'a mut Index<'tcx>,
79113
parent_stab: Option<&'tcx Stability>,
80-
parent_depr: Option<Deprecation>,
114+
parent_depr: Option<DeprecationEntry>,
81115
access_levels: &'a AccessLevels,
82116
in_trait_impl: bool,
83117
}
@@ -184,14 +218,15 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
184218

185219
// `Deprecation` is just two pointers, no need to intern it
186220
let def_id = self.tcx.map.local_def_id(id);
187-
self.index.depr_map.insert(def_id, Some(depr.clone()));
221+
let depr_entry = Some(DeprecationEntry::local(depr, def_id));
222+
self.index.depr_map.insert(def_id, depr_entry.clone());
188223

189-
let orig_parent_depr = replace(&mut self.parent_depr, Some(depr));
224+
let orig_parent_depr = replace(&mut self.parent_depr, depr_entry);
190225
visit_children(self);
191226
self.parent_depr = orig_parent_depr;
192-
} else if let Some(depr) = self.parent_depr.clone() {
227+
} else if let parent_depr @ Some(_) = self.parent_depr.clone() {
193228
let def_id = self.tcx.map.local_def_id(id);
194-
self.index.depr_map.insert(def_id, Some(depr));
229+
self.index.depr_map.insert(def_id, parent_depr);
195230
visit_children(self);
196231
} else {
197232
visit_children(self);
@@ -685,6 +720,10 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
685720
}
686721

687722
pub fn lookup_deprecation(self, id: DefId) -> Option<Deprecation> {
723+
self.lookup_deprecation_entry(id).map(|depr| depr.attr)
724+
}
725+
726+
pub fn lookup_deprecation_entry(self, id: DefId) -> Option<DeprecationEntry> {
688727
if let Some(depr) = self.stability.borrow().depr_map.get(&id) {
689728
return depr.clone();
690729
}
@@ -703,12 +742,12 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
703742
}
704743
}
705744

706-
fn lookup_deprecation_uncached(self, id: DefId) -> Option<Deprecation> {
745+
fn lookup_deprecation_uncached(self, id: DefId) -> Option<DeprecationEntry> {
707746
debug!("lookup(id={:?})", id);
708747
if id.is_local() {
709748
None // The stability cache is filled partially lazily
710749
} else {
711-
self.sess.cstore.deprecation(id)
750+
self.sess.cstore.deprecation(id).map(DeprecationEntry::external)
712751
}
713752
}
714753
}

0 commit comments

Comments
 (0)