1
Fork 0

Rustdoc: ignore deref-inherited static methods

Fixes #24575
This commit is contained in:
Adolfo Ochagavía 2015-04-26 21:03:38 +02:00
parent 2bc0bf2586
commit 87038831f1
5 changed files with 44 additions and 23 deletions

View file

@ -51,26 +51,20 @@ use std::sync::Arc;
use externalfiles::ExternalHtml; use externalfiles::ExternalHtml;
use serialize::json; use serialize::json::{self, ToJson};
use serialize::json::ToJson; use syntax::{abi, ast, ast_util, attr};
use syntax::abi;
use syntax::ast;
use syntax::ast_util;
use syntax::attr;
use rustc::util::nodemap::NodeSet; use rustc::util::nodemap::NodeSet;
use clean; use clean::{self, SelfTy};
use doctree; use doctree;
use fold::DocFolder; use fold::DocFolder;
use html::escape::Escape; use html::escape::Escape;
use html::format::{ConstnessSpace}; use html::format::{ConstnessSpace};
use html::format::{TyParamBounds, WhereClause, href, AbiSpace}; use html::format::{TyParamBounds, WhereClause, href, AbiSpace};
use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace}; use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
use html::highlight;
use html::item_type::ItemType; use html::item_type::ItemType;
use html::layout; use html::markdown::{self, Markdown};
use html::markdown::Markdown; use html::{highlight, layout};
use html::markdown;
/// A pair of name and its optional document. /// A pair of name and its optional document.
pub type NameDoc = (String, Option<String>); pub type NameDoc = (String, Option<String>);
@ -2329,6 +2323,9 @@ fn render_deref_methods(w: &mut fmt::Formatter, impl_: &Impl) -> fmt::Result {
} }
} }
// Render_header is false when we are rendering a `Deref` impl and true
// otherwise. If render_header is false, we will avoid rendering static
// methods, since they are not accessible for the type implementing `Deref`
fn render_impl(w: &mut fmt::Formatter, i: &Impl, link: AssocItemLink, fn render_impl(w: &mut fmt::Formatter, i: &Impl, link: AssocItemLink,
render_header: bool) -> fmt::Result { render_header: bool) -> fmt::Result {
if render_header { if render_header {
@ -2348,14 +2345,17 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl, link: AssocItemLink,
} }
fn doctraititem(w: &mut fmt::Formatter, item: &clean::Item, fn doctraititem(w: &mut fmt::Formatter, item: &clean::Item,
link: AssocItemLink) -> fmt::Result { link: AssocItemLink, render_static: bool) -> fmt::Result {
match item.inner { match item.inner {
clean::MethodItem(..) | clean::TyMethodItem(..) => { clean::MethodItem(..) | clean::TyMethodItem(..) => {
try!(write!(w, "<h4 id='method.{}' class='{}'><code>", // Only render when the method is not static or we allow static methods
*item.name.as_ref().unwrap(), if !is_static_method(item) || render_static {
shortty(item))); try!(write!(w, "<h4 id='method.{}' class='{}'><code>",
*item.name.as_ref().unwrap(),
shortty(item)));
try!(render_assoc_item(w, item, link)); try!(render_assoc_item(w, item, link));
try!(write!(w, "</code></h4>\n")); try!(write!(w, "</code></h4>\n"));
}
} }
clean::TypedefItem(ref tydef) => { clean::TypedefItem(ref tydef) => {
let name = item.name.as_ref().unwrap(); let name = item.name.as_ref().unwrap();
@ -2389,22 +2389,36 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl, link: AssocItemLink,
} }
_ => panic!("can't make docs for trait item with name {:?}", item.name) _ => panic!("can't make docs for trait item with name {:?}", item.name)
} }
if let AssocItemLink::Anchor = link {
document(w, item) return if let AssocItemLink::Anchor = link {
if is_static_method(item) && !render_static {
Ok(())
} else {
document(w, item)
}
} else { } else {
Ok(()) Ok(())
};
fn is_static_method(item: &clean::Item) -> bool {
match item.inner {
clean::MethodItem(ref method) => method.self_ == SelfTy::SelfStatic,
clean::TyMethodItem(ref method) => method.self_ == SelfTy::SelfStatic,
_ => false
}
} }
} }
try!(write!(w, "<div class='impl-items'>")); try!(write!(w, "<div class='impl-items'>"));
for trait_item in i.impl_.items.iter() { for trait_item in i.impl_.items.iter() {
try!(doctraititem(w, trait_item, link)); try!(doctraititem(w, trait_item, link, render_header));
} }
fn render_default_items(w: &mut fmt::Formatter, fn render_default_items(w: &mut fmt::Formatter,
did: ast::DefId, did: ast::DefId,
t: &clean::Trait, t: &clean::Trait,
i: &clean::Impl) -> fmt::Result { i: &clean::Impl,
render_static: bool) -> fmt::Result {
for trait_item in &t.items { for trait_item in &t.items {
let n = trait_item.name.clone(); let n = trait_item.name.clone();
match i.items.iter().find(|m| { m.name == n }) { match i.items.iter().find(|m| { m.name == n }) {
@ -2412,7 +2426,7 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl, link: AssocItemLink,
None => {} None => {}
} }
try!(doctraititem(w, trait_item, AssocItemLink::GotoSource(did))); try!(doctraititem(w, trait_item, AssocItemLink::GotoSource(did), render_static));
} }
Ok(()) Ok(())
} }
@ -2423,7 +2437,8 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl, link: AssocItemLink,
// for them work. // for them work.
if let Some(clean::ResolvedPath { did, .. }) = i.impl_.trait_ { if let Some(clean::ResolvedPath { did, .. }) = i.impl_.trait_ {
if let Some(t) = cache().traits.get(&did) { if let Some(t) = cache().traits.get(&did) {
try!(render_default_items(w, did, t, &i.impl_)); try!(render_default_items(w, did, t, &i.impl_, render_header));
} }
} }
try!(write!(w, "</div>")); try!(write!(w, "</div>"));

View file

@ -22,6 +22,7 @@ pub struct Baz;
impl Baz { impl Baz {
pub fn baz(&self) {} pub fn baz(&self) {}
pub fn static_baz() {}
} }
impl Deref for Bar { impl Deref for Bar {

View file

@ -19,4 +19,4 @@ impl Deref for Bar {
// @has issue_19190_2/struct.Bar.html // @has issue_19190_2/struct.Bar.html
// @has - '//*[@id="method.count_ones"]' 'fn count_ones(self) -> u32' // @has - '//*[@id="method.count_ones"]' 'fn count_ones(self) -> u32'
// @!has - '//*[@id="method.min_value"]' 'fn min_value() -> i32'

View file

@ -18,14 +18,17 @@ use issue_19190_3::Baz;
// @has issue_19190_3/struct.Foo.html // @has issue_19190_3/struct.Foo.html
// @has - '//*[@id="method.count_ones"]' 'fn count_ones(self) -> u32' // @has - '//*[@id="method.count_ones"]' 'fn count_ones(self) -> u32'
// @!has - '//*[@id="method.min_value"]' 'fn min_value() -> i32'
pub use issue_19190_3::Foo; pub use issue_19190_3::Foo;
// @has issue_19190_3/struct.Bar.html // @has issue_19190_3/struct.Bar.html
// @has - '//*[@id="method.baz"]' 'fn baz(&self)' // @has - '//*[@id="method.baz"]' 'fn baz(&self)'
// @!has - '//*[@id="method.static_baz"]' 'fn static_baz()'
pub use issue_19190_3::Bar; pub use issue_19190_3::Bar;
// @has issue_19190_3/struct.MyBar.html // @has issue_19190_3/struct.MyBar.html
// @has - '//*[@id="method.baz"]' 'fn baz(&self)' // @has - '//*[@id="method.baz"]' 'fn baz(&self)'
// @!has - '//*[@id="method.static_baz"]' 'fn static_baz()'
pub struct MyBar; pub struct MyBar;
impl Deref for MyBar { impl Deref for MyBar {

View file

@ -15,6 +15,7 @@ pub struct Bar;
impl Foo { impl Foo {
pub fn foo(&self) {} pub fn foo(&self) {}
pub fn static_foo() {}
} }
impl Deref for Bar { impl Deref for Bar {
@ -24,3 +25,4 @@ impl Deref for Bar {
// @has issue_19190/struct.Bar.html // @has issue_19190/struct.Bar.html
// @has - '//*[@id="method.foo"]' 'fn foo(&self)' // @has - '//*[@id="method.foo"]' 'fn foo(&self)'
// @!has - '//*[@id="method.static_foo"]' 'fn static_foo()'