-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Suggest try_into
when possible
#60159
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
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
| ^^^^^^^^^ | ||
| ^^ | ||
| | | ||
| expected i16, found i8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should change the mismathced types
root message to expected i16, found i8
so the suggestion can be inlined properly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It took me a second to understand but now I see what you mean... That's probably something worthy of doing in a separate PR. The actual code change should be smallish, but the effect in stderr
files will probably be extensive.
r? @oli-obk |
|
||
error[E0308]: mismatched types | ||
--> $DIR/repeat_count.rs:28:23 | ||
| | ||
LL | let f = [0_usize; -1_isize]; | ||
| ^^^^^^^^ expected usize, found isize | ||
help: you can convert an `isize` to `usize` or panic if it the converted value wouldn't fit | ||
| | ||
LL | let f = [0_usize; (-1_isize).try_into().unwrap()]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for now ok, but we should try to not suggest code that is known to panic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is indeed a bad corner case :-/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #60384
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does this code interact with constants? Do we have some tests for that? For now we should not be suggesting that const FOO: u32 = bar();
should use try_from
, or from
irrelevant of the return type of bar
I'm checking but, I think we have any guards against that at the moment, given this current example where rustc gives suggestions in expr context, but not const context. It does the right thing:
|
You'll also need to extend that check to know about |
@oli-obk oh :( |
f474297
to
31eb5cc
Compare
@oli-obk |
@bors r+ |
📌 Commit 31eb5cc has been approved by |
Suggest `try_into` when possible CC #47168
☀️ Test successful - checks-travis, status-appveyor |
CC #47168