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

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

Open
Aaron1011 opened this issue Apr 5, 2021 · 2 comments
Open

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

Aaron1011 opened this issue Apr 5, 2021 · 2 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-bug Category: This is a bug. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

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.

@Aaron1011 Aaron1011 added A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system A-associated-items Area: Associated items (types, constants & functions) C-bug Category: This is a bug. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. labels Apr 5, 2021
@eggyal
Copy link
Contributor

eggyal commented Apr 5, 2021

Of course, it's perfectly valid for the impl to have explicitly replaced the Self::Assoc in my_method's return type with AssertCopy<String> ... presumably the error should still be suppressed in that case, but doing so presumably requires checking the signature in the trait?

@Aaron1011
Copy link
Member Author

I would argue that we shouldn't suppress the error when AssertCopy<String> is explicitly written in the return type. In general, we shouldn't deduplicate well-formed errors for the same type - AssertCopy<String> could be written in two completely unrelated in the project, and we'd want to tell the user that there are two distinct problems with their code.

With an associated type, the error is caused by the concrete type that it resolves to, so displaying it multiple times doesn't really provide any additional information.

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-bug Category: This is a bug. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. 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

3 participants