Skip to content
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

feat(core): impl Step for NonZero<u*> #127534

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jalil-salame
Copy link
Contributor

@jalil-salame jalil-salame commented Jul 9, 2024

Implement Step for NonZero unsigned integers as discussed in libs-team#130.

step_nonzero_impls was adapted from step_integer_impls and the tests were adapted from the step tests.

It seems to compile and pass the tests c:

I would like to test the edge cases, specially around overflows. To ensure safe code cannot generate a 0 NonZero, but I don't know how to go about it. I would love some feedback on that (and the PR overall).

The impls are tagges with #[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]. I assumed this was appropriate, but I am not sure.

@rustbot
Copy link
Collaborator

rustbot commented Jul 9, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cuviper (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@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 Jul 9, 2024
@rust-log-analyzer

This comment has been minimized.

@jalil-salame
Copy link
Contributor Author

jalil-salame commented Jul 9, 2024

UI tests are failing because the output of diagnostics was reorganized now that NonZero<u*> implement Step. A preliminary look at the contribution docs (https://rustc-dev-guide.rust-lang.org/tests/ui.html#ui-tests) does not show how to update the test output.

Its getting late so I'll take a look at it tomorrow and see if I find a way to update the ui tests.

@cuviper
Copy link
Member

cuviper commented Jul 10, 2024

You can use ./x.py test --bless ui to update the expected output.

I know you already went through an ACP, but I think this still needs a libs-api reviewer since it is instantly stable -- Step is unstable, but it expands the stable impl<A> Iterator for Range<A> etc.

@rustbot label -T-libs +T-libs-api
r? libs-api

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 10, 2024
@rustbot rustbot assigned joshtriplett and unassigned cuviper Jul 10, 2024
Copy link
Member

@the8472 the8472 left a comment

Choose a reason for hiding this comment

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

Regarding the stable API surface: We should also add the appropriate ExactSizeIterator impls, without repeating the 16-bit-platform mistakes.

@jalil-salame
Copy link
Contributor Author

jalil-salame commented Jul 10, 2024

I'm having issues with the ui tests due to NixOS not having the library headers for rust-LLD in the right place. I'll see if I can fix later today. Apparently the failing tests were not preventing me from updating the ui tests 🤦

@jalil-salame
Copy link
Contributor Author

Regarding the stable API surface: We should also add the appropriate ExactSizeIterator impls, without repeating the 16-bit-platform mistakes.

I see that TrustedRandomAccess is cfg gated based on the pointer width but that is not the case for ExactSizeIterator for Range and RangeInclusive. See here. I assume that is because you don't want to implement a non-marker trait on types conditionally.

Implement Step for NonZero unsigned integers as discussed in
[libs-team#130][1].

[1]: rust-lang/libs-team#130

`step_nonzero_impls` was adapted from `step_integer_impls` and the tests
were adapted from the step tests.
Implementing `Step` for the `NonZero<u*>` changed the diagnostics from
the compiler.
@jalil-salame
Copy link
Contributor Author

@joshtriplett friendly ping. Should I assign someone else if you are too busy?

@joshtriplett
Copy link
Member

r? libs-api

@rustbot rustbot assigned m-ou-se and unassigned joshtriplett Aug 26, 2024
@Amanieu
Copy link
Member

Amanieu commented Nov 13, 2024

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Nov 13, 2024

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. labels Nov 13, 2024
@ChrisDenton ChrisDenton added the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label Feb 6, 2025
@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Feb 6, 2025
@rfcbot
Copy link

rfcbot commented Feb 6, 2025

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

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Feb 6, 2025
@ChrisDenton ChrisDenton removed the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label Feb 6, 2025
@leonardo-m
Copy link

See also #73121

@jalil-salame

This comment was marked as resolved.

@leonardo-m
Copy link

do you want to reword your issue accordingly, or close it with this PR and create a new issue regarding NonZero* construction?

For now I've reworded and renamed that ER issue. But I'm now less sure it's a useful enough thing to have.

@jalil-salame
Copy link
Contributor Author

do you want to reword your issue accordingly, or close it with this PR and create a new issue regarding NonZero* construction?

For now I've reworded and renamed that ER issue. But I'm now less sure it's a useful enough thing to have.

Having the issue there is nice though, it might prompt some further discussion in the future, although NonZeroUsize::new(1).unwrap() should be so simple to optimize that I'd file a rustc/llvm bug if the panic is kept.

@leonardo-m
Copy link

although NonZeroUsize::new(1).unwrap() should be so simple to optimize that I'd file a rustc/llvm bug if the panic is kept.

My point isn't to remove the panic from the binary, but to remove it from the source code. I try to minimize the number of visible unwraps in my Rust modules.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Feb 16, 2025
@rfcbot
Copy link

rfcbot commented Feb 16, 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.

@@ -960,6 +1104,12 @@ range_incl_exact_iter_impl! {
i16
}

#[cfg(target_pointer_width = "32")]
Copy link
Member

Choose a reason for hiding this comment

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

do we want these ExactSizeIterator impls to be dependent on pointer size? the impls for primitive integer types don't depend on pointer size, presumably to help you write more portable code that doesn't break on 32 or 16-bit platforms.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a known bug, if they don't depend on the pointer width (size of usize) they can't be ExactSizeIterators (for anything >u16).

(0..=u32::MAX).len() causes an overflow on usize == u16. If you prefer code not to break on usize == u16 platforms, then I would prefer to remove the impls over removing the constraint.

Copy link
Member

@programmerjake programmerjake Mar 3, 2025

Choose a reason for hiding this comment

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

yeah, i'm proposing implementing ExactSizeIterator as follows,
with no #[cfg] attributes (edit: i forgot RangeInclusive still can't have len == 2^16 because NonZero excludes one value):

T Range<NonZero<T>> RangeInclusive<NonZero<T>>
u8 Yes Yes
u16 Yes Yes
u32 No No
u64 No No
u128 No No
usize Yes Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know how to address this, my preference is implementing it as it currently stands, and this concern was raised after the FCP. Would the changes need a new FCP? Am I allowed to edit the PR after the reviews?

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.