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

Misleading error message involving blanket implementations and associated types #55591

Closed
jturner314 opened this issue Nov 1, 2018 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jturner314
Copy link
Contributor

Under some circumstances involving blanket implementations and associated types, the compiler gives a misleading error message describing the wrong trait bound.

I tried this code:

pub trait IntoProducer {
    type Output: Producer;
}

impl<P: Producer> IntoProducer for P {
    type Output = Self;
}

impl<'a, T> IntoProducer for &'a Vec<T> {
    type Output = &'a [T];
}

pub trait Producer {}

impl<'a, T> Producer for &'a [T] {}

fn dostuff<IP, P>(arg: IP)
where
    IP: IntoProducer<Output = P>,
    P: Producer,
{
    unimplemented!()
}

fn main() {
    dostuff(Vec::<i32>::new());
}

(Playground)

I expected to see an error message like this (the correct bound is IntoProducer):

error[E0277]: the trait bound `std::vec::Vec<i32>: IntoProducer` is not satisfied
  --> src/main.rs:26:5
   |
26 |     dostuff(Vec::<i32>::new());
   |     ^^^^^^^ the trait `IntoProducer` is not implemented for `std::vec::Vec<i32>`
   |
note: required by `dostuff`
  --> src/main.rs:17:1
   |
17 | / fn dostuff<IP, P>(arg: IP)
18 | | where
19 | |     IP: IntoProducer<Output = P>,
20 | |     P: Producer,
21 | | {
22 | |     unimplemented!()
23 | | }
   | |_^

Instead, the compiler gave this error message (the bound is Producer):

error[E0277]: the trait bound `std::vec::Vec<i32>: Producer` is not satisfied
  --> src/main.rs:26:5
   |
26 |     dostuff(Vec::<i32>::new());
   |     ^^^^^^^ the trait `Producer` is not implemented for `std::vec::Vec<i32>`
   |
note: required by `dostuff`
  --> src/main.rs:17:1
   |
17 | / fn dostuff<IP, P>(arg: IP)
18 | | where
19 | |     IP: IntoProducer<Output = P>,
20 | |     P: Producer,
21 | | {
22 | |     unimplemented!()
23 | | }
   | |_^

This occurs on stable (1.30.0), beta (1.31.0-beta.3), and nightly (1.31.0-nightly) on the Rust Playground.

What appears to be happening is that the compiler sees the impl<P: Producer> IntoProducer for P implementation and assumes that this implementation that will always be used, even though it has a P: Producer constraint and other implementations of IntoProducer exist.

@jturner314
Copy link
Contributor Author

jturner314 commented Nov 1, 2018

This appears to be closely related to #40120 and #28894, if not a duplicate.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 11, 2019
@estebank
Copy link
Contributor

Triage: current output:

error[E0277]: the trait bound `std::vec::Vec<i32>: Producer` is not satisfied
  --> src/main.rs:26:13
   |
17 | fn dostuff<IP, P>(arg: IP)
   |    -------
...
20 |     P: Producer,
   |        -------- required by this bound in `dostuff`
...
26 |     dostuff(Vec::<i32>::new());
   |             ^^^^^^^^^^^^^^^^^ the trait `Producer` is not implemented for `std::vec::Vec<i32>`

@estebank estebank added A-traits Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 25, 2020
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@kadiwa4
Copy link
Contributor

kadiwa4 commented Jan 28, 2023

The current output is helpful (rust 2023-01-27):

error[E0277]: the trait bound `Vec<i32>: IntoProducer` is not satisfied
  --> src/main.rs:26:13
   |
26 |     dostuff(Vec::<i32>::new());
   |     ------- ^^^^^^^^^^^^^^^^^ the trait `IntoProducer` is not implemented for `Vec<i32>`
   |     |
   |     required by a bound introduced by this call
   |
note: required for `Vec<i32>` to implement `IntoProducer`
  --> src/main.rs:5:19
   |
5  | impl<P: Producer> IntoProducer for P {
   |         --------  ^^^^^^^^^^^^     ^
   |         |
   |         unsatisfied trait bound introduced here
note: required by a bound in `dostuff`
  --> src/main.rs:19:9
   |
17 | fn dostuff<IP, P>(arg: IP)
   |    ------- required by a bound in this
18 | where
19 |     IP: IntoProducer<Output = P>,
   |         ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `dostuff`
help: consider borrowing here
   |
26 |     dostuff(&Vec::<i32>::new());
   |             +

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

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-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

4 participants