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

Help is not helpful for error #37338

Closed
est31 opened this issue Oct 22, 2016 · 2 comments
Closed

Help is not helpful for error #37338

est31 opened this issue Oct 22, 2016 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@est31
Copy link
Member

est31 commented Oct 22, 2016

If you try to compile the code (play link):

vec![0.0].iter().map(|s| s as i16).collect::<Vec<i16>>();

You get the error

rustc 1.14.0-nightly (6e8f92f11 2016-10-07)
error: casting `&f64` as `i16` is invalid
 --> <anon>:2:30
  |
2 |     vec![0.0].iter().map(|s| s as i16).collect();
  |                              ^^^^^^^^
  |
  = help: cast through a raw pointer first

error: aborting due to previous error

But casting through a raw pointer (s as *const f64 as *const i16 as i16) is not the idiomatic way IMO. Instead it should be fixed by simply dereferencing:

vec![0.0].iter().map(|s| *s as i16).collect();
@bluss bluss added the A-diagnostics Area: Messages for errors, warnings, and lints label Oct 22, 2016
@fhartwig
Copy link
Contributor

But casting through a raw pointer (s as *const f64 as *const i16 as i16) is not the idiomatic way IMO.

It's not just unidiomatic, it actually does something completely different.

@GuillaumeGomez
Copy link
Member

Funny thing: casting *const f32 as i16 doesn't throw errors. I replace this error with another one which fit better.

bors added a commit that referenced this issue Nov 17, 2016
Improve reference cast help message

Fixes #37338.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Nov 23, 2016
…ndturner

Provide hint when cast needs a dereference

For a given code:

``` rust
vec![0.0].iter().map(|s| s as i16).collect::<Vec<i16>>();
```

display:

``` nocode
error: casting `&f64` as `i16` is invalid
 --> file3.rs:2:35
  |
2 |     vec![0.0].iter().map(|s| s as i16).collect::<Vec<i16>>();
  |                              -    ^^^
  |                              |
  |                              did you mean `*s`?
```

instead of:

``` nocode
error: casting `&f64` as `i16` is invalid
 --> <anon>:2:30
  |
2 |     vec![0.0].iter().map(|s| s as i16).collect();
  |                              ^^^^^^^^
  |
  = help: cast through a raw pointer first
```

Fixes rust-lang#37338.
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
Projects
None yet
Development

No branches or pull requests

4 participants