Skip to content

GenericArgs::types triage + possible fixes - #159899

Open
LorrensP-2158466 wants to merge 2 commits into
rust-lang:mainfrom
LorrensP-2158466:generic_args_terms
Open

GenericArgs::types triage + possible fixes#159899
LorrensP-2158466 wants to merge 2 commits into
rust-lang:mainfrom
LorrensP-2158466:generic_args_terms

Conversation

@LorrensP-2158466

@LorrensP-2158466 LorrensP-2158466 commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 25, 2026
@LorrensP-2158466

Copy link
Copy Markdown
Contributor Author

Don't know what tests can/will fail, so i'll see what a CI run says and go from there.

@rustbot rustbot added A-CI Area: Our Github Actions CI A-testsuite Area: The testsuite used to check the correctness of rustc T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. labels Jul 25, 2026
@LorrensP-2158466
LorrensP-2158466 force-pushed the generic_args_terms branch 2 times, most recently from a16db39 to ea9f31c Compare July 25, 2026 16:36
@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Jul 25, 2026
@LorrensP-2158466

LorrensP-2158466 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

This is all of the list posted in #t-types/call-for-participation > `args.types()` triage and fixes expect 2, both seem to be a bit harder to fix (to me at least:).

Currently separate commits because wanted to check CI after each "major enough" change

ping @lcnr

@LorrensP-2158466
LorrensP-2158466 marked this pull request as ready for review July 27, 2026 10:42
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 27, 2026
@rustbot

rustbot commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

Some changes occurred in need_type_info.rs

cc @lcnr

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

rustbot commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

r? @chenyukang

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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 74 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

This comment has been minimized.

&& matches!(cx.tcx.get_diagnostic_name(adt_def.did()), Some(sym::Rc | sym::Arc))
{
args.types().next() == Some(parent_ty)
args.iter().filter_map(ty::GenericArg::as_type).nth(0) == Some(parent_ty)

@LorrensP-2158466 LorrensP-2158466 Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From @lcnr in the zulip discussion:

yeah, the fact that accessing just types is often vaguely wrong and doesn't allow for random access, so doing iter().filter_map(GenericArg::as_type).nth(1) is beter for that

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

that can just be next().as_type() 🤔

@chenyukang

Copy link
Copy Markdown
Member

is it this PR still in WIP?
r? @lcnr

@rustbot rustbot assigned lcnr and unassigned chenyukang Jul 30, 2026
@LorrensP-2158466 LorrensP-2158466 changed the title [WIP]: GenericArgs::types triage + possible fixes GenericArgs::types triage + possible fixes Jul 31, 2026
fn get_ty_param(ty: Ty<'_>) -> Option<Ty<'_>> {
if let ty::Adt(_, subs) = ty.kind() {
subs.types().next()
subs.iter().filter_map(ty::GenericArg::as_type).nth(0)

@lcnr lcnr Aug 6, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

subs.iter().filter_map(ty::GenericArg::as_type).next() .nth(0) is always worse than next 🤔

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

so iter().next().as_type(), gotcha.

@khyperia khyperia 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.

nice, thanks! lcnr asked me to take a peek.

I have a vague preference for .iter().nth(N).and_then(ty::GenericArg::as_type) over .iter().filter_map(ty::GenericArg::as_type).nth(N) when fetching the Nth type arg of a known type (the latter gives me spidey sense tingles around weird indexing, even though they're both equivalent since there's only type args for these known types), but that's an opinion that you're free to disregard~

View changes since this review

},
// FIXME: check const parameters better as well. Currently this will consider `Array<5>` the same as
// `Array<6>`
(ty::TermKind::Const(c1), ty::TermKind::Const(c2)) if c1 == c2 => todo!(),

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.

there's a todo!() here - probably should either keep the old behavior of considering all consts to be equal, or properly implement this, instead of panicing~

(today I learned that tidy doesn't run on src/tools/clippy, todo!() is banned by tidy)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I meant to ask a follow up question on this.

Consts are interned as well, right? But are there cases where they are not the same "const" but are still the same? Otherwise this can just return true indeed.

(today I learned that tidy doesn't run on src/tools/clippy, todo!() is banned by tidy)

Yeah me to :D.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I changed it to => true for now.

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.

For purposes of this real wonky function, yeah, consts can contain params. IMO, drop the guard, just have (ty::TermKind::Const(c1), ty::TermKind::Const(c2)) => (), to retain the old behavior, with the old comment. This function is only called in clippy::transmute_undefined_repr, which "has had multiple problems in the past and was moved to nursery" rust-lang/rust-clippy#8496 so preserving whatever wonky behavior it has for now seems best. (Hypothesizing a future fix, this should probably be a type relation or somesuch rather than doing a wonky custom relation on just ADTs? I mean, properly, I think maybe it should be doing a trait solver .eq() and checking if there's a solution. Idk.)

(Also, shouldn't return true but rather continue to the next term)

Comment thread tests/ui/lint/function-item-references.rs
@LorrensP-2158466

Copy link
Copy Markdown
Contributor Author

I have a vague preference for .iter().nth(N).and_then(ty::GenericArg::as_type) over .iter().filter_map(ty::GenericArg::as_type).nth(N) when fetching the Nth type arg of a known type

I can get behind why this is better in the case of a known type. I'll change them.

@LorrensP-2158466

Copy link
Copy Markdown
Contributor Author

So i went with the recommendation of lcnr, doing next().and_then(as_type) instead of nth(0).and_then(as_type).

@rustbot ready.

@rust-log-analyzer

This comment has been minimized.

@LorrensP-2158466

Copy link
Copy Markdown
Contributor Author

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 8, 2026
@rustbot

rustbot commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 8, 2026
fn get_ty_param(ty: Ty<'_>) -> Option<Ty<'_>> {
if let ty::Adt(_, subs) = ty.kind() {
subs.types().next()
subs.iter().next().and_then(ty::GenericArg::as_type)

@khyperia khyperia Aug 10, 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.

could do subs.iter().next()?.as_type() in a few of these places, but whatever, that's a style choice, I don't care :P

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I mean, why not :D? It does look cleaner.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, I just now noticed that using types() is actually correct here.

it is also used on the Cow<'a, T> type, and the first parameter is the lifetime. That's why the filter_map is correct here and that this new change cause a ci job to fail.

@LorrensP-2158466

Copy link
Copy Markdown
Contributor Author

@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 10, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@LorrensP-2158466

Copy link
Copy Markdown
Contributor Author

rebased to fix merge conflict and squash the commits that fix the CI failures.
@rustbot ready.

@rustbot

rustbot commented Aug 18, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-CI Area: Our Github Actions CI A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants