-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use NonNull::new_unchecked
and NonNull::len
in rustc_arena
.
#110921
Conversation
`rustc_arena`.
r? @b-naber (rustbot has picked a reviewer for you, use r? to override) |
@@ -74,7 +74,7 @@ impl<T> ArenaChunk<T> { | |||
#[inline] | |||
unsafe fn new(capacity: usize) -> ArenaChunk<T> { | |||
ArenaChunk { | |||
storage: NonNull::new(Box::into_raw(Box::new_uninit_slice(capacity))).unwrap(), | |||
storage: NonNull::new_unchecked(Box::into_raw(Box::new_uninit_slice(capacity))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very nice example of why unsafe_op_in_unsafe_fn
not being on by default is a big mistake. The safety condition of the function (which I actually don't know because it's not documented :/) has nothing to do with the safety condition of new_umchecked
.
I'd love to take a PR denying that lint on rustc_arena if you want to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i have created a PR for this here #110950.
r? Nilstrieb |
@@ -74,7 +74,7 @@ impl<T> ArenaChunk<T> { | |||
#[inline] | |||
unsafe fn new(capacity: usize) -> ArenaChunk<T> { | |||
ArenaChunk { | |||
storage: NonNull::new(Box::into_raw(Box::new_uninit_slice(capacity))).unwrap(), | |||
storage: NonNull::new_unchecked(Box::into_raw(Box::new_uninit_slice(capacity))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
storage: NonNull::new_unchecked(Box::into_raw(Box::new_uninit_slice(capacity))), | |
storage: NonNull::from(Box::leak(Box::new_uninit_slice(capacity))), |
…=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.**
⌛ Testing commit 618841b with merge d1500bb15183239bf2d3423529fe31e86ada738d... |
💔 Test failed - checks-actions |
It failed to install awscli, seems spurious. |
⌛ Testing commit 618841b with merge 332e1cbb35ef7e9287e50b458751d98390f9612c... |
💔 Test failed - checks-actions |
@bors treeclosed=50 |
@bors retry python setuptools issue |
…=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.**
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#110877 (Provide better type hints when a type doesn't support a binary operator) - rust-lang#110917 (only error combining +whole-archive and +bundle for rlibs) - rust-lang#110921 (Use `NonNull::new_unchecked` and `NonNull::len` in `rustc_arena`.) - rust-lang#110927 (Encoder/decoder cleanups) - rust-lang#110944 (share BinOp::Offset between CTFE and Miri) - rust-lang#110948 (run-make test: using single quotes to not trigger the shell) - rust-lang#110957 (Fix an ICE in conflict error diagnostics) - rust-lang#110960 (fix false negative for `unused_mut`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
This avoids a few extra dereferences as well as an
unwrap
.According to the docs for
NonNull::len
this also ensures that:I am also fairly sure that the
unwrap
is unneeded in this case, as the docs forBox::into_raw
also state: