Skip to content

Commit 3962bfd

Browse files
Rollup merge of rust-lang#110921 - JohnBobbo96:rustc_arena_nonnull, r=Nilstrieb
Use `NonNull::new_unchecked` and `NonNull::len` in `rustc_arena`. This avoids a few extra dereferences as well as an `unwrap`. According to the docs for [`NonNull::len`](https://doc.rust-lang.org/std/ptr/struct.NonNull.html#method.len) this also ensures that: > This function is safe, even when the non-null raw slice cannot be dereferenced to a slice because the pointer does not have a valid address. I am also fairly sure that the `unwrap` is unneeded in this case, as the docs for [`Box::into_raw`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.into_raw) also state: > Consumes the Box, returning a wrapped raw pointer. **The pointer will be properly aligned and non-null.**
2 parents 48413b8 + 618841b commit 3962bfd

File tree

1 file changed

+5
-5
lines changed
  • compiler/rustc_arena/src

1 file changed

+5
-5
lines changed

compiler/rustc_arena/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<T> ArenaChunk<T> {
7474
#[inline]
7575
unsafe fn new(capacity: usize) -> ArenaChunk<T> {
7676
ArenaChunk {
77-
storage: NonNull::new(Box::into_raw(Box::new_uninit_slice(capacity))).unwrap(),
77+
storage: NonNull::new_unchecked(Box::into_raw(Box::new_uninit_slice(capacity))),
7878
entries: 0,
7979
}
8080
}
@@ -85,7 +85,7 @@ impl<T> ArenaChunk<T> {
8585
// The branch on needs_drop() is an -O1 performance optimization.
8686
// Without the branch, dropping TypedArena<u8> takes linear time.
8787
if mem::needs_drop::<T>() {
88-
let slice = &mut *(self.storage.as_mut());
88+
let slice = self.storage.as_mut();
8989
ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(&mut slice[..len]));
9090
}
9191
}
@@ -104,7 +104,7 @@ impl<T> ArenaChunk<T> {
104104
// A pointer as large as possible for zero-sized elements.
105105
ptr::invalid_mut(!0)
106106
} else {
107-
self.start().add((*self.storage.as_ptr()).len())
107+
self.start().add(self.storage.len())
108108
}
109109
}
110110
}
@@ -288,7 +288,7 @@ impl<T> TypedArena<T> {
288288
// If the previous chunk's len is less than HUGE_PAGE
289289
// bytes, then this chunk will be least double the previous
290290
// chunk's size.
291-
new_cap = (*last_chunk.storage.as_ptr()).len().min(HUGE_PAGE / elem_size / 2);
291+
new_cap = last_chunk.storage.len().min(HUGE_PAGE / elem_size / 2);
292292
new_cap *= 2;
293293
} else {
294294
new_cap = PAGE / elem_size;
@@ -396,7 +396,7 @@ impl DroplessArena {
396396
// If the previous chunk's len is less than HUGE_PAGE
397397
// bytes, then this chunk will be least double the previous
398398
// chunk's size.
399-
new_cap = (*last_chunk.storage.as_ptr()).len().min(HUGE_PAGE / 2);
399+
new_cap = last_chunk.storage.len().min(HUGE_PAGE / 2);
400400
new_cap *= 2;
401401
} else {
402402
new_cap = PAGE;

0 commit comments

Comments
 (0)