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
This commit is contained in:
Aaron Turon 2014-11-10 15:33:21 -08:00
parent 93c85eb8bd
commit 5f09a50e8f
3 changed files with 42 additions and 22 deletions

View File

@ -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<clean::Stability>,
pub def_id: ast::DefId,
pub generics: clean::Generics,
pub trait_: clean::Type,
pub for_: clean::Type,
pub stability: Option<clean::Stability>,
}
/// Metadata about implementations for a type.
#[deriving(Clone)]
pub struct Impl {
impl_: clean::Impl,
dox: Option<String>,
stability: Option<clean::Stability>,
pub impl_: clean::Impl,
pub dox: Option<String>,
pub stability: Option<clean::Stability>,
}
/// 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)
}

View File

@ -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;

View File

@ -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<ModuleSummary>) {
// 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<ModuleSummary>) {
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<ModuleSummary>) {
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.