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

Poor error message with associated constrained trait type #57663

Closed
hellow554 opened this issue Jan 16, 2019 · 5 comments
Closed

Poor error message with associated constrained trait type #57663

hellow554 opened this issue Jan 16, 2019 · 5 comments
Labels
A-associated-items Area: Associated items such as associated types and consts. A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@hellow554
Copy link
Contributor

hellow554 commented Jan 16, 2019

trait Bar {
    type Ok;
    type Sibling: Bar2<Ok=Self::Ok>;
}
trait Bar2 {
    type Ok;
}

struct Foo;
struct Foo2;

impl Bar for Foo {
    type Ok = ();
    type Sibling = Foo2;
}
impl Bar2 for Foo2 {
    type Ok = u32; // <- `u32` instead of `()`
}

(Playground)

Errors:

error[E0271]: type mismatch resolving `<Foo2 as Bar2>::Ok == ()`
  --> src/lib.rs:14:6
   |
14 | impl Bar for Foo {
   |      ^^^ expected u32, found ()
   |
   = note: expected type `u32`
              found type `()`

I don't like the error message, I would like to see something similar to this

error[E0271]: type mismatch resolving `<Foo2 as Bar2>::Ok == ()`
...

LN | type Ok = ();
   | ^^^^^^^^^^^^ associated type Ok defined here
LN | type Sibling: Bar2<Ok=Self::Ok>;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type constrained here
   | ...
LN | type Ok = u32; // <- `u32` instead of `()`
   | ^^^^^^^^^^^^^ type violated here
@LukasKalbertodt
Copy link
Member

A more simple example with the same "not so great" error message:


This code:

trait Bar {}

trait Foo {
    type Assoc: Bar;
}

impl Foo for () {
    type Assoc = bool;
}

Leads to (stable 1.32 and nightly 1.34):

error[E0277]: the trait bound `bool: Bar` is not satisfied
 --> src/lib.rs:7:6
  |
7 | impl Foo for () {
  |      ^^^ the trait `Bar` is not implemented for `bool`

The error is that bool does not implement Bar. I would expect that the error points to the line type Assoc = bool;, potentially with a good note. Example:

error[E0277]: the trait bound `bool: Bar` is not satisfied
 --> src/lib.rs:7:6
  |
8 |     type Assoc = bool;
  |     ^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `bool`
  |
  = note: trait `Foo` requires the associated type `Assoc` to satisfy the bound `Bar`

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-associated-items Area: Associated items such as associated types and consts. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 25, 2019
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Oct 9, 2019
bors added a commit that referenced this issue Oct 11, 2019
Point at associated type for some obligations

Partially address #57663.
@estebank
Copy link
Contributor

With #65288, the case brought up in the last comment will look like this:

error[E0277]: the trait bound `bool: Bar` is not satisfied
 --> file8.rs:8:5
  |
4 |     type Assoc: Bar;
  |          ----- associated type defined here
...
7 | impl Foo for () {
  | --------------- in this `impl` item
8 |     type Assoc = bool;
  |     ^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `bool`

and the original report like this:

error[E0271]: type mismatch resolving `<Foo2 as Bar2>::Ok == ()`
  --> file8.rs:13:5
   |
6  |     type Ok;
   |          -- associated type defined here
...
12 | impl Bar for Foo {
   | ---------------- in this `impl` item
13 |     type Ok = ();
   |     ^^^^^^^^^^^^^ expected u32, found ()
   |
   = note: expected type `u32`
              found type `()`

@LukasKalbertodt
Copy link
Member

Thanks a bunch! :)

Centril added a commit to Centril/rust that referenced this issue Oct 26, 2019
…omatsakis

Point at associated type for some obligations

Partially address rust-lang#57663.
bors added a commit that referenced this issue Oct 27, 2019
Point at associated type for some obligations

Partially address #57663.
Centril added a commit to Centril/rust that referenced this issue Nov 8, 2019
Point at where clauses where the associated item was restricted

CC rust-lang#57663.
r? @nikomatsakis
Centril added a commit to Centril/rust that referenced this issue Nov 9, 2019
Point at where clauses where the associated item was restricted

CC rust-lang#57663.
r? @nikomatsakis
Centril added a commit to Centril/rust that referenced this issue Nov 9, 2019
Point at where clauses where the associated item was restricted

CC rust-lang#57663.
r? @nikomatsakis
@estebank estebank added D-papercut Diagnostics: An error or lint that needs small tweaks. and removed D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Dec 13, 2019
@hellow554
Copy link
Contributor Author

On current stable (1.52.1) it looks like this:

error[E0277]: the trait bound `bool: Bar` is not satisfied
 --> src/lib.rs:8:5
  |
4 |     type Assoc: Bar;
  |                 --- required by this bound in `Foo::Assoc`
...
8 |     type Assoc = bool;
  |     ^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `bool`

therefore I think this can be closed. Open it up again if you like to :)

@estebank
Copy link
Contributor

estebank commented May 30, 2021

@hellow554 because we can always improve things, #85799 :)

Screen Shot 2021-05-30 at 7 59 49 AM

error[E0271]: type mismatch resolving `<Foo2 as Bar2>::Ok == ()`
  --> f23.rs:16:20
   |
5  |     type Sibling: Bar2<Ok=Self::Ok>;
   |                        ----------- required by this bound in `Bar::Sibling`
...
16 |     type Sibling = Foo2;
   |                    ^^^^ type mismatch resolving `<Foo2 as Bar2>::Ok == ()`
...
20 |     type Ok = u32; // <- `u32` instead of `()`
   |               --- expected this to be `()`

vs the current

error[E0271]: type mismatch resolving `<Foo2 as Bar2>::Ok == ()`
  --> src/lib.rs:16:5
   |
5  |     type Sibling: Bar2<Ok=Self::Ok>;
   |                        ----------- required by this bound in `Bar::Sibling`
...
16 |     type Sibling = Foo2;
   |     ^^^^^^^^^^^^^^^^^^^^ expected `()`, found `u32`

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 such as associated types and consts. A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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

5 participants