Skip to content

Commit

Permalink
rustdoc: normalize all typedef inner types
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Aug 25, 2023
1 parent af6889c commit 9443f84
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
25 changes: 25 additions & 0 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,11 @@ pub(crate) fn clean_variant_def_with_args<'tcx>(
ty::VariantDiscr::Relative(_) => None,
};

use rustc_middle::traits::ObligationCause;
use rustc_trait_selection::infer::TyCtxtInferExt;
use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;

let infcx = cx.tcx.infer_ctxt().build();
let kind = match variant.ctor_kind() {
Some(CtorKind::Const) => VariantKind::CLike,
Some(CtorKind::Fn) => VariantKind::Tuple(
Expand All @@ -2414,6 +2419,16 @@ pub(crate) fn clean_variant_def_with_args<'tcx>(
.iter()
.map(|field| {
let ty = cx.tcx.type_of(field.did).instantiate(cx.tcx, args);

// normalize the type to only show concrete types
// note: we do not use try_normalize_erasing_regions since we
// do care about showing the regions
let ty = infcx
.at(&ObligationCause::dummy(), cx.param_env)
.query_normalize(ty)
.map(|normalized| normalized.value)
.unwrap_or(ty);

clean_field_with_def_id(
field.did,
field.name,
Expand All @@ -2429,6 +2444,16 @@ pub(crate) fn clean_variant_def_with_args<'tcx>(
.iter()
.map(|field| {
let ty = cx.tcx.type_of(field.did).instantiate(cx.tcx, args);

// normalize the type to only show concrete types
// note: we do not use try_normalize_erasing_regions since we
// do care about showing the regions
let ty = infcx
.at(&ObligationCause::dummy(), cx.param_env)
.query_normalize(ty)
.map(|normalized| normalized.value)
.unwrap_or(ty);

clean_field_with_def_id(
field.did,
field.name,
Expand Down
30 changes: 23 additions & 7 deletions tests/rustdoc/typedef-inner-variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@ extern crate cross_crate_generic_typedef;

pub struct Adt;
pub struct Ty;
pub struct TyCtxt;

pub trait Interner {
type Adt;
type Ty;
}

impl Interner for TyCtxt {
type Adt = Adt;
type Ty = Ty;
}

// @has 'inner_variants/type.AliasTy.html'
// @count - '//*[@id="variants"]' 0
// @count - '//*[@id="fields"]' 0
pub type AliasTy = Ty;

// @has 'inner_variants/enum.IrTyKind.html'
pub enum IrTyKind<A, B> {
pub enum IrTyKind<A, I: Interner> {
/// Doc comment for AdtKind
AdtKind(A),
AdtKind(I::Adt),
/// and another one for TyKind
TyKind(A, B),
TyKind(I::Adt, <I as Interner>::Ty),
// no comment
StructKind { a: A, },
#[doc(hidden)]
Expand All @@ -29,14 +40,18 @@ pub enum IrTyKind<A, B> {
// @has 'inner_variants/type.NearlyTyKind.html'
// @count - '//*[@id="variants"]' 0
// @count - '//*[@id="fields"]' 0
pub type NearlyTyKind<B> = IrTyKind<Adt, B>;
pub type NearlyTyKind<A> = IrTyKind<A, TyCtxt>;

// @has 'inner_variants/type.TyKind.html'
// @count - '//*[@id="variants"]' 1
// @count - '//*[@id="fields"]' 0
// @count - '//*[@class="variant"]' 3
// @matches - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code' "enum TyKind"
pub type TyKind = IrTyKind<Adt, Ty>;
// @has - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code/a[1]' "Adt"
// @has - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code/a[2]' "Adt"
// @has - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code/a[3]' "Ty"
// @has - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code/a[4]' "i64"
pub type TyKind = IrTyKind<i64, TyCtxt>;

// @has 'inner_variants/union.OneOr.html'
pub union OneOr<A: Copy> {
Expand Down Expand Up @@ -68,13 +83,14 @@ pub type OneU64 = One<u64>;

// @has 'inner_variants/struct.OnceA.html'
pub struct OnceA<'a, A> {
a: &'a A, // private
pub a: &'a A,
}

// @has 'inner_variants/type.Once.html'
// @count - '//*[@id="variants"]' 0
// @count - '//*[@id="fields"]' 0
// @count - '//*[@id="fields"]' 1
// @matches - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code' "struct Once<'a>"
// @matches - '//details[@class="toggle"]//pre[@class="rust item-decl"]//code' "&'a"
pub type Once<'a> = OnceA<'a, i64>;

// @has 'inner_variants/struct.HighlyGenericStruct.html'
Expand Down

0 comments on commit 9443f84

Please sign in to comment.