Commit Graph

796 Commits

Author SHA1 Message Date
Jonathan Brouwer 4783d35218 Rollup merge of #154216 - airblast-dev:main, r=tgross35
unstably mark `NonNull::with_exposed_provenance` as const

Feature: `const_nonnull_with_exposed_provenance`
Tracking issue: https://github.com/rust-lang/rust/issues/154215

This PR constifies `NonNull::with_exposed_provenance`.

r? @tgross35
2026-03-25 19:52:58 +01:00
Theemathas Chirananthavat 1e4c1d6f75 Make PinCoerceUnsized require Deref
Also, delete impls on non-Deref types.

Pin doesn't do anything useful for non-Deref types, so PinCoerceUnsized
on such types makes no sense.

This is a breaking change, since stable code can observe the deleted
`PinCoerceUnsized` impls by uselessly coercing between such types
inside a `Pin`.

There is still some strange behavior, such as `Pin<&mut i32>` being
able to coerce to `Pin<&dyn Send>`, but not being able to coerce to
`Pin<&i32>`. However, I don't think it's possible to fix this.

Fixes https://github.com/rust-lang/rust/issues/145081
2026-03-25 11:40:27 +07:00
Tayfun Bocek 1223441926 Constify NonNull::with_exposed_provenance 2026-03-22 17:28:41 +03:00
Josh Stone 78157ddde9 Replace version placeholders with 1.95.0
(cherry picked from commit bad24ccbec)
2026-03-07 10:42:01 -08:00
Jonathan Brouwer 94923f231c Rollup merge of #152615 - RalfJung:null-not-valid-for-read-write, r=Mark-Simulacrum
refactor 'valid for read/write' definition: exclude null

This is an attempt to resolve https://github.com/rust-lang/rust/issues/138351.

The underlying problem is that when we decided to allow reads/writes/copies of size 0 even for null pointers, we documented that by changing the definition of "valid for read/write" in the standard library to say that null pointers are valid for 0-sized reads/writes. Unfortunately, that definition is also used in other places that assume that a valid-for-read/write pointer can be converted into a reference, and of course that's UB if the pointer is null, even if the pointee is a ZST.

The proposal for fixing this is to make "valid for reads/writes" slightly [weaker](https://faultlore.com/blah/tower-of-weakenings/) than it has to be, and require the pointer to be non-null, and then to add exceptions to the most basic functions (read/write/copy) to explicitly allow arbitrary pointers when the size is 0. This isn't pretty but it's the best solution that has been suggested so far I think.

Cc @rust-lang/opsem @rust-lang/libs-api
2026-02-28 12:52:54 +01:00
bors 3a70d0349f Auto merge of #152702 - oli-obk:nonnulltransmute, r=scottmcm
Prepare NonNull for pattern types



Pull out the changes that affect some tests, but do not require pattern types.

See https://github.com/rust-lang/rust/pull/136006#discussion_r2806740338 for what triggered this PR

r? @scottmcm
2026-02-27 13:04:46 +00:00
Ralf Jung 54e9a6a994 refactor 'valid for read/write' definition: exclude null 2026-02-25 11:43:56 +01:00
Oli Scherer 241cd7eeed Prepare NonNull for pattern types 2026-02-24 08:33:15 +00:00
Ralf Jung 2484cfe007 ptr::replace: make calls on ZST null ptr not UB 2026-02-19 08:39:03 +01:00
mu001999 73c42c1800 Remove or allow unused features in library doc and tests 2026-02-13 09:27:16 +08:00
Mark Rousskov 4a979d546b Stop having two different alignment constants
* Stop having two different alignment constants
* Update library/core/src/alloc/global.rs
2026-02-08 19:54:03 +00:00
Ralf Jung 6490e0ecb5 stabilize ptr_as_ref_unchecked 2026-02-02 19:47:32 +01:00
bors 905b926967 Auto merge of #151622 - scottmcm:elide-more-transmutes, r=cjgillot
GVN: Elide more intermediate transmutes

We already skipped intermediate steps like `u32` or `i32` that support any (initialized) value.

This extends that to also allow skipping intermediate steps whose values are a superset of either the source or destination type.  Most importantly, that means that `usize` → `NonZeroUsize` → `ptr::Alignment` and `ptr::Alignment` → `NonZeroUsize` → `usize` can skip the middle because `NonZeroUsize` is a superset of `Alignment`.

Then `Alignment::as_usize` is updated to take advantage of that and let us remove some more locals in a few places.

r? cjgillot
2026-01-31 20:42:37 +00:00
Stuart Cook 034b53c863 Rollup merge of #149110 - Paladynee:acp/ptr_cast_slice, r=jhpratt
Implement `cast_slice` for raw pointer types

Tracking issue: https://github.com/rust-lang/rust/issues/149103

This PR implements the method `cast_slice` that defers to the respective `slice_from_raw_parts` call for `*const T`, `*mut T` and `NonNull<T>`, and copies over their documentation.
```rust
impl<T> *const T {
    pub const fn cast_slice(self, len: usize) -> *const [T];
}
impl<T> *mut T {
    pub const fn cast_slice(self, len: usize) -> *mut [T];
}
impl<T> NonNull<T> {
    pub const fn cast_slice(self, len: usize) -> NonNull<[T]>;
}
```
2026-01-29 19:03:31 +11:00
Scott McMurray 9288c208a2 Adjust Alignment to emphasize that we don't look at its field 2026-01-25 17:24:45 -08:00
Scott McMurray 929e280973 Update ptr::Alignment to go through transmuting 2026-01-25 17:19:28 -08:00
Matthias Krüger 2da5959600 Rollup merge of #148764 - GrigorenkoPV:aligment_api, r=scottmcm
ptr_aligment_type: add more APIs

As per https://github.com/rust-lang/rust/issues/102070#issuecomment-1650043557

Tracking issue: rust-lang/rust#102070

Mostly duplicating methods that previously worked with `usize`-represented alignments.

Naming follows a convention of `align: usize`, `alignment: Alignment`.
2026-01-25 07:42:57 +01:00
Zachary S c3205f98dd Replace #[rustc_do_not_implement_via_object] with #[rustc_dyn_incompatible_trait], which makes the marked trait dyn-incompatible.
Removes the attribute from `MetaSized` and `PointeeSized`, with a special case in the trait solvers for `MetaSized`.

`dyn MetaSized` is a perfectly cromulent type, and seems to only have had #[rustc_do_not_implement_via_object] so the builtin object
candidate does not overlap with the builtin MetaSized impl that all `dyn` types get.
Resolves this with a special case by checking `is_sizedness_trait` where the trait solvers previously checked `implement_via_object`.

`dyn PointeeSized` alone is rejected for other reasons (since `dyn PointeeSized` is considered to have no principal trait because `PointeeSized`
is removed at an earlier stage of the compiler), but `(dyn PointeeSized + Send)` is valid and equivalent to `dyn Send`.

Add suggestions from code review

Update compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs and tests

Co-authored-by: lcnr <rust@lcnr.de>
2026-01-20 12:54:40 -06:00
Pavel Grigorenko bc0cce1595 ptr_aligment_type: add more APIs 2026-01-20 17:15:50 +03:00
Leon Schuermann b327e309b9 core: ptr: split_at_mut: fix typo in safety doc
Removes the double subject "it" in the safety documentation of
`core::ptr::split_at_mut` for raw slice pointers, as it does not refer
to anything.

Reported-by: Johnathan Van Why <jrvanwhy@betterbytes.org>
2026-01-12 03:21:16 -05:00
Martin Nordholts 8e3d60447c Finish transition from semitransparent to semiopaque for rustc_macro_transparency 2026-01-08 19:14:45 +01:00
Coca 7061adc5a4 Add diagnostic items for without_provenance and without_provenance_mut
Adds diagnostic items for `core::ptr::without_provenance` and `core::ptr::without_provenance_mut`. Will be used to enhance clippy lint `transmuting_null`, see https://github.com/rust-lang/rust-clippy/pull/16336.
2026-01-02 22:34:17 +00:00
Boxy Uwu 90a33f69f4 replace version placeholder 2025-12-19 15:04:30 -08:00
Alice Ryhl 78f52c1d12 Update tracking issue for PinCoerceUnsized 2025-12-17 20:26:41 +00:00
Theemathas Chirananthavat 9e4e9e4390 Use PointeeSized bound for TrivialClone impls
These `TrivialClone` impls previously had `?Sized` bounds, which are
different from the `PointeeSized` bounds on the corresponding
`Clone` and `Copy` impls. So, I've changed the `?Sized` bounds into
`PointeeSized` bounds.

This mistake was made presumably because the `TrivialClone` PR
(https://github.com/rust-lang/rust/pull/135634) was opened in Jan 2025,
but merged in Nov 2025. During that time, the sized hierachy PR
(https://github.com/rust-lang/rust/pull/137944) was opened in Mar 2025,
and merged in Jun 2025. The `TrivialClone` PR was not updated to account
for the sized hierachy changes.
2025-12-10 15:38:16 +07:00
Oli Scherer 9218298caa Add a diagnostic attribute for special casing const bound errors for non-const impls 2025-11-26 07:40:47 +00:00
Voxell Paladynee 055cd2717f ptr_cast_slice: add new methods to raw pointers 2025-11-20 11:27:22 +01:00
Ralf Jung 907fd85e16 const-eval: fix and re-enable pointer fragment support 2025-11-15 10:09:42 +01:00
joboet 16d2b5534e prevent TrivialClone implementations from appearing in rustdoc output 2025-11-09 22:26:15 +01:00
joboet 5fb5861765 (almost) get rid of the unsound #[rustc_unsafe_specialization_marker] on Copy, introduce TrivialClone 2025-11-09 15:51:25 +01:00
Gabriel Bjørnager Jensen fa8e8649ad Stabilise 'as_array' in '[_]' and '*const [_]'; Stabilise 'as_mut_array' in '[_]' and '*mut [_]'; Update feature gate and tracking issue for 'alloc_slice_into_array' items; 2025-11-07 15:14:41 +01:00
Matthias Krüger 252974a717 Rollup merge of #146187 - clarfonthey:const-drop-in-place, r=oli-obk
Unstably constify `ptr::drop_in_place` and related methods

Tracking: rust-lang/rust#109342
Supercedes: rust-lang/rust#145725

Makes methods const:

* `core::ptr::drop_in_place`
* `core::mem::ManuallyDrop::drop`
* `core::mem::MaybeUninit::assume_init_drop`
* `<[core::mem::MaybeUninit<_>]>::assume_init_drop`
* `<*mut _>::drop_in_place`
* `core::ptr::NonNull::drop_in_place`
2025-10-14 19:47:28 +02:00
Mark Rousskov 4e9716fbc5 Update CURRENT_RUSTC_VERSION post-bump 2025-09-26 18:41:32 -04:00
Stuart Cook 602841dc8c Rollup merge of #142315 - lolbinarycat:core-dedup-ptr-docs-139190-pt3, r=workingjubilee
core::ptr: deduplicate docs for as_ref, addr, and as_uninit_ref

also add INFO.md file explaining the purpose of the ptr/docs dir, and give some pointers (heh) to future maintainers.

follow up to rust-lang/rust#142101

part of rust-lang/rust#139190

r? `@workingjubilee`
2025-09-11 14:06:26 +10:00
binarycat d50a80e1d0 core::ptr: deduplicate docs for as_ref, addr, and as_uninit_ref
also add INFO.md file explaining the purpouse of the ptr/docs dir.
2025-09-10 11:00:08 -05:00
Ralf Jung aed0ed4c93 const-eval: disable pointer fragment support 2025-09-08 13:22:52 +02:00
ltdk 07d3e923ac Unstably constify ptr::drop_in_place and related methods 2025-09-05 17:28:22 -04:00
Stuart Cook b27c94af61 Rollup merge of #146136 - AudaciousAxiom:docs/missing-closing-code-block-fences, r=tgross35
docs(std): add missing closing code block fences in doc comments

This PR adds a few closing code block fences which I believe are missing in some doc comments. It seems that rustdoc just autocloses code blocks at the end of doc comments and thus these were easily overlooked: I do not think these code blocks are special in any way.

I found these when working on a Clippy lint that checks the last sentence of doc comments for terminal punctuation, and these were failing cases when testing against the std. Therefore I am not entirely sure these are all such cases, but still have high hopes that they are (or at least a well-defined subset of them).
2025-09-04 10:02:02 +10:00
Stuart Cook f4b946a147 Rollup merge of #145279 - clarfonthey:const-convert-initial, r=tgross35
Constify conversion traits (part 1)

This is the first part of rust-lang/rust#144289 being split into smaller pieces. It adds/moves constness of several traits under the `const_convert` feature:

* `From`
* `Into`
* `TryFrom`
* `TryInto`
* `FromStr`
* `AsRef`
* `AsMut`
* `Borrow`
* `BorrowMut`
* `Deref`
* `DerefMut`

There are a few methods that are intrinsically tied to these traits which I've included in the feature. Particularly, those which are wrappers over `AsRef`:

* `ByteStr::new` (unstable under `bstr` feature)
* `OsStr::new`
* `Path::new`

Those which directly use `Into`:

* `Result::into_ok`
* `Result::into_err`

And those which use `Deref` and `DerefMut`:

* `Pin::as_ref`
* `Pin::as_mut`
* `Pin::as_deref_mut`
* `Option::as_deref`
* `Option::as_deref_mut`
* `Result::as_deref`
* `Result::as_deref_mut`

(note: the `Option` and `Result` methods were suggested by ``@npmccallum`` initially as rust-lang/rust#146101)

The parts which are missing from this PR are:

* Anything that involves heap-allocated types
* Making any method const than the ones listed above
* Anything that could rely on the above, *or* could rely on system-specific code for `OsStr` or `Path` (note: this mostly makes these methods useless since `str` doesn't implement `AsRef<OsStr>` yet, but it's better to track the method for now and add impls later, IMHO)

r? ``@tgross35`` (who mostly already reviewed this)
2025-09-03 23:08:06 +10:00
AudaciousAxiom 5a9e2e4aa8 docs(std): add missing closing code block fences in doc comments 2025-09-02 22:11:29 +02:00
ltdk 1c64d3e6d1 Constify conversion traits 2025-09-01 21:38:26 -04:00
gonzalobg f29327ff60 Clarify that align_offset overaligns
The current documentation is not clear whether adding `a` to a pointer overaligns (align up) or underaligns (align down).

It should say this explicitly.
2025-08-28 11:07:38 -07:00
Matthias Krüger 0c02bdc901 Rollup merge of #143341 - Manishearth:from-raw-parts-ptr-cast, r=samueltardieu
Mention that casting to *const () is a way to roundtrip with from_raw_parts

See discussion on rust-lang/rust#81513
2025-08-27 07:45:54 +02:00
Urgau f25bf37f1f Allow integer_to_ptr_transmutes in core 2025-08-24 00:03:54 +02:00
bors 99ba556567 Auto merge of #144081 - RalfJung:const-ptr-fragments, r=oli-obk
const-eval: full support for pointer fragments

This fixes https://github.com/rust-lang/const-eval/issues/72 and makes `swap_nonoverlapping` fully work in const-eval by enhancing per-byte provenance tracking with tracking of *which* of the bytes of the pointer this one is. Later, if we see all the same bytes in the exact same order, we can treat it like a whole pointer again without ever risking a leak of the data bytes (that encode the offset into the allocation). This lifts the limitation that was discussed quite a bit in https://github.com/rust-lang/rust/pull/137280.

For a concrete piece of code that used to fail and now works properly consider this example doing a byte-for-byte memcpy in const without using intrinsics:
```rust
use std::{mem::{self, MaybeUninit}, ptr};

type Byte = MaybeUninit<u8>;

const unsafe fn memcpy(dst: *mut Byte, src: *const Byte, n: usize) {
    let mut i = 0;
    while i < n {
        *dst.add(i) = *src.add(i);
        i += 1;
    }
}

const _MEMCPY: () = unsafe {
    let ptr = &42;
    let mut ptr2 = ptr::null::<i32>();
    // Copy from ptr to ptr2.
    memcpy(&mut ptr2 as *mut _ as *mut _, &ptr as *const _ as *const _, mem::size_of::<&i32>());
    assert!(*ptr2 == 42);
};
```
What makes this code tricky is that pointers are "opaque blobs" in const-eval, we cannot just let people look at the individual bytes since *we don't know what those bytes look like* -- that depends on the absolute address the pointed-to object will be placed at. The code above "breaks apart" a pointer into individual bytes, and then puts them back together in the same order elsewhere. This PR implements the logic to properly track how those individual bytes relate to the original pointer, and to recognize when they are in the right order again.

We still reject constants where the final value contains a not-fully-put-together pointer: I have no idea how one could construct an LLVM global where one byte is defined as "the 3rd byte of a pointer to that other global over there" -- and even if LLVM supports this somehow, we can leave implementing that to a future PR. It seems unlikely to me anyone would even want this, but who knows.^^

This also changes the behavior of Miri, by tracking the order of bytes with provenance and only considering a pointer to have valid provenance if all bytes are in the original order again. This is related to https://github.com/rust-lang/unsafe-code-guidelines/issues/558. It means one cannot implement XOR linked lists with strict provenance any more, which is however only of theoretical interest. Practically I am curious if anyone will show up with any code that Miri now complains about - that would be interesting data. Cc `@rust-lang/opsem`
2025-08-17 04:33:31 +00:00
Kivooeo 5ee2224daa stabilize const exposed provenance 2025-08-15 16:19:19 +00:00
Guillaume Gomez 31d8277abe Rollup merge of #144515 - scottmcm:ptr_cast_array, r=Mark-Simulacrum
Implement `ptr_cast_array`

ACP: https://github.com/rust-lang/libs-team/issues/602
Tracking Issue: https://github.com/rust-lang/rust/issues/144514
2025-08-14 11:39:34 +02:00
Jakub Beránek 8cb78fe3b6 Rollup merge of #145325 - clarfonthey:cast-init, r=scottmcm
Add `cast_init` and `cast_uninit` methods for pointers

ACP: rust-lang/libs-team#627
Tracking issue: rust-lang/rust#145036

This includes an incredibly low-effort search to find uses that could be switched to using these methods. I only searched for `cast::<\w>` and `cast::<MaybeUninit` because there would otherwise be way too much to look through, and I also didn't modify anything inside submodules/subtrees.
2025-08-13 07:03:52 +02:00
ltdk d6945f6d8c Add cast_init and cast_uninit methods for pointers 2025-08-12 16:57:56 -04:00
Sacha Ayoun 427be23b22 Address dangling doc
Signed-off-by: Sacha Ayoun <sachaayoun@gmail.com>
2025-08-12 13:18:48 +01:00