diff options
| author | bors <bors@rust-lang.org> | 2024-02-21 21:48:38 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-02-21 21:48:38 +0000 |
| commit | 3406ada96f8e16e49e947a91db3eba0db45245fa (patch) | |
| tree | a587fbd99e1127cc7e7fb4690c05323cdb6e6ff1 /library/alloc | |
| parent | d7bd9cd469ff6871420007f091ef52fc32d2ca99 (diff) | |
| parent | b58f647d5488dce73bba517907c44af2c2a618c4 (diff) | |
Auto merge of #117658 - RalfJung:ptr-dangling, r=m-ou-se
rename ptr::invalid -> ptr::without_provenance It has long bothered me that `ptr::invalid` returns a pointer that is actually valid for zero-sized memory accesses. In general, it doesn't even make sense to ask "is this pointer valid", you have to ask "is this pointer valid for a given memory access". We could say that a pointer is invalid if it is not valid for *any* memory access, but [the way this FCP is going](https://github.com/rust-lang/unsafe-code-guidelines/issues/472), it looks like *all* pointers will be valid for zero-sized memory accesses. Two possible alternative names emerged as people's favorites: 1. Something involving `dangling`, in analogy to `NonNull::dangling`. To avoid inconsistency with the `NonNull` method, the address-taking method could be called `dangling_at(addr: usize) -> *const T`. 2. `without_provenance`, to be symmetric with the inverse operation `ptr.addr_without_provenance()` (currently still called `ptr.addr()` but probably going to be renamed) I have no idea which one of these is better. I read [this comment](https://github.com/rust-lang/rust/pull/117658#issuecomment-1830934701) as expressing a slight preference for something like the second option, so I went for that. I'm happy to go with `dangling_at` as well. Cc `@rust-lang/opsem`
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/rc.rs | 8 | ||||
| -rw-r--r-- | library/alloc/src/sync.rs | 8 | ||||
| -rw-r--r-- | library/alloc/tests/fmt.rs | 14 | ||||
| -rw-r--r-- | library/alloc/tests/vec.rs | 2 |
4 files changed, 20 insertions, 12 deletions
diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 2cc38d90ffe..c3d0019be39 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -2804,7 +2804,9 @@ impl<T> Weak<T> { #[must_use] pub const fn new() -> Weak<T> { Weak { - ptr: unsafe { NonNull::new_unchecked(ptr::invalid_mut::<RcBox<T>>(usize::MAX)) }, + ptr: unsafe { + NonNull::new_unchecked(ptr::without_provenance_mut::<RcBox<T>>(usize::MAX)) + }, alloc: Global, } } @@ -2829,7 +2831,9 @@ impl<T, A: Allocator> Weak<T, A> { #[unstable(feature = "allocator_api", issue = "32838")] pub fn new_in(alloc: A) -> Weak<T, A> { Weak { - ptr: unsafe { NonNull::new_unchecked(ptr::invalid_mut::<RcBox<T>>(usize::MAX)) }, + ptr: unsafe { + NonNull::new_unchecked(ptr::without_provenance_mut::<RcBox<T>>(usize::MAX)) + }, alloc, } } diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index e1211da4c61..524aa35e045 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -2555,7 +2555,9 @@ impl<T> Weak<T> { #[must_use] pub const fn new() -> Weak<T> { Weak { - ptr: unsafe { NonNull::new_unchecked(ptr::invalid_mut::<ArcInner<T>>(usize::MAX)) }, + ptr: unsafe { + NonNull::new_unchecked(ptr::without_provenance_mut::<ArcInner<T>>(usize::MAX)) + }, alloc: Global, } } @@ -2583,7 +2585,9 @@ impl<T, A: Allocator> Weak<T, A> { #[unstable(feature = "allocator_api", issue = "32838")] pub fn new_in(alloc: A) -> Weak<T, A> { Weak { - ptr: unsafe { NonNull::new_unchecked(ptr::invalid_mut::<ArcInner<T>>(usize::MAX)) }, + ptr: unsafe { + NonNull::new_unchecked(ptr::without_provenance_mut::<ArcInner<T>>(usize::MAX)) + }, alloc, } } diff --git a/library/alloc/tests/fmt.rs b/library/alloc/tests/fmt.rs index 04da95bbb83..379e09ab69a 100644 --- a/library/alloc/tests/fmt.rs +++ b/library/alloc/tests/fmt.rs @@ -77,14 +77,14 @@ fn test_format_macro_interface() { t!(format!("{}", "foo"), "foo"); t!(format!("{}", "foo".to_string()), "foo"); if cfg!(target_pointer_width = "32") { - t!(format!("{:#p}", ptr::invalid::<isize>(0x1234)), "0x00001234"); - t!(format!("{:#p}", ptr::invalid_mut::<isize>(0x1234)), "0x00001234"); + t!(format!("{:#p}", ptr::without_provenance::<isize>(0x1234)), "0x00001234"); + t!(format!("{:#p}", ptr::without_provenance_mut::<isize>(0x1234)), "0x00001234"); } else { - t!(format!("{:#p}", ptr::invalid::<isize>(0x1234)), "0x0000000000001234"); - t!(format!("{:#p}", ptr::invalid_mut::<isize>(0x1234)), "0x0000000000001234"); + t!(format!("{:#p}", ptr::without_provenance::<isize>(0x1234)), "0x0000000000001234"); + t!(format!("{:#p}", ptr::without_provenance_mut::<isize>(0x1234)), "0x0000000000001234"); } - t!(format!("{:p}", ptr::invalid::<isize>(0x1234)), "0x1234"); - t!(format!("{:p}", ptr::invalid_mut::<isize>(0x1234)), "0x1234"); + t!(format!("{:p}", ptr::without_provenance::<isize>(0x1234)), "0x1234"); + t!(format!("{:p}", ptr::without_provenance_mut::<isize>(0x1234)), "0x1234"); t!(format!("{A:x}"), "aloha"); t!(format!("{B:X}"), "adios"); t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃"); @@ -208,7 +208,7 @@ fn test_format_macro_interface() { { let val = usize::MAX; let exp = format!("{val:#x}"); - t!(format!("{:p}", std::ptr::invalid::<isize>(val)), exp); + t!(format!("{:p}", std::ptr::without_provenance::<isize>(val)), exp); } // Escaping diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index 04bb20e96b7..15ee4d65205 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -2575,7 +2575,7 @@ fn test_box_zero_allocator() { assert!(state.0.insert(addr)); state.1 += 1; std::println!("allocating {addr}"); - std::ptr::invalid_mut(addr) + std::ptr::without_provenance_mut(addr) } else { unsafe { std::alloc::alloc(layout) } }; |
