From 53761e12226b03e742ae3c4b6e0d7716ad38b18f Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume.gomez@huawei.com>
Date: Wed, 21 Jun 2023 13:37:17 +0200
Subject: [PATCH] Correctly handle Weak type aliases in rustdoc

---
 src/librustdoc/clean/mod.rs     | 21 +++++++++++++++++----
 tests/rustdoc/alias-reexport.rs |  6 ++----
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index 851002d5e79..67d433dd0ad 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -2024,8 +2024,8 @@ pub(crate) fn clean_middle_ty<'tcx>(
             Tuple(t.iter().map(|t| clean_middle_ty(bound_ty.rebind(t), cx, None, None)).collect())
         }
 
-        ty::Alias(ty::Projection, ref data) => {
-            clean_projection(bound_ty.rebind(*data), cx, parent_def_id)
+        ty::Alias(ty::Projection, data) => {
+            clean_projection(bound_ty.rebind(data), cx, parent_def_id)
         }
 
         ty::Alias(ty::Inherent, alias_ty) => {
@@ -2053,8 +2053,21 @@ pub(crate) fn clean_middle_ty<'tcx>(
         }
 
         ty::Alias(ty::Weak, data) => {
-            let ty = cx.tcx.type_of(data.def_id).subst(cx.tcx, data.substs);
-            clean_middle_ty(bound_ty.rebind(ty), cx, None, None)
+            if cx.tcx.features().lazy_type_alias {
+                // Weak type alias `data` represents the `type X` in `type X = Y`. If we need `Y`,
+                // we need to use `type_of`.
+                let path = external_path(
+                    cx,
+                    data.def_id,
+                    false,
+                    ThinVec::new(),
+                    bound_ty.rebind(data.substs),
+                );
+                Type::Path { path }
+            } else {
+                let ty = cx.tcx.type_of(data.def_id).subst(cx.tcx, data.substs);
+                clean_middle_ty(bound_ty.rebind(ty), cx, None, None)
+            }
         }
 
         ty::Param(ref p) => {
diff --git a/tests/rustdoc/alias-reexport.rs b/tests/rustdoc/alias-reexport.rs
index 029891197fc..a2a8e651caf 100644
--- a/tests/rustdoc/alias-reexport.rs
+++ b/tests/rustdoc/alias-reexport.rs
@@ -7,11 +7,9 @@
 extern crate alias_reexport2;
 
 // @has 'foo/reexport/fn.foo.html'
-// FIXME: should be 'pub fn foo() -> Reexport'
-// @has - '//*[@class="rust item-decl"]' 'pub fn foo() -> u8'
+// @has - '//*[@class="rust item-decl"]' 'pub fn foo() -> Reexported'
 // @has 'foo/reexport/fn.foo2.html'
-// FIXME: should be 'pub fn foo2() -> Result<Reexport, ()>'
-// @has - '//*[@class="rust item-decl"]' 'pub fn foo2() -> Result<u8, ()>'
+// @has - '//*[@class="rust item-decl"]' 'pub fn foo2() -> Result<Reexported, ()>'
 // @has 'foo/reexport/type.Reexported.html'
 // @has - '//*[@class="rust item-decl"]' 'pub type Reexported = u8;'
 #[doc(inline)]