Skip to content

Commit 9d052cb

Browse files
Rollup merge of rust-lang#88490 - GuillaumeGomez:associated-types-implementors-display, r=camelid,Manishearth
Display associated types of implementors Fixes rust-lang#86631. Contrary to before, it doesn't display methods. I also had to "resurrect" the `auto-hide-trait-implementations` setting. :3 Only question at this point: should I move the `render_impl` boolean arguments into one struct? We're starting to have quite a lot of them... cc `@cynecx` r? `@camelid`
2 parents c08d0c0 + 92dae39 commit 9d052cb

File tree

7 files changed

+112
-50
lines changed

7 files changed

+112
-50
lines changed

src/librustdoc/html/render/mod.rs

+62-29
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,15 @@ fn render_impls(
710710
containing_item,
711711
assoc_link,
712712
RenderMode::Normal,
713-
true,
714713
None,
715-
false,
716-
true,
717714
&[],
715+
ImplRenderingParameters {
716+
show_def_docs: true,
717+
is_on_foreign_type: false,
718+
show_default_items: true,
719+
show_non_assoc_items: true,
720+
toggle_open_by_default: true,
721+
},
718722
);
719723
buffer.into_inner()
720724
})
@@ -1049,11 +1053,15 @@ fn render_assoc_items(
10491053
containing_item,
10501054
AssocItemLink::Anchor(None),
10511055
render_mode,
1052-
true,
10531056
None,
1054-
false,
1055-
true,
10561057
&[],
1058+
ImplRenderingParameters {
1059+
show_def_docs: true,
1060+
is_on_foreign_type: false,
1061+
show_default_items: true,
1062+
show_non_assoc_items: true,
1063+
toggle_open_by_default: true,
1064+
},
10571065
);
10581066
}
10591067
}
@@ -1243,20 +1251,26 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
12431251
out.into_inner()
12441252
}
12451253

1254+
#[derive(Clone, Copy, Debug)]
1255+
struct ImplRenderingParameters {
1256+
show_def_docs: bool,
1257+
is_on_foreign_type: bool,
1258+
show_default_items: bool,
1259+
/// Whether or not to show methods.
1260+
show_non_assoc_items: bool,
1261+
toggle_open_by_default: bool,
1262+
}
1263+
12461264
fn render_impl(
12471265
w: &mut Buffer,
12481266
cx: &Context<'_>,
12491267
i: &Impl,
12501268
parent: &clean::Item,
12511269
link: AssocItemLink<'_>,
12521270
render_mode: RenderMode,
1253-
show_def_docs: bool,
12541271
use_absolute: Option<bool>,
1255-
is_on_foreign_type: bool,
1256-
show_default_items: bool,
1257-
// This argument is used to reference same type with different paths to avoid duplication
1258-
// in documentation pages for trait with automatic implementations like "Send" and "Sync".
12591272
aliases: &[String],
1273+
rendering_params: ImplRenderingParameters,
12601274
) {
12611275
let cache = cx.cache();
12621276
let traits = &cache.traits;
@@ -1279,17 +1293,18 @@ fn render_impl(
12791293
render_mode: RenderMode,
12801294
is_default_item: bool,
12811295
trait_: Option<&clean::Trait>,
1282-
show_def_docs: bool,
1296+
rendering_params: ImplRenderingParameters,
12831297
) {
12841298
let item_type = item.type_();
12851299
let name = item.name.as_ref().unwrap();
12861300

1287-
let render_method_item = match render_mode {
1288-
RenderMode::Normal => true,
1289-
RenderMode::ForDeref { mut_: deref_mut_ } => {
1290-
should_render_item(&item, deref_mut_, cx.cache())
1291-
}
1292-
};
1301+
let render_method_item = rendering_params.show_non_assoc_items
1302+
&& match render_mode {
1303+
RenderMode::Normal => true,
1304+
RenderMode::ForDeref { mut_: deref_mut_ } => {
1305+
should_render_item(&item, deref_mut_, cx.cache())
1306+
}
1307+
};
12931308

12941309
let in_trait_class = if trait_.is_some() { " trait-impl" } else { "" };
12951310

@@ -1312,18 +1327,32 @@ fn render_impl(
13121327
} else {
13131328
// In case the item isn't documented,
13141329
// provide short documentation from the trait.
1315-
document_short(&mut doc_buffer, it, cx, link, parent, show_def_docs);
1330+
document_short(
1331+
&mut doc_buffer,
1332+
it,
1333+
cx,
1334+
link,
1335+
parent,
1336+
rendering_params.show_def_docs,
1337+
);
13161338
}
13171339
}
13181340
} else {
13191341
document_item_info(&mut info_buffer, cx, item, Some(parent));
1320-
if show_def_docs {
1342+
if rendering_params.show_def_docs {
13211343
document_full(&mut doc_buffer, item, cx);
13221344
short_documented = false;
13231345
}
13241346
}
13251347
} else {
1326-
document_short(&mut doc_buffer, item, cx, link, parent, show_def_docs);
1348+
document_short(
1349+
&mut doc_buffer,
1350+
item,
1351+
cx,
1352+
link,
1353+
parent,
1354+
rendering_params.show_def_docs,
1355+
);
13271356
}
13281357
}
13291358
let w = if short_documented && trait_.is_some() { interesting } else { boring };
@@ -1455,7 +1484,7 @@ fn render_impl(
14551484
render_mode,
14561485
false,
14571486
trait_.map(|t| &t.trait_),
1458-
show_def_docs,
1487+
rendering_params,
14591488
);
14601489
}
14611490

@@ -1468,7 +1497,7 @@ fn render_impl(
14681497
parent: &clean::Item,
14691498
containing_item: &clean::Item,
14701499
render_mode: RenderMode,
1471-
show_def_docs: bool,
1500+
rendering_params: ImplRenderingParameters,
14721501
) {
14731502
for trait_item in &t.items {
14741503
let n = trait_item.name;
@@ -1490,7 +1519,7 @@ fn render_impl(
14901519
render_mode,
14911520
true,
14921521
Some(t),
1493-
show_def_docs,
1522+
rendering_params,
14941523
);
14951524
}
14961525
}
@@ -1499,7 +1528,7 @@ fn render_impl(
14991528
// default items which weren't overridden in the implementation block.
15001529
// We don't emit documentation for default items if they appear in the
15011530
// Implementations on Foreign Types or Implementors sections.
1502-
if show_default_items {
1531+
if rendering_params.show_default_items {
15031532
if let Some(t) = trait_ {
15041533
render_default_items(
15051534
&mut default_impl_items,
@@ -1510,15 +1539,19 @@ fn render_impl(
15101539
&i.impl_item,
15111540
parent,
15121541
render_mode,
1513-
show_def_docs,
1542+
rendering_params,
15141543
);
15151544
}
15161545
}
15171546
if render_mode == RenderMode::Normal {
15181547
let toggled = !(impl_items.is_empty() && default_impl_items.is_empty());
15191548
if toggled {
15201549
close_tags.insert_str(0, "</details>");
1521-
write!(w, "<details class=\"rustdoc-toggle implementors-toggle\" open>");
1550+
write!(
1551+
w,
1552+
"<details class=\"rustdoc-toggle implementors-toggle\"{}>",
1553+
if rendering_params.toggle_open_by_default { " open" } else { "" }
1554+
);
15221555
write!(w, "<summary>")
15231556
}
15241557
render_impl_summary(
@@ -1527,9 +1560,9 @@ fn render_impl(
15271560
i,
15281561
parent,
15291562
parent,
1530-
show_def_docs,
1563+
rendering_params.show_def_docs,
15311564
use_absolute,
1532-
is_on_foreign_type,
1565+
rendering_params.is_on_foreign_type,
15331566
aliases,
15341567
);
15351568
if toggled {

src/librustdoc/html/render/print_item.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rustc_span::symbol::{kw, sym, Symbol};
1616
use super::{
1717
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,
1818
render_assoc_item, render_assoc_items, render_attributes_in_code, render_attributes_in_pre,
19-
render_impl, render_impl_summary, render_stability_since_raw, write_srclink, AssocItemLink,
20-
Context,
19+
render_impl, render_stability_since_raw, write_srclink, AssocItemLink, Context,
20+
ImplRenderingParameters,
2121
};
2222
use crate::clean::{self, GetDefId};
2323
use crate::formats::item_type::ItemType;
@@ -736,11 +736,15 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
736736
it,
737737
assoc_link,
738738
RenderMode::Normal,
739-
false,
740739
None,
741-
true,
742-
false,
743740
&[],
741+
ImplRenderingParameters {
742+
show_def_docs: false,
743+
is_on_foreign_type: true,
744+
show_default_items: false,
745+
show_non_assoc_items: true,
746+
toggle_open_by_default: false,
747+
},
744748
);
745749
}
746750
}
@@ -1395,16 +1399,22 @@ fn render_implementor(
13951399
} => implementor_dups[&path.last()].1,
13961400
_ => false,
13971401
};
1398-
render_impl_summary(
1402+
render_impl(
13991403
w,
14001404
cx,
14011405
implementor,
14021406
trait_,
1403-
trait_,
1404-
false,
1407+
AssocItemLink::Anchor(None),
1408+
RenderMode::Normal,
14051409
Some(use_absolute),
1406-
false,
14071410
aliases,
1411+
ImplRenderingParameters {
1412+
show_def_docs: false,
1413+
is_on_foreign_type: false,
1414+
show_default_items: false,
1415+
show_non_assoc_items: false,
1416+
toggle_open_by_default: false,
1417+
},
14081418
);
14091419
}
14101420

src/test/rustdoc-gui/implementors.goml

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
goto: file://|DOC_PATH|/implementors/trait.Whatever.html
44
assert: "#implementors-list"
55
// There are supposed to be two implementors listed.
6-
assert-count: ("#implementors-list > .impl", 2)
6+
assert-count: ("#implementors-list .impl", 2)
77
// Now we check that both implementors have an anchor, an ID and a similar DOM.
8-
assert: ("#implementors-list > .impl:nth-child(1) > a.anchor")
9-
assert-attribute: ("#implementors-list > .impl:nth-child(1)", {"id": "impl-Whatever"})
10-
assert-attribute: ("#implementors-list > .impl:nth-child(1) > a.anchor", {"href": "#impl-Whatever"})
11-
assert: "#implementors-list > .impl:nth-child(1) > .code-header.in-band"
8+
assert: ("#implementors-list .impl:nth-child(1) > a.anchor")
9+
assert-attribute: ("#implementors-list .impl:nth-child(1)", {"id": "impl-Whatever"})
10+
assert-attribute: ("#implementors-list .impl:nth-child(1) > a.anchor", {"href": "#impl-Whatever"})
11+
assert: "#implementors-list .impl:nth-child(1) > .code-header.in-band"
1212

13-
assert: ("#implementors-list > .impl:nth-child(2) > a.anchor")
14-
assert-attribute: ("#implementors-list > .impl:nth-child(2)", {"id": "impl-Whatever-1"})
15-
assert-attribute: ("#implementors-list > .impl:nth-child(2) > a.anchor", {"href": "#impl-Whatever-1"})
16-
assert: "#implementors-list > .impl:nth-child(2) > .code-header.in-band"
13+
assert: ("#implementors-list .impl:nth-child(2) > a.anchor")
14+
assert-attribute: ("#implementors-list .impl:nth-child(2)", {"id": "impl-Whatever-1"})
15+
assert-attribute: ("#implementors-list .impl:nth-child(2) > a.anchor", {"href": "#impl-Whatever-1"})
16+
assert: "#implementors-list .impl:nth-child(2) > .code-header.in-band"
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
pub trait Whatever {
2+
type Foo;
3+
24
fn method() {}
35
}
46

57
pub struct Struct;
68

7-
impl Whatever for Struct {}
9+
impl Whatever for Struct {
10+
type Foo = u8;
11+
}

src/test/rustdoc-gui/src/lib2/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ impl Trait for Foo {
3838
}
3939

4040

41-
impl implementors::Whatever for Foo {}
41+
impl implementors::Whatever for Foo {
42+
type Foo = u32;
43+
}
4244

4345
pub mod sub_mod {
4446
/// ```txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// This test ensures that the implementors toggle are not open by default.
2+
goto: file://|DOC_PATH|/implementors/trait.Whatever.html
3+
4+
assert-attribute-false: ("#implementors-list > details", {"open": ""}, ALL)

src/test/rustdoc/trait-impl-items-links-and-anchors.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub trait MyTrait {
22
type Assoc;
3-
const VALUE: u32;
3+
const VALUE: u32 = 12;
44
fn trait_function(&self);
55
fn defaulted(&self) {}
66
fn defaulted_override(&self) {}
@@ -38,9 +38,11 @@ impl MyTrait for Vec<u8> {
3838
}
3939

4040
impl MyTrait for MyStruct {
41+
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedtype.Assoc-3"]//a[@class="anchor"]/@href' #associatedtype.Assoc-3
4142
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedtype.Assoc"]//a[@class="type"]/@href' trait.MyTrait.html#associatedtype.Assoc
4243
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedtype.Assoc"]//a[@class="anchor"]/@href' #associatedtype.Assoc
4344
type Assoc = bool;
45+
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedconstant.VALUE-3"]//a[@class="anchor"]/@href' #associatedconstant.VALUE-3
4446
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedconstant.VALUE"]//a[@class="constant"]/@href' trait.MyTrait.html#associatedconstant.VALUE
4547
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedconstant.VALUE"]//a[@class="anchor"]/@href' #associatedconstant.VALUE
4648
const VALUE: u32 = 20;
@@ -55,3 +57,10 @@ impl MyTrait for MyStruct {
5557
}
5658

5759
pub struct MyStruct;
60+
61+
// We check that associated items with default values aren't generated in the implementors list.
62+
impl MyTrait for (u8, u8) {
63+
// @!has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedconstant.VALUE-4"]'
64+
type Assoc = bool;
65+
fn trait_function(&self) {}
66+
}

0 commit comments

Comments
 (0)