Suggest impl Trait return type when incorrectly using a generic return type#89892
Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @estebank (or someone else) soon. Please see the contribution instructions for more information. |
|
This is my first PR, so I hope I did everything correctly! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
As a new Rust programmer, I really want this feature |
This comment has been minimized.
This comment has been minimized.
|
I'm done with the changes, you can review it again if you have time |
|
What's the status on this PR? @estebank |
jyn514
left a comment
There was a problem hiding this comment.
This is a neat idea :) it looks in pretty good shape, just a few suggestions.
96573f1 to
4572455
Compare
|
I added the notes, also added |
This comment has been minimized.
This comment has been minimized.
|
@Nilstrieb you need to add the |
|
It does have the fixed code, but the problem is that this fixed code is not compiling successfully because of |
|
@Nilstrieb I'd suggest making the rustfix test separate from the rest of the tests. |
4572455 to
eafa4bf
Compare
|
I've squashed it again since this is hopefully the last change |
|
Thank you for your review, I'll implement this next week! |
eafa4bf to
606f6e2
Compare
This comment has been minimized.
This comment has been minimized.
|
r? @jackh726 |
jackh726
left a comment
There was a problem hiding this comment.
Sorry, more reviews.
Also, can you squash the commits?
There was a problem hiding this comment.
This doesn't necessarily cover cases where T appears in the bounds, right? So like Option<()>: Trait<T>
There was a problem hiding this comment.
Hmm, I wasn't able to figure out how I'd do that. Do you have an idea?
There was a problem hiding this comment.
Maybe instead of making contains be on Ty, you can make it a visitor. Then just visit both the ty and bounds.
There was a problem hiding this comment.
For the contains I was able to use the visitor on Ty. How should I visit the PolyTraitRef in the bounds? I found hir::intravisit but wasn't able to find a good way to use it.
There was a problem hiding this comment.
Sorry, thought I replied to this before.
There is a hir visitor: https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/intravisit/trait.Visitor.html
Alternatively, you could lower the trait ref.
I'm also fine it you just want to add this as a test case with a FIXME. This is just suggestion after all and doesn't have to he "perfect"
There was a problem hiding this comment.
I'll just leave a fixme in a test 👍
|
I'll squash it once it's done. |
|
☔ The latest upstream changes (presumably #87648) made this pull request unmergeable. Please resolve the merge conflicts. |
This comment has been minimized.
This comment has been minimized.
5d2ddbd to
3005d4d
Compare
This comment has been minimized.
This comment has been minimized.
3005d4d to
2400a75
Compare
|
Finally did it, I wanted to have |
|
☔ The latest upstream changes (presumably #93148) made this pull request unmergeable. Please resolve the merge conflicts. |
|
Sorry I didn't get to this before; r=me here after rebase |
|
@bors delegate+ (just do r=jackh726) |
|
✌️ @Nilstrieb can now approve this pull request |
2400a75 to
3e6e9df
Compare
This comment has been minimized.
This comment has been minimized.
Address rust-lang#85991 Suggest the `impl Trait` return type syntax if the user tried to return a generic parameter and we get a type mismatch The suggestion is not emitted if the param appears in the function parameters, and only get the bounds that actually involve `T: ` directly It also checks whether the generic param is contained in any where bound (where it isn't the self type), and if one is found (like `Option<T>: Send`), it is not suggested. This also adds `TyS::contains`, which recursively vistits the type and looks if the other type is contained anywhere
3e6e9df to
4bed748
Compare
|
@bors r=jackh726 |
|
📌 Commit 4bed748 has been approved by |
…askrgr Rollup of 10 pull requests Successful merges: - rust-lang#89892 (Suggest `impl Trait` return type when incorrectly using a generic return type) - rust-lang#91675 (Add MemTagSanitizer Support) - rust-lang#92806 (Add more information to `impl Trait` error) - rust-lang#93497 (Pass `--test` flag through rustdoc to rustc so `#[test]` functions can be scraped) - rust-lang#93814 (mips64-openwrt-linux-musl: correct soft-foat) - rust-lang#93847 (kmc-solid: Use the filesystem thread-safety wrapper) - rust-lang#93877 (asm: Allow the use of r8-r14 as clobbers on Thumb1) - rust-lang#93892 (Only mark projection as ambiguous if GAT substs are constrained) - rust-lang#93915 (Implement --check-cfg option (RFC 3013), take 2) - rust-lang#93953 (Add the `known-bug` test directive, use it, and do some cleanup) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Address #85991
When there is a type mismatch error and the return type is generic, and that generic parameter is not used in the function parameters, suggest replacing that generic with the
impl Traitsyntax.r? @estebank