Skip to content

Mark kj_rs::new_callbacks_promise_future unsafe - #8454

Merged
guybedford merged 1 commit into
mainfrom
gbedford/kj-promise-future-unsafe
Nov 13, 2026
Merged

guybedford merged 1 commit into
mainfrom
gbedford/kj-promise-future-unsafe

Conversation

@guybedford

Copy link
Copy Markdown
Member

This marks kj_rs::new_callbacks_promise_future as unsafe, since it adopts a raw promise node and trusts the caller's choice of result type.

The function is what a generated bridge calls to turn the KjPromiseNodeImpl returned by an async C++ function into a Rust future. It takes the descriptor's node pointer as an owned kj::_::PromiseNode and later has the unwrap callback write the result into a MaybeUninit<T>, for a T the caller picks. KjPromiseNodeImpl has public fields and is Copy, so safe code could hand it any pointer, the same node twice, or ask for the wrong T. Only the bridge macro calls it, always with a descriptor C++ just built from a real kj::Promise<T>, and that call already sits inside the macro's unsafe block.

  • new_callbacks_promise_future is now an unsafe fn with its contract documented: the node is uniquely owned and the callback is the one C++ made for it, producing a T.

No bridge or generated code changes. This adds //src/rust/cxx/kj-rs/compile-tests with a snippet checking that the call needs unsafe; the kj-rs promise tests cover the generated path.

The function takes ownership of the kj::_::PromiseNode that a
KjPromiseNodeImpl points to, and later has the descriptor's unwrap
callback write the promise's result into a MaybeUninit<T> for a T that
the caller chooses. KjPromiseNodeImpl has public fields and is Copy, so
as a safe function it accepted any pointer, the same node twice, or the
wrong T.

Its only caller is the bridge macro, which passes the descriptor that
C++ built from a kj::Promise<T> and already makes the call in an unsafe
block. Make the function unsafe and document that contract.
@guybedford
guybedford requested review from a team as code owners November 13, 2026 20:18

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found: the function is now unsafe with its contract documented, the existing generated call site already runs inside an unsafe block, and a compile test checks the new requirement.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

The unsafe marking matches the function's real preconditions (an owned raw PromiseNode and a result type the caller picks). The one generated call site in expand.rs sits inside the existing unsafe dispatch block. The new wd_rust_compile_test target and snippet check that a call without unsafe is rejected.


📝 Documentation: ✅ LGTM

The new doc comment and # Safety section describe the code as it is now and state the caller's obligations clearly.


Reviewed commit: 1bb4c892

@guybedford
guybedford merged commit 741cc9d into main Nov 13, 2026
23 of 27 checks passed
@guybedford
guybedford deleted the gbedford/kj-promise-future-unsafe branch November 13, 2026 22:23
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.

2 participants