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

Confusing error "trait is not implemented for the type" on syntax error #24354

Closed
Virtlink opened this issue Apr 12, 2015 · 4 comments · Fixed by #65192
Closed

Confusing error "trait is not implemented for the type" on syntax error #24354

Virtlink opened this issue Apr 12, 2015 · 4 comments · Fixed by #65192
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@Virtlink
Copy link

This code:

pub trait A<T : B> { fn foo(self) -> T; }

pub struct AImpl;

impl<BImpl> A<BImpl> for AImpl {
    fn foo(self) -> BImpl { unimplemented!(); }
}

pub trait B { fn bar(self); }

pub struct BImpl;

impl B for BImpl {
    fn bar(self) { unimplemented!(); }
}

...give me this error:

   Compiling testproject v0.0.1 (file:///home/virtlink/projects/orion/testproject)
src/lib.rs:6:1: 8:2 error: the trait `B` is not implemented for the type `BImpl` [E0277]
src/lib.rs:6 impl<BImpl> A<BImpl> for AImpl {
src/lib.rs:7     fn foo(self) -> BImpl { unimplemented!(); }
src/lib.rs:8 }
error: aborting due to previous error
Could not compile `testproject`.

This is confusing. The trait B is implemented for the type BImpl.

Instead, I have to change this:

impl<BImpl> A<BImpl> for AImpl

...into this:

impl A<BImpl> for AImpl

The error should make that clear.

rustc 1.0.0-nightly (c89de2c56 2015-03-28) (built 2015-03-29)
binary: rustc
commit-hash: c89de2c56baeb61e7cc434924dcc8bedd32b26b8
commit-date: 2015-03-28
build-date: 2015-03-29
host: x86_64-unknown-linux-gnu
release: 1.0.0-nightly
@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 13, 2015
@apasel422
Copy link
Contributor

I'm not really sure what the compiler can do here, as there is both a type parameter and a type named BImpl. The compiler's error message is correct, in that the type parameter BImpl does not implement B. Maybe there could be a lint for shadowing a type with a type parameter?

@Emilgardis
Copy link
Contributor

Emilgardis commented Jan 21, 2017

I believe this should be closed, the new error messages give a good explanation of what is happening.
Link to Playground: https://is.gd/D44BaU

@arielb1 arielb1 closed this as completed Jan 22, 2017
@arielb1 arielb1 reopened this Jan 22, 2017
@Mark-Simulacrum
Copy link
Member

Today we give the following; ideally I think we would have a span on BImpl in impl<BImpl> which we'd reference with something like "this generic must implement B because of the bound on trait A." This is likely quite hard though.

error[E0277]: the trait bound `BImpl: B` is not satisfied
 --> test.rs:5:13
  |
5 | impl<BImpl> A<BImpl> for AImpl {
  |             ^^^^^^^^ the trait `B` is not implemented for `BImpl`
  |
  = help: consider adding a `where BImpl: B` bound
  = note: required by `A`

error: aborting due to previous error

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@estebank
Copy link
Contributor

Related to #47319. Once that ticket is fixed, pointing at BImpl becomes feasible.

Centril added a commit to Centril/rust that referenced this issue Oct 19, 2019
…asper

Use structured suggestion for restricting bounds

When a trait bound is not met and restricting a type parameter would
make the restriction hold, use a structured suggestion pointing at an
appropriate place (type param in param list or `where` clause).

Account for opaque parameters where instead of suggesting extending
the `where` clause, we suggest appending the new restriction:
`fn foo(impl Trait + UnmetTrait)`. Fix rust-lang#64565, fix rust-lang#41817, fix rust-lang#24354,
cc rust-lang#26026, cc rust-lang#37808, cc rust-lang#24159, fix rust-lang#37138, fix rust-lang#24354, cc rust-lang#20671.
@bors bors closed this as completed in 7e4ff91 Oct 19, 2019
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants