Skip to content

Make NonZero<char> possible #141001

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

hkBst
Copy link
Member

@hkBst hkBst commented May 14, 2025

I'd like to use NonZero<char> for representing units of CStr in https://github.com/rust-lang/literal-escaper

@rustbot
Copy link
Collaborator

rustbot commented May 14, 2025

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 14, 2025
@hanna-kruppe
Copy link
Contributor

It’s not clear from the diff, but I guess since NonZero<T> works by trait impls on T, this can’t be feature-gated and would immediately allow NonZero<char> on stable?

@jieyouxu
Copy link
Member

r? libs-api

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 14, 2025
@rustbot rustbot assigned joshtriplett and unassigned joboet May 14, 2025
@hkBst
Copy link
Member Author

hkBst commented May 14, 2025

It’s not clear from the diff, but I guess since NonZero<T> works by trait impls on T, this can’t be feature-gated and would immediately allow NonZero<char> on stable?

That's a good point I hadn't considered. I'll look into whether a feature gate is possible.

@tgross35
Copy link
Contributor

Could you add a test so we don't accidentally regress this? library/coretests/tests/num/niche_types.rs doesn't exist yet but probably would be the place.

@hkBst
Copy link
Member Author

hkBst commented May 15, 2025

Could you add a test so we don't accidentally regress this? library/coretests/tests/num/niche_types.rs doesn't exist yet but probably would be the place.

Do you mean a test that just uses all the possible NonZero-able types as NonZeros? Meaning they are tested for being possible. Or is there another thing you meant?

@tgross35
Copy link
Contributor

I did only mean something to check basic usage of the type, and was only thinking of char since the other types are covered by the NonZeroUxx doctests. It wouldn't be bad to just add the same kind of test for all the types, though.

@hkBst
Copy link
Member Author

hkBst commented May 21, 2025

@tgross35 I added a simple test for NonZero::<char>. Let me know if it is okay, or if you have any more ideas to make it better.

@tgross35 tgross35 added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label May 21, 2025
@dtolnay dtolnay removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label May 27, 2025
@Amanieu Amanieu removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label May 27, 2025
@Amanieu
Copy link
Member

Amanieu commented May 27, 2025

I'd like to use NonZero<char> for representing units of CStr in rust-lang/literal-escaper

Could give some examples of how this is going to be used in literal-escaper?

@hkBst
Copy link
Member Author

hkBst commented May 27, 2025

Could give some examples of how this is going to be used in literal-escaper?

The characters of a C string are checked for being non-null as a matter of correctness anyway, and it may be a good idea to preserve this in the type.

I have a PR that did this (as a side quest) and the relevant file is here: https://github.com/rust-lang/rust/blob/30822ec0ec25723f36f9e73c42d91a83dc121388/compiler/rustc_lexer/src/unescape.rs

/// Used for mixed utf8 string literals, i.e. those that allow both unicode
/// chars and high bytes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MixedUnit {
    /// Used for ASCII chars (written directly or via `\x01`..`\x7f` escapes)
    /// and Unicode chars (written directly or via `\u` escapes).
    ///
    /// For example, if '¥' appears in a string it is represented here as
    /// `MixedUnit::Char('¥')`, and it will be appended to the relevant byte
    /// string as the two-byte UTF-8 sequence `[0xc2, 0xa5]`
    Char(NonZero<char>),

    /// Used for high bytes (`\x80`..`\xff`).
    ///
    /// For example, if `\xa5` appears in a string it is represented here as
    /// `MixedUnit::HighByte(0xa5)`, and it will be appended to the relevant
    /// byte string as the single byte `0xa5`.
    HighByte(NonZero<u8>),
}

impl From<NonZero<char>> for MixedUnit {
    fn from(c: NonZero<char>) -> Self {
        MixedUnit::Char(c)
    }
}

impl From<NonZero<u8>> for MixedUnit {
    fn from(byte: NonZero<u8>) -> Self {
        if byte.get().is_ascii() {
            MixedUnit::Char(NonZero::new(byte.get() as char).unwrap())
        } else {
            MixedUnit::HighByte(byte)
        }
    }
}

impl TryFrom<char> for MixedUnit {
    type Error = EscapeError;

    fn try_from(c: char) -> Result<Self, EscapeError> {
        NonZero::new(c).map(MixedUnit::Char).ok_or(EscapeError::NulInCStr)
    }
}

impl TryFrom<u8> for MixedUnit {
    type Error = EscapeError;

    fn try_from(byte: u8) -> Result<Self, EscapeError> {
        NonZero::<u8>::new(byte).map(From::from).ok_or(EscapeError::NulInCStr)
    }
}

@Amanieu
Copy link
Member

Amanieu commented May 27, 2025

Seems reasonable.

@rfcbot merge

@rfcbot
Copy link
Collaborator

rfcbot commented May 27, 2025

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels May 27, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented May 27, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Jun 6, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Jun 6, 2025

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Jun 6, 2025
@tgross35
Copy link
Contributor

tgross35 commented Jun 6, 2025

@hkBst mind rebasing to get a fresh set of CI runs? IIRC library/coretests/tests/num/mod.rs had some changes, I'd like to make sure checks pass before merging.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thank you!

@dtolnay
Copy link
Member

dtolnay commented Jun 7, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Jun 7, 2025

📌 Commit a87cd55 has been approved by dtolnay

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 7, 2025
@dtolnay dtolnay assigned dtolnay and unassigned joshtriplett Jun 7, 2025
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 7, 2025
Make NonZero<char> possible

I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
bors added a commit that referenced this pull request Jun 7, 2025
Rollup of 14 pull requests

Successful merges:

 - #138062 (Enable Non-determinism of float operations in Miri and change std tests )
 - #140560 (Allow `#![doc(test(attr(..)))]` everywhere)
 - #141001 (Make NonZero<char> possible)
 - #141295 (Stabilize `if let` guards (`feature(if_let_guard)`))
 - #141435 (Add (back) `unsupported_calling_conventions` lint to reject more invalid calling conventions)
 - #141447 (Document representation of `Option<unsafe fn()>`)
 - #142008 (const-eval error: always say in which item the error occurred)
 - #142053 (Add new Tier-3 targets: `loongarch32-unknown-none*`)
 - #142065 (Stabilize `const_eq_ignore_ascii_case`)
 - #142116 (Fix bootstrap tracing imports)
 - #142126 (Treat normalizing consts like normalizing types in deeply normalize)
 - #142140 (compiler: Sort and doc ExternAbi variants)
 - #142148 (compiler: Treat ForceWarning as a Warning for diagnostic level)
 - #142154 (get rid of spurious cfg(bootstrap))

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 8, 2025
Make NonZero<char> possible

I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
bors added a commit that referenced this pull request Jun 8, 2025
Rollup of 7 pull requests

Successful merges:

 - #141001 (Make NonZero<char> possible)
 - #141700 (Atomic intrinsics : use const generic ordering, part 2)
 - #141993 (Use the in-tree `compiler-builtins` for the sysroot)
 - #142008 (const-eval error: always say in which item the error occurred)
 - #142053 (Add new Tier-3 targets: `loongarch32-unknown-none*`)
 - #142132 (`tests/ui`: A New Order [6/N])
 - #142179 (store `target.min_global_align` as an `Align`)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 8, 2025
Make NonZero<char> possible

I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
bors added a commit that referenced this pull request Jun 8, 2025
Rollup of 6 pull requests

Successful merges:

 - #141001 (Make NonZero<char> possible)
 - #141700 (Atomic intrinsics : use const generic ordering, part 2)
 - #142008 (const-eval error: always say in which item the error occurred)
 - #142053 (Add new Tier-3 targets: `loongarch32-unknown-none*`)
 - #142132 (`tests/ui`: A New Order [6/N])
 - #142179 (store `target.min_global_align` as an `Align`)

r? `@ghost`
`@rustbot` modify labels: rollup
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 8, 2025
Make NonZero<char> possible

I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
bors added a commit that referenced this pull request Jun 8, 2025
Rollup of 11 pull requests

Successful merges:

 - #140774 (Affirm `-Cforce-frame-pointers=off` does not override)
 - #141001 (Make NonZero<char> possible)
 - #141700 (Atomic intrinsics : use const generic ordering, part 2)
 - #142008 (const-eval error: always say in which item the error occurred)
 - #142053 (Add new Tier-3 targets: `loongarch32-unknown-none*`)
 - #142089 (Replace all uses of sysroot_candidates with get_or_default_sysroot)
 - #142108 (compiler: Add track_caller to AbiMapping::unwrap)
 - #142132 (`tests/ui`: A New Order [6/N])
 - #142162 (UnsafePinned: update get() docs and signature to allow shared mutation)
 - #142171 (`tests/ui`: A New Order [7/N])
 - #142179 (store `target.min_global_align` as an `Align`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors
Copy link
Collaborator

bors commented Jun 8, 2025

⌛ Testing commit a87cd55 with merge dcd1b16...

bors added a commit that referenced this pull request Jun 8, 2025
Make NonZero<char> possible

I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
@bors
Copy link
Collaborator

bors commented Jun 8, 2025

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting
Projects
None yet
Development

Successfully merging this pull request may close these issues.