Skip to content

Add a warning to LateContext::get_def_path #142593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

blyxyas
Copy link
Member

@blyxyas blyxyas commented Jun 16, 2025

Preventing anyone from doing the same error as rust-lang/rust-clippy#15043 fixed

@rustbot
Copy link
Collaborator

rustbot commented Jun 16, 2025

r? @jdonszelmann

rustbot has assigned @jdonszelmann.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 16, 2025
@@ -709,6 +709,12 @@ impl<'tcx> LateContext<'tcx> {
}

/// Gets the absolute path of `def_id` as a vector of `Symbol`.
/// Note that this is kinda expensive because it has to
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Note that this is kinda expensive because it has to
///
/// Note that this is kinda expensive because it has to

/// Note that this is kinda expensive because it has to
/// travel the tree and pretty-print. Use sparingly (or find a
/// way to only get the segments you're interested in, e.g. via
/// [`TyCtxt::crate_name`]).
Copy link
Member

Choose a reason for hiding this comment

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

or use a diagnostic item if the intention was to match an item by its path.

@compiler-errors
Copy link
Member

I'm not sure how this connects to rust-lang/clippy#15030. That looks like some test harness problem?

@compiler-errors
Copy link
Member

I'm also somewhat skeptical that this function is that notable. Grabbing the symbols for every segment in a def path doesn't seem like that much work in the grand scheme of things. Of course, if this is being done in a tight loop it may be expensive, but we use similar a to compute symbol names and stuff even on the good path.

Can you explain more about what led you to this function being expensive?

@blyxyas
Copy link
Member Author

blyxyas commented Jun 16, 2025

Oops, I meant rust-lang/rust-clippy#15043, 15030 is the one I'm working on right now. I'll edit that.

In our case, get_def_path was the 3rd heaviest function, we used it in a semi-tight loop, but still, it was accounting for more time than late_lint_mod.

Can you explain more about what led you to this function being expensive?

The call graph is like this:

get_def_path -> AbsolutePathPrinter::default_print_def_path -> core::fmt::write -> <Ty as core::fmt::Display>::fmt -> <FmtPrinter as Printer>::print_type -> ... -> rustc_interface::passes::early_lint_checks -> Running the early lint checks, somehow?

I haven't really looked into why pretty-printing a type would execute early lint checks. Maybe it has something to do with the pretty-printer calling tcx.visible_parent_map.

Graph


As to the necessity of this little warning, I've looked very briefly into other uses of get_def_path and a lot seem to be optimizable away with other, cheaper and more specific functions. So at least warning the user to think about alternatives I think it's necessary.

@blyxyas blyxyas force-pushed the improve-docs-itty-bitty-change branch from 3537451 to 6d04085 Compare June 17, 2025 00:07
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-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants