Skip to content

[rustdoc] Fix how Deref items is handled. - #160915

Open
GuillaumeGomez wants to merge 3 commits into
rust-lang:mainfrom
GuillaumeGomez:deref-items
Open

[rustdoc] Fix how Deref items is handled.#160915
GuillaumeGomez wants to merge 3 commits into
rust-lang:mainfrom
GuillaumeGomez:deref-items

Conversation

@GuillaumeGomez

@GuillaumeGomez GuillaumeGomez commented Aug 11, 2026

Copy link
Copy Markdown
Member

View all comments

Fixes #160236.

This PR fixes a few things around how we handle Deref:

  • Since nothing except for methods is callable through a Deref, only methods should be kept
  • If the Deref::Target is a type implementing Copy, then methods taking self work and should be displayed.

During the clean pass, I store the item DefId on which Deref is implemented in a DefIdSet if the Deref::Target implements Copy. Then during rendering, we check if the "parent item" is in in the DefIdSet, and if so, we keep methods with self.

Now you might wonder why the item and not the derefed item. It's because the DefId we get from the computed type doesn't match the DefId of the actual type (that's where I spent most of my time, finding an ID (DefId/ItemId) I can use as key T_T).

It computes in html/render if the Deref::Target item is copy before rendering it.

So to resume:

  • &self is always kept.
  • &mut self is kept if DerefMut is implemented (nothing changed there).
  • self is kept only if Deref::Target is Copy.
  • Everything else disappears as they're not callable through Deref.

r? @camelid

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Aug 11, 2026
@rustbot

rustbot commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

camelid is currently at their maximum review capacity.
They may take a while to respond.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Oof, that's a big diff. It's mostly because of reindent, not much that we can do about. ^^'

@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Fixed fmt. ^^'

@camelid camelid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw there's some missing wording in this sentence of the PR description, so I'm not quite sure what it says:

Since since except for methods is callable through a Deref, only methods should be kept

View changes since this review

Comment thread src/librustdoc/clean/inline.rs Outdated
Comment thread src/librustdoc/clean/utils.rs Outdated
Comment thread tests/rustdoc-html/deref/deref-to-primitive.rs
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 11, 2026
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Btw there's some missing wording in this sentence of the PR description, so I'm not quite sure what it says:

Since since except for methods is callable through a Deref, only methods should be kept

Fixed the typo. Correct sentence is:

Since nothing except for methods is callable through a Deref, only methods should be kept

@GuillaumeGomez GuillaumeGomez removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 11, 2026
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Thanks to @camelid's suggestion, code is now much simpler. Thanks!

@rustbot ready

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 11, 2026
Comment thread src/librustdoc/clean/mod.rs Outdated
Comment on lines +3046 to +3047
let for_ = clean_ty(impl_.self_ty, cx);

@camelid camelid Aug 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Moving this let is now unnecessary. Better to move it back to where it's used.

View changes since the review

Comment thread src/librustdoc/clean/utils.rs Outdated
@@ -295,6 +295,8 @@ pub(crate) fn build_deref_target_impls(
inline::build_impls(cx, did, None, ret);
});
}
} else {
break;

@camelid camelid Aug 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this break added?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's actually looking for the Deref::Target item and doesn't do anything. So at that point, we can break as soon as we found it. Can remove it though, considering there are only 2 items, doesn't matter much.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also just realized that it shouldn't be in a else condition. Anyway, removing it.

Comment thread src/librustdoc/html/render/mod.rs Outdated
@@ -3150,3 +3174,30 @@ fn repr_attribute<'tcx>(

(!result.is_empty()).then(|| format!("#[repr({})]", result.join(", ")).into())
}

pub(crate) fn compute_if_deref_target_implements_copy(
cx: &Context<'_>,

@camelid camelid Aug 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have this take TyCtxt instead of Context and change the callers as well. It's better to take as little input as possible.

View changes since the review

Comment thread src/librustdoc/html/render/sidebar.rs Outdated
AlsoCollectAssocFns { assoc_fns: &'r mut Vec<Link<'l>> },
}

fn get_methods<'a>(
i: &'a clean::Impl,
mut mode: GetMethodsMode<'_, 'a>,
used_links: &mut FxHashSet<String>,
tcx: TyCtxt<'_>,
cx: &Context<'_>,

@camelid camelid Aug 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camelid camelid added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2026
@rustbot

rustbot commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Applied suggestions.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustdoc: Methods from deref is missing methods

4 participants