-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Allow initializing NonZero
with literals
#143594
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
base: master
Are you sure you want to change the base?
Conversation
We'll also need to update the Reference for this, and the details in that PR would help form another basis for the FCP here. cc @ehuss |
|
||
fn main() { | ||
let _: std::num::NonZero<u8> = 256; | ||
//~^ ERROR literal out of range |
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.
Here's the erroring example
cf76d4e
to
8797188
Compare
8797188
to
94dadf4
Compare
Limited nonzero coercion coercing `NonZero<T>` to `T` if all the types are available at the cast site. This is fairly limited, as (as the tests show), this does not allow "reborrowing" `&NonZero<T>` as `&T`. It also fails quickly if the types aren't fully clear at the coercion site, but only resolved later. Also cannot aid in trait selection or similar, even if there is clearly only one option. Related: * #143594 * rust-lang/rfcs#3786 cc `@traviscross`
☔ The latest upstream changes (presumably #144044) made this pull request unmergeable. Please resolve the merge conflicts. |
This basically implements rust-lang/rfcs#3786 by teaching the language about
NonZero
and the literalsNonZero
supports.I believe it is a minor enough feature that it either doesn't need an RFC and can just be FCPed or it is a simple enough RFC that @rust-lang/lang could FCP it to set a precedent for doing this for other libcore types. Anything like this for user types would of course require a full RFC as there are many open questions around it that
NonZero<T>
just doesn't have.