From 706d010c8b590f40cc2fb3de7b744d9a59a93ac9 Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 25 Aug 2023 23:07:03 +0200 Subject: [PATCH] rustdoc: always print type alias inner type (with it's where clauses) --- src/librustdoc/clean/types.rs | 17 ---------- src/librustdoc/html/render/print_item.rs | 17 +++++++--- src/librustdoc/html/render/sidebar.rs | 2 +- .../typedef-inner-variants-lazy_type_alias.rs | 34 +++++++++++++++++++ tests/rustdoc/typedef-inner-variants.rs | 8 +++-- 5 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 tests/rustdoc/typedef-inner-variants-lazy_type_alias.rs diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index ce3f1bcf215a8..9cc5d7c2911f2 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -2252,23 +2252,6 @@ pub(crate) struct TypeAlias { pub(crate) item_type: Option, } -impl TypeAlias { - pub(crate) fn should_display_inner_type(&self) -> bool { - // Only show inner variants if: - // - the typealias does NOT have any generics (modulo lifetimes) - // - AND the aliased type has some generics - // - // Otherwise, showing a non-generic type is rendurant with its own page, or - // if it still has some generics, it's not as useful. - self.generics - .params - .iter() - .all(|param| matches!(param.kind, GenericParamDefKind::Lifetime { .. })) - && self.generics.where_predicates.is_empty() - && self.type_.generics().is_some_and(|generics| !generics.is_empty()) - } -} - #[derive(Clone, Debug)] pub(crate) struct OpaqueTy { pub(crate) bounds: Vec, diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index fc0549c3f64d1..2da7cb01c9016 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -1237,7 +1237,7 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); - if let Some(inner_type) = &t.inner_type && t.should_display_inner_type() { + if let Some(inner_type) = &t.inner_type { write!( w, "

\ @@ -1256,7 +1256,7 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c render_enum_fields( w, cx, - None, + Some(&t.generics), variants_iter(), variants_count, has_stripped_entries, @@ -1271,7 +1271,16 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c let has_stripped_fields = fields.len() != fields_count; write!(w, "union {}{}", it.name.unwrap(), t.generics.print(cx)); - render_struct_fields(w, None, None, fields, "", true, has_stripped_fields, cx); + render_struct_fields( + w, + Some(&t.generics), + None, + fields, + "", + true, + has_stripped_fields, + cx, + ); }); item_fields(w, cx, it, fields, None); } @@ -1283,7 +1292,7 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c write!(w, "struct {}{}", it.name.unwrap(), t.generics.print(cx)); render_struct_fields( w, - None, + Some(&t.generics), *ctor_kind, fields, "", diff --git a/src/librustdoc/html/render/sidebar.rs b/src/librustdoc/html/render/sidebar.rs index c82a45303a49e..fce31a8423d16 100644 --- a/src/librustdoc/html/render/sidebar.rs +++ b/src/librustdoc/html/render/sidebar.rs @@ -236,7 +236,7 @@ fn sidebar_type_alias<'a>( t: &'a clean::TypeAlias, ) -> Vec> { let mut items = vec![]; - if let Some(inner_type) = &t.inner_type && t.should_display_inner_type() { + if let Some(inner_type) = &t.inner_type { match inner_type { clean::TypeAliasInnerType::Enum { variants, is_non_exhaustive: _ } => { let mut variants = variants diff --git a/tests/rustdoc/typedef-inner-variants-lazy_type_alias.rs b/tests/rustdoc/typedef-inner-variants-lazy_type_alias.rs new file mode 100644 index 0000000000000..ff84352d7169f --- /dev/null +++ b/tests/rustdoc/typedef-inner-variants-lazy_type_alias.rs @@ -0,0 +1,34 @@ +#![crate_name = "inner_types_lazy"] + +#![feature(lazy_type_alias)] +#![allow(incomplete_features)] + +// @has 'inner_types_lazy/struct.Pair.html' +pub struct Pair { + pub first: A, + pub second: B, +} + +// @has 'inner_types_lazy/type.ReversedTypesPair.html' +// @count - '//*[@id="aliased-type"]' 1 +// @count - '//*[@id="variants"]' 0 +// @count - '//*[@id="fields"]' 1 +// @count - '//span[@class="where fmt-newline"]' 0 +pub type ReversedTypesPair = Pair; + +// @has 'inner_types_lazy/type.ReadWrite.html' +// @count - '//*[@id="aliased-type"]' 1 +// @count - '//*[@id="variants"]' 0 +// @count - '//*[@id="fields"]' 1 +// @count - '//span[@class="where fmt-newline"]' 2 +pub type ReadWrite = Pair +where + R: std::io::Read, + W: std::io::Write; + +// @has 'inner_types_lazy/type.VecPair.html' +// @count - '//*[@id="aliased-type"]' 1 +// @count - '//*[@id="variants"]' 0 +// @count - '//*[@id="fields"]' 1 +// @count - '//span[@class="where fmt-newline"]' 0 +pub type VecPair = Pair, Vec>; diff --git a/tests/rustdoc/typedef-inner-variants.rs b/tests/rustdoc/typedef-inner-variants.rs index 31edb0a8706ac..b734714fd6432 100644 --- a/tests/rustdoc/typedef-inner-variants.rs +++ b/tests/rustdoc/typedef-inner-variants.rs @@ -38,7 +38,8 @@ pub enum IrTyKind { } // @has 'inner_variants/type.NearlyTyKind.html' -// @count - '//*[@id="variants"]' 0 +// @count - '//*[@id="aliased-type"]' 1 +// @count - '//*[@id="variants"]' 1 // @count - '//*[@id="fields"]' 0 pub type NearlyTyKind = IrTyKind; @@ -103,11 +104,12 @@ pub struct HighlyGenericStruct { pub z: (A, B, C, D) } -// VERIFY that we NOT show the Aliased Type // @has 'inner_variants/type.HighlyGenericAABB.html' // @count - '//*[@id="aliased-type"]' 1 // @count - '//*[@id="variants"]' 0 -// @count - '//*[@id="fields"]' 0 +// @count - '//*[@id="fields"]' 1 +// @matches - '//pre[@class="rust item-decl"]//code' "struct HighlyGenericAABB" +// @matches - '//pre[@class="rust item-decl"]//code' "pub z" pub type HighlyGenericAABB = HighlyGenericStruct; // @has 'inner_variants/type.InlineU64.html'