Skip to content

Commit 5fba2bf

Browse files
committed
Rollup merge of rust-lang#33133 - mitaa:rdoc-smth-smth-impl, r=alexcrichton
rustdoc: inline all the impls This used to be done to avoid inlining impls referencing private items, but is now unnecessary since we actually check that impls do not reference non-doc-reachable items. fixes rust-lang#32881 fixes rust-lang#33025 fixes rust-lang#33113 r? @alexcrichton
2 parents 1bc30a5 + 6603c95 commit 5fba2bf

File tree

10 files changed

+132
-26
lines changed

10 files changed

+132
-26
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ fn try_inline_def(cx: &DocContext, tcx: &TyCtxt,
7676
let inner = match def {
7777
Def::Trait(did) => {
7878
record_extern_fqn(cx, did, clean::TypeTrait);
79+
ret.extend(build_impls(cx, tcx, did));
7980
clean::TraitItem(build_external_trait(cx, tcx, did))
8081
}
8182
Def::Fn(did) => {
@@ -247,12 +248,10 @@ pub fn build_impls(cx: &DocContext,
247248
// Primarily, the impls will be used to populate the documentation for this
248249
// type being inlined, but impls can also be used when generating
249250
// documentation for primitives (no way to find those specifically).
250-
if !cx.all_crate_impls.borrow_mut().contains_key(&did.krate) {
251-
let mut impls = Vec::new();
251+
if cx.populated_crate_impls.borrow_mut().insert(did.krate) {
252252
for item in tcx.sess.cstore.crate_top_level_items(did.krate) {
253253
populate_impls(cx, tcx, item.def, &mut impls);
254254
}
255-
cx.all_crate_impls.borrow_mut().insert(did.krate, impls);
256255

257256
fn populate_impls(cx: &DocContext, tcx: &TyCtxt,
258257
def: cstore::DefLike,
@@ -269,21 +268,7 @@ pub fn build_impls(cx: &DocContext,
269268
}
270269
}
271270

272-
let mut candidates = cx.all_crate_impls.borrow_mut();
273-
let candidates = candidates.get_mut(&did.krate).unwrap();
274-
for i in (0..candidates.len()).rev() {
275-
let remove = match candidates[i].inner {
276-
clean::ImplItem(ref i) => {
277-
i.for_.def_id() == Some(did) || i.for_.primitive_type().is_some()
278-
}
279-
_ => continue,
280-
};
281-
if remove {
282-
impls.push(candidates.swap_remove(i));
283-
}
284-
}
285-
286-
return impls;
271+
impls
287272
}
288273

289274
pub fn build_impl(cx: &DocContext,

src/librustdoc/core.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use syntax::feature_gate::UnstableFeatures;
3030
use syntax::parse::token;
3131

3232
use std::cell::{RefCell, Cell};
33-
use std::collections::HashMap;
33+
use std::collections::{HashMap, HashSet};
3434
use std::rc::Rc;
3535

3636
use visit_ast::RustdocVisitor;
@@ -54,7 +54,7 @@ pub struct DocContext<'a, 'tcx: 'a> {
5454
pub map: &'a hir_map::Map<'tcx>,
5555
pub maybe_typed: MaybeTyped<'a, 'tcx>,
5656
pub input: Input,
57-
pub all_crate_impls: RefCell<HashMap<ast::CrateNum, Vec<clean::Item>>>,
57+
pub populated_crate_impls: RefCell<HashSet<ast::CrateNum>>,
5858
pub deref_trait_did: Cell<Option<DefId>>,
5959
// Note that external items for which `doc(hidden)` applies to are shown as
6060
// non-reachable while local items aren't. This is because we're reusing
@@ -189,7 +189,7 @@ pub fn run_core(search_paths: SearchPaths,
189189
map: &tcx.map,
190190
maybe_typed: Typed(tcx),
191191
input: input,
192-
all_crate_impls: RefCell::new(HashMap::new()),
192+
populated_crate_impls: RefCell::new(HashSet::new()),
193193
deref_trait_did: Cell::new(None),
194194
access_levels: RefCell::new(access_levels),
195195
external_traits: RefCell::new(HashMap::new()),

src/librustdoc/html/format.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,19 @@ impl fmt::Display for clean::Path {
291291

292292
pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
293293
let cache = cache();
294+
if !did.is_local() && !cache.access_levels.is_doc_reachable(did) {
295+
return None
296+
}
297+
294298
let loc = CURRENT_LOCATION_KEY.with(|l| l.borrow().clone());
295299
let &(ref fqp, shortty) = match cache.paths.get(&did) {
296300
Some(p) => p,
297301
None => return None,
298302
};
303+
299304
let mut url = if did.is_local() || cache.inlined.contains(&did) {
300305
repeat("../").take(loc.len()).collect::<String>()
301306
} else {
302-
if !cache.access_levels.is_doc_reachable(did) {
303-
return None
304-
}
305307
match cache.extern_locations[&did.krate] {
306308
(_, render::Remote(ref s)) => s.to_string(),
307309
(_, render::Local) => repeat("../").take(loc.len()).collect(),

src/librustdoc/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use std::cell::{RefCell, Cell};
12-
use std::collections::HashMap;
12+
use std::collections::{HashMap, HashSet};
1313
use std::env;
1414
use std::ffi::OsString;
1515
use std::io::prelude::*;
@@ -111,7 +111,7 @@ pub fn run(input: &str,
111111
maybe_typed: core::NotTyped(&sess),
112112
input: input,
113113
external_traits: RefCell::new(HashMap::new()),
114-
all_crate_impls: RefCell::new(HashMap::new()),
114+
populated_crate_impls: RefCell::new(HashSet::new()),
115115
deref_trait_did: Cell::new(None),
116116
access_levels: Default::default(),
117117
renderinfo: Default::default(),

src/test/auxiliary/issue-33113.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 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+
#![crate_name="bar"]
12+
13+
pub trait Bar {}
14+
pub struct Foo;
15+
16+
impl<'a> Bar for &'a char {}
17+
impl Bar for Foo {}

src/test/auxiliary/rustdoc-hidden.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2016 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+
#[doc(hidden)]
12+
pub struct Foo;
13+
14+
pub struct Bar;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2016 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+
use std::fmt;
12+
13+
pub trait Bar {}
14+
15+
impl<'a> Bar + 'a {
16+
pub fn bar(&self) -> usize { 42 }
17+
}
18+
19+
impl<'a> fmt::Debug for Bar + 'a {
20+
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
21+
Ok(())
22+
}
23+
}
24+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016 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:rustdoc-hidden.rs
12+
// build-aux-docs
13+
// ignore-cross-compile
14+
15+
extern crate rustdoc_hidden;
16+
17+
#[doc(no_inline)]
18+
pub use rustdoc_hidden::Foo;
19+
20+
// @has inline_hidden/fn.foo.html
21+
// @!has - '//a/@title' 'Foo'
22+
pub fn foo(_: Foo) {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016 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:rustdoc-trait-object-impl.rs
12+
// build-aux-docs
13+
// ignore-cross-compile
14+
15+
extern crate rustdoc_trait_object_impl;
16+
17+
// @has issue_32881/trait.Bar.html
18+
// @has - '//code' "impl<'a> Bar"
19+
// @has - '//code' "impl<'a> Debug for Bar"
20+
21+
pub use rustdoc_trait_object_impl::Bar;
22+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 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-33113.rs
12+
// build-aux-docs
13+
// ignore-cross-compile
14+
15+
extern crate bar;
16+
17+
// @has issue_33113/trait.Bar.html
18+
// @has - '//code' "for &'a char"
19+
// @has - '//code' "for Foo"
20+
pub use bar::Bar;

0 commit comments

Comments
 (0)