Skip to content

Commit

Permalink
rustdoc: Run external traits through filters
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexcrichton committed Apr 8, 2015
1 parent d3647fe commit 458102e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 23 deletions.
7 changes: 4 additions & 3 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>>,
}
Expand Down Expand Up @@ -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),
};
Expand All @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
20 changes: 6 additions & 14 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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));
}

Expand All @@ -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(())
Expand Down
16 changes: 16 additions & 0 deletions src/test/auxiliary/issue-13698.rs
Original file line number Diff line number Diff line change
@@ -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 {}
25 changes: 25 additions & 0 deletions src/test/rustdoc/issue-13698.rs
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit 458102e

Please sign in to comment.