rustdoc: Run external traits through filters

This ensures that all external traits are run through the same filters that the
rest of the AST goes through, stripping hidden function as necessary.

Closes #13698
This commit is contained in:
Alex Crichton 2015-04-06 15:10:55 -07:00
parent d3647fe815
commit 458102eefa
7 changed files with 63 additions and 23 deletions

View File

@ -331,9 +331,10 @@ fn build_impl(cx: &DocContext,
let did = assoc_ty.def_id;
let type_scheme = ty::lookup_item_type(tcx, did);
let predicates = ty::lookup_predicates(tcx, did);
// Not sure the choice of ParamSpace actually matters here, because an
// associated type won't have generics on the LHS
let typedef = (type_scheme, predicates, subst::ParamSpace::TypeSpace).clean(cx);
// Not sure the choice of ParamSpace actually matters here,
// because an associated type won't have generics on the LHS
let typedef = (type_scheme, predicates,
subst::ParamSpace::TypeSpace).clean(cx);
Some(clean::Item {
name: Some(assoc_ty.name.clean(cx)),
inner: clean::TypedefItem(typedef),

View File

@ -44,9 +44,10 @@ use rustc::middle::subst::{self, ParamSpace, VecPerParamSpace};
use rustc::middle::ty;
use rustc::middle::stability;
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
use std::u32;
use std::path::PathBuf;
use core::DocContext;
use doctree;
@ -119,6 +120,7 @@ pub struct Crate {
pub module: Option<Item>,
pub externs: Vec<(ast::CrateNum, ExternalCrate)>,
pub primitives: Vec<PrimitiveType>,
pub external_traits: HashMap<ast::DefId, Trait>,
}
impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
@ -197,6 +199,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
module: Some(module),
externs: externs,
primitives: primitives,
external_traits: cx.external_traits.borrow_mut().take().unwrap(),
}
}
}

View File

@ -75,7 +75,6 @@ pub struct CrateAnalysis {
pub exported_items: privacy::ExportedItems,
pub public_items: privacy::PublicItems,
pub external_paths: ExternalPaths,
pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
}
@ -155,7 +154,6 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
exported_items: exported_items,
public_items: public_items,
external_paths: RefCell::new(None),
external_traits: RefCell::new(None),
external_typarams: RefCell::new(None),
inlined: RefCell::new(None),
};
@ -168,8 +166,6 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
let external_paths = ctxt.external_paths.borrow_mut().take();
*analysis.external_paths.borrow_mut() = external_paths;
let map = ctxt.external_traits.borrow_mut().take();
*analysis.external_traits.borrow_mut() = map;
let map = ctxt.external_typarams.borrow_mut().take();
*analysis.external_typarams.borrow_mut() = map;
let map = ctxt.inlined.borrow_mut().take();

View File

@ -9,7 +9,7 @@
// except according to those terms.
use clean::*;
use std::iter::Extend;
use std::collections::HashMap;
use std::mem::{replace, swap};
pub trait DocFolder : Sized {
@ -80,6 +80,13 @@ pub trait DocFolder : Sized {
c.module = match replace(&mut c.module, None) {
Some(module) => self.fold_item(module), None => None
};
let external_traits = replace(&mut c.external_traits, HashMap::new());
c.external_traits = external_traits.into_iter().map(|(k, mut v)| {
let items = replace(&mut v.items, Vec::new());
v.items = items.into_iter().filter_map(|i| self.fold_item(i))
.collect();
(k, v)
}).collect();
return c;
}
}

View File

@ -44,6 +44,7 @@ use std::fs::{self, File};
use std::io::prelude::*;
use std::io::{self, BufWriter, BufReader};
use std::iter::repeat;
use std::mem;
use std::path::{PathBuf, Path};
use std::str;
use std::sync::Arc;
@ -383,9 +384,7 @@ pub fn run(mut krate: clean::Crate,
privmod: false,
public_items: public_items,
orphan_methods: Vec::new(),
traits: analysis.as_ref().map(|a| {
a.external_traits.borrow_mut().take().unwrap()
}).unwrap_or(HashMap::new()),
traits: mem::replace(&mut krate.external_traits, HashMap::new()),
typarams: analysis.as_ref().map(|a| {
a.external_typarams.borrow_mut().take().unwrap()
}).unwrap_or(HashMap::new()),
@ -2239,7 +2238,7 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
}
try!(write!(w, "<div class='impl-items'>"));
for trait_item in &i.impl_.items {
for trait_item in i.impl_.items.iter() {
try!(doctraititem(w, trait_item, true));
}
@ -2262,17 +2261,10 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
// default methods which weren't overridden in the implementation block.
// FIXME: this also needs to be done for associated types, whenever defaults
// for them work.
match i.impl_.trait_ {
Some(clean::ResolvedPath { did, .. }) => {
try!({
match cache().traits.get(&did) {
Some(t) => try!(render_default_methods(w, t, &i.impl_)),
None => {}
}
Ok(())
})
if let Some(clean::ResolvedPath { did, .. }) = i.impl_.trait_ {
if let Some(t) = cache().traits.get(&did) {
try!(render_default_methods(w, t, &i.impl_));
}
Some(..) | None => {}
}
try!(write!(w, "</div>"));
Ok(())

View File

@ -0,0 +1,16 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
pub trait Foo {
#[doc(hidden)]
fn foo(&self) {}
}
impl Foo for i32 {}

View File

@ -0,0 +1,25 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// aux-build:issue-13698.rs
extern crate issue_13698;
pub struct Foo;
// @!has issue_13698/struct.Foo.html '//*[@id="method.foo"]' 'fn foo'
impl issue_13698::Foo for Foo {}
pub trait Bar {
#[doc(hidden)]
fn bar(&self) {}
}
// @!has issue_13698/struct.Foo.html '//*[@id="method.foo"]' 'fn bar'
impl Bar for Foo {}