Skip to content

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

Closed
@scottmcm

Description

@scottmcm

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.

Metadata

Metadata

Assignees

Labels

A-strict-provenanceArea: Strict provenance for raw pointersC-cleanupCategory: PRs that clean code up or issues documenting cleanup.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-tediousCall for participation: An issue involves lots of work and is better handled as many small tasks.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions