Skip to content

Commit

Permalink
rustdoc: always print type alias inner type (with it's where clauses)
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Aug 28, 2023
1 parent 282acb9 commit 706d010
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
17 changes: 0 additions & 17 deletions src/librustdoc/clean/types.rs
Expand Up @@ -2252,23 +2252,6 @@ pub(crate) struct TypeAlias {
pub(crate) item_type: Option<Type>,
}

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<GenericBound>,
Expand Down
17 changes: 13 additions & 4 deletions src/librustdoc/html/render/print_item.rs
Expand Up @@ -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,
"<h2 id=\"aliased-type\" class=\"small-section-header\">\
Expand All @@ -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,
Expand All @@ -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);
}
Expand All @@ -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,
"",
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/sidebar.rs
Expand Up @@ -236,7 +236,7 @@ fn sidebar_type_alias<'a>(
t: &'a clean::TypeAlias,
) -> Vec<LinkBlock<'a>> {
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
Expand Down
34 changes: 34 additions & 0 deletions 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<A, B> {
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<Q, R> = Pair<R, Q>;

// @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<R, W> = Pair<R, W>
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<U, V> = Pair<Vec<U>, Vec<V>>;
8 changes: 5 additions & 3 deletions tests/rustdoc/typedef-inner-variants.rs
Expand Up @@ -38,7 +38,8 @@ pub enum IrTyKind<A, I: Interner> {
}

// @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<A> = IrTyKind<A, TyCtxt>;

Expand Down Expand Up @@ -103,11 +104,12 @@ pub struct HighlyGenericStruct<A, B, C, D> {
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<A, B>"
// @matches - '//pre[@class="rust item-decl"]//code' "pub z"
pub type HighlyGenericAABB<A, B> = HighlyGenericStruct<A, A, B, B>;

// @has 'inner_variants/type.InlineU64.html'
Expand Down

0 comments on commit 706d010

Please sign in to comment.