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

Provide hint when cast needs a dereference #37442

Merged
merged 1 commit into from
Nov 23, 2016

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Oct 28, 2016

For a given code:

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

display:

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:

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 #37338.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Aatch (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@@ -139,6 +140,21 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {

fn report_cast_error(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, e: CastError) {
match e {
CastError::NeedDeref => {
let cast_ty = fcx.ty_to_string(self.cast_ty);
let mut err = fcx.type_error_struct(self.cast_span,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointing at the self.cast_span instead of self.span (i16 instead of s as i16 in the example) because when trying to apply the secondary label on just self.expr.span (s in the example) the actual output looks like this:

error: casting `&f64` as `i16` is invalid
 --> file3.rs:2:30
  |
2 |     vec![0.0].iter().map(|s| s as i16).collect::<Vec<i16>>();
  |                              ^^^^^^^^
  |                              |
  |                              casting `&f64` as `i16` is invalid      // <<< this in red
  |                              try dereferencing for the cast to work  // <<< this in blue

losing the fact that the secondary label's hint should only apply to the s only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #37453 for this.

self.expr_ty);
err.span_label(self.cast_span,
&format!("casting `{}` as `{}` is invalid", self.expr_ty, cast_ty));
err.span_label(self.expr.span,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn’t be a span_label, but rather span_help instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we show the deref, it should be span_help.

Another way would be to change the label to, for example, "did you mean *{}?"

CastError::NeedViaUsize => "a usize",
_ => bug!(),
}))
.span_label(self.span,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree about label here as well.

}
}
_ => Err(CastError::NeedViaPtr),
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct A;
&A as A;

stays without a note/help. I’m fine with it personally, but that’s something that could use such a help message.

@estebank
Copy link
Contributor Author

estebank commented Nov 9, 2016

r? @jonathandturner

@rust-highfive rust-highfive assigned sophiajt and unassigned Aatch Nov 9, 2016
@sophiajt
Copy link
Contributor

@estebank - I like it! Can we do one small tweak while you're at it?

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

@estebank
Copy link
Contributor Author

@jonathandturner done!

@sophiajt
Copy link
Contributor

Great!

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Nov 14, 2016

📌 Commit f291bbe has been approved by jonathandturner

@bors
Copy link
Contributor

bors commented Nov 17, 2016

☔ The latest upstream changes (presumably #37375) made this pull request unmergeable. Please resolve the merge conflicts.

This was referenced Nov 17, 2016
bors added a commit that referenced this pull request Nov 17, 2016
Rollup of 8 pull requests

- Successful merges: #37752, #37757, #37759, #37766, #37772, #37799, #37806, #37821
- Failed merges: #37442
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
 --> foo.rs:2:35
  |
2 |     vec![0.0].iter().map(|s| s as i16).collect::<Vec<i16>>();
  |                              -    ^^^ cannot cast `&f64` as `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
```
@estebank
Copy link
Contributor Author

@jonathandturner fixed merge conflict.

@sophiajt
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Nov 22, 2016

📌 Commit ec24442 has been approved by jonathandturner

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request 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.
bors added a commit that referenced this pull request Nov 23, 2016
Rollup of 7 pull requests

- Successful merges: #37442, #37760, #37836, #37851, #37859, #37913, #37925
- Failed merges:
@bors bors merged commit ec24442 into rust-lang:master Nov 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants