Skip to content

Commit 45cad04

Browse files
committed
rustdoc: Populate external_traits with traits only seen in impls
This means default methods can always be found and "Important traits" will include all spotlight traits.
1 parent ee220da commit 45cad04

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

src/librustdoc/clean/inline.rs

+9
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ pub fn build_impl(cx: &DocContext, did: DefId, ret: &mut Vec<clean::Item>) {
349349
if trait_.def_id() == tcx.lang_items().deref_trait() {
350350
super::build_deref_target_impls(cx, &trait_items, ret);
351351
}
352+
if let Some(trait_did) = trait_.def_id() {
353+
record_extern_trait(cx, trait_did);
354+
}
352355

353356
let provided = trait_.def_id().map(|did| {
354357
tcx.provided_trait_methods(did)
@@ -504,3 +507,9 @@ fn separate_supertrait_bounds(mut g: clean::Generics)
504507
});
505508
(g, ty_bounds)
506509
}
510+
511+
pub fn record_extern_trait(cx: &DocContext, did: DefId) {
512+
cx.external_traits.borrow_mut().entry(did).or_insert_with(|| {
513+
build_external_trait(cx, did)
514+
});
515+
}

src/librustdoc/clean/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3189,8 +3189,7 @@ fn register_def(cx: &DocContext, def: Def) -> DefId {
31893189
if did.is_local() { return did }
31903190
inline::record_extern_fqn(cx, did, kind);
31913191
if let TypeKind::Trait = kind {
3192-
let t = inline::build_external_trait(cx, did);
3193-
cx.external_traits.borrow_mut().insert(did, t);
3192+
inline::record_extern_trait(cx, did);
31943193
}
31953194
did
31963195
}

src/librustdoc/html/render.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3291,8 +3291,7 @@ fn spotlight_decl(decl: &clean::FnDecl) -> Result<String, fmt::Error> {
32913291
if let Some(impls) = c.impls.get(&did) {
32923292
for i in impls {
32933293
let impl_ = i.inner_impl();
3294-
if impl_.trait_.def_id().and_then(|d| c.traits.get(&d))
3295-
.map_or(false, |t| t.is_spotlight) {
3294+
if impl_.trait_.def_id().map_or(false, |d| c.traits[&d].is_spotlight) {
32963295
if out.is_empty() {
32973296
out.push_str(
32983297
&format!("<h3 class=\"important\">Important traits for {}</h3>\
@@ -3458,7 +3457,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
34583457
}
34593458

34603459
let traits = &cache().traits;
3461-
let trait_ = i.trait_did().and_then(|did| traits.get(&did));
3460+
let trait_ = i.trait_did().map(|did| &traits[&did]);
34623461

34633462
if !show_def_docs {
34643463
write!(w, "<span class='docblock autohide'>")?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 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 MyTrait {
12+
/// docs for my_trait_method
13+
fn my_trait_method() {}
14+
}
15+
16+
pub struct MyStruct;
17+
18+
impl MyTrait for MyStruct {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 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:impl-inline-without-trait.rs
12+
// build-aux-docs
13+
// ignore-cross-compile
14+
15+
#![crate_name = "foo"]
16+
17+
extern crate impl_inline_without_trait;
18+
19+
// @has 'foo/struct.MyStruct.html'
20+
// @has - '//*[@id="method.my_trait_method"]' 'fn my_trait_method()'
21+
// @has - '//*[@class="docblock"]' 'docs for my_trait_method'
22+
pub use impl_inline_without_trait::MyStruct;

0 commit comments

Comments
 (0)