Allow initializing NonZero with literals#143594
Draft
oli-obk wants to merge 3 commits intorust-lang:mainfrom
Draft
Allow initializing NonZero with literals#143594oli-obk wants to merge 3 commits intorust-lang:mainfrom
NonZero with literals#143594oli-obk wants to merge 3 commits intorust-lang:mainfrom
Conversation
Contributor
|
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 |
oli-obk
commented
Jul 7, 2025
|
|
||
| fn main() { | ||
| let _: std::num::NonZero<u8> = 256; | ||
| //~^ ERROR literal out of range |
Contributor
Author
There was a problem hiding this comment.
Here's the erroring example
8797188 to
94dadf4
Compare
rust-bors bot
added a commit
that referenced
this pull request
Jul 8, 2025
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`
Collaborator
|
☔ The latest upstream changes (presumably #144044) made this pull request unmergeable. Please resolve the merge conflicts. |
94dadf4 to
cd9959a
Compare
Collaborator
|
☔ The latest upstream changes (presumably #148957) made this pull request unmergeable. Please resolve the merge conflicts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.