Skip to content

Duplicate error when impl associated type is not well-formed #83868

Open
@Aaron1011

Description

@Aaron1011

The following code:

struct AssertCopy<T: Copy>(T);

trait Foo {
    type Assoc;
    fn my_method() -> Self::Assoc;
}

impl Foo for bool {
    type Assoc = AssertCopy<String>;
    fn my_method() -> Self::Assoc {
        panic!();
    }
}

produces the following two errors:

error[E0277]: the trait bound `String: Copy` is not satisfied
 --> src/lib.rs:9:5
  |
1 | struct AssertCopy<T: Copy>(T);
  |                      ---- required by this bound in `AssertCopy`
...
9 |     type Assoc = AssertCopy<String>;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`

error[E0277]: the trait bound `String: Copy` is not satisfied
  --> src/lib.rs:10:23
   |
1  | struct AssertCopy<T: Copy>(T);
   |                      ---- required by this bound in `AssertCopy`
...
10 |     fn my_method() -> Self::Assoc {
   |                       ^^^^^^^^^^^ the trait `Copy` is not implemented for `String`

Only the first error is useful - the second error occurs because we're trying to use the 'malformed' associated type. This can be especially bad when proc-macros are involved, since the span of the return type might not be meaningful. See #83383

We should suppress the second error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions