Skip to content

Commit

Permalink
Auto merge of rust-lang#80095 - jyn514:stability-on-demand, r=Guillau…
Browse files Browse the repository at this point in the history
…meGomez

[rustdoc] Calculate stability, const_stability, and deprecation on-demand

Previously, they would always be calculated ahead of time, which bloated the size of `clean::Item`.

Builds on rust-lang#80090 and should not be merged before. Helps with rust-lang#79103 and rust-lang#76382.

cc rust-lang#80014 (comment)

This brings `Item` down to 568 bytes, down from 616.
  • Loading branch information
bors committed Dec 23, 2020
2 parents 89886e6 + 1523f67 commit 18b745e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 80 deletions.
3 changes: 0 additions & 3 deletions src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
attrs: Default::default(),
visibility: Inherited,
def_id: self.cx.next_def_id(param_env_def_id.krate),
stability: None,
const_stability: None,
deprecation: None,
kind: ImplItem(Impl {
unsafety: hir::Unsafety::Normal,
generics: new_generics,
Expand Down
3 changes: 0 additions & 3 deletions src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
attrs: Default::default(),
visibility: Inherited,
def_id: self.cx.next_def_id(impl_def_id.krate),
stability: None,
const_stability: None,
deprecation: None,
kind: ImplItem(Impl {
unsafety: hir::Unsafety::Normal,
generics: (
Expand Down
3 changes: 0 additions & 3 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,6 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
source: clean::Span::dummy(),
def_id: DefId::local(CRATE_DEF_INDEX),
visibility: clean::Public,
stability: None,
const_stability: None,
deprecation: None,
kind: clean::ImportItem(clean::Import::new_simple(
item.ident.name,
clean::ImportSource {
Expand Down
9 changes: 0 additions & 9 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2141,9 +2141,6 @@ fn clean_extern_crate(
source: krate.span.clean(cx),
def_id: crate_def_id,
visibility: krate.vis.clean(cx),
stability: None,
const_stability: None,
deprecation: None,
kind: ExternCrateItem(name, orig_name),
}]
}
Expand Down Expand Up @@ -2212,9 +2209,6 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
source: self.span.clean(cx),
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
visibility: self.vis.clean(cx),
stability: None,
const_stability: None,
deprecation: None,
kind: ImportItem(Import::new_simple(
self.name,
resolve_use_source(cx, path),
Expand All @@ -2233,9 +2227,6 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
source: self.span.clean(cx),
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
visibility: self.vis.clean(cx),
stability: None,
const_stability: None,
deprecation: None,
kind: ImportItem(inner),
}]
}
Expand Down
34 changes: 19 additions & 15 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ crate struct Item {
crate visibility: Visibility,
crate kind: ItemKind,
crate def_id: DefId,
crate stability: Option<Stability>,
crate deprecation: Option<Deprecation>,
crate const_stability: Option<ConstStability>,
}

impl fmt::Debug for Item {
Expand All @@ -102,13 +99,23 @@ impl fmt::Debug for Item {
.field("kind", &self.kind)
.field("visibility", &self.visibility)
.field("def_id", def_id)
.field("stability", &self.stability)
.field("deprecation", &self.deprecation)
.finish()
}
}

impl Item {
crate fn stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx Stability> {
if self.is_fake() { None } else { tcx.lookup_stability(self.def_id) }
}

crate fn const_stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx ConstStability> {
if self.is_fake() { None } else { tcx.lookup_const_stability(self.def_id) }
}

crate fn deprecation(&self, tcx: TyCtxt<'_>) -> Option<Deprecation> {
if self.is_fake() { None } else { tcx.lookup_deprecation(self.def_id) }
}

/// Finds the `doc` attribute as a NameValue and returns the corresponding
/// value found.
crate fn doc_value(&self) -> Option<&str> {
Expand Down Expand Up @@ -150,9 +157,6 @@ impl Item {
source: source.clean(cx),
attrs: cx.tcx.get_attrs(def_id).clean(cx),
visibility: cx.tcx.visibility(def_id).clean(cx),
stability: cx.tcx.lookup_stability(def_id).cloned(),
deprecation: cx.tcx.lookup_deprecation(def_id),
const_stability: cx.tcx.lookup_const_stability(def_id).cloned(),
}
}

Expand Down Expand Up @@ -236,32 +240,32 @@ impl Item {
}
}

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

if s.level.is_unstable() {
classes.push("unstable");
}

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

if !classes.is_empty() { Some(classes.join(" ")) } else { None }
})
}

crate fn stable_since(&self) -> Option<SymbolStr> {
match self.stability?.level {
crate fn stable_since(&self, tcx: TyCtxt<'_>) -> Option<SymbolStr> {
match self.stability(tcx)?.level {
StabilityLevel::Stable { since, .. } => Some(since.as_str()),
StabilityLevel::Unstable { .. } => None,
}
}

crate fn const_stable_since(&self) -> Option<SymbolStr> {
match self.const_stability?.level {
crate fn const_stable_since(&self, tcx: TyCtxt<'_>) -> Option<SymbolStr> {
match self.const_stability(tcx)?.level {
StabilityLevel::Stable { since, .. } => Some(since.as_str()),
StabilityLevel::Unstable { .. } => None,
}
Expand Down
Loading

0 comments on commit 18b745e

Please sign in to comment.