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

Misleading type error when generic parameter is sole mismatch. #76086

Closed
iximeow opened this issue Aug 29, 2020 · 0 comments · Fixed by #117959
Closed

Misleading type error when generic parameter is sole mismatch. #76086

iximeow opened this issue Aug 29, 2020 · 0 comments · Fixed by #117959
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@iximeow
Copy link
Contributor

iximeow commented Aug 29, 2020

Some Rust (playground):

struct Generic<T> {
    inner: T,
}

impl<T> Generic<T> {
    fn foo() -> Generic<()> {
        Self { inner: () }
    }
}

yields an error about the mismatched type of inner:

error[E0308]: mismatched types
 --> src/lib.rs:7:23
  |
5 | impl<T> Generic<T> {
  |      - this type parameter
6 |     fn foo() -> Generic<()> {
7 |         Self { inner: () }
  |                       ^^ expected type parameter `T`, found `()`
  |
  = note: expected type parameter `T`
                  found unit type `()`

but I know it should be () - the return type on foo also expects T to be ()! The second error is somewhat more helpful:

error[E0308]: mismatched types
 --> src/lib.rs:7:9
  |
5 | impl<T> Generic<T> {
  |      - this type parameter
6 |     fn foo() -> Generic<()> {
  |                 ----------- expected `Generic<()>` because of return type
7 |         Self { inner: () }
  |         ^^^^^^^^^^^^^^^^^^ expected `()`, found type parameter `T`
  |
  = note: expected struct `Generic<()>`
             found struct `Generic<T>`

The actual issue I'd had writing this was Self { inner: () } instead of Generic { inner: () }. It would have been helpful if rustc could have concluded that the type I'd wanted matched the function's return type, and that the issue was Self being Generic<T> and causing the mismatch.

cc @estebank who'd asked me to write up an issue with a reproduction.

Apologies for the mushy title, I couldn't think of something more precise - very open to rewording if someone's got a better one-liner.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 29, 2020
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Aug 29, 2020
estebank added a commit to estebank/rust that referenced this issue Nov 16, 2023
When encountering a type error involving a `Self` literal, point at the
self type of the enclosing `impl`.

CC rust-lang#76086.
estebank added a commit to estebank/rust that referenced this issue Nov 16, 2023
When encountering a type error caused by the use of `Self`, suggest
using the actual type name instead.

```
error[E0308]: mismatched types
  --> $DIR/struct-path-self-type-mismatch.rs:13:9
   |
LL |   impl<T> Foo<T> {
   |        -  ------ this is the type of the `Self` literal
   |        |
   |        found type parameter
LL |       fn new<U>(u: U) -> Foo<U> {
   |              -           ------ expected `Foo<U>` because of return type
   |              |
   |              expected type parameter
LL | /         Self {
LL | |
LL | |             inner: u
LL | |
LL | |         }
   | |_________^ expected `Foo<U>`, found `Foo<T>`
   |
   = note: expected struct `Foo<U>`
              found struct `Foo<T>`
   = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
help: use the type name directly
   |
LL |         Foo::<U> {
   |         ~~~~~~~~
```

Fix rust-lang#76086.
estebank added a commit to estebank/rust that referenced this issue Nov 16, 2023
When encountering a type error caused by the use of `Self`, suggest
using the actual type name instead.

```
error[E0308]: mismatched types
  --> $DIR/struct-path-self-type-mismatch.rs:13:9
   |
LL |   impl<T> Foo<T> {
   |        -  ------ this is the type of the `Self` literal
   |        |
   |        found type parameter
LL |       fn new<U>(u: U) -> Foo<U> {
   |              -           ------ expected `Foo<U>` because of return type
   |              |
   |              expected type parameter
LL | /         Self {
LL | |
LL | |             inner: u
LL | |
LL | |         }
   | |_________^ expected `Foo<U>`, found `Foo<T>`
   |
   = note: expected struct `Foo<U>`
              found struct `Foo<T>`
   = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
help: use the type name directly
   |
LL |         Foo::<U> {
   |         ~~~~~~~~
```

Fix rust-lang#76086.
estebank added a commit to estebank/rust that referenced this issue Nov 16, 2023
When encountering a type error involving a `Self` literal, point at the
self type of the enclosing `impl`.

CC rust-lang#76086.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 16, 2023
…rrors

Better handle type errors involving `Self` literals

When encountering a type error involving a `Self` literal, point at the self type of the enclosing `impl` and suggest using the actual type name instead.

```
error[E0308]: mismatched types
  --> $DIR/struct-path-self-type-mismatch.rs:13:9
   |
LL |   impl<T> Foo<T> {
   |        -  ------ this is the type of the `Self` literal
   |        |
   |        found type parameter
LL |       fn new<U>(u: U) -> Foo<U> {
   |              -           ------ expected `Foo<U>` because of return type
   |              |
   |              expected type parameter
LL | /         Self {
LL | |
LL | |             inner: u
LL | |
LL | |         }
   | |_________^ expected `Foo<U>`, found `Foo<T>`
   |
   = note: expected struct `Foo<U>`
              found struct `Foo<T>`
   = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
help: use the type name directly
   |
LL |         Foo::<U> {
   |         ~~~~~~~~
```
Fix rust-lang#76086.
@bors bors closed this as completed in d845685 Nov 17, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 17, 2023
Rollup merge of rust-lang#117959 - estebank:issue-76086, r=compiler-errors

Better handle type errors involving `Self` literals

When encountering a type error involving a `Self` literal, point at the self type of the enclosing `impl` and suggest using the actual type name instead.

```
error[E0308]: mismatched types
  --> $DIR/struct-path-self-type-mismatch.rs:13:9
   |
LL |   impl<T> Foo<T> {
   |        -  ------ this is the type of the `Self` literal
   |        |
   |        found type parameter
LL |       fn new<U>(u: U) -> Foo<U> {
   |              -           ------ expected `Foo<U>` because of return type
   |              |
   |              expected type parameter
LL | /         Self {
LL | |
LL | |             inner: u
LL | |
LL | |         }
   | |_________^ expected `Foo<U>`, found `Foo<T>`
   |
   = note: expected struct `Foo<U>`
              found struct `Foo<T>`
   = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
help: use the type name directly
   |
LL |         Foo::<U> {
   |         ~~~~~~~~
```
Fix rust-lang#76086.
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. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants