From 09ec4ff1b0befd364b1d40e7193bbbbdb20b3278 Mon Sep 17 00:00:00 2001 From: slightlyoutofphase Date: Sat, 25 Dec 2021 03:29:21 -0500 Subject: [PATCH] Re-implement #83826 unintrusively via a new command line flag. --- src/librustdoc/config.rs | 4 +++ src/librustdoc/html/render/context.rs | 4 +++ src/librustdoc/html/render/mod.rs | 44 +++++++++++++++++++-------- src/librustdoc/lib.rs | 7 +++++ 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index d300afa313237..6584d1305ad0a 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -276,6 +276,8 @@ crate struct RenderOptions { crate call_locations: AllCallLocations, /// If `true`, Context::init will not emit shared files. crate no_emit_shared: bool, + /// If `true`, "Methods From Deref" will be displayed after all other sections. + crate show_deref_methods_last: bool, } #[derive(Copy, Clone, Debug, PartialEq, Eq)] @@ -658,6 +660,7 @@ impl Options { let generate_link_to_definition = matches.opt_present("generate-link-to-definition"); let extern_html_root_takes_precedence = matches.opt_present("extern-html-root-takes-precedence"); + let show_deref_methods_last = matches.opt_present("show-deref-methods-last"); if generate_link_to_definition && (show_coverage || output_format != OutputFormat::Html) { diag.struct_err( @@ -736,6 +739,7 @@ impl Options { generate_link_to_definition, call_locations, no_emit_shared: false, + show_deref_methods_last, }, crate_name, output_format, diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 45a436c448710..251d591056cdf 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -127,6 +127,8 @@ crate struct SharedContext<'tcx> { crate cache: Cache, crate call_locations: AllCallLocations, + /// If `true`, "Methods From Deref" will be displayed after all other sections. + crate show_deref_methods_last: bool, } impl SharedContext<'_> { @@ -398,6 +400,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { generate_link_to_definition, call_locations, no_emit_shared, + show_deref_methods_last, .. } = options; @@ -485,6 +488,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { span_correspondance_map: matches, cache, call_locations, + show_deref_methods_last, }; // Add the default themes to the `Vec` of stylepaths diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index eb606178d244d..80c65c9bdb02d 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1105,12 +1105,18 @@ fn render_assoc_items_inner( } if !traits.is_empty() { - let deref_impl = - traits.iter().find(|t| t.trait_did() == cx.tcx().lang_items().deref_trait()); - if let Some(impl_) = deref_impl { - let has_deref_mut = - traits.iter().any(|t| t.trait_did() == cx.tcx().lang_items().deref_mut_trait()); - render_deref_methods(w, cx, impl_, containing_item, has_deref_mut, derefs); + let mut deref_methods = |w: &mut Buffer| { + let deref_impl = + traits.iter().find(|t| t.trait_did() == cx.tcx().lang_items().deref_trait()); + if let Some(impl_) = deref_impl { + let has_deref_mut = + traits.iter().any(|t| t.trait_did() == cx.tcx().lang_items().deref_mut_trait()); + render_deref_methods(w, cx, impl_, containing_item, has_deref_mut, derefs); + } + }; + + if !cx.shared.show_deref_methods_last { + deref_methods(w); } // If we were already one level into rendering deref methods, we don't want to render @@ -1161,6 +1167,10 @@ fn render_assoc_items_inner( render_impls(cx, w, &blanket_impl, containing_item); w.write_str(""); } + + if cx.shared.show_deref_methods_last { + deref_methods(w); + } } } @@ -2003,12 +2013,18 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { } if v.iter().any(|i| i.inner_impl().trait_.is_some()) { - if let Some(impl_) = - v.iter().find(|i| i.trait_did() == cx.tcx().lang_items().deref_trait()) - { - let mut derefs = FxHashSet::default(); - derefs.insert(did); - sidebar_deref_methods(cx, out, impl_, v, &mut derefs); + let deref_methods = |out: &mut Buffer| { + if let Some(impl_) = + v.iter().find(|i| i.trait_did() == cx.tcx().lang_items().deref_trait()) + { + let mut derefs = FxHashSet::default(); + derefs.insert(did); + sidebar_deref_methods(cx, out, impl_, v, &mut derefs); + } + }; + + if !cx.shared.show_deref_methods_last { + deref_methods(out); } let format_impls = |impls: Vec<&Impl>| { @@ -2077,6 +2093,10 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { ); write_sidebar_links(out, blanket_format); } + + if cx.shared.show_deref_methods_last { + deref_methods(out); + } } } } diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 95de28e0c5b26..4e5f6ae50e9c4 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -626,6 +626,13 @@ fn opts() -> Vec { "path to function call information (for displaying examples in the documentation)", ) }), + unstable("show-deref-methods-last", |o| { + o.optflagmulti( + "", + "show-deref-methods-last", + "display trait implementations before methods from deref", + ) + }), // deprecated / removed options stable("plugin-path", |o| { o.optmulti(