Skip to content

Commit 3eb0da2

Browse files
committed
rustdoc: be more strict about "Methods from Deref"
Note that this does not yet fix the sidebar logic.
1 parent d497e43 commit 3eb0da2

File tree

1 file changed

+13
-2
lines changed
  • src/librustdoc/html/render

1 file changed

+13
-2
lines changed

Diff for: src/librustdoc/html/render/mod.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub(crate) fn ensure_trailing_slash(v: &str) -> impl fmt::Display {
8989

9090
/// Specifies whether rendering directly implemented trait items or ones from a certain Deref
9191
/// impl.
92-
#[derive(Copy, Clone)]
92+
#[derive(Copy, Clone, Debug)]
9393
pub(crate) enum AssocItemRender<'a> {
9494
All,
9595
DerefFor { trait_: &'a clean::Path, type_: &'a clean::Type, deref_mut_: bool },
@@ -1296,7 +1296,7 @@ fn render_assoc_items_inner(
12961296
info!("Documenting associated items of {:?}", containing_item.name);
12971297
let cache = &cx.shared.cache;
12981298
let Some(v) = cache.impls.get(&it) else { return };
1299-
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
1299+
let (mut non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
13001300
if !non_trait.is_empty() {
13011301
let mut close_tags = <Vec<&str>>::with_capacity(1);
13021302
let mut tmp_buf = String::new();
@@ -1314,6 +1314,16 @@ fn render_assoc_items_inner(
13141314
AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => {
13151315
let id =
13161316
cx.derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(cx))));
1317+
// the `impls.get` above only looks at the outermost type,
1318+
// and the Deref impl may only be implemented for certain
1319+
// values of generic parameters.
1320+
// for example, if an item impls `Deref<[u8]>`,
1321+
// we should not show methods from `[MaybeUninit<u8>]`.
1322+
// this `retain` filters out any instances where
1323+
// the types do not line up perfectly.
1324+
non_trait.retain(|impl_| {
1325+
type_.is_doc_subtype_of(&impl_.inner_impl().for_, &cx.shared.cache)
1326+
});
13171327
let derived_id = cx.derive_id(&id);
13181328
close_tags.push("</details>");
13191329
write_str(
@@ -1392,6 +1402,7 @@ fn render_assoc_items_inner(
13921402
}
13931403
}
13941404

1405+
/// `derefs` is the set of all deref targets that have already been handled.
13951406
fn render_deref_methods(
13961407
mut w: impl Write,
13971408
cx: &Context<'_>,

0 commit comments

Comments
 (0)