about summary refs log tree commit diff
path: root/src/librustdoc/html/render
diff options
context:
space:
mode:
authorNoah Lev <camelidcamel@gmail.com>2024-07-31 16:45:05 -0700
committerNoah Lev <camelidcamel@gmail.com>2024-08-04 12:49:27 -0700
commit664b3ffbe932d93677ac7dff52252e02aee60ef9 (patch)
treedd070d567793922f464194558382385c16e95eaa /src/librustdoc/html/render
parent249d686c7005ebb4d05234e9db6d29c2923296e2 (diff)
rustdoc: Create `SelfTy` to replace `Generic(kw::SelfUpper)`
Rustdoc often has to special-case `Self` because it is, well, a special
type of generic parameter (although it also behaves as an alias in
concrete impls). Instead of spreading this special-casing throughout the
code base, create a new variant of the `clean::Type` enum that is for
`Self` types.

This is a refactoring that has almost no impact on rustdoc's behavior,
except that `&Self`, `(Self,)`, `&[Self]`, and other similar occurrences
of `Self` no longer link to the wrapping type (reference primitive,
tuple primitive, etc.) as regular generics do. I felt this made more
sense since users would expect `Self` to link to the containing trait or
aliased type (though those are usually expanded), not the primitive that
is wrapping it. For an example of the change, see the docs for
`std::alloc::Allocator::by_ref`.
Diffstat (limited to 'src/librustdoc/html/render')
-rw-r--r--src/librustdoc/html/render/search_index.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs
index b3f4d82e054..fc3d6c99b80 100644
--- a/src/librustdoc/html/render/search_index.rs
+++ b/src/librustdoc/html/render/search_index.rs
@@ -797,7 +797,11 @@ fn get_index_type_id(
             }
         }
         // Not supported yet
-        clean::Type::Pat(..) | clean::Generic(_) | clean::ImplTrait(_) | clean::Infer => None,
+        clean::Type::Pat(..)
+        | clean::Generic(_)
+        | clean::SelfTy
+        | clean::ImplTrait(_)
+        | clean::Infer => None,
     }
 }
 
@@ -848,13 +852,18 @@ fn simplify_fn_type<'tcx, 'a>(
         (false, arg)
     };
 
+    let as_arg_s = |t: &Type| match *t {
+        Type::Generic(arg_s) => Some(arg_s),
+        Type::SelfTy => Some(kw::SelfUpper),
+        _ => None,
+    };
     // If this argument is a type parameter and not a trait bound or a type, we need to look
     // for its bounds.
-    if let Type::Generic(arg_s) = *arg {
+    if let Some(arg_s) = as_arg_s(arg) {
         // First we check if the bounds are in a `where` predicate...
         let mut type_bounds = Vec::new();
         for where_pred in generics.where_predicates.iter().filter(|g| match g {
-            WherePredicate::BoundPredicate { ty: Type::Generic(ty_s), .. } => *ty_s == arg_s,
+            WherePredicate::BoundPredicate { ty, .. } => *ty == *arg,
             _ => false,
         }) {
             let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);