GenericArgs::types triage + possible fixes - #159899
Conversation
|
Don't know what tests can/will fail, so i'll see what a CI run says and go from there. |
a16db39 to
ea9f31c
Compare
|
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:).
ping @lcnr |
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt
cc @rust-lang/clippy Some changes occurred in need_type_info.rs cc @lcnr |
|
r? @chenyukang rustbot has assigned @chenyukang. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
- be explicit when only needing types of `GenericArgs`
26018f7 to
043bfcc
Compare
| && 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
that can just be next().as_type() 🤔
|
is it this PR still in WIP? |
GenericArgs::types triage + possible fixesGenericArgs::types triage + possible fixes
| 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) |
There was a problem hiding this comment.
subs.iter().filter_map(ty::GenericArg::as_type).next() .nth(0) is always worse than next 🤔
There was a problem hiding this comment.
so iter().next().as_type(), gotcha.
There was a problem hiding this comment.
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~
| }, | ||
| // 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!(), |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I changed it to => true for now.
There was a problem hiding this comment.
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)
I can get behind why this is better in the case of a known type. I'll change them. |
|
So i went with the recommendation of lcnr, doing @rustbot ready. |
This comment has been minimized.
This comment has been minimized.
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
| 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
I mean, why not :D? It does look cleaner.
There was a problem hiding this comment.
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.
|
@rustbot ready |
|
The job Click to see the possible cause of the failure (guessed by this bot)Important For more information how to resolve CI failures of this job, visit this link. |
|
☔ The latest upstream changes (presumably #161031) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
View all comments
cc #t-types/call-for-participation > `args.types()` triage and fixes