Skip to content

Commit 09ec4ff

Browse files
Re-implement rust-lang#83826 unintrusively via a new command line flag.
1 parent 83bde52 commit 09ec4ff

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

src/librustdoc/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ crate struct RenderOptions {
276276
crate call_locations: AllCallLocations,
277277
/// If `true`, Context::init will not emit shared files.
278278
crate no_emit_shared: bool,
279+
/// If `true`, "Methods From Deref" will be displayed after all other sections.
280+
crate show_deref_methods_last: bool,
279281
}
280282

281283
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -658,6 +660,7 @@ impl Options {
658660
let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
659661
let extern_html_root_takes_precedence =
660662
matches.opt_present("extern-html-root-takes-precedence");
663+
let show_deref_methods_last = matches.opt_present("show-deref-methods-last");
661664

662665
if generate_link_to_definition && (show_coverage || output_format != OutputFormat::Html) {
663666
diag.struct_err(
@@ -736,6 +739,7 @@ impl Options {
736739
generate_link_to_definition,
737740
call_locations,
738741
no_emit_shared: false,
742+
show_deref_methods_last,
739743
},
740744
crate_name,
741745
output_format,

src/librustdoc/html/render/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ crate struct SharedContext<'tcx> {
127127
crate cache: Cache,
128128

129129
crate call_locations: AllCallLocations,
130+
/// If `true`, "Methods From Deref" will be displayed after all other sections.
131+
crate show_deref_methods_last: bool,
130132
}
131133

132134
impl SharedContext<'_> {
@@ -398,6 +400,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
398400
generate_link_to_definition,
399401
call_locations,
400402
no_emit_shared,
403+
show_deref_methods_last,
401404
..
402405
} = options;
403406

@@ -485,6 +488,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
485488
span_correspondance_map: matches,
486489
cache,
487490
call_locations,
491+
show_deref_methods_last,
488492
};
489493

490494
// Add the default themes to the `Vec` of stylepaths

src/librustdoc/html/render/mod.rs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,12 +1105,18 @@ fn render_assoc_items_inner(
11051105
}
11061106

11071107
if !traits.is_empty() {
1108-
let deref_impl =
1109-
traits.iter().find(|t| t.trait_did() == cx.tcx().lang_items().deref_trait());
1110-
if let Some(impl_) = deref_impl {
1111-
let has_deref_mut =
1112-
traits.iter().any(|t| t.trait_did() == cx.tcx().lang_items().deref_mut_trait());
1113-
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut, derefs);
1108+
let mut deref_methods = |w: &mut Buffer| {
1109+
let deref_impl =
1110+
traits.iter().find(|t| t.trait_did() == cx.tcx().lang_items().deref_trait());
1111+
if let Some(impl_) = deref_impl {
1112+
let has_deref_mut =
1113+
traits.iter().any(|t| t.trait_did() == cx.tcx().lang_items().deref_mut_trait());
1114+
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut, derefs);
1115+
}
1116+
};
1117+
1118+
if !cx.shared.show_deref_methods_last {
1119+
deref_methods(w);
11141120
}
11151121

11161122
// 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(
11611167
render_impls(cx, w, &blanket_impl, containing_item);
11621168
w.write_str("</div>");
11631169
}
1170+
1171+
if cx.shared.show_deref_methods_last {
1172+
deref_methods(w);
1173+
}
11641174
}
11651175
}
11661176

@@ -2003,12 +2013,18 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
20032013
}
20042014

20052015
if v.iter().any(|i| i.inner_impl().trait_.is_some()) {
2006-
if let Some(impl_) =
2007-
v.iter().find(|i| i.trait_did() == cx.tcx().lang_items().deref_trait())
2008-
{
2009-
let mut derefs = FxHashSet::default();
2010-
derefs.insert(did);
2011-
sidebar_deref_methods(cx, out, impl_, v, &mut derefs);
2016+
let deref_methods = |out: &mut Buffer| {
2017+
if let Some(impl_) =
2018+
v.iter().find(|i| i.trait_did() == cx.tcx().lang_items().deref_trait())
2019+
{
2020+
let mut derefs = FxHashSet::default();
2021+
derefs.insert(did);
2022+
sidebar_deref_methods(cx, out, impl_, v, &mut derefs);
2023+
}
2024+
};
2025+
2026+
if !cx.shared.show_deref_methods_last {
2027+
deref_methods(out);
20122028
}
20132029

20142030
let format_impls = |impls: Vec<&Impl>| {
@@ -2077,6 +2093,10 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
20772093
);
20782094
write_sidebar_links(out, blanket_format);
20792095
}
2096+
2097+
if cx.shared.show_deref_methods_last {
2098+
deref_methods(out);
2099+
}
20802100
}
20812101
}
20822102
}

src/librustdoc/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,13 @@ fn opts() -> Vec<RustcOptGroup> {
626626
"path to function call information (for displaying examples in the documentation)",
627627
)
628628
}),
629+
unstable("show-deref-methods-last", |o| {
630+
o.optflagmulti(
631+
"",
632+
"show-deref-methods-last",
633+
"display trait implementations before methods from deref",
634+
)
635+
}),
629636
// deprecated / removed options
630637
stable("plugin-path", |o| {
631638
o.optmulti(

0 commit comments

Comments
 (0)