Skip to content

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

Closed
@Virtlink

Description

@Virtlink

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

Activity

apasel422

apasel422 commented on Jul 19, 2015

@apasel422
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

Emilgardis commented on Jan 21, 2017

@Emilgardis
Contributor

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

Mark-Simulacrum

Mark-Simulacrum commented on May 20, 2017

@Mark-Simulacrum
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
estebank

estebank commented on Jan 28, 2018

@estebank
Contributor

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

added 2 commits that reference this issue on Oct 19, 2019

Rollup merge of rust-lang#65192 - estebank:restrict-bound, r=matthewj…

ef8ac78

Rollup merge of rust-lang#65192 - estebank:restrict-bound, r=matthewj…

7e4ff91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @steveklabnik@Virtlink@Emilgardis@estebank@arielb1

      Issue actions

        Confusing error "trait is not implemented for the type" on syntax error · Issue #24354 · rust-lang/rust