Skip to content

Commit ef03fda

Browse files
committed
Auto merge of rust-lang#106967 - saethlin:remove-vec-as-ptr-assume, r=thomcc
Remove the assume(!is_null) from Vec::as_ptr At a guess, this code is leftover from LLVM was worse at keeping track of the niche information here. In any case, we don't need this anymore: Removing this `assume` doesn't get rid of the `nonnull` attribute on the return type.
2 parents 84c47b8 + 738c8b0 commit ef03fda

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

library/alloc/src/vec/mod.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ use core::cmp::Ordering;
5959
use core::convert::TryFrom;
6060
use core::fmt;
6161
use core::hash::{Hash, Hasher};
62-
use core::intrinsics::assume;
6362
use core::iter;
6463
#[cfg(not(no_global_oom_handling))]
6564
use core::iter::FromIterator;
@@ -1240,11 +1239,7 @@ impl<T, A: Allocator> Vec<T, A> {
12401239
pub fn as_ptr(&self) -> *const T {
12411240
// We shadow the slice method of the same name to avoid going through
12421241
// `deref`, which creates an intermediate reference.
1243-
let ptr = self.buf.ptr();
1244-
unsafe {
1245-
assume(!ptr.is_null());
1246-
}
1247-
ptr
1242+
self.buf.ptr()
12481243
}
12491244

12501245
/// Returns an unsafe mutable pointer to the vector's buffer, or a dangling
@@ -1277,11 +1272,7 @@ impl<T, A: Allocator> Vec<T, A> {
12771272
pub fn as_mut_ptr(&mut self) -> *mut T {
12781273
// We shadow the slice method of the same name to avoid going through
12791274
// `deref_mut`, which creates an intermediate reference.
1280-
let ptr = self.buf.ptr();
1281-
unsafe {
1282-
assume(!ptr.is_null());
1283-
}
1284-
ptr
1275+
self.buf.ptr()
12851276
}
12861277

12871278
/// Returns a reference to the underlying allocator.

tests/codegen/vec-as-ptr.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile-flags: -O -Zmerge-functions=disabled
2+
3+
#![crate_type = "lib"]
4+
5+
// Test that even though we return a *const u8 not a &[u8] or a NonNull<u8>, LLVM knows that this
6+
// pointer is nonnull.
7+
// CHECK: nonnull {{i8\*|ptr}} @vec_as_ptr
8+
#[no_mangle]
9+
pub fn vec_as_ptr(v: &Vec<u8>) -> *const u8 {
10+
v.as_ptr()
11+
}
12+
13+
// Test that even though we return a *const u8 not a &[u8] or a NonNull<u8>, LLVM knows that this
14+
// pointer is nonnull.
15+
// CHECK: nonnull {{i8\*|ptr}} @vec_as_mut_ptr
16+
#[no_mangle]
17+
pub fn vec_as_mut_ptr(v: &mut Vec<u8>) -> *mut u8 {
18+
v.as_mut_ptr()
19+
}

0 commit comments

Comments
 (0)