Skip to content

Improve E0277 diagnostics for conditionally implemented traits#158494

Open
raushan728 wants to merge 2 commits into
rust-lang:mainfrom
raushan728:issues/158423
Open

Improve E0277 diagnostics for conditionally implemented traits#158494
raushan728 wants to merge 2 commits into
rust-lang:mainfrom
raushan728:issues/158423

Conversation

@raushan728

@raushan728 raushan728 commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

View all comments

Fixes #158423.

The diagnostic previously reported that a trait was "implemented" even when the matching impl could not apply because its where-clause requirements were not satisfied, which was misleading.

This change:

  • reports that the trait is "conditionally implemented" when a matching impl is rejected due to unsatisfied where-clause requirements;
  • highlights the unsatisfied requirement in the impl and includes the corresponding predicate in the diagnostic;
  • preserves the existing diagnostic flow while making it clearer why the candidate impl was rejected.

@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jun 27, 2026
@rustbot

rustbot commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

r? @mu001999

rustbot has assigned @mu001999.
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
  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

This comment has been minimized.

@rustbot

This comment has been minimized.

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

Could you try to add the label on unsatisfied bounds, as desired in the issue? I think we can achieve this with ObligationCtxt::new_with_diagnostics.

And for the logic here, perhaps you could check the implementation of impl_may_apply. Maybe the logic could even be shared between them.

@rustbot author

View changes since this review

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

rustbot commented Jun 28, 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 Jun 28, 2026
@raushan728

Copy link
Copy Markdown
Contributor Author

Could you try to add the label on unsatisfied bounds, as desired in the issue? I think we can achieve this with ObligationCtxt::new_with_diagnostics.

And for the logic here, perhaps you could check the implementation of impl_may_apply. Maybe the logic could even be shared between them.

@rustbot author

View changes since this review

Thanks! I updated this to use ObligationCtxt::new_with_diagnostics, so the diagnostic now highlights the unsatisfied trait bound. I also looked into impl_may_apply, but kept the logic local since it serves a different purpose.

@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 Jun 29, 2026
@raushan728 raushan728 requested a review from mu001999 June 29, 2026 11:58
@rust-log-analyzer

This comment has been minimized.

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

Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs Outdated
Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs Outdated
Comment thread tests/ui/errors/trait-bound-error-spans/blame-trait-error.stderr
Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.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 Jun 29, 2026
@rustbot

rustbot commented Jun 30, 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

@rustbot author

View changes since this review

@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 Jun 30, 2026
@raushan728 raushan728 requested a review from mu001999 June 30, 2026 06:53

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

Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs Outdated
Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs Outdated
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 30, 2026
@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 Jun 30, 2026
@raushan728 raushan728 requested a review from mu001999 June 30, 2026 09:26
@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 Jun 30, 2026

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

Comment thread tests/ui/traits/error_reporting/conditionally-implemented-trait-158423.stderr Outdated
Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.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 Jun 30, 2026
@raushan728 raushan728 requested a review from mu001999 June 30, 2026 15:12
@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 Jun 30, 2026

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

Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs Outdated
Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.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 Jun 30, 2026
@rust-log-analyzer

This comment has been minimized.

@raushan728 raushan728 requested a review from mu001999 July 1, 2026 05:49
@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 Jul 1, 2026
@mu001999

mu001999 commented Jul 2, 2026

Copy link
Copy Markdown
Member

Thanks!

@bors r+

@rust-bors

rust-bors Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4613bf9 has been approved by mu001999

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 1000. This pull request will be tested once the tree is reopened.

@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 Jul 2, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 2, 2026
Improve E0277 diagnostics for conditionally implemented traits

Fixes rust-lang#158423.

The diagnostic previously reported that a trait was "implemented" even when the matching impl could not apply because its where-clause requirements were not satisfied, which was misleading.

This change:

- reports that the trait is "conditionally implemented" when a matching impl is rejected due to unsatisfied where-clause requirements;
- highlights the unsatisfied requirement in the impl and includes the corresponding predicate in the diagnostic;
- preserves the existing diagnostic flow while making it clearer why the candidate impl was rejected.
rust-bors Bot pushed a commit that referenced this pull request Jul 3, 2026
…uwer

Rollup of 22 pull requests

Successful merges:

 - #155429 (Support `u128`/`i128` c-variadic arguments)
 - #158100 (Emit retags in codegen to support BorrowSanitizer (part 4))
 - #158494 (Improve E0277 diagnostics for conditionally implemented traits)
 - #158606 (use ProjectionPredicate instead of AliasRelate)
 - #158627 (Simplify option-iterator flattening in the compiler)
 - #158658 (Update LLVM submodule)
 - #158665 (Revert "Remove redundant dyn-compatibility check.")
 - #158021 (Remove old MinGW workaround)
 - #158473 (Add `riscv32imfc-unknown-none-elf` bare-metal target)
 - #158549 (process::exec: using appropriate exit code on vxworks.)
 - #158585 (Improve diagnostic for too many super keywords)
 - #158637 (hir_ty_lowering: avoid self type lookup for inherent aliases)
 - #158651 (ptr doc: reduce use of unsafe block to where needed)
 - #158669 (Remove `src/tools/test-float-parse/Cargo.lock`)
 - #158674 (library: Polish transmute's `split_at_stdlib` example)
 - #158677 (Add extra splat tests)
 - #158680 (Avoid ICE for `NonZero<char>` in improper_ctypes)
 - #158681 (Remove unnecessary `Hash` derives from MIR types)
 - #158682 (Avoid delayed bug for disabled on_type_error arguments)
 - #158684 (Add missing generic test coverage for ```#[splat]```)
 - #158687 (Streamline `MacEager`)
 - #158688 (Cleanup attribute docs and add links to other mentioned attributes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misleading "trait bound not satisfied" with non-generic where clauses

4 participants