Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc_on_unimplemented should work on specific trait methods #135117

Open
joshtriplett opened this issue Jan 5, 2025 · 4 comments
Open

rustc_on_unimplemented should work on specific trait methods #135117

joshtriplett opened this issue Jan 5, 2025 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-diagnostic-infra Diagnostics: Issues that affect all diagnostics, or relate to the diagnostic machinery itself. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@joshtriplett
Copy link
Member

Code

`var_with_complicated_type >= another_var_with_complicated_type`

Current output

implementation for `ComplicatedType < ComplicatedType` and `ComplicatedType > ComplicatedType`

Desired output

implementation for `ComplicatedType >= ComplicatedType`

Rationale and extra context

rustc_on_unimplemented should be applicable to specific trait methods, in addition to the trait as a whole. Then, if the error involves calling a specific trait method, the compiler can give a more specific error message.

In the specific case of PartialOrd, this would also avoid repeating both type names twice.

Other cases

Rust Version

git commit 7349f6b50359fd1f11738765b8deec5ee02d8710

Anything else?

No response

@joshtriplett joshtriplett added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 5, 2025
@jieyouxu jieyouxu added D-diagnostic-infra Diagnostics: Issues that affect all diagnostics, or relate to the diagnostic machinery itself. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jan 5, 2025
@compiler-errors
Copy link
Member

Can you explain your example, perhaps with code? I really have no idea what you're asking for in this issue.

@joshtriplett
Copy link
Member Author

Sure.

Consider code like this:

fn main() {
    if 5 >= String::new() {
        todo!();
    }
}

This produces:

error[E0277]: can't compare `{integer}` with `String`
 --> src/main.rs:2:10
  |
2 |     if 5 >= String::new() {
  |          ^^ no implementation for `{integer} < String` and `{integer} > String`
  |
  = help: the trait `PartialOrd<String>` is not implemented for `{integer}`

This comes from the rustc_on_unimplemented attribute on PartialOrd:

#[rustc_on_unimplemented(
    message = "can't compare `{Self}` with `{Rhs}`",
    label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`",
    append_const_msg
)]

I'm suggesting that, in cases like this where the failure comes from a specific operator, we should give a more specific error message, like:

error[E0277]: can't compare `{integer}` with `String`
 --> src/main.rs:2:10
  |
2 |     if 5 >= String::new() {
  |          ^^ no implementation for `{integer} >= String`
  |
  = help: the trait `PartialOrd<String>` is not implemented for `{integer}`

This would be more obviously related to the code the user wrote, and would avoid repeating the two types, which is particularly important when they're large and complex types.

@compiler-errors
Copy link
Member

I actually don't think I agree. I just think that label is redundant, and should be removed. Maybe this is useful in a more general case, but I think that label doesn't add much value when it says "can't compare" directly above it.

@joshtriplett
Copy link
Member Author

That works too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-diagnostic-infra Diagnostics: Issues that affect all diagnostics, or relate to the diagnostic machinery itself. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants