Skip to content

Commit 282acb9

Browse files
committed
rustdoc: remove details for type alias inner type and fix sidebar
1 parent 9443f84 commit 282acb9

File tree

4 files changed

+110
-104
lines changed

4 files changed

+110
-104
lines changed

src/librustdoc/clean/types.rs

+17
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,23 @@ pub(crate) struct TypeAlias {
22522252
pub(crate) item_type: Option<Type>,
22532253
}
22542254

2255+
impl TypeAlias {
2256+
pub(crate) fn should_display_inner_type(&self) -> bool {
2257+
// Only show inner variants if:
2258+
// - the typealias does NOT have any generics (modulo lifetimes)
2259+
// - AND the aliased type has some generics
2260+
//
2261+
// Otherwise, showing a non-generic type is rendurant with its own page, or
2262+
// if it still has some generics, it's not as useful.
2263+
self.generics
2264+
.params
2265+
.iter()
2266+
.all(|param| matches!(param.kind, GenericParamDefKind::Lifetime { .. }))
2267+
&& self.generics.where_predicates.is_empty()
2268+
&& self.type_.generics().is_some_and(|generics| !generics.is_empty())
2269+
}
2270+
}
2271+
22552272
#[derive(Clone, Debug)]
22562273
pub(crate) struct OpaqueTy {
22572274
pub(crate) bounds: Vec<GenericBound>,

src/librustdoc/html/render/print_item.rs

+51-92
Original file line numberDiff line numberDiff line change
@@ -1237,104 +1237,63 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
12371237

12381238
write!(w, "{}", document(cx, it, None, HeadingOffset::H2));
12391239

1240-
// Only show inner variants if:
1241-
// - the typealias does NOT have any generics (modulo lifetimes)
1242-
// - AND the aliased type has some generics
1243-
//
1244-
// Otherwise, showing a non-generic type is rendurant with its own page, or
1245-
// if it still has some generics, it's not as useful.
1246-
let should_print_inner_type = t
1247-
.generics
1248-
.params
1249-
.iter()
1250-
.all(|param| matches!(param.kind, clean::GenericParamDefKind::Lifetime { .. }))
1251-
&& t.generics.where_predicates.is_empty()
1252-
&& t.type_.generics().is_some_and(|generics| !generics.is_empty());
1253-
1254-
if should_print_inner_type {
1255-
fn toggle<W, F>(w: &mut W, f: F)
1256-
where
1257-
W: fmt::Write,
1258-
F: FnOnce(&mut W),
1259-
{
1260-
write!(
1261-
w,
1262-
"<details class=\"toggle\">\
1263-
<summary>\
1264-
<span>Show Aliased Type</span>\
1265-
</summary>",
1266-
)
1267-
.unwrap();
1268-
f(w);
1269-
write!(w, "</details>").unwrap();
1270-
}
1271-
1272-
match &t.inner_type {
1273-
Some(clean::TypeAliasInnerType::Enum { variants, is_non_exhaustive }) => {
1274-
toggle(w, |w| {
1275-
let variants_iter = || variants.iter().filter(|i| !i.is_stripped());
1276-
wrap_item(w, |w| {
1277-
let variants_len = variants.len();
1278-
let variants_count = variants_iter().count();
1279-
let has_stripped_entries = variants_len != variants_count;
1280-
1281-
write!(w, "enum {}{}", it.name.unwrap(), t.generics.print(cx));
1282-
render_enum_fields(
1283-
w,
1284-
cx,
1285-
None,
1286-
variants_iter(),
1287-
variants_count,
1288-
has_stripped_entries,
1289-
*is_non_exhaustive,
1290-
)
1291-
});
1292-
item_variants(w, cx, it, variants_iter());
1240+
if let Some(inner_type) = &t.inner_type && t.should_display_inner_type() {
1241+
write!(
1242+
w,
1243+
"<h2 id=\"aliased-type\" class=\"small-section-header\">\
1244+
Aliased Type<a href=\"#aliased-type\" class=\"anchor\">§</a></h2>"
1245+
);
1246+
1247+
match inner_type {
1248+
clean::TypeAliasInnerType::Enum { variants, is_non_exhaustive } => {
1249+
let variants_iter = || variants.iter().filter(|i| !i.is_stripped());
1250+
wrap_item(w, |w| {
1251+
let variants_len = variants.len();
1252+
let variants_count = variants_iter().count();
1253+
let has_stripped_entries = variants_len != variants_count;
1254+
1255+
write!(w, "enum {}{}", it.name.unwrap(), t.generics.print(cx));
1256+
render_enum_fields(
1257+
w,
1258+
cx,
1259+
None,
1260+
variants_iter(),
1261+
variants_count,
1262+
has_stripped_entries,
1263+
*is_non_exhaustive,
1264+
)
12931265
});
1266+
item_variants(w, cx, it, variants_iter());
12941267
}
1295-
Some(clean::TypeAliasInnerType::Union { fields }) => {
1296-
toggle(w, |w| {
1297-
wrap_item(w, |w| {
1298-
let fields_count = fields.iter().filter(|i| !i.is_stripped()).count();
1299-
let has_stripped_fields = fields.len() != fields_count;
1300-
1301-
write!(w, "union {}{}", it.name.unwrap(), t.generics.print(cx));
1302-
render_struct_fields(
1303-
w,
1304-
None,
1305-
None,
1306-
fields,
1307-
"",
1308-
true,
1309-
has_stripped_fields,
1310-
cx,
1311-
);
1312-
});
1313-
item_fields(w, cx, it, fields, None);
1268+
clean::TypeAliasInnerType::Union { fields } => {
1269+
wrap_item(w, |w| {
1270+
let fields_count = fields.iter().filter(|i| !i.is_stripped()).count();
1271+
let has_stripped_fields = fields.len() != fields_count;
1272+
1273+
write!(w, "union {}{}", it.name.unwrap(), t.generics.print(cx));
1274+
render_struct_fields(w, None, None, fields, "", true, has_stripped_fields, cx);
13141275
});
1276+
item_fields(w, cx, it, fields, None);
13151277
}
1316-
Some(clean::TypeAliasInnerType::Struct { ctor_kind, fields }) => {
1317-
toggle(w, |w| {
1318-
wrap_item(w, |w| {
1319-
let fields_count = fields.iter().filter(|i| !i.is_stripped()).count();
1320-
let has_stripped_fields = fields.len() != fields_count;
1321-
1322-
write!(w, "struct {}{}", it.name.unwrap(), t.generics.print(cx));
1323-
render_struct_fields(
1324-
w,
1325-
None,
1326-
*ctor_kind,
1327-
fields,
1328-
"",
1329-
true,
1330-
has_stripped_fields,
1331-
cx,
1332-
);
1333-
});
1334-
item_fields(w, cx, it, fields, None);
1278+
clean::TypeAliasInnerType::Struct { ctor_kind, fields } => {
1279+
wrap_item(w, |w| {
1280+
let fields_count = fields.iter().filter(|i| !i.is_stripped()).count();
1281+
let has_stripped_fields = fields.len() != fields_count;
1282+
1283+
write!(w, "struct {}{}", it.name.unwrap(), t.generics.print(cx));
1284+
render_struct_fields(
1285+
w,
1286+
None,
1287+
*ctor_kind,
1288+
fields,
1289+
"",
1290+
true,
1291+
has_stripped_fields,
1292+
cx,
1293+
);
13351294
});
1295+
item_fields(w, cx, it, fields, None);
13361296
}
1337-
None => {}
13381297
}
13391298
}
13401299

src/librustdoc/html/render/sidebar.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buf
8282
clean::PrimitiveItem(_) => sidebar_primitive(cx, it),
8383
clean::UnionItem(ref u) => sidebar_union(cx, it, u),
8484
clean::EnumItem(ref e) => sidebar_enum(cx, it, e),
85-
clean::TypeAliasItem(_) => sidebar_type_alias(cx, it),
85+
clean::TypeAliasItem(ref t) => sidebar_type_alias(cx, it, t),
8686
clean::ModuleItem(ref m) => vec![sidebar_module(&m.items)],
8787
clean::ForeignTypeItem => sidebar_foreign_type(cx, it),
8888
_ => vec![],
@@ -230,8 +230,32 @@ fn sidebar_primitive<'a>(cx: &'a Context<'_>, it: &'a clean::Item) -> Vec<LinkBl
230230
}
231231
}
232232

233-
fn sidebar_type_alias<'a>(cx: &'a Context<'_>, it: &'a clean::Item) -> Vec<LinkBlock<'a>> {
233+
fn sidebar_type_alias<'a>(
234+
cx: &'a Context<'_>,
235+
it: &'a clean::Item,
236+
t: &'a clean::TypeAlias,
237+
) -> Vec<LinkBlock<'a>> {
234238
let mut items = vec![];
239+
if let Some(inner_type) = &t.inner_type && t.should_display_inner_type() {
240+
match inner_type {
241+
clean::TypeAliasInnerType::Enum { variants, is_non_exhaustive: _ } => {
242+
let mut variants = variants
243+
.iter()
244+
.filter(|i| !i.is_stripped())
245+
.filter_map(|v| v.name)
246+
.map(|name| Link::new(format!("variant.{name}"), name.to_string()))
247+
.collect::<Vec<_>>();
248+
variants.sort_unstable();
249+
250+
items.push(LinkBlock::new(Link::new("variants", "Variants"), variants));
251+
}
252+
clean::TypeAliasInnerType::Union { fields }
253+
| clean::TypeAliasInnerType::Struct { ctor_kind: _, fields } => {
254+
let fields = get_struct_fields_name(fields);
255+
items.push(LinkBlock::new(Link::new("fields", "Fields"), fields));
256+
}
257+
}
258+
}
235259
sidebar_assoc_items(cx, it, &mut items);
236260
items
237261
}

tests/rustdoc/typedef-inner-variants.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ pub enum IrTyKind<A, I: Interner> {
4343
pub type NearlyTyKind<A> = IrTyKind<A, TyCtxt>;
4444

4545
// @has 'inner_variants/type.TyKind.html'
46+
// @count - '//*[@id="aliased-type"]' 1
4647
// @count - '//*[@id="variants"]' 1
4748
// @count - '//*[@id="fields"]' 0
4849
// @count - '//*[@class="variant"]' 3
49-
// @matches - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code' "enum TyKind"
50-
// @has - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code/a[1]' "Adt"
51-
// @has - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code/a[2]' "Adt"
52-
// @has - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code/a[3]' "Ty"
53-
// @has - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code/a[4]' "i64"
50+
// @matches - '//pre[@class="rust item-decl"]//code' "enum TyKind"
51+
// @has - '//pre[@class="rust item-decl"]//code/a[1]' "Adt"
52+
// @has - '//pre[@class="rust item-decl"]//code/a[2]' "Adt"
53+
// @has - '//pre[@class="rust item-decl"]//code/a[3]' "Ty"
54+
// @has - '//pre[@class="rust item-decl"]//code/a[4]' "i64"
5455
pub type TyKind = IrTyKind<i64, TyCtxt>;
5556

5657
// @has 'inner_variants/union.OneOr.html'
@@ -60,10 +61,11 @@ pub union OneOr<A: Copy> {
6061
}
6162

6263
// @has 'inner_variants/type.OneOrF64.html'
64+
// @count - '//*[@id="aliased-type"]' 1
6365
// @count - '//*[@id="variants"]' 0
6466
// @count - '//*[@id="fields"]' 1
6567
// @count - '//*[@class="structfield small-section-header"]' 2
66-
// @matches - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code' "union OneOrF64"
68+
// @matches - '//pre[@class="rust item-decl"]//code' "union OneOrF64"
6769
pub type OneOrF64 = OneOr<f64>;
6870

6971
// @has 'inner_variants/struct.One.html'
@@ -75,10 +77,12 @@ pub struct One<T> {
7577
}
7678

7779
// @has 'inner_variants/type.OneU64.html'
80+
// @count - '//*[@id="aliased-type"]' 1
7881
// @count - '//*[@id="variants"]' 0
7982
// @count - '//*[@id="fields"]' 1
8083
// @count - '//*[@class="structfield small-section-header"]' 1
81-
// @matches - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code' "struct OneU64"
84+
// @matches - '//pre[@class="rust item-decl"]//code' "struct OneU64"
85+
// @matches - '//pre[@class="rust item-decl"]//code' "pub val"
8286
pub type OneU64 = One<u64>;
8387

8488
// @has 'inner_variants/struct.OnceA.html'
@@ -87,10 +91,11 @@ pub struct OnceA<'a, A> {
8791
}
8892

8993
// @has 'inner_variants/type.Once.html'
94+
// @count - '//*[@id="aliased-type"]' 1
9095
// @count - '//*[@id="variants"]' 0
9196
// @count - '//*[@id="fields"]' 1
92-
// @matches - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code' "struct Once<'a>"
93-
// @matches - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code' "&'a"
97+
// @matches - '//pre[@class="rust item-decl"]//code' "struct Once<'a>"
98+
// @matches - '//pre[@class="rust item-decl"]//code' "&'a"
9499
pub type Once<'a> = OnceA<'a, i64>;
95100

96101
// @has 'inner_variants/struct.HighlyGenericStruct.html'
@@ -100,12 +105,13 @@ pub struct HighlyGenericStruct<A, B, C, D> {
100105

101106
// VERIFY that we NOT show the Aliased Type
102107
// @has 'inner_variants/type.HighlyGenericAABB.html'
103-
// @count - '//details[@class="toggle"]' 0
108+
// @count - '//*[@id="aliased-type"]' 1
104109
// @count - '//*[@id="variants"]' 0
105110
// @count - '//*[@id="fields"]' 0
106111
pub type HighlyGenericAABB<A, B> = HighlyGenericStruct<A, A, B, B>;
107112

108113
// @has 'inner_variants/type.InlineU64.html'
114+
// @count - '//*[@id="aliased-type"]' 1
109115
// @count - '//*[@id="variants"]' 0
110116
// @count - '//*[@id="fields"]' 1
111117
pub use cross_crate_generic_typedef::InlineU64;

0 commit comments

Comments
 (0)