Skip to content

Incorrect compiler hint on missing stricter trait bound #99597

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

Closed
SadiinsoSnowfall opened this issue Jul 22, 2022 · 0 comments · Fixed by #111610
Closed

Incorrect compiler hint on missing stricter trait bound #99597

SadiinsoSnowfall opened this issue Jul 22, 2022 · 0 comments · Fixed by #111610
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@SadiinsoSnowfall
Copy link

Let's consider the following snippet:

trait T1 { }

trait T2 {
    fn test(&self) { }
}

fn go(s: &impl T1) {
    s.test();
}

The compiler will throw the following error + hint:

error[[E0599]](https://doc.rust-lang.org/stable/error-index.html#E0599): no method named `test` found for reference `&impl T1` in the current scope
 --> src/lib.rs:8:7
  |
8 |     s.test();
  |       ^^^^ method not found in `&impl T1`
  |
  = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `test`, perhaps you need to restrict type parameter `impl T1` with it:
  |
7 | fn go(s: &impl T1 + T2) {
  |                   ++++

For more information about this error, try `rustc --explain E0599`.

But the hint is not correct: changing the go function as suggested by the compiler

fn go(s: &impl T1 + T2) {
    s.test();
}

Will result in a new error:

error: ambiguous `+` in a type
 --> src/lib.rs:7:11
  |
7 | fn go(s: &impl T1 + T2) {
  |           ^^^^^^^^^^^^ help: use parentheses to disambiguate: `(impl T1 + T2)`

The compiler hint should be something like this:

help: the following trait defines an item `test`, perhaps you need to restrict type parameter `impl T1` with it:
  |
7 | fn go(s: &(impl T1 + T2)) {
  |           +        +++++

The only case where just adding the restriction works out of the box is when taking an owned value as a parameter:

fn go(s: impl T1 + T2) {
    s.test();
}
@jyn514 jyn514 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. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-trait-system Area: Trait system C-bug Category: This is a bug. labels Apr 15, 2023
@bors bors closed this as completed in d2e52ea May 16, 2023
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 A-trait-system Area: Trait system C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants