Skip to content

Commit 1523f67

Browse files
committed
Calculate stability, const_stability, and deprecation on-demand
Previously, they would always be calculated ahead of time, which bloated the size of `clean::Item`.
1 parent d8d3ab9 commit 1523f67

File tree

7 files changed

+75
-80
lines changed

7 files changed

+75
-80
lines changed

src/librustdoc/clean/auto_trait.rs

-3
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
123123
attrs: Default::default(),
124124
visibility: Inherited,
125125
def_id: self.cx.next_def_id(param_env_def_id.krate),
126-
stability: None,
127-
const_stability: None,
128-
deprecation: None,
129126
kind: ImplItem(Impl {
130127
unsafety: hir::Unsafety::Normal,
131128
generics: new_generics,

src/librustdoc/clean/blanket_impl.rs

-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
112112
attrs: Default::default(),
113113
visibility: Inherited,
114114
def_id: self.cx.next_def_id(impl_def_id.krate),
115-
stability: None,
116-
const_stability: None,
117-
deprecation: None,
118115
kind: ImplItem(Impl {
119116
unsafety: hir::Unsafety::Normal,
120117
generics: (

src/librustdoc/clean/inline.rs

-3
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,6 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
482482
source: clean::Span::dummy(),
483483
def_id: DefId::local(CRATE_DEF_INDEX),
484484
visibility: clean::Public,
485-
stability: None,
486-
const_stability: None,
487-
deprecation: None,
488485
kind: clean::ImportItem(clean::Import::new_simple(
489486
item.ident.name,
490487
clean::ImportSource {

src/librustdoc/clean/mod.rs

-9
Original file line numberDiff line numberDiff line change
@@ -2139,9 +2139,6 @@ fn clean_extern_crate(
21392139
source: krate.span.clean(cx),
21402140
def_id: crate_def_id,
21412141
visibility: krate.vis.clean(cx),
2142-
stability: None,
2143-
const_stability: None,
2144-
deprecation: None,
21452142
kind: ExternCrateItem(name, orig_name),
21462143
}]
21472144
}
@@ -2210,9 +2207,6 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22102207
source: self.span.clean(cx),
22112208
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
22122209
visibility: self.vis.clean(cx),
2213-
stability: None,
2214-
const_stability: None,
2215-
deprecation: None,
22162210
kind: ImportItem(Import::new_simple(
22172211
self.name,
22182212
resolve_use_source(cx, path),
@@ -2231,9 +2225,6 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22312225
source: self.span.clean(cx),
22322226
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
22332227
visibility: self.vis.clean(cx),
2234-
stability: None,
2235-
const_stability: None,
2236-
deprecation: None,
22372228
kind: ImportItem(inner),
22382229
}]
22392230
}

src/librustdoc/clean/types.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ crate struct Item {
8686
crate visibility: Visibility,
8787
crate kind: ItemKind,
8888
crate def_id: DefId,
89-
crate stability: Option<Stability>,
90-
crate deprecation: Option<Deprecation>,
91-
crate const_stability: Option<ConstStability>,
9289
}
9390

9491
impl fmt::Debug for Item {
@@ -102,13 +99,23 @@ impl fmt::Debug for Item {
10299
.field("kind", &self.kind)
103100
.field("visibility", &self.visibility)
104101
.field("def_id", def_id)
105-
.field("stability", &self.stability)
106-
.field("deprecation", &self.deprecation)
107102
.finish()
108103
}
109104
}
110105

111106
impl Item {
107+
crate fn stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx Stability> {
108+
if self.is_fake() { None } else { tcx.lookup_stability(self.def_id) }
109+
}
110+
111+
crate fn const_stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx ConstStability> {
112+
if self.is_fake() { None } else { tcx.lookup_const_stability(self.def_id) }
113+
}
114+
115+
crate fn deprecation(&self, tcx: TyCtxt<'_>) -> Option<Deprecation> {
116+
if self.is_fake() { None } else { tcx.lookup_deprecation(self.def_id) }
117+
}
118+
112119
/// Finds the `doc` attribute as a NameValue and returns the corresponding
113120
/// value found.
114121
crate fn doc_value(&self) -> Option<&str> {
@@ -150,9 +157,6 @@ impl Item {
150157
source: source.clean(cx),
151158
attrs: cx.tcx.get_attrs(def_id).clean(cx),
152159
visibility: cx.tcx.visibility(def_id).clean(cx),
153-
stability: cx.tcx.lookup_stability(def_id).cloned(),
154-
deprecation: cx.tcx.lookup_deprecation(def_id),
155-
const_stability: cx.tcx.lookup_const_stability(def_id).cloned(),
156160
}
157161
}
158162

@@ -236,32 +240,32 @@ impl Item {
236240
}
237241
}
238242

239-
crate fn stability_class(&self) -> Option<String> {
240-
self.stability.as_ref().and_then(|ref s| {
243+
crate fn stability_class(&self, tcx: TyCtxt<'_>) -> Option<String> {
244+
self.stability(tcx).as_ref().and_then(|ref s| {
241245
let mut classes = Vec::with_capacity(2);
242246

243247
if s.level.is_unstable() {
244248
classes.push("unstable");
245249
}
246250

247251
// FIXME: what about non-staged API items that are deprecated?
248-
if self.deprecation.is_some() {
252+
if self.deprecation(tcx).is_some() {
249253
classes.push("deprecated");
250254
}
251255

252256
if !classes.is_empty() { Some(classes.join(" ")) } else { None }
253257
})
254258
}
255259

256-
crate fn stable_since(&self) -> Option<SymbolStr> {
257-
match self.stability?.level {
260+
crate fn stable_since(&self, tcx: TyCtxt<'_>) -> Option<SymbolStr> {
261+
match self.stability(tcx)?.level {
258262
StabilityLevel::Stable { since, .. } => Some(since.as_str()),
259263
StabilityLevel::Unstable { .. } => None,
260264
}
261265
}
262266

263-
crate fn const_stable_since(&self) -> Option<SymbolStr> {
264-
match self.const_stability?.level {
267+
crate fn const_stable_since(&self, tcx: TyCtxt<'_>) -> Option<SymbolStr> {
268+
match self.const_stability(tcx)?.level {
265269
StabilityLevel::Stable { since, .. } => Some(since.as_str()),
266270
StabilityLevel::Unstable { .. } => None,
267271
}

0 commit comments

Comments
 (0)