Skip to content

Make unique_ptr::as_pin and virtual_unique_ptr::as_pin panic on null instead of returning Option. - #2901

Open
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1017592150
Open

copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1017592150

Conversation

@copybara-service

Copy link
Copy Markdown

Make unique_ptr::as_pin and virtual_unique_ptr::as_pin panic on null instead of returning Option.

With nonnull_smart_pointers, nullable C++ std::unique_ptrs bind in Rust as Nullable<unique_ptr<T>> (or Nullable<virtual_unique_ptr<T>>), while bare unique_ptr<T> and virtual_unique_ptr<T> represent pointers that are non-null unless moved-from or constructed via from_raw(null). Deref, DerefMut, as_mut, into_box, into_pin_box, and into_inner already panic on null; only as_pin returned Option<Pin<&mut T>>, forcing every !Unpin caller to unwrap a None that cannot occur.

Change unique_ptr::as_pin and virtual_unique_ptr::as_pin to return Pin<&mut T> directly and panic on null ("dereferencing a null unique_ptr" / "dereferencing a null virtual_unique_ptr"), matching as_mut and Deref. Callers that legitimately hold a possibly-null bare unique_ptr can check with unique_ptr::is_null first, or use Nullable<unique_ptr<T>>.

@copybara-service
copybara-service Bot force-pushed the test_1017592150 branch 7 times, most recently from 86708d2 to 2df034b Compare November 12, 2026 03:41
…ull instead of returning `Option`.

With `nonnull_smart_pointers`, nullable C++ `std::unique_ptr`s bind in Rust as `Nullable<unique_ptr<T>>` (or `Nullable<virtual_unique_ptr<T>>`), while bare `unique_ptr<T>` and `virtual_unique_ptr<T>` represent pointers that are non-null unless moved-from or constructed via `from_raw(null)`. `Deref`, `DerefMut`, `as_mut`, `into_box`, `into_pin_box`, and `into_inner` already panic on null; only `as_pin` returned `Option<Pin<&mut T>>`, forcing every `!Unpin` caller to unwrap a `None` that cannot occur.

Change `unique_ptr::as_pin` and `virtual_unique_ptr::as_pin` to return `Pin<&mut T>` directly and panic on null (`"dereferencing a null unique_ptr"` / `"dereferencing a null virtual_unique_ptr"`), matching `as_mut` and `Deref`. Callers that legitimately hold a possibly-null bare `unique_ptr` can check with `unique_ptr::is_null` first, or use `Nullable<unique_ptr<T>>`.

PiperOrigin-RevId: 1017592150

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants