Skip to content

Commit 706d010

Browse files
committed
rustdoc: always print type alias inner type (with it's where clauses)
1 parent 282acb9 commit 706d010

File tree

5 files changed

+53
-25
lines changed

5 files changed

+53
-25
lines changed

src/librustdoc/clean/types.rs

-17
Original file line numberDiff line numberDiff line change
@@ -2252,23 +2252,6 @@ 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-
22722255
#[derive(Clone, Debug)]
22732256
pub(crate) struct OpaqueTy {
22742257
pub(crate) bounds: Vec<GenericBound>,

src/librustdoc/html/render/print_item.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ 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-
if let Some(inner_type) = &t.inner_type && t.should_display_inner_type() {
1240+
if let Some(inner_type) = &t.inner_type {
12411241
write!(
12421242
w,
12431243
"<h2 id=\"aliased-type\" class=\"small-section-header\">\
@@ -1256,7 +1256,7 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
12561256
render_enum_fields(
12571257
w,
12581258
cx,
1259-
None,
1259+
Some(&t.generics),
12601260
variants_iter(),
12611261
variants_count,
12621262
has_stripped_entries,
@@ -1271,7 +1271,16 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
12711271
let has_stripped_fields = fields.len() != fields_count;
12721272

12731273
write!(w, "union {}{}", it.name.unwrap(), t.generics.print(cx));
1274-
render_struct_fields(w, None, None, fields, "", true, has_stripped_fields, cx);
1274+
render_struct_fields(
1275+
w,
1276+
Some(&t.generics),
1277+
None,
1278+
fields,
1279+
"",
1280+
true,
1281+
has_stripped_fields,
1282+
cx,
1283+
);
12751284
});
12761285
item_fields(w, cx, it, fields, None);
12771286
}
@@ -1283,7 +1292,7 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
12831292
write!(w, "struct {}{}", it.name.unwrap(), t.generics.print(cx));
12841293
render_struct_fields(
12851294
w,
1286-
None,
1295+
Some(&t.generics),
12871296
*ctor_kind,
12881297
fields,
12891298
"",

src/librustdoc/html/render/sidebar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn sidebar_type_alias<'a>(
236236
t: &'a clean::TypeAlias,
237237
) -> Vec<LinkBlock<'a>> {
238238
let mut items = vec![];
239-
if let Some(inner_type) = &t.inner_type && t.should_display_inner_type() {
239+
if let Some(inner_type) = &t.inner_type {
240240
match inner_type {
241241
clean::TypeAliasInnerType::Enum { variants, is_non_exhaustive: _ } => {
242242
let mut variants = variants
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![crate_name = "inner_types_lazy"]
2+
3+
#![feature(lazy_type_alias)]
4+
#![allow(incomplete_features)]
5+
6+
// @has 'inner_types_lazy/struct.Pair.html'
7+
pub struct Pair<A, B> {
8+
pub first: A,
9+
pub second: B,
10+
}
11+
12+
// @has 'inner_types_lazy/type.ReversedTypesPair.html'
13+
// @count - '//*[@id="aliased-type"]' 1
14+
// @count - '//*[@id="variants"]' 0
15+
// @count - '//*[@id="fields"]' 1
16+
// @count - '//span[@class="where fmt-newline"]' 0
17+
pub type ReversedTypesPair<Q, R> = Pair<R, Q>;
18+
19+
// @has 'inner_types_lazy/type.ReadWrite.html'
20+
// @count - '//*[@id="aliased-type"]' 1
21+
// @count - '//*[@id="variants"]' 0
22+
// @count - '//*[@id="fields"]' 1
23+
// @count - '//span[@class="where fmt-newline"]' 2
24+
pub type ReadWrite<R, W> = Pair<R, W>
25+
where
26+
R: std::io::Read,
27+
W: std::io::Write;
28+
29+
// @has 'inner_types_lazy/type.VecPair.html'
30+
// @count - '//*[@id="aliased-type"]' 1
31+
// @count - '//*[@id="variants"]' 0
32+
// @count - '//*[@id="fields"]' 1
33+
// @count - '//span[@class="where fmt-newline"]' 0
34+
pub type VecPair<U, V> = Pair<Vec<U>, Vec<V>>;

tests/rustdoc/typedef-inner-variants.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ pub enum IrTyKind<A, I: Interner> {
3838
}
3939

4040
// @has 'inner_variants/type.NearlyTyKind.html'
41-
// @count - '//*[@id="variants"]' 0
41+
// @count - '//*[@id="aliased-type"]' 1
42+
// @count - '//*[@id="variants"]' 1
4243
// @count - '//*[@id="fields"]' 0
4344
pub type NearlyTyKind<A> = IrTyKind<A, TyCtxt>;
4445

@@ -103,11 +104,12 @@ pub struct HighlyGenericStruct<A, B, C, D> {
103104
pub z: (A, B, C, D)
104105
}
105106

106-
// VERIFY that we NOT show the Aliased Type
107107
// @has 'inner_variants/type.HighlyGenericAABB.html'
108108
// @count - '//*[@id="aliased-type"]' 1
109109
// @count - '//*[@id="variants"]' 0
110-
// @count - '//*[@id="fields"]' 0
110+
// @count - '//*[@id="fields"]' 1
111+
// @matches - '//pre[@class="rust item-decl"]//code' "struct HighlyGenericAABB<A, B>"
112+
// @matches - '//pre[@class="rust item-decl"]//code' "pub z"
111113
pub type HighlyGenericAABB<A, B> = HighlyGenericStruct<A, A, B, B>;
112114

113115
// @has 'inner_variants/type.InlineU64.html'

0 commit comments

Comments
 (0)