Skip to content

Commit 41696f5

Browse files
authored
Rollup merge of rust-lang#47313 - ollie27:rustdoc_record_extern_trait, r=QuietMisdreavus
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.
2 parents 3dc6f3c + 45cad04 commit 41696f5

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
@@ -328,6 +328,9 @@ pub fn build_impl(cx: &DocContext, did: DefId, ret: &mut Vec<clean::Item>) {
328328
if trait_.def_id() == tcx.lang_items().deref_trait() {
329329
super::build_deref_target_impls(cx, &trait_items, ret);
330330
}
331+
if let Some(trait_did) = trait_.def_id() {
332+
record_extern_trait(cx, trait_did);
333+
}
331334

332335
let provided = trait_.def_id().map(|did| {
333336
tcx.provided_trait_methods(did)
@@ -483,3 +486,9 @@ fn separate_supertrait_bounds(mut g: clean::Generics)
483486
});
484487
(g, ty_bounds)
485488
}
489+
490+
pub fn record_extern_trait(cx: &DocContext, did: DefId) {
491+
cx.external_traits.borrow_mut().entry(did).or_insert_with(|| {
492+
build_external_trait(cx, did)
493+
});
494+
}

src/librustdoc/clean/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3163,8 +3163,7 @@ fn register_def(cx: &DocContext, def: Def) -> DefId {
31633163
if did.is_local() { return did }
31643164
inline::record_extern_fqn(cx, did, kind);
31653165
if let TypeKind::Trait = kind {
3166-
let t = inline::build_external_trait(cx, did);
3167-
cx.external_traits.borrow_mut().insert(did, t);
3166+
inline::record_extern_trait(cx, did);
31683167
}
31693168
did
31703169
}

src/librustdoc/html/render.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3277,8 +3277,7 @@ fn spotlight_decl(decl: &clean::FnDecl) -> Result<String, fmt::Error> {
32773277
if let Some(impls) = c.impls.get(&did) {
32783278
for i in impls {
32793279
let impl_ = i.inner_impl();
3280-
if impl_.trait_.def_id().and_then(|d| c.traits.get(&d))
3281-
.map_or(false, |t| t.is_spotlight) {
3280+
if impl_.trait_.def_id().map_or(false, |d| c.traits[&d].is_spotlight) {
32823281
if out.is_empty() {
32833282
out.push_str(
32843283
&format!("<h3 class=\"important\">Important traits for {}</h3>\
@@ -3444,7 +3443,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
34443443
}
34453444

34463445
let traits = &cache().traits;
3447-
let trait_ = i.trait_did().and_then(|did| traits.get(&did));
3446+
let trait_ = i.trait_did().map(|did| &traits[&did]);
34483447

34493448
if !show_def_docs {
34503449
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)