From 2e7b5abbfdf196cf1adb9f0804bb04cbac768e08 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 21 Apr 2022 16:22:03 -0700 Subject: [PATCH] rustdoc: make primitive synthetic impls for correct doc module This improves the accuracy of libcore primitive docs, which was missing the blanket and auto impls for most primitive types. To test this, compare nightly [libcore::str] docs, which lack auto traits like Send, with [std::str] docs, which show them. [libcore::str]: https://doc.rust-lang.org/nightly/core/primitive.str.html [libstd::str]: https://doc.rust-lang.org/nightly/std/primitive.str.html It also avoids getting synthetic impls for primitive types on crates that do not actually show them.
Before and After trace logs Before: [notriddle@deep-thought test-dingus]$ RUSTDOC_LOG=rustdoc=trace rustdoc +nightly test.rs 2>&1 | grep -E 'get_blanket_impls\(' TRACE rustdoc::clean::blanket_impl get_blanket_impls(Whatever) TRACE rustdoc::clean::blanket_impl get_blanket_impls(isize) TRACE rustdoc::clean::blanket_impl get_blanket_impls([T]) TRACE rustdoc::clean::blanket_impl get_blanket_impls([u8]) TRACE rustdoc::clean::blanket_impl get_blanket_impls([T]) TRACE rustdoc::clean::blanket_impl get_blanket_impls([u8]) TRACE rustdoc::clean::blanket_impl get_blanket_impls(char) TRACE rustdoc::clean::blanket_impl get_blanket_impls(u128) TRACE rustdoc::clean::blanket_impl get_blanket_impls(u16) TRACE rustdoc::clean::blanket_impl get_blanket_impls(i128) TRACE rustdoc::clean::blanket_impl get_blanket_impls(i16) TRACE rustdoc::clean::blanket_impl get_blanket_impls(str) TRACE rustdoc::clean::blanket_impl get_blanket_impls(str) TRACE rustdoc::clean::blanket_impl get_blanket_impls(f64) TRACE rustdoc::clean::blanket_impl get_blanket_impls(f64) TRACE rustdoc::clean::blanket_impl get_blanket_impls(u64) TRACE rustdoc::clean::blanket_impl get_blanket_impls(u8) TRACE rustdoc::clean::blanket_impl get_blanket_impls(i64) TRACE rustdoc::clean::blanket_impl get_blanket_impls(i8) TRACE rustdoc::clean::blanket_impl get_blanket_impls(*const T) TRACE rustdoc::clean::blanket_impl get_blanket_impls(*mut T) TRACE rustdoc::clean::blanket_impl get_blanket_impls(*const [T]) TRACE rustdoc::clean::blanket_impl get_blanket_impls(*mut [T]) TRACE rustdoc::clean::blanket_impl get_blanket_impls([T; N]) TRACE rustdoc::clean::blanket_impl get_blanket_impls(bool) TRACE rustdoc::clean::blanket_impl get_blanket_impls(f32) TRACE rustdoc::clean::blanket_impl get_blanket_impls(f32) TRACE rustdoc::clean::blanket_impl get_blanket_impls(u32) TRACE rustdoc::clean::blanket_impl get_blanket_impls(usize) TRACE rustdoc::clean::blanket_impl get_blanket_impls(i32) After: [notriddle@deep-thought test-dingus]$ RUSTDOC_LOG=rustdoc=trace rustdoc +dev test.rs 2>&1 | grep -E 'get_blanket_impls\(' TRACE rustdoc::clean::blanket_impl get_blanket_impls(Whatever)
--- src/librustdoc/passes/collect_trait_impls.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index 65459913eea..6962b5c7ee3 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -43,14 +43,19 @@ } }); - // Also try to inline primitive impls from other crates. cx.tcx.sess.prof.generic_activity("build_primitive_trait_impls").run(|| { for def_id in PrimitiveType::all_impls(cx.tcx) { + // Try to inline primitive impls from other crates. if !def_id.is_local() { inline::build_impl(cx, None, def_id, None, &mut new_items); - - // FIXME(eddyb) is this `doc(hidden)` check needed? - if !cx.tcx.is_doc_hidden(def_id) { + } + } + for (prim, did) in PrimitiveType::primitive_locations(cx.tcx) { + // Do not calculate blanket impl list for docs that are not going to be rendered. + // While the `impl` blocks themselves are only in `libcore`, the module with `doc` + // attached is directly included in `libstd` as well. + if did.is_local() { + for def_id in prim.impls(cx.tcx) { let impls = get_auto_trait_and_blanket_impls(cx, def_id); new_items.extend(impls.filter(|i| cx.inlined.insert(i.item_id))); }