From 458102eefa0cf8fc3f33928ab90a86ac4c564f90 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 6 Apr 2015 15:10:55 -0700 Subject: [PATCH] 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 --- src/librustdoc/clean/inline.rs | 7 ++++--- src/librustdoc/clean/mod.rs | 5 ++++- src/librustdoc/core.rs | 4 ---- src/librustdoc/fold.rs | 9 ++++++++- src/librustdoc/html/render.rs | 20 ++++++-------------- src/test/auxiliary/issue-13698.rs | 16 ++++++++++++++++ src/test/rustdoc/issue-13698.rs | 25 +++++++++++++++++++++++++ 7 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 src/test/auxiliary/issue-13698.rs create mode 100644 src/test/rustdoc/issue-13698.rs diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index aa17bf20d74b..e4b2e82b21b6 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -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), diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index e0ed83f40194..53824d088eef 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -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, pub externs: Vec<(ast::CrateNum, ExternalCrate)>, pub primitives: Vec, + pub external_traits: HashMap, } impl<'a, 'tcx> Clean for visit_ast::RustdocVisitor<'a, 'tcx> { @@ -197,6 +199,7 @@ impl<'a, 'tcx> Clean for visit_ast::RustdocVisitor<'a, 'tcx> { module: Some(module), externs: externs, primitives: primitives, + external_traits: cx.external_traits.borrow_mut().take().unwrap(), } } } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 113a622b07ae..a637ba9f2970 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -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>>, pub external_typarams: RefCell>>, pub inlined: RefCell>>, } @@ -155,7 +154,6 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec, 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, 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(); diff --git a/src/librustdoc/fold.rs b/src/librustdoc/fold.rs index cdeeacfb7839..0a1860c66f27 100644 --- a/src/librustdoc/fold.rs +++ b/src/librustdoc/fold.rs @@ -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; } } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index ac097d051b28..da59ffd785a2 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -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, "
")); - 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, "
")); Ok(()) diff --git a/src/test/auxiliary/issue-13698.rs b/src/test/auxiliary/issue-13698.rs new file mode 100644 index 000000000000..0bb2133c833c --- /dev/null +++ b/src/test/auxiliary/issue-13698.rs @@ -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 or the MIT license +// , 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 {} diff --git a/src/test/rustdoc/issue-13698.rs b/src/test/rustdoc/issue-13698.rs new file mode 100644 index 000000000000..81cee0998abe --- /dev/null +++ b/src/test/rustdoc/issue-13698.rs @@ -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 or the MIT license +// , 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 {}