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

mismatches types [E0308] doesn't offer enough information to fix the error #34721

Closed
gnzlbg opened this issue Jul 8, 2016 · 7 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. WG-diagnostics Working group: Diagnostics

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Jul 8, 2016

I get an error message like this:

src/x86/bmi2/mod.rs:51:35: 51:36 error: mismatched types [E0308]
src/x86/bmi2/mod.rs:51         unsafe { intrinsics::bzhi(x, bit_position) }
                                                         ^
src/x86/bmi2/mod.rs:51:35: 51:36 help: run `rustc --explain E0308` to see a detailed explanation
src/x86/bmi2/mod.rs:51:35: 51:36 note: expected type `()`
src/x86/bmi2/mod.rs:51:35: 51:36 note:    found type `T`

This tells me "what the error is", but gives me too little context (or no context at all) to actually solve it: I have to go to the intrinsics module, find the bzhi function, read its signature, and in my case, I end up baffled because it actually takes a T (so the expected type () makes no sense).

I still haven't figured out what is going on, but it would be good if when rustc detects a type error when calling a function, it would not only tell me where the error is, which type i am passing, and what type is expected, but where exactly the function is and what's its complete signature so that if needed I know where to go to read up the comments. Probably the real error here is that I am calling a function with the same name but from a different module, so it might be even better if rustc would check all functions in all modules in scope for functions with the same name and the type signature I am passing it and make a suggestion like "did you meant to call the bzhi function from the bar module instead?".

In particular the next error is a type error on the second function argument, so it would be even better if rustc could collapse these two errors into one.


EDIT: An example of the error is given here:

https://play.rust-lang.org/?gist=841b50e5845c61f2a1ebfd141673c151&version=nightly&backtrace=0

where the following error message is not very helpful:

error[E0308]: mismatched types
  --> src/main.rs:21:14
   |
21 |     bar::bar(x.zero())
   |              ^^^^^^^^ expected (), found type parameter
   |
   = note: expected type `()`
              found type `T`
   = help: here are some functions which might fulfill your needs:
           - .zero()

In particular the expected (), found type parameter T is very misleading.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Jul 8, 2016

This was the error (see the comment):

https://play.rust-lang.org/?gist=841b50e5845c61f2a1ebfd141673c151&version=nightly&backtrace=0

The error message is just completely help less.

@apasel422 apasel422 added the A-diagnostics Area: Messages for errors, warnings, and lints label Jul 8, 2016
@gmatht
Copy link

gmatht commented Aug 26, 2016

Errors from rustc are usually so helpful that I can learn rust without documentation, just by reading the error messages. The first thing that I couldn't learn from error messages was how to convert types. Even with something as simple as: let x : u32 = 0; let y : i32 = x, and after reading --explain E0308 it wasn't at all clear that I was meant to use as. Maybe the error could also suggest the conversion that we should use, similar to how it suggests adding .iter() to for [...] {...}?

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 25, 2017
@GuillaumeGomez
Copy link
Member

I think this error has been fixed long ago and improved recently with the add of method suggestions. I'm closing it then.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Aug 16, 2017

@GuillaumeGomez You are correct that right now there is a method suggestion, which is nice.

But the error right now is the same as the error back then: expected type (). This hasn't changed, and it's still useless.

So I don't think this has been fixed at all. And while it has improved a little bit, it is far from being a good error message.


Just check the playground link.

@GuillaumeGomez
Copy link
Member

You didn't add the playground link. Reopening the issue.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Aug 16, 2017

Sorry, the playground example was in the second comment on this issue. I've added it to the issue as well so that it cannot be missed.

@estebank
Copy link
Contributor

estebank commented Sep 25, 2017

The compiler now suggests adding a semicolon:

error[E0308]: mismatched types
  --> src/main.rs:23:5
   |
23 |     x.zero()
   |     ^^^^^^^^- help: did you mean to add a semicolon here?: `;`
   |     |
   |     expected (), found type parameter
   |
   = note: expected type `()`
              found type `T`

When following the suggestion, it (correctly) complains about visibility, after fixing that, it complains about using a moved value, adding a constraint on T: Copy, it compiles.

There're still 3 errors when only one is actually relevant.


The lack of Copy constraint should be clearer:

error[E0382]: use of moved value: `x`
  --> src/main.rs:25:3
   |
   | pub fn baz<T: Foo>(x: T) -> T {
   |            ------ help: consider adding a `Copy` constraint here: `T: Foo + Copy`
   |   if 0 == 1 {
21 |     bar::bar(x.zero())
   |              - value moved here
...
25 |   x.zero()
   |   ^ value used here after move
   |
   = note: move occurs because `x` has type `T`, which does not implement the `Copy` trait

@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Dec 6, 2017
estebank added a commit to estebank/rust that referenced this issue Jan 18, 2019
Centril added a commit to Centril/rust that referenced this issue Jan 21, 2019
…sakis

When using value after move, point at span of local

When trying to use a value after move, instead of using a note, point
at the local declaration that has a type that doesn't implement `Copy`
trait.

```
error[E0382]: use of moved value: `x`
  --> $DIR/issue-34721.rs:27:9
   |
LL |     pub fn baz<T: Foo>(x: T) -> T {
   |                -       - move occurs because `x` has type `T`, which does not implement the `Copy` trait
   |                |
   |                consider adding a `Copy` constraint to this type argument
LL |         if 0 == 1 {
LL |             bar::bar(x.zero())
   |                      - value moved here
LL |         } else {
LL |             x.zero()
   |             - value moved here
LL |         };
LL |         x.zero()
   |         ^ value used here after move
```

Fix rust-lang#34721.
estebank added a commit to estebank/rust that referenced this issue Jan 24, 2019
@bors bors closed this as completed in f20c6c8 Jan 25, 2019
VardhanThigle pushed a commit to jethrogb/rust that referenced this issue Jan 31, 2019
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. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

7 participants