From 5f09a50e8f5fe8cbf4ce8fed5bfd49b9e12b9aa1 Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Mon, 10 Nov 2014 15:33:21 -0800 Subject: [PATCH] rustdoc: revise method counts in stability summary Previously, the stability summary page attempted to associate impl blocks with the module in which they were defined, rather than the module defining the type they apply to (which is usually, but not always, the same). Unfortunately, due to the basic architecture of rustdoc, this meant that impls from re-exports were not being counted. This commit makes the stability summary work the same way that rustdoc's rendered output does: all methods are counted alongside the type they apply to, no matter where the methods are defined. In addition, for trait impl blocks only the stability of the overall block is counted; the stability of the methods within is not counted (since that stability level is part of the trait definition). Fixes #18812 --- src/librustdoc/html/render.rs | 24 +++++++++--------- src/librustdoc/lib.rs | 2 +- src/librustdoc/stability_summary.rs | 38 +++++++++++++++++++++++------ 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index fbd2611acb9..264b20ddac1 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -114,19 +114,19 @@ pub enum ExternalLocation { /// Metadata about an implementor of a trait. pub struct Implementor { - def_id: ast::DefId, - generics: clean::Generics, - trait_: clean::Type, - for_: clean::Type, - stability: Option, + pub def_id: ast::DefId, + pub generics: clean::Generics, + pub trait_: clean::Type, + pub for_: clean::Type, + pub stability: Option, } /// Metadata about implementations for a type. #[deriving(Clone)] pub struct Impl { - impl_: clean::Impl, - dox: Option, - stability: Option, + pub impl_: clean::Impl, + pub dox: Option, + pub stability: Option, } /// This cache is used to store information about the `clean::Crate` being @@ -254,11 +254,6 @@ pub fn run(mut krate: clean::Crate, external_html: &ExternalHtml, dst: Path) -> try!(mkdir(&cx.dst)); - // Crawl the crate, building a summary of the stability levels. NOTE: this - // summary *must* be computed with the original `krate`; the folding below - // removes the impls from their modules. - let summary = stability_summary::build(&krate); - // Crawl the crate attributes looking for attributes which control how we're // going to emit HTML let default: &[_] = &[]; @@ -372,6 +367,9 @@ pub fn run(mut krate: clean::Crate, external_html: &ExternalHtml, dst: Path) -> try!(write_shared(&cx, &krate, &*cache, index)); let krate = try!(render_sources(&mut cx, krate)); + // Crawl the crate, building a summary of the stability levels. + let summary = stability_summary::build(&krate); + // And finally render the whole crate's documentation cx.krate(krate, summary) } diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 5e2f56e00fc..9b59a258ce1 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -16,7 +16,7 @@ #![crate_type = "rlib"] #![allow(unknown_features)] -#![feature(globs, struct_variant, macro_rules, phase, slicing_syntax)] +#![feature(globs, struct_variant, macro_rules, phase, slicing_syntax, tuple_indexing)] extern crate arena; extern crate getopts; diff --git a/src/librustdoc/stability_summary.rs b/src/librustdoc/stability_summary.rs index 5cc5698edfc..37eb7308191 100644 --- a/src/librustdoc/stability_summary.rs +++ b/src/librustdoc/stability_summary.rs @@ -22,7 +22,9 @@ use syntax::ast::Public; use clean::{Crate, Item, ModuleItem, Module, StructItem, Struct, EnumItem, Enum}; use clean::{ImplItem, Impl, Trait, TraitItem, TraitMethod, ProvidedMethod, RequiredMethod}; -use clean::{TypeTraitItem, ViewItemItem, PrimitiveItem}; +use clean::{TypeTraitItem, ViewItemItem, PrimitiveItem, Stability}; + +use html::render::cache_key; #[deriving(Zero, Encodable, Decodable, PartialEq, Eq)] /// The counts for each stability level. @@ -88,12 +90,8 @@ fn visible(item: &Item) -> bool { } } -// Produce the summary for an arbitrary item. If the item is a module, include a -// module summary. The counts for items with nested items (e.g. modules, traits, -// impls) include all children counts. -fn summarize_item(item: &Item) -> (Counts, Option) { - // count this item - let item_counts = match item.stability { +fn count_stability(stab: Option<&Stability>) -> Counts { + match stab { None => Counts { unmarked: 1, .. Zero::zero() }, Some(ref stab) => match stab.level { Deprecated => Counts { deprecated: 1, .. Zero::zero() }, @@ -103,7 +101,31 @@ fn summarize_item(item: &Item) -> (Counts, Option) { Frozen => Counts { frozen: 1, .. Zero::zero() }, Locked => Counts { locked: 1, .. Zero::zero() }, } - }; + } +} + +fn summarize_methods(item: &Item) -> Counts { + match cache_key.get().unwrap().impls.get(&item.def_id) { + Some(v) => { + v.iter().map(|i| { + let mut count = count_stability(i.stability.as_ref()); + if i.impl_.trait_.is_none() { + count = count + + i.impl_.items.iter().map(|ti| summarize_item(ti).0).sum(); + } + count + }).sum() + } + None => Zero::zero() + } +} + + +// Produce the summary for an arbitrary item. If the item is a module, include a +// module summary. The counts for items with nested items (e.g. modules, traits, +// impls) include all children counts. +fn summarize_item(item: &Item) -> (Counts, Option) { + let item_counts = count_stability(item.stability.as_ref()) + summarize_methods(item); // Count this item's children, if any. Note that a trait impl is // considered to have no children.