Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
raushan728:accurate-targeting-157117
Aug 30, 2026
Merged

Add accurate targeting for imperfect derives diagnostic in E0277#159363
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
raushan728:accurate-targeting-157117

Conversation

@raushan728

@raushan728 raushan728 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

View all comments

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
Member

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
@raushan728
raushan728 force-pushed the accurate-targeting-157117 branch from e4402f3 to 8575e8b Compare August 8, 2026 16:34
@rustbot

This comment has been minimized.

@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
@raushan728

Copy link
Copy Markdown
Contributor Author

Hi, @estebank @khyperia @mejrs please help me here so we can move forward 🙃

|
LL | #[derive(Clone)]
| ----- in this derive macro expansion
LL | struct List<T> {

@mejrs mejrs Aug 27, 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.

Doesn't this span need to point at T, not the name of the type? (it looks like this wasn't something you did, but was already off)

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.

Confirmed, this is existing span issue. Feel free to file a separate issue for it if you think it's worth tracking.

Comment on lines +4 to +5
LL | require_clone(S::<X>(PhantomData));
| ------------- ^^^^^^^^^^^ the trait `Clone` is not implemented for `S<X>`

@mejrs mejrs Aug 27, 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.

this span also looks a bit off 🤔

View changes since the review

Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 27, 2026
@rustbot

rustbot commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

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

@raushan728
raushan728 force-pushed the accurate-targeting-157117 branch from 8575e8b to ab83e7d Compare August 29, 2026 05:25
@rustbot

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

@rust-log-analyzer

This comment has been minimized.

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 ab83e7d to e735229 Compare August 29, 2026 05:52
@raushan728
raushan728 requested a review from mejrs August 29, 2026 06:09
@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 29, 2026

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

Thanks!

r? me @bors r+ rollup

View changes since this review

@rust-bors

rust-bors Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

📌 Commit e735229 has been approved by mejrs

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 29, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 30, 2026
Add accurate targeting for imperfect derives diagnostic in `E0277`



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
jhpratt added a commit to jhpratt/rust that referenced this pull request Aug 30, 2026
…17, r=mejrs

Add accurate targeting for imperfect derives diagnostic in `E0277`

Resolves rust-lang#157117
Follow-up rust-lang#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
@jhpratt

jhpratt commented Aug 30, 2026

Copy link
Copy Markdown
Member

@bors yield to rollup

@rust-bors

rust-bors Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #161997.

rust-bors Bot pushed a commit that referenced this pull request Aug 30, 2026
Rollup of 3 pull requests

Successful merges:

 - #159363 (Add accurate targeting for imperfect derives diagnostic in `E0277`)
 - #161969 (Add regression test for TAIT in extern fn ICE with the new solver)
 - #161993 (add regression test for ICE unexpected region)
@rust-bors

This comment has been minimized.

@rust-bors rust-bors Bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 30, 2026
@rust-bors

rust-bors Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: mejrs
Duration: 3h 6m 25s
Pushing d0ea9cd to main...

@rust-bors
rust-bors Bot merged commit d0ea9cd into rust-lang:main Aug 30, 2026
14 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Aug 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor
What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 4545c83 (parent) -> d0ea9cd (this PR)

Test differences

Show 11 test diffs

Stage 1

  • [ui (polonius)] tests/ui/derives/imperfect-derive-advanced.rs: [missing] -> pass (J0)
  • [ui (polonius)] tests/ui/derives/imperfect-derive-copy.rs: [missing] -> pass (J0)
  • [ui (polonius)] tests/ui/derives/imperfect-derive-phantom.rs: [missing] -> pass (J0)
  • [ui] tests/ui/derives/imperfect-derive-advanced.rs: [missing] -> pass (J2)
  • [ui] tests/ui/derives/imperfect-derive-copy.rs: [missing] -> pass (J2)
  • [ui] tests/ui/derives/imperfect-derive-phantom.rs: [missing] -> pass (J2)

Stage 2

  • [ui] tests/ui/derives/imperfect-derive-advanced.rs: [missing] -> pass (J1)
  • [ui] tests/ui/derives/imperfect-derive-copy.rs: [missing] -> pass (J1)
  • [ui] tests/ui/derives/imperfect-derive-phantom.rs: [missing] -> pass (J1)

Additionally, 2 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard d0ea9cd00084a023259154a8631075bdbc1713ea --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. optional-x86_64-gnu-autodiff: 58m -> 33m 10s (-42.8%)
  2. x86_64-gnu-stdlib-semver-check: 18m 13s -> 10m 26s (-42.7%)
  3. x86_64-mingw-1: 2h 6m -> 2h 59m (+42.0%)
  4. dist-arm-linux-musl: 1h 47m -> 1h 6m (-38.5%)
  5. x86_64-gnu-miri: 1h 32m -> 1h 1m (-33.1%)
  6. x86_64-gnu-nopt: 2h 20m -> 1h 34m (-32.7%)
  7. dist-x86_64-solaris: 1h 43m -> 1h 11m (-31.1%)
  8. x86_64-gnu-tools: 1h 9m -> 49m 38s (-28.5%)
  9. pr-check-2: 42m 43s -> 30m 52s (-27.7%)
  10. dist-s390x-linux: 1h 33m -> 1h 8m (-27.4%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (d0ea9cd): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (secondary 3.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.8% [3.8%, 3.8%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results (secondary 14.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
14.8% [14.8%, 14.8%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

Results (secondary -0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 1
All ❌✅ (primary) - - 0

Bootstrap: 474.853s -> 474.031s (-0.17%)
Artifact size: 402.86 MiB -> 402.86 MiB (0.00%)

@raushan728
raushan728 deleted the accurate-targeting-157117 branch August 31, 2026 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. 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

8 participants