about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2022-11-07 11:13:01 -0700
committerMichael Howell <michael@notriddle.com>2022-11-07 11:13:01 -0700
commit8e0cac18cd2951e2679ea55e15242d04e2d410c9 (patch)
treec9e2662883510d37f5f5cf211a1cf923b78f5e40
parent391ba78ab442610a63310b9a3d24646082628081 (diff)
downloadrust-8e0cac18cd2951e2679ea55e15242d04e2d410c9.tar.gz
rust-8e0cac18cd2951e2679ea55e15242d04e2d410c9.zip
rustdoc: refactor `notable_traits_decl` to just act on the type directly
-rw-r--r--src/librustdoc/html/render/mod.rs133
-rw-r--r--src/librustdoc/html/render/print_item.rs7
2 files changed, 72 insertions, 68 deletions
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index 3a041ae15d6..881f1079253 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -861,7 +861,11 @@ fn assoc_method(
         name = name,
         generics = g.print(cx),
         decl = d.full_print(header_len, indent, cx),
-        notable_traits = notable_traits_decl(d, cx),
+        notable_traits = d
+            .output
+            .as_return()
+            .and_then(|output| notable_traits_decl(output, cx))
+            .unwrap_or_default(),
         where_clause = print_where_clause(g, cx, indent, end_newline),
     )
 }
@@ -1273,71 +1277,64 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
     }
 }
 
-fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
+fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> Option<String> {
     let mut out = Buffer::html();
 
-    if let Some((did, ty)) = decl.output.as_return().and_then(|t| Some((t.def_id(cx.cache())?, t)))
+    let did = ty.def_id(cx.cache())?;
+
+    // Box has pass-through impls for Read, Write, Iterator, and Future when the
+    // boxed type implements one of those. We don't want to treat every Box return
+    // as being notably an Iterator (etc), though, so we exempt it. Pin has the same
+    // issue, with a pass-through impl for Future.
+    if Some(did) == cx.tcx().lang_items().owned_box()
+        || Some(did) == cx.tcx().lang_items().pin_type()
     {
-        // Box has pass-through impls for Read, Write, Iterator, and Future when the
-        // boxed type implements one of those. We don't want to treat every Box return
-        // as being notably an Iterator (etc), though, so we exempt it. Pin has the same
-        // issue, with a pass-through impl for Future.
-        if Some(did) == cx.tcx().lang_items().owned_box()
-            || Some(did) == cx.tcx().lang_items().pin_type()
-        {
-            return "".to_string();
-        }
-        if let Some(impls) = cx.cache().impls.get(&did) {
-            for i in impls {
-                let impl_ = i.inner_impl();
-                if !impl_.for_.without_borrowed_ref().is_same(ty.without_borrowed_ref(), cx.cache())
-                {
-                    // Two different types might have the same did,
-                    // without actually being the same.
-                    continue;
-                }
-                if let Some(trait_) = &impl_.trait_ {
-                    let trait_did = trait_.def_id();
-
-                    if cx
-                        .cache()
-                        .traits
-                        .get(&trait_did)
-                        .map_or(false, |t| t.is_notable_trait(cx.tcx()))
-                    {
-                        if out.is_empty() {
-                            write!(
-                                &mut out,
-                                "<span class=\"notable\">Notable traits for {}</span>\
-                             <code class=\"content\">",
-                                impl_.for_.print(cx)
-                            );
-                        }
+        return None;
+    }
+    if let Some(impls) = cx.cache().impls.get(&did) {
+        for i in impls {
+            let impl_ = i.inner_impl();
+            if !impl_.for_.without_borrowed_ref().is_same(ty.without_borrowed_ref(), cx.cache()) {
+                // Two different types might have the same did,
+                // without actually being the same.
+                continue;
+            }
+            if let Some(trait_) = &impl_.trait_ {
+                let trait_did = trait_.def_id();
 
-                        //use the "where" class here to make it small
+                if cx.cache().traits.get(&trait_did).map_or(false, |t| t.is_notable_trait(cx.tcx()))
+                {
+                    if out.is_empty() {
                         write!(
                             &mut out,
-                            "<span class=\"where fmt-newline\">{}</span>",
-                            impl_.print(false, cx)
+                            "<span class=\"notable\">Notable traits for {}</span>\
+                         <code class=\"content\">",
+                            impl_.for_.print(cx)
                         );
-                        for it in &impl_.items {
-                            if let clean::AssocTypeItem(ref tydef, ref _bounds) = *it.kind {
-                                out.push_str("<span class=\"where fmt-newline\">    ");
-                                let empty_set = FxHashSet::default();
-                                let src_link =
-                                    AssocItemLink::GotoSource(trait_did.into(), &empty_set);
-                                assoc_type(
-                                    &mut out,
-                                    it,
-                                    &tydef.generics,
-                                    &[], // intentionally leaving out bounds
-                                    Some(&tydef.type_),
-                                    src_link,
-                                    0,
-                                    cx,
-                                );
-                                out.push_str(";</span>");
-                            }
+                    }
+
+                    //use the "where" class here to make it small
+                    write!(
+                        &mut out,
+                        "<span class=\"where fmt-newline\">{}</span>",
+                        impl_.print(false, cx)
+                    );
+                    for it in &impl_.items {
+                        if let clean::AssocTypeItem(ref tydef, ref _bounds) = *it.kind {
+                            out.push_str("<span class=\"where fmt-newline\">    ");
+                            let empty_set = FxHashSet::default();
+                            let src_link = AssocItemLink::GotoSource(trait_did.into(), &empty_set);
+                            assoc_type(
+                                &mut out,
+                                it,
+                                &tydef.generics,
+                                &[], // intentionally leaving out bounds
+                                Some(&tydef.type_),
+                                src_link,
+                                0,
+                                cx,
+                            );
+                            out.push_str(";</span>");
                         }
                     }
                 }
@@ -1345,16 +1342,18 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
         }
     }
 
-    if !out.is_empty() {
-        out.insert_str(
-            0,
-            "<span class=\"notable-traits\"><span class=\"notable-traits-tooltip\">ⓘ\
-            <span class=\"notable-traits-tooltiptext\"><span class=\"docblock\">",
-        );
-        out.push_str("</code></span></span></span></span>");
+    if out.is_empty() {
+        return None;
     }
 
-    out.into_inner()
+    out.insert_str(
+        0,
+        "<span class=\"notable-traits\"><span class=\"notable-traits-tooltip\">ⓘ\
+        <span class=\"notable-traits-tooltiptext\"><span class=\"docblock\">",
+    );
+    out.push_str("</code></span></span></span></span>");
+
+    Some(out.into_inner())
 }
 
 #[derive(Clone, Copy, Debug)]
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index ce4fc4d68fa..e6abd23eb95 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -533,7 +533,12 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
                 generics = f.generics.print(cx),
                 where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
                 decl = f.decl.full_print(header_len, 0, cx),
-                notable_traits = notable_traits_decl(&f.decl, cx),
+                notable_traits = f
+                    .decl
+                    .output
+                    .as_return()
+                    .and_then(|output| notable_traits_decl(output, cx))
+                    .unwrap_or_default(),
             );
         });
     });