Skip to content

Add accurate targeting for imperfect derives diagnostic in E0277 - #159363

Open
raushan728 wants to merge 1 commit into
rust-lang:mainfrom
raushan728:accurate-targeting-157117
Open

Add accurate targeting for imperfect derives diagnostic in E0277#159363
raushan728 wants to merge 1 commit into
rust-lang:mainfrom
raushan728:accurate-targeting-157117

Conversation

@raushan728

@raushan728 raushan728 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Resolves #157117
Follow-up #158764

This PR implements accurate targeting for the E0277 "imperfect derives" suggestion so it only triggers when all fields of the ADT satisfy the trait independently of the failing generic parameter. False positives (like struct S<T>(T)) are now successfully filtered out, while true cases (like PhantomData<T>) still correctly emit the help note.

r? @estebank
cc @mejrs @khyperia

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

This comment has been minimized.

@raushan728
raushan728 force-pushed the accurate-targeting-157117 branch from b7656d5 to a8756f1 Compare July 16, 2026 09:33
@khyperia

Copy link
Copy Markdown
Contributor

neat! I haven't looked through the PR at all, just wanted to suggest having a test with associated types as well, not just PhantomData. You can find an example of what that looks like in my comment here - feel free to rework it to be cleaner or whatever, but also copypasting it directly would work as well, up to you. #158764 (comment)

Perhaps also Rc with Clone, and also some coinductive shenanigans too (the List type from Niko's blog post we were talking about in the previous PR)

@raushan728

Copy link
Copy Markdown
Contributor Author

Thanks! Will update shortly.

@raushan728
raushan728 force-pushed the accurate-targeting-157117 branch from a8756f1 to e4402f3 Compare July 20, 2026 16:23
@raushan728

raushan728 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

just wanted to suggest having a test with associated types as well, not just PhantomData.

Done! please take a look.

@raushan728

Copy link
Copy Markdown
Contributor Author

Hi @mejrs @khyperia @estebank this PR has been open for a while and I haven't received any review yet. If you could help me out a bit, we can get this landed sooner 🙂

@estebank estebank left a comment

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.

My apologies, the logic is a bit subtle and that makes it harder to follow. Would you mind adding more comments explaining what each section is accomplishing?

I believe the changes to be directionally correct, but suspect there might be helper methods to construct the obligation with fewer moving parts.

I am currently traveling, so my response rate is lower than it otherwise would be.

View changes since this review

Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs Outdated
Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs Outdated
Filters out false positives for the 'imperfect derives' E0277 note by checking if ADT fields satisfy the trait independently of the failing generic parameter.
@raushan728
raushan728 force-pushed the accurate-targeting-157117 branch from e4402f3 to 8575e8b Compare August 8, 2026 16:34
@rustbot

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

@raushan728

Copy link
Copy Markdown
Contributor Author

My apologies, the logic is a bit subtle and that makes it harder to follow. Would you mind adding more comments explaining what each section is accomplishing?

Added

I believe the changes to be directionally correct, but suspect there might be helper methods to construct the obligation with fewer moving parts.

I looked but didn't find an existing helper for this exact "swap self_ty, keep the rest of the trait args" pattern, so I kept the explicit construction. Open to suggestions if you know of something more idiomatic.

I am currently traveling, so my response rate is lower than it otherwise would be.

No worries, thanks for taking the time to review!

@raushan728
raushan728 requested a review from estebank August 8, 2026 16:41
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.

Bound error caused by imperfect derives should include links to further explanation

5 participants