Allow initializing NonZero with literals#143594
Allow initializing NonZero with literals#143594oli-obk wants to merge 3 commits intorust-lang:mainfrom
NonZero with literals#143594Conversation
|
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.
Here's the erroring example
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. |
94dadf4 to
cd9959a
Compare
|
☔ The latest upstream changes (presumably #148957) made this pull request unmergeable. Please resolve the merge conflicts. |
@sandersaares do you want to do this instead of your RFC? |
|
Small note, but while it doesn't have to be implemented in this PR, it's probably worth noting for later that |
This basically implements rust-lang/rfcs#3786 by teaching the language about
NonZeroand the literalsNonZerosupports.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.