Skip to content

Conversation

Jarcho
Copy link
Contributor

@Jarcho Jarcho commented Sep 16, 2025

This introduces a new way of identifying items/paths using extension traits with a composable set of functions rather than unique functions for various combinations of starting points and target items. Altogether this is a set of five traits:

  • MaybeTypeckRes: Allows both LateContext and TypeckResults to be used for type-dependent lookup. The implementation here will avoid ICEs by returning None when debug assertions are disabled. With assertions this will assert that we don't silently lookup anything from a different body than the current one and that a definition actually exists.
  • HasHirId: Simply a convenience to allow not typing .hir_id at call sites.
  • MaybeQPath: This is the old MaybePath. Extension functions for type-dependent path lookups exist here. A lot of these functions aren't used in the current PR, but what they accomplish is done in various places I haven't cleaned up yet.
  • MaybeResPath: Like MaybeQPath, but only does non-type-dependent lookup (QPath::Resolved).
  • MaybeDef: Extension functions for identifying the current definition and accessing properties. Implemented for several types for convenience.

MaybeDef is implemented for Option to allow chaining methods together. e.g. cx.ty_based_def(e).opt_parent(cx).opt_impl_ty(cx).is_diag_item(..) would chaining and_then or if let on every step. MaybeQPath and MaybeResPath are also implemented for Option for the same reason.

ty_based_def is just a shorter name for type_dependent_def. I'm not really attached to it, but it's nice that it's a little shorter.

changelog: none

@Jarcho Jarcho force-pushed the util_1 branch 2 times, most recently from 73804d2 to bdd440a Compare September 16, 2025 07:01
@Jarcho Jarcho marked this pull request as ready for review September 16, 2025 07:06
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 16, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 16, 2025

Some changes occurred in clippy_lints/src/doc

cc @notriddle

@rustbot
Copy link
Collaborator

rustbot commented Sep 16, 2025

r? @samueltardieu

rustbot has assigned @samueltardieu.
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

}

if let Some(id) = path_to_local(cast_expr)
if let Some(id) = cast_expr.res_local_id()
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be opt_res_local_id() for consistency with opt_diag_name() and others?

Copy link
Contributor Author

@Jarcho Jarcho Sep 16, 2025

Choose a reason for hiding this comment

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

I originally had it as opt_local_id, but changed it to res_local_id to match all the other names to get a path's resolution. I'm not strongly against opt_res_local_id, but it's getting a little on the wordy side. I'm not really attached to many of the names here.

File you want to look at for the new functions is clippy_utils/src/res.rs. Just noticed this is 279 files.

@samueltardieu
Copy link
Member

I've opened this Zulip thread for the non-technical discussion.

Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

LGTM apart from minor nits

The code is pleasant to read, and should be pleasant to use.

View changes since this review

// It's possible to get the `TypeckResults` for any other body, but
// attempting to lookup the type of something across bodies like this
// is a good indication of a bug.
debug_assert!(false, "attempted type-dependent lookup in a non-body context");
Copy link
Member

Choose a reason for hiding this comment

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

Maybe indicate in comments that if this is not a bug, then TyCtxt::typeck_body() can be used to get the appropriate TypeCkResults, so that developpers hit by the assertion can use it if justified.

}

/// A `QPath` with the `HirId` of the node containing it.
type QPathId<'tcx> = (&'tcx QPath<'tcx>, HirId);
Copy link
Member

Choose a reason for hiding this comment

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

Could be made pub since it is used in pub trait method signatures.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These aliases only exist to make the trait signatures shorter since they're written quite a few times. Outside the module it shouldn't even come up and if it does it will be written once.

}

/// A resolved path and the explicit `Self` type if there is one.
type OptResPath<'tcx> = (Option<&'tcx hir::Ty<'tcx>>, Option<&'tcx Path<'tcx>>);
Copy link
Member

Choose a reason for hiding this comment

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

Could be useful if pub as well

@rustbot

This comment has been minimized.

@samueltardieu
Copy link
Member

@Jarcho Can you rebase?

@rustbot
Copy link
Collaborator

rustbot commented Oct 11, 2025

This PR was rebased onto a different master 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.

@samueltardieu samueltardieu added this pull request to the merge queue Oct 11, 2025
Merged via the queue into rust-lang:master with commit ebbbbeb Oct 11, 2025
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants