Skip to content

Commit 458102e

Browse files
committed
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
1 parent d3647fe commit 458102e

File tree

7 files changed

+63
-23
lines changed

7 files changed

+63
-23
lines changed

src/librustdoc/clean/inline.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,10 @@ fn build_impl(cx: &DocContext,
331331
let did = assoc_ty.def_id;
332332
let type_scheme = ty::lookup_item_type(tcx, did);
333333
let predicates = ty::lookup_predicates(tcx, did);
334-
// Not sure the choice of ParamSpace actually matters here, because an
335-
// associated type won't have generics on the LHS
336-
let typedef = (type_scheme, predicates, subst::ParamSpace::TypeSpace).clean(cx);
334+
// Not sure the choice of ParamSpace actually matters here,
335+
// because an associated type won't have generics on the LHS
336+
let typedef = (type_scheme, predicates,
337+
subst::ParamSpace::TypeSpace).clean(cx);
337338
Some(clean::Item {
338339
name: Some(assoc_ty.name.clean(cx)),
339340
inner: clean::TypedefItem(typedef),

src/librustdoc/clean/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ use rustc::middle::subst::{self, ParamSpace, VecPerParamSpace};
4444
use rustc::middle::ty;
4545
use rustc::middle::stability;
4646

47+
use std::collections::HashMap;
48+
use std::path::PathBuf;
4749
use std::rc::Rc;
4850
use std::u32;
49-
use std::path::PathBuf;
5051

5152
use core::DocContext;
5253
use doctree;
@@ -119,6 +120,7 @@ pub struct Crate {
119120
pub module: Option<Item>,
120121
pub externs: Vec<(ast::CrateNum, ExternalCrate)>,
121122
pub primitives: Vec<PrimitiveType>,
123+
pub external_traits: HashMap<ast::DefId, Trait>,
122124
}
123125

124126
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> {
197199
module: Some(module),
198200
externs: externs,
199201
primitives: primitives,
202+
external_traits: cx.external_traits.borrow_mut().take().unwrap(),
200203
}
201204
}
202205
}

src/librustdoc/core.rs

-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ pub struct CrateAnalysis {
7575
pub exported_items: privacy::ExportedItems,
7676
pub public_items: privacy::PublicItems,
7777
pub external_paths: ExternalPaths,
78-
pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
7978
pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
8079
pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
8180
}
@@ -155,7 +154,6 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
155154
exported_items: exported_items,
156155
public_items: public_items,
157156
external_paths: RefCell::new(None),
158-
external_traits: RefCell::new(None),
159157
external_typarams: RefCell::new(None),
160158
inlined: RefCell::new(None),
161159
};
@@ -168,8 +166,6 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
168166

169167
let external_paths = ctxt.external_paths.borrow_mut().take();
170168
*analysis.external_paths.borrow_mut() = external_paths;
171-
let map = ctxt.external_traits.borrow_mut().take();
172-
*analysis.external_traits.borrow_mut() = map;
173169
let map = ctxt.external_typarams.borrow_mut().take();
174170
*analysis.external_typarams.borrow_mut() = map;
175171
let map = ctxt.inlined.borrow_mut().take();

src/librustdoc/fold.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use clean::*;
12-
use std::iter::Extend;
12+
use std::collections::HashMap;
1313
use std::mem::{replace, swap};
1414

1515
pub trait DocFolder : Sized {
@@ -80,6 +80,13 @@ pub trait DocFolder : Sized {
8080
c.module = match replace(&mut c.module, None) {
8181
Some(module) => self.fold_item(module), None => None
8282
};
83+
let external_traits = replace(&mut c.external_traits, HashMap::new());
84+
c.external_traits = external_traits.into_iter().map(|(k, mut v)| {
85+
let items = replace(&mut v.items, Vec::new());
86+
v.items = items.into_iter().filter_map(|i| self.fold_item(i))
87+
.collect();
88+
(k, v)
89+
}).collect();
8390
return c;
8491
}
8592
}

src/librustdoc/html/render.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use std::fs::{self, File};
4444
use std::io::prelude::*;
4545
use std::io::{self, BufWriter, BufReader};
4646
use std::iter::repeat;
47+
use std::mem;
4748
use std::path::{PathBuf, Path};
4849
use std::str;
4950
use std::sync::Arc;
@@ -383,9 +384,7 @@ pub fn run(mut krate: clean::Crate,
383384
privmod: false,
384385
public_items: public_items,
385386
orphan_methods: Vec::new(),
386-
traits: analysis.as_ref().map(|a| {
387-
a.external_traits.borrow_mut().take().unwrap()
388-
}).unwrap_or(HashMap::new()),
387+
traits: mem::replace(&mut krate.external_traits, HashMap::new()),
389388
typarams: analysis.as_ref().map(|a| {
390389
a.external_typarams.borrow_mut().take().unwrap()
391390
}).unwrap_or(HashMap::new()),
@@ -2239,7 +2238,7 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
22392238
}
22402239

22412240
try!(write!(w, "<div class='impl-items'>"));
2242-
for trait_item in &i.impl_.items {
2241+
for trait_item in i.impl_.items.iter() {
22432242
try!(doctraititem(w, trait_item, true));
22442243
}
22452244

@@ -2262,17 +2261,10 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
22622261
// default methods which weren't overridden in the implementation block.
22632262
// FIXME: this also needs to be done for associated types, whenever defaults
22642263
// for them work.
2265-
match i.impl_.trait_ {
2266-
Some(clean::ResolvedPath { did, .. }) => {
2267-
try!({
2268-
match cache().traits.get(&did) {
2269-
Some(t) => try!(render_default_methods(w, t, &i.impl_)),
2270-
None => {}
2271-
}
2272-
Ok(())
2273-
})
2264+
if let Some(clean::ResolvedPath { did, .. }) = i.impl_.trait_ {
2265+
if let Some(t) = cache().traits.get(&did) {
2266+
try!(render_default_methods(w, t, &i.impl_));
22742267
}
2275-
Some(..) | None => {}
22762268
}
22772269
try!(write!(w, "</div>"));
22782270
Ok(())

src/test/auxiliary/issue-13698.rs

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

src/test/rustdoc/issue-13698.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue-13698.rs
12+
13+
extern crate issue_13698;
14+
15+
pub struct Foo;
16+
// @!has issue_13698/struct.Foo.html '//*[@id="method.foo"]' 'fn foo'
17+
impl issue_13698::Foo for Foo {}
18+
19+
pub trait Bar {
20+
#[doc(hidden)]
21+
fn bar(&self) {}
22+
}
23+
24+
// @!has issue_13698/struct.Foo.html '//*[@id="method.foo"]' 'fn bar'
25+
impl Bar for Foo {}

0 commit comments

Comments
 (0)