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

Update the standard library to use the new NonNull::without_provenance #135343

Open
scottmcm opened this issue Jan 10, 2025 · 4 comments · May be fixed by #135347
Open

Update the standard library to use the new NonNull::without_provenance #135343

scottmcm opened this issue Jan 10, 2025 · 4 comments · May be fixed by #135347
Assignees
Labels
A-strict-provenance Area: Strict provenance for raw pointers C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-tedious Call for participation: An issue involves lots of work and is better handled as many small tasks. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Jan 10, 2025

The function is new, cc #135243

But there's a bunch of places in the code that can be simplified now that it exists. A couple I found quickly:

library/alloc\src\rc.rs
3032:                NonNull::new_unchecked(ptr::without_provenance_mut::<RcInner<T>>(usize::MAX))
3059:                NonNull::new_unchecked(ptr::without_provenance_mut::<RcInner<T>>(usize::MAX))

library/alloc\src\sync.rs
2692:                NonNull::new_unchecked(ptr::without_provenance_mut::<ArcInner<T>>(usize::MAX))
2722:                NonNull::new_unchecked(ptr::without_provenance_mut::<ArcInner<T>>(usize::MAX))

library/core\src\alloc\layout.rs
237:        unsafe { NonNull::new_unchecked(crate::ptr::without_provenance_mut::<u8>(self.align())) }

library/std\src\io\error\repr_bitpacked.rs
179:            unsafe { NonNull::new_unchecked(ptr::without_provenance_mut(utagged)) },
196:            unsafe { NonNull::new_unchecked(ptr::without_provenance_mut(utagged)) },

It would be nice to simplify those like

-                NonNull::new_unchecked(ptr::without_provenance_mut::<RcInner<T>>(usize::MAX))
+                NonNull::without_provenance::<RcInner<T>>(NonZeroUsize::MAX)

and

-        unsafe { NonNull::new_unchecked(crate::ptr::without_provenance_mut::<u8>(self.align())) }
+        NonNull::without_provenance::<u8>(self.alignment.as_nonzero())

(or whatever it would be) to have shorter code that needs to prove fewer soundness preconditions.

@scottmcm scottmcm added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 10, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 10, 2025
@lolbinarycat lolbinarycat added C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-tedious Call for participation: An issue involves lots of work and is better handled as many small tasks. A-strict-provenance Area: Strict provenance for raw pointers and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 10, 2025
@cod10129
Copy link
Contributor

I can do this, it's just a matter of finding the places where this function would be useful. (I would find most of those with a grep for without_provenance_mut, right?). If there are more relevant cases that wouldn't be really hard to find then please tell me.

@rustbot claim

@samueltardieu
Copy link
Contributor

@cod10129 In case you haven't done it yet, I have a PR ready already, I was waiting for the complete test results. If you want do to it anyway you can get inspiration from samueltardieu@9ab77f1 that I was about to submit.

@cod10129
Copy link
Contributor

Never mind then. Go ahead and submit yours @samueltardieu

@rustbot release-assignment

@samueltardieu
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-strict-provenance Area: Strict provenance for raw pointers C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-tedious Call for participation: An issue involves lots of work and is better handled as many small tasks. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants