From 5302c9698712a83c6cf80b0b4c91a9ae464609ef Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 17:17:00 -0800 Subject: [PATCH 01/30] chore(quest): claim quest/m1/ffi-export/checks-raise.md Quest-Claim: ed1ecb7545259280 From eda9dacd66c68bb58ff4bdf49b4a206aff32309b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 19:42:48 -0800 Subject: [PATCH 02/30] wip(ffi): failed run-time checks raise the language's error Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/jni.rs | 21 ++ ffi/runtime/python.rs | 22 +++ ffi/runtime/rt.go | 29 +++ ffi/runtime/rt.rs | 300 +++++++++++++++++++++------- ffi/runtime/rt.swift | 44 ++++- ffi/src/abi.rs | 117 ++++++++++- ffi/src/c.rs | 21 +- ffi/src/cpp.rs | 45 +++-- ffi/src/dart.rs | 100 ++++++++-- ffi/src/glue.rs | 340 ++++++++++++++++++++++++++------ ffi/src/go.rs | 109 ++++++++-- ffi/src/ir.rs | 22 +++ ffi/src/kotlin.rs | 43 ++-- ffi/src/python.rs | 44 ++++- ffi/src/swift.rs | 143 +++++++++++--- ffi/tests/api.c | 448 +++++++++++++++++++++--------------------- ffi/tests/api.go | 18 +- ffi/tests/api.swift | 270 ++++++++++++------------- ffi/tests/fixtures.rs | 14 +- 19 files changed, 1553 insertions(+), 597 deletions(-) diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index bebc2d8ac..37ec35625 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -92,6 +92,27 @@ pub fn fail(env: *mut JNIEnv, what: &str, msg: &str) -> ! { rt::fail(what, msg) } +/// Throw the `IllegalStateException` a call of `what` raises once its check failed with +/// `status`, returning a zeroed value Kotlin never reads, as JNI's pending exception +/// throws once the native method returns. +#[cold] +pub fn raise(env: *mut JNIEnv, status: i32, what: &str) -> R { + let message = match rt::Status::of(status) { + Some(status) => format!("{what}: {}", status.message()), + None => format!("{what}: an unknown status {status}"), + }; + let message = std::ffi::CString::new(message).expect("no NUL in a message"); + // SAFETY: `env` is this thread's, and the class a local reference while it throws. + unsafe { + let class = ((**env).v1_2.FindClass)(env, c"java/lang/IllegalStateException".as_ptr()); + if class.is_null() { + fail(env, what, "IllegalStateException is missing"); + } + ((**env).v1_2.ThrowNew)(env, class, message.as_ptr()); + std::mem::zeroed() + } +} + /// Run `f` calling into Kotlin on this thread, freeing the local references it makes, and /// abort if what it calls throws: Rust has nowhere to unwind it to. pub fn upcall(what: &str, f: impl FnOnce(*mut JNIEnv) -> R) -> R { diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 7f39cf902..15b8a7d96 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -397,6 +397,28 @@ impl Scope { unsafe { close(held.ptr().cast()) }; } } + self.given(); + } + + /// Raise for the call of `what` unless its `status` is zero: a run-time check failed, + /// so Rust moved nothing out, though it took and dropped what it was given. + pub fn status(&mut self, status: i32, what: &str) -> R<()> { + if status == 0 { + return Ok(()); + } + self.given(); + let message = format!("{what}: {}", rt::Status::text(status).to_string_lossy()); + // A value moved out or outlived is as a closed one, and any other check is a misuse. + // SAFETY: static exception types. + let exc = match rt::Status::of(status) { + Some(rt::Status::Moved | rt::Status::Outlived) => unsafe { PyExc_ValueError }, + _ => unsafe { PyExc_RuntimeError }, + }; + raise(exc, &message) + } + + /// Note that what the call was given is Rust's. + fn given(&mut self) { while let Some(given) = self.given.pop() { given.into_ptr(); } diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index 97883c97c..afc8dacb2 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -17,6 +17,35 @@ const ( // once that callback returned. var ErrClosed = errors.New("closed") +// ErrBorrowed is what a call returns, wrapped with the function's name, once Rust's rules +// refuse a borrow it makes: of an object another call or future borrows in a way the +// borrow checker forbids beside it, or of one that is not Sync from a second thread. +var ErrBorrowed = errors.New("borrowed") + +// checkError_ is what a call returns once Rust refuses it before running: its status, or 0 +// for an item not on the library's target, which matches errors.ErrUnsupported. +type checkError_ struct { + what string + message string + status int32 +} + +func (e checkError_) Error() string { + return e.what + ": " + e.message +} + +func (e checkError_) Is(target error) bool { + switch e.status { + case 0: + return target == errors.ErrUnsupported + // Moved out, or lent from a handle that since changed: closed either way. + case 1, 6: + return target == ErrClosed + default: + return target == ErrBorrowed + } +} + type closedError_ struct { name string } diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index 4c52657ad..15a37fab4 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -1,9 +1,11 @@ //! The runtime every export shares: buffers, handles, lent functions, wakers, and futures //! across the C ABI. rust2-ffi copies it beside the generated code. //! -//! A broken contract across the ABI (a null or consumed handle, a handle borrowed in a -//! way Rust's rules forbid, invalid UTF-8, a future polled after it finished) aborts with -//! a message naming the C function: there is no Rust caller to return an error to. +//! A handle borrowed or moved in a way Rust's rules forbid fails the call with a +//! [`Status`], before any Rust code runs, which a binding raises as its language's own +//! error. Any other broken contract across the ABI (a null handle, invalid UTF-8, a future +//! polled after it finished) aborts with a message naming the C function: no binding +//! makes one, and there is no Rust caller to return an error to. use std::cell::UnsafeCell; use std::ffi::c_void; @@ -21,6 +23,95 @@ pub fn fail(what: &str, msg: &str) -> ! { std::process::abort() } +/// Why a call's check of a handle failed: a borrow or move Rust's borrow checker would +/// refuse. The call returns before any Rust code runs, so nothing changed. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[repr(i32)] +pub enum Status { + /// The handle's value was moved out, or the handle closed. + Moved = 1, + /// Another call or future borrows the value exclusively. + Exclusive = 2, + /// Another call or future borrows the value, so it cannot be borrowed exclusively or + /// moved out. + Borrowed = 3, + /// A value that is not `Sync` is borrowed on another thread. + Thread = 4, + /// A handle a call lent is borrowed exclusively or moved out. + Lent = 5, + /// A handle lent from another outlived it. + Outlived = 6, +} + +impl Status { + /// Every status, by its number less one. + pub const ALL: [Self; 6] = [ + Self::Moved, + Self::Exclusive, + Self::Borrowed, + Self::Thread, + Self::Lent, + Self::Outlived, + ]; + + /// What broke, as a binding's error says it after the function's name. + pub fn message(self) -> &'static str { + self.c_message().to_str().expect("ASCII") + } + + fn c_message(self) -> &'static std::ffi::CStr { + match self { + Self::Moved => c"a handle's value was moved out", + Self::Exclusive => c"a handle is borrowed exclusively", + Self::Borrowed => c"a handle is already borrowed", + Self::Thread => c"a handle is borrowed on another thread", + Self::Lent => c"a handle a call lent is borrowed exclusively or moved out", + Self::Outlived => { + c"a handle lent from another outlived it: the other was borrowed exclusively, moved out, or freed" + } + } + } + + /// The message of the status numbered `n`, for `_status_message`. + pub fn text(n: i32) -> &'static std::ffi::CStr { + Self::of(n).map_or(c"an unknown status", Self::c_message) + } + + /// The status numbered `n`, as a function wrote it. + pub fn of(n: i32) -> Option { + Self::ALL.get(usize::try_from(n).ok()?.checked_sub(1)?).copied() + } + + /// Abort, naming the function `what`, where no caller can take the status. + #[cold] + pub fn fail(self, what: &str) -> ! { + fail(what, self.message()) + } +} + +/// The value of a check, or an abort naming `what` where no caller can take its status. +pub fn check(checked: Result, what: &str) -> T { + checked.unwrap_or_else(|status| status.fail(what)) +} + +/// Fail a C function with `status`: write it through the function's `out`, returning a +/// zeroed value the caller ignores, or abort naming `what` if `out` is null, as a C caller +/// that takes no status passes. +/// +/// # Safety +/// `out` is null or points where an `int32_t` goes, and a zeroed `R` is a valid value. +#[cold] +pub unsafe fn failed(out: *mut i32, status: Status, what: &str) -> R { + if out.is_null() { + status.fail(what); + } + // SAFETY: the caller's contract. + unsafe { + out.write(status as i32); + std::mem::zeroed() + } +} + /// `s` for as long as the program runs, for a parameter that must be `'static`: each /// distinct string is leaked once and lent to every later call with equal content, so the /// strings held grow only with the distinct ones passed. @@ -486,8 +577,8 @@ pub struct Object { /// The value a lent handle points at, where Rust lent it, or null if it owns `value`. lent: *mut T, value: UnsafeCell>, - /// Bumped each time the value is borrowed exclusively or moved out, or the handle is - /// freed, so no handle lent from the value outlives any of them. + /// Bumped each time the value is changed through an exclusive borrow or moved out, or + /// the handle is freed, so no handle lent from the value outlives any of them. epoch: AtomicUsize, /// For a handle a call returned lent from another's value, that handle. lender: Option, @@ -505,24 +596,22 @@ pub struct Lender { /// A handle another is lent from. trait Lends: Send + Sync { /// Borrow the value shared for a use of a handle lent from it, if still at `epoch`. - fn hold(&self, epoch: usize, local: bool, what: &str); + fn hold(&self, epoch: usize, local: bool) -> Result<(), Status>; /// End what [`Self::hold`] took. fn unhold(&self, local: bool); } impl Lends for Object { - fn hold(&self, epoch: usize, local: bool, what: &str) { + fn hold(&self, epoch: usize, local: bool) -> Result<(), Status> { match local { - true => self.acquire_local(what), - false => self.acquire(what), + true => self.acquire_local()?, + false => self.acquire()?, } if self.epoch.load(Ordering::Acquire) != epoch { self.unhold(local); - fail( - what, - "a handle lent from another outlived it: the other was borrowed exclusively, moved out, or freed", - ); + return Err(Status::Outlived); } + Ok(()) } fn unhold(&self, local: bool) { @@ -570,7 +659,7 @@ impl Object { /// A handle to `value`, which a call returned lent from `lender`'s value: shared, as a /// returned `&T` is, and borrowed as its own type is, until that value is borrowed - /// exclusively, moved out, or freed, after which a use aborts. It is never borrowed + /// exclusively, moved out, or freed, after which a use fails. It is never borrowed /// exclusively nor moved out itself. pub fn lent(value: &T, lender: Lender) -> *const c_void { Arc::into_raw(Arc::new(Self { @@ -638,12 +727,12 @@ impl Object { unsafe { &*handle.cast::() } } - fn acquire(&self, what: &str) { + fn acquire(&self) -> Result<(), Status> { let mut state = self.state.load(Ordering::Relaxed); loop { match state { - TAKEN => fail(what, "a handle's value was moved out"), - EXCLUSIVE => fail(what, "a handle is borrowed exclusively"), + TAKEN => return Err(Status::Moved), + EXCLUSIVE => return Err(Status::Exclusive), _ => {} } match self @@ -654,7 +743,9 @@ impl Object { Err(now) => state = now, } } - self.hold_lender(what); + self.hold_lender().inspect_err(|_| { + self.state.fetch_sub(1, Ordering::Release); + }) } /// End a shared borrow [`Self::acquire`] took. @@ -665,23 +756,28 @@ impl Object { /// Borrow a value that is not `Sync` shared: any number of times on one thread, as /// Rust allows, and on no other thread meanwhile. - fn acquire_local(&self, what: &str) { + fn acquire_local(&self) -> Result<(), Status> { let me = thread(); match self.state.compare_exchange(0, me, Ordering::Acquire, Ordering::Relaxed) { Ok(_) => {} Err(owner) if owner == me => {} - Err(TAKEN) => fail(what, "a handle's value was moved out"), - Err(EXCLUSIVE) => fail(what, "a handle is borrowed exclusively"), - Err(_) => fail(what, "a handle is borrowed on another thread"), + Err(TAKEN) => return Err(Status::Moved), + Err(EXCLUSIVE) => return Err(Status::Exclusive), + Err(_) => return Err(Status::Thread), } // SAFETY: only the thread in `state` touches `depth`. unsafe { *self.depth.get() += 1 }; - self.hold_lender(what); + self.hold_lender().inspect_err(|_| self.unlocal()) } /// End a shared borrow [`Self::acquire_local`] took, on its thread. fn release_local(&self) { self.release_lender(); + self.unlocal(); + } + + /// Count down this thread's borrows, letting go of the value at the last. + fn unlocal(&self) { // SAFETY: this thread is the one in `state`, so `depth` is its own. let depth = unsafe { &mut *self.depth.get() }; *depth -= 1; @@ -691,9 +787,10 @@ impl Object { } /// For a handle lent from another, hold that one for a use of this one. - fn hold_lender(&self, what: &str) { - if let Some(lender) = &self.lender { - lender.handle.hold(lender.epoch, lender.local, what); + fn hold_lender(&self) -> Result<(), Status> { + match &self.lender { + Some(lender) => lender.handle.hold(lender.epoch, lender.local), + None => Ok(()), } } @@ -703,34 +800,30 @@ impl Object { } } - fn acquire_mut(&self, what: &str) { - if self.lender.is_some() { - fail(what, "a handle a call lent shared is borrowed exclusively"); + /// Take `state` from 0 to `to`, as an exclusive borrow or a move does. + fn acquire_from_zero(&self, to: usize) -> Result<(), Status> { + if self.lender.is_some() || (to == TAKEN && !self.lent.is_null()) { + return Err(Status::Lent); } - if let Err(state) = self - .state - .compare_exchange(0, EXCLUSIVE, Ordering::Acquire, Ordering::Relaxed) - { - match state { - TAKEN => fail(what, "a handle's value was moved out"), - _ => fail(what, "a handle is already borrowed"), - } + match self.state.compare_exchange(0, to, Ordering::Acquire, Ordering::Relaxed) { + Ok(_) => Ok(()), + Err(TAKEN) => Err(Status::Moved), + Err(_) => Err(Status::Borrowed), } - self.epoch.fetch_add(1, Ordering::Release); } /// Borrow the value shared for a call. /// /// # Safety /// `handle` is a live handle of this type, kept alive through the borrow. - pub unsafe fn borrow<'a>(handle: *const c_void, what: &str) -> Shared<'a, T> + pub unsafe fn borrow<'a>(handle: *const c_void, what: &str) -> Result, Status> where T: Sync, { // SAFETY: the caller's contract. let object = unsafe { Self::get(handle, what) }; - object.acquire(what); - Shared { object } + object.acquire()?; + Ok(Shared { object }) } /// Borrow a value that is not `Sync` shared for a call: any number of times on one @@ -738,47 +831,54 @@ impl Object { /// /// # Safety /// As [`Self::borrow`]. - pub unsafe fn borrow_local<'a>(handle: *const c_void, what: &str) -> Local<'a, T> { + pub unsafe fn borrow_local<'a>(handle: *const c_void, what: &str) -> Result, Status> { // SAFETY: the caller's contract. let object = unsafe { Self::get(handle, what) }; - object.acquire_local(what); - Local { object } + object.acquire_local()?; + Ok(Local { object }) } /// Borrow the value exclusively for a call. /// /// # Safety /// As [`Self::borrow`]. - pub unsafe fn borrow_mut<'a>(handle: *const c_void, what: &str) -> Exclusive<'a, T> { + pub unsafe fn borrow_mut<'a>(handle: *const c_void, what: &str) -> Result, Status> { // SAFETY: the caller's contract. let object = unsafe { Self::get(handle, what) }; - object.acquire_mut(what); - Exclusive { object } + object.acquire_from_zero(EXCLUSIVE)?; + Ok(Exclusive { object, changed: false }) } /// Borrow the value shared for as long as a future runs, keeping the handle alive. /// /// # Safety /// `handle` is a live handle of this type. - pub unsafe fn borrow_arc(handle: *const c_void, what: &str) -> SharedArc + pub unsafe fn borrow_arc(handle: *const c_void, what: &str) -> Result, Status> where T: Sync, { // SAFETY: the caller's contract. - let object = unsafe { Self::arc(handle, what) }; - object.acquire(what); - SharedArc { object } + let object = unsafe { Self::get(handle, what) }; + object.acquire()?; + // SAFETY: as above. + Ok(SharedArc { + object: unsafe { Self::arc(handle, what) }, + }) } /// Borrow the value exclusively for as long as a future runs. /// /// # Safety /// As [`Self::borrow_arc`]. - pub unsafe fn borrow_mut_arc(handle: *const c_void, what: &str) -> ExclusiveArc { + pub unsafe fn borrow_mut_arc(handle: *const c_void, what: &str) -> Result, Status> { // SAFETY: the caller's contract. - let object = unsafe { Self::arc(handle, what) }; - object.acquire_mut(what); - ExclusiveArc { object } + let object = unsafe { Self::get(handle, what) }; + object.acquire_from_zero(EXCLUSIVE)?; + // SAFETY: as above. + Ok(ExclusiveArc { + object: unsafe { Self::arc(handle, what) }, + changed: false, + }) } /// # Safety @@ -836,8 +936,8 @@ impl Object { result } - /// Whether a call or a future borrows the value, so moving it out would abort. A value - /// moved out is not, so the move refuses it, naming itself. + /// Whether a call or a future borrows the value, so moving it out would fail. A value + /// moved out is not, so the move fails as moved. /// /// # Safety /// As [`Self::borrow`]. @@ -851,21 +951,36 @@ impl Object { /// /// # Safety /// As [`Self::borrow`]. - pub unsafe fn take(handle: *const c_void, what: &str) -> T { + pub unsafe fn take(handle: *const c_void, what: &str) -> Result { + // SAFETY: the caller's contract. + unsafe { Self::get(handle, what) }.acquire_from_zero(TAKEN)?; + // SAFETY: as above, and the value is reserved. + Ok(unsafe { Self::taken(handle) }) + } + + /// Reserve the value to move out once every other check of the call passed, which + /// [`Self::taken`] then does: `takes` gives it back if the call fails first. + /// + /// # Safety + /// As [`Self::borrow`], kept alive until `takes` commits or drops. + pub unsafe fn reserve<'a>(handle: *const c_void, takes: &mut Takes<'a>, what: &str) -> Result<(), Status> { // SAFETY: the caller's contract. let object = unsafe { Self::get(handle, what) }; - if !object.lent.is_null() { - fail(what, "a lent handle's value cannot move out"); - } - if let Err(state) = object - .state - .compare_exchange(0, TAKEN, Ordering::Acquire, Ordering::Relaxed) - { - match state { - TAKEN => fail(what, "a handle's value was moved out"), - _ => fail(what, "a handle is borrowed"), - } - } + object.acquire_from_zero(TAKEN)?; + // SAFETY: the state outlives `takes`, by the caller's contract; a pointer to it, + // so the value's type need not. + takes.push(unsafe { &*::std::ptr::addr_of!(object.state) }); + Ok(()) + } + + /// Move out the value [`Self::take`] reserved, leaving the handle to be freed. + /// + /// # Safety + /// [`Self::take`] reserved `handle`'s value for a call that passed its checks, and no + /// other read moves it out. + pub unsafe fn taken(handle: *const c_void) -> T { + // SAFETY: the caller's contract. + let object = unsafe { &*handle.cast::() }; object.epoch.fetch_add(1, Ordering::Release); // SAFETY: TAKEN excludes every other access, and the value is never read again. unsafe { (*object.value.get()).assume_init_read() } @@ -881,7 +996,7 @@ impl Object { T: Copy, { // SAFETY: the caller's contract. - let value = *unsafe { Self::borrow_local(handle, what) }; + let value = *check(unsafe { Self::borrow_local(handle, what) }, what); // SAFETY: as above; the borrow ended. unsafe { Self::free(handle) }; value @@ -911,6 +1026,37 @@ impl Drop for Object { } } +/// The values a call reserved to move out, given back unless it commits: so a call whose +/// later check fails moved nothing. A call's first few allocate nothing. +#[derive(Default)] +pub struct Takes<'a> { + first: [Option<&'a AtomicUsize>; 2], + rest: Vec<&'a AtomicUsize>, +} + +impl<'a> Takes<'a> { + fn push(&mut self, state: &'a AtomicUsize) { + match self.first.iter_mut().find(|slot| slot.is_none()) { + Some(slot) => *slot = Some(state), + None => self.rest.push(state), + } + } + + /// Keep every value reserved, for the call to move out. + pub fn commit(mut self) { + self.first = [None; 2]; + self.rest.clear(); + } +} + +impl Drop for Takes<'_> { + fn drop(&mut self) { + for state in self.first.iter().flatten().chain(&self.rest) { + state.store(0, Ordering::Release); + } + } +} + pub struct Shared<'a, T> { object: &'a Object, } @@ -947,8 +1093,10 @@ impl Drop for Local<'_, T> { } } +/// An exclusive borrow, which once used to change the value ends every handle lent from it. pub struct Exclusive<'a, T> { object: &'a Object, + changed: bool, } impl Deref for Exclusive<'_, T> { @@ -961,6 +1109,7 @@ impl Deref for Exclusive<'_, T> { impl DerefMut for Exclusive<'_, T> { fn deref_mut(&mut self) -> &mut T { + self.changed = true; // SAFETY: the exclusive borrow is held. unsafe { &mut *self.object.value() } } @@ -968,6 +1117,9 @@ impl DerefMut for Exclusive<'_, T> { impl Drop for Exclusive<'_, T> { fn drop(&mut self) { + if self.changed { + self.object.epoch.fetch_add(1, Ordering::Release); + } self.object.state.store(0, Ordering::Release); } } @@ -990,8 +1142,10 @@ impl Drop for SharedArc { } } +/// [`Exclusive`], for as long as a future runs. pub struct ExclusiveArc { object: Arc>, + changed: bool, } impl Deref for ExclusiveArc { @@ -1004,6 +1158,7 @@ impl Deref for ExclusiveArc { impl DerefMut for ExclusiveArc { fn deref_mut(&mut self) -> &mut T { + self.changed = true; // SAFETY: the exclusive borrow is held. unsafe { &mut *self.object.value() } } @@ -1011,6 +1166,9 @@ impl DerefMut for ExclusiveArc { impl Drop for ExclusiveArc { fn drop(&mut self) { + if self.changed { + self.object.epoch.fetch_add(1, Ordering::Release); + } self.object.state.store(0, Ordering::Release); } } diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index 42d6fc4d1..8d835cfb7 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -55,6 +55,18 @@ public struct U128: Hashable, Comparable, Sendable, CustomStringConvertible { /// Where a seek counts its offset from, as swift-system's `FileDescriptor.SeekOrigin` and /// Rust's `SeekFrom` do. +/// A call Rust's rules refuse, thrown before any Rust runs: a handle borrowed in a way +/// the borrow checker forbids beside another call's, used once its value moved out or its +/// lend ended, or an item called on a library whose target lacks it. +public struct CheckError: Swift.Error, CustomStringConvertible, Sendable { + /// The function called, by its C name. + public let function: Swift.String + /// What broke. + public let message: Swift.String + + public var description: Swift.String { "\(function): \(message)" } +} + public enum SeekOrigin: Sendable { case start case current @@ -399,14 +411,22 @@ extension Native { } func hold() -> OpaquePointer? { + guard held() else { + fatalError("\(type) is closed") + } + return raw + } + + /// Hold it for a call, unless it is closed. + func held() -> Bool { var n = load(state) while true { if n & Handle.closed != 0 { - fatalError("\(type) is closed") + return false } let seen = swap(state, n, n + Handle.held) if seen == n { - return raw + return true } n = seen } @@ -426,6 +446,17 @@ extension Native { return try body(raw) } + /// Run `body` with the raw handle, held until it returns, or throw for a call of + /// `what` if it is closed. + @inline(__always) + func with(_ what: StaticString, _ body: (OpaquePointer?) throws -> R) throws -> R { + guard held() else { + throw CheckError(function: "\(what)", message: "\(type) is closed") + } + defer { release() } + return try body(raw) + } + func close() { var n = load(state) while n & Handle.closed == 0 { @@ -464,6 +495,15 @@ extension Native { return try handle.with(body) } + /// As `with`, throwing for a call of `what` if `handle` is closed. + @inline(__always) + static func with( + _ what: StaticString, _ handle: Handle?, _ body: (OpaquePointer?) throws -> R + ) throws -> R { + guard let handle else { return try body(nil) } + return try handle.with(what, body) + } + /// What a call lowers nested values into, kept until it returns: copied bytes, arrays, /// and the handles it holds. final class Arena { diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index f522ffd59..0bc32c154 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -16,7 +16,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; use rustc_span::Span; -use crate::ir::{Access, Api, Closure, Dyn, Function, Home, Int, Literal, On, Receiver, Type}; +use crate::ir::{Access, Api, Closure, Dyn, Function, Home, Int, Literal, Mutability, On, Receiver, Type}; /// A C name one item claims twice, which no rename can tell apart: the name, then what /// claimed it first and second. @@ -88,6 +88,9 @@ pub enum Ty { ForeignReader, /// `_poll` Poll, + /// `_status`, an `int32_t`: why a call failed a check of a handle, which a + /// function that can fail one writes through its last parameter. + Status, /// A pointer the callee writes a result through. Out(Box), /// A pointer the callee only reads through: a value's receiver. @@ -105,6 +108,18 @@ pub struct Sig { pub ret: Ty, } +impl Sig { + /// The `status` parameter it takes last, if a call can fail a check of a handle. + pub fn status(&self) -> Option<&str> { + self.params.last().filter(|(_, ty)| is_status(ty)).map(|(name, _)| name.as_str()) + } +} + +/// Whether `ty` is a `status` parameter's. +pub fn is_status(ty: &Ty) -> bool { + matches!(ty, Ty::Out(inner) if **inner == Ty::Status) +} + /// The C side of an exported function. pub struct Export { /// The function itself: for a future, the call that makes it. @@ -116,6 +131,71 @@ pub struct Export { pub future: Option<(String, Sig, Sig)>, } +/// Each `_status` a call can fail with, numbered from 1, and what it means, as +/// the runtime's `Status` names them. +pub const STATUSES: [(&str, &str); 6] = [ + ("moved", "the handle's value was moved out"), + ("exclusive", "another call or future borrows the value exclusively"), + ("borrowed", "another call or future borrows the value"), + ("thread", "a value that is not Sync is borrowed on another thread"), + ("lent", "a handle a call lent is borrowed exclusively or moved out"), + ("outlived", "a handle lent from another outlived it"), +]; + +/// Whether a call can fail a check of each object's handle, by Rust's rules alone: its +/// value is not `Sync`, so a borrow on another thread fails; an export borrows it +/// exclusively or moves it out, so a borrow beside another call's fails; or a handle to +/// it is lent, by a call returning a borrow or to a closure or trait for a call, so a use +/// past the lend fails. Every other object's handles are only ever borrowed shared, which +/// never fails. +fn checked(api: &Api) -> Vec { + /// Mark each object a parameter of type `ty` borrows exclusively, moves, or lends, + /// `owned` when `ty` is passed by value rather than behind a shared reference. + fn param(api: &Api, ty: &Type, owned: bool, checked: &mut [bool]) { + match ty { + Type::Object(i) if owned && !api.objects[*i].copy => checked[*i] = true, + Type::Ref(Mutability::Mut, inner) => { + if let Type::Object(i) = inner.base() { + checked[*i] = true; + } + param(api, inner, true, checked); + } + Type::Ref(Mutability::Not, inner) => param(api, inner, false, checked), + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => param(api, inner, owned, checked), + Type::Tuple(elements) => elements.iter().for_each(|ty| param(api, ty, owned, checked)), + Type::Closure(closure) => closure.params.iter().for_each(|ty| lent(api, ty, checked)), + _ => {} + } + } + /// Mark each object a value of `ty` Rust lends the foreign side holds. + fn lent(api: &Api, ty: &Type, checked: &mut [bool]) { + let mut handles = Vec::new(); + api.handles(ty, &mut handles, &mut Vec::new()); + handles.into_iter().for_each(|i| checked[i] = true); + } + let mut checked: Vec = api.objects.iter().map(|o| !o.sync).collect(); + let functions = (api.records.iter().flat_map(|r| &r.methods)) + .chain(api.enums.iter().flat_map(|e| &e.methods)) + .chain(&api.functions) + .map(|f| (None, f)) + .chain((api.objects.iter().enumerate()).flat_map(|(i, o)| o.methods.iter().map(move |f| (Some(i), f)))); + for (owner, f) in functions { + match (owner, f.receiver) { + (Some(i), Some(Receiver::Mut)) => checked[i] = true, + (Some(i), Some(Receiver::Owned)) if !api.objects[i].copy => checked[i] = true, + _ => {} + } + f.params.iter().for_each(|p| param(api, &p.ty, true, &mut checked)); + if f.lends { + lent(api, &f.ok, &mut checked); + } + } + for method in api.callbacks.iter().flat_map(|c| &c.methods) { + method.params.iter().for_each(|p| lent(api, &p.ty, &mut checked)); + } + checked +} + /// `ty` as a clone of each borrow it holds. fn owned(ty: &Type) -> Type { match ty { @@ -153,6 +233,8 @@ pub struct Abi<'a> { /// Each `Copy` object a closure takes or returns by value, which `_copy` copies into /// a new handle as Rust copies it, never running its `Clone`. pub copies: BTreeSet, + /// Whether a call can fail a check of each object's handle: see [`checked`]. + pub checked: Vec, /// Every C name claimed. claims: Vec, /// The C name of each item whose own clashed. @@ -173,6 +255,7 @@ impl<'a> Abi<'a> { tuples: BTreeMap::new(), arrays: BTreeMap::new(), copies: BTreeSet::new(), + checked: checked(api), claims: Vec::new(), renames: HashMap::new(), clashes: Vec::new(), @@ -773,7 +856,15 @@ impl<'a> Abi<'a> { if let Some(err) = &function.err { outs.push(("err".to_string(), Ty::Out(Box::new(self.returned(err))))); } + let status = self.status(function, owner).then(|| { + let mut status = "status".to_string(); + while function.params.iter().any(|p| escape(&p.name) == status) { + status.push('_'); + } + (status, Ty::Out(Box::new(Ty::Status))) + }); if function.future { + params.extend(status); let handle = format!("{name}_future"); let mut poll_params = vec![ ("future".to_string(), Ty::Future(handle.clone())), @@ -814,6 +905,7 @@ impl<'a> Abi<'a> { self.returned(ok) } }; + params.extend(status); Export { call: Sig { name, params, ret }, future: None, @@ -821,6 +913,27 @@ impl<'a> Abi<'a> { } } + /// Whether a call of `function`, a method of `owner` if it has one, can fail a check of + /// a handle it borrows or moves, so it takes a `status` to write why. + pub fn status(&self, function: &Function, owner: Option<&Ty>) -> bool { + let receiver = match (function.receiver, owner) { + (Some(_), Some(Ty::Handle { name, .. })) => (0..self.api.objects.len()).find(|&i| &self.object(i) == name), + _ => None, + }; + let mut handles: Vec = receiver.into_iter().collect(); + let mut seen = Vec::new(); + for param in &function.params { + self.api.handles(¶m.ty, &mut handles, &mut seen); + } + handles.iter().any(|&i| self.checked[i]) + } + + /// Whether `_clone`, `_eq`, `_to_string`, `_debug`, and `_copy` of object `i` take a + /// `status`, as each borrows its handles shared. + pub fn traits_status(&self, i: usize) -> bool { + self.checked[i] + } + /// The out pointer Rust writes back the value C passed for `&mut` as parameter `name` /// through, named apart from every parameter of `function`. pub fn written(function: &Function, name: &str) -> String { @@ -890,6 +1003,8 @@ impl<'a> Abi<'a> { for runtime in [ "contract", "target", + "status", + "status_message", "buf", "buf_free", "u128", diff --git a/ffi/src/c.rs b/ffi/src/c.rs index acdee884d..08558512a 100644 --- a/ffi/src/c.rs +++ b/ffi/src/c.rs @@ -78,6 +78,12 @@ impl Header<'_> { self.line(format!( "/* The index of the library's target among those this header serves:{targets} */\nuint32_t {p}_target(void);\n" )); + let statuses: String = (crate::abi::STATUSES.iter().enumerate()) + .map(|(n, (status, _))| format!("\t{upper}_STATUS_{} = {},\n", status.to_uppercase(), n + 1)) + .collect(); + self.line(format!( + "/* Why a call failed a check of a handle, before it ran any Rust: a borrow or move\n * Rust's borrow checker would refuse. A function that can fail one takes a status\n * last, through which it writes the failure, returning a zeroed value to ignore;\n * passed null, it aborts with the message instead. */\ntypedef int32_t {p}_status;\nenum {{\n{statuses}}};\n\n/* What a status says broke. */\nconst char *{p}_status_message({p}_status status);\n" + )); self.line(format!( "/* Bytes. Passed in, a view Rust reads during the call and never frees. Returned,\n * an owned buffer to free once with {p}_buf_free. A null ptr is None. */\ntypedef struct {p}_buf {{\n\tconst uint8_t *ptr;\n\tsize_t len;\n\tvoid *owner;\n}} {p}_buf;\n\nvoid {p}_buf_free({p}_buf buf);\n\n/* An owned copy of view's bytes: what a closure or reader C implements returns Rust,\n * which then owns it. */\n{p}_buf {p}_buf_copy({p}_buf view);\n" )); @@ -234,8 +240,12 @@ impl Header<'_> { self.line(format!("bool {name}_borrowed(const {name} *self);")); } let t = object.traits; + let status = match abi.traits_status(i) { + true => format!(", {p}_status *status"), + false => String::new(), + }; if t.clone { - self.line(format!("{name} *{name}_clone(const {name} *self);")); + self.line(format!("{name} *{name}_clone(const {name} *self{status});")); } if abi.copies.contains(&i) { self.line( @@ -245,16 +255,16 @@ impl Header<'_> { ) .trim_end(), ); - self.line(format!("{name} *{name}_copy(const {name} *self);")); + self.line(format!("{name} *{name}_copy(const {name} *self{status});")); } if t.eq { - self.line(format!("bool {name}_eq(const {name} *a, const {name} *b);")); + self.line(format!("bool {name}_eq(const {name} *a, const {name} *b{status});")); } if t.display { - self.line(format!("{p}_buf {name}_to_string(const {name} *self);")); + self.line(format!("{p}_buf {name}_to_string(const {name} *self{status});")); } if t.debug { - self.line(format!("{p}_buf {name}_debug(const {name} *self);")); + self.line(format!("{p}_buf {name}_debug(const {name} *self{status});")); } if t.default { self.line(format!("{name} *{name}_default(void);")); @@ -533,6 +543,7 @@ pub fn decl(abi: &Abi<'_>, ty: &Ty, name: &str) -> String { Ty::ForeignFuture => space(&abi.rt("foreign_future")), Ty::ForeignReader => space(&abi.rt("foreign_reader")), Ty::Poll => space(&abi.rt("poll")), + Ty::Status => space(&abi.rt("status")), Ty::Out(inner) => decl(abi, inner, &format!("*{name}")), Ty::In(inner) => format!("const {}", decl(abi, inner, &format!("*{name}"))), Ty::Fn(params, ret) => { diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 2fb058209..ed96357ac 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -623,11 +623,22 @@ impl<'a> Header<'a> { let t = object.traits; let text = format!("{}\n\n{}", abi.api.rust(&object.path, &object.args), object.docs); let d = self.d(); + // A failed check of a trait's borrow aborts with its message, as C++ throws nothing. + let status = match abi.traits_status(i) { + true => ", nullptr", + false => "", + }; // Copies share a handle only where no call tells that apart from a clone, but by an // `Arc`'s or `Rc`'s count, which counts one for every copy. - let handle = match object.shared { - true => format!("{d}::shared<::{c}, &::{c}_free, &::{c}_clone, &::{c}_borrowed>"), - false => format!("{d}::unique<::{c}, &::{c}_free>"), + let handle = match (object.shared, status.is_empty()) { + (true, true) => format!("{d}::shared<::{c}, &::{c}_free, &::{c}_clone, &::{c}_borrowed>"), + (true, false) => { + self.line(format!( + "namespace detail {{\n\ninline ::{c} *{c}_clone(const ::{c} *raw) noexcept {{\n\treturn ::{c}_clone(raw, nullptr);\n}}\n\n}} // namespace detail\n" + )); + format!("{d}::shared<::{c}, &::{c}_free, &{d}::{c}_clone, &::{c}_borrowed>") + } + (false, _) => format!("{d}::unique<::{c}, &::{c}_free>"), }; let mut body = vec![ format!("\t{handle} raw_;\n"), @@ -649,16 +660,16 @@ impl<'a> Header<'a> { "\t/// Rust's `Clone`, a value of its own: copies sharing one value could be told apart\n\t/// from clones, so it does not copy.\n\tRUST2_FFI_CALLABLE {name} {clone}() const;" )); self.def(format!( - "inline {name} {name}::{clone}() const {{\n\treturn {d}::access::wrap<{name}>(::{c}_clone(raw_.get()));\n}}\n" + "inline {name} {name}::{clone}() const {{\n\treturn {d}::access::wrap<{name}>(::{c}_clone(raw_.get(){status}));\n}}\n" )); members.push(clone); } if t.eq { body.push(format!( - "\tfriend bool operator==(const {name} &a, const {name} &b) {{\n\t\treturn ::{c}_eq(a.raw_.get(), b.raw_.get());\n\t}}\n\tfriend bool operator!=(const {name} &a, const {name} &b) {{\n\t\treturn !(a == b);\n\t}}" + "\tfriend bool operator==(const {name} &a, const {name} &b) {{\n\t\treturn ::{c}_eq(a.raw_.get(), b.raw_.get(){status});\n\t}}\n\tfriend bool operator!=(const {name} &a, const {name} &b) {{\n\t\treturn !(a == b);\n\t}}" )); } - self.traits(&name, &c, t, None, &mut body, &mut members); + self.traits(&name, &c, t, None, status, &mut body, &mut members); let owner = Ty::Handle { name: c.clone(), constant: false, @@ -685,6 +696,7 @@ impl<'a> Header<'a> { c: &str, t: Traits, value: Option<&Type>, + status: &str, body: &mut Vec, members: &mut Vec, ) { @@ -706,7 +718,7 @@ impl<'a> Header<'a> { let member = apart("to_string", name); body.push(format!("\t{callable}::{ns}::buf {member}() const;")); self.def(format!( - "inline ::{ns}::buf {name}::{member}() const {{\n{lower}\treturn {d}::access::adopt(::{c}_to_string({this}));\n}}\n" + "inline ::{ns}::buf {name}::{member}() const {{\n{lower}\treturn {d}::access::adopt(::{c}_to_string({this}{status}));\n}}\n" )); members.push(member); } @@ -714,7 +726,7 @@ impl<'a> Header<'a> { let member = apart("debug", name); body.push(format!("\t{callable}::{ns}::buf {member}() const;")); self.def(format!( - "inline ::{ns}::buf {name}::{member}() const {{\n{lower}\treturn {d}::access::adopt(::{c}_debug({this}));\n}}\n" + "inline ::{ns}::buf {name}::{member}() const {{\n{lower}\treturn {d}::access::adopt(::{c}_debug({this}{status}));\n}}\n" )); members.push(member); } @@ -863,7 +875,7 @@ impl<'a> Header<'a> { self.lower(ty, "other"), )); } - self.traits(&name, &c, t, Some(ty), &mut body, &mut members); + self.traits(&name, &c, t, Some(ty), "", &mut body, &mut members); let owner = Ty::Struct(c.clone()); let mut taken = fields.clone(); taken.push(name.clone()); @@ -1205,7 +1217,7 @@ impl<'a> Header<'a> { params.push(declare(&self.param(¶m.ty), &name)); args.push(self.lower(¶m.ty, &name)); } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); // Each value passed for `&mut` takes what Rust wrote back, on every path. let writes: Vec = (function.params.iter().zip(&names)) .filter(|(param, _)| param.ty.written()) @@ -1217,6 +1229,10 @@ impl<'a> Header<'a> { for (name, _) in &outs { args.push(format!("&c_{name}_")); } + // A failed check aborts with its message, as C++ throws nothing. + if export.call.status().is_some() { + args.push("nullptr".to_string()); + } let call = format!("::{}({})", export.call.name, args.join(", ")); let mut body = prelude; if let Some((handle, _, _)) = &export.future { @@ -1414,7 +1430,14 @@ impl<'a> Header<'a> { Type::Option(inner) if matches!(**inner, Type::Object(_)) => { format!("({e} ? {} : nullptr)", self.returned(inner, &format!("(*{e})"))) } - Type::Object(i) => format!("::{}_copy({d}::access::raw({e}))", abi.object(*i)), + Type::Object(i) => format!( + "::{}_copy({d}::access::raw({e}){})", + abi.object(*i), + match abi.traits_status(*i) { + true => ", nullptr", + false => "", + } + ), ty => self.lower(ty, e), } } diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 78150bdef..03bd148b3 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -117,7 +117,9 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result>> _target) & 1 == 0) {{\n\t\tthrow UnsupportedError(\"$what: it is not on this library's target\");\n\t}}\n}}\n\n" + void _on(int on, String what) {{\n\tif ((on >>> _target) & 1 == 0) {{\n\t\tthrow UnsupportedError(\"$what: it is not on this library's target\");\n\t}}\n}}\n\n\ + /// Throws unless [status], which the call [what] wrote, is zero: a run-time check failed.\n\ + void _status(int status, String what) {{\n\tif (status == 0) return;\n\tfinal m = _statusMessage(status);\n\tvar n = 0;\n\twhile (m[n] != 0) n++;\n\tthrow StateError('$what: ${{const convert.Utf8Decoder().convert(m.asTypedList(n))}}');\n}}\n\n" ); for (name, params, ret, dart_params, dart_ret) in [ ("contract", "", "ffi.Uint64", "", "int"), @@ -209,6 +211,13 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result", + "int p0", + "ffi.Pointer", + ), ( "dart_reader_read", "ffi.Pointer, ffi.Pointer, ffi.Size, ffi.Pointer, ffi.Pointer<_Buf>", @@ -228,6 +237,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result format!("_{p}_{name}"), "buf_free" => "_bufFree".to_string(), "buf_copy" => "_bufCopy".to_string(), + "status_message" => "_statusMessage".to_string(), other => format!("_{}", camel(other)), }; let symbol = match name.strip_prefix("dart") { @@ -558,6 +568,7 @@ impl<'a> Gen<'a> { Ty::F64 => "ffi.Double".to_string(), Ty::Char => "ffi.Uint32".to_string(), Ty::Enum(_) | Ty::Poll => "ffi.Int".to_string(), + Ty::Status => "ffi.Int32".to_string(), Ty::Handle { .. } | Ty::Future(_) | Ty::Data => "ffi.Pointer".to_string(), Ty::Out(inner) | Ty::In(inner) => format!("ffi.Pointer<{}>", self.nt(inner)), Ty::Fn(params, ret) => { @@ -579,7 +590,7 @@ impl<'a> Gen<'a> { match ty { Ty::Void => "void".to_string(), Ty::Bool => "bool".to_string(), - Ty::Int(_) | Ty::Char | Ty::Enum(_) | Ty::Poll => "int".to_string(), + Ty::Int(_) | Ty::Char | Ty::Enum(_) | Ty::Poll | Ty::Status => "int".to_string(), Ty::F32 | Ty::F64 => "double".to_string(), ty => self.nt(ty), } @@ -1330,38 +1341,65 @@ impl<'a> Gen<'a> { }, false => Ty::In(Box::new(c_ty.clone())), }; + // An object whose run-time checks may fail passes each its status, which throws. + let status = matches!(ty, Type::Object(i) if self.abi.traits_status(*i)); + let params = |args: Vec| -> Vec { + let mut args = args; + if status { + args.push(Ty::Out(Box::new(Ty::Status))); + } + args + }; + // The statements calling `f` with `args`, returning what `lift` makes of its result. + let call = |f: &str, args: &str, what: &str, lift: &dyn Fn(&str) -> String| -> String { + match status { + true => format!( + "final s_ = a_()..value = 0;\n\t\t\tfinal r_ = {f}(_n.home, {args}, s_);\n\t\t\t_status(s_.value, '{what}');\n\t\t\treturn {};", + lift("r_") + ), + false => format!("return {};", lift(&format!("{f}(_n.home, {args})"))), + } + }; if t.display { members.push("toString".to_string()); - let f = self.served_fn(&format!("{c}_to_string"), vec![recv.clone()], Ty::Buf); + let what = format!("{c}_to_string"); + let f = self.served_fn(&what, params(vec![recv.clone()]), Ty::Buf); let (pre, arg) = this(self, "this"); + let call = call(&f, &arg, &what, &|r| format!("_n.string({r})")); body.push(format!( - "\t@override\n\tString toString() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pre}return _n.string({f}(_n.home, {arg}));\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + "\t@override\n\tString toString() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pre}{call}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" )); } if t.debug { members.push("debug".to_string()); - let f = self.served_fn(&format!("{c}_debug"), vec![recv.clone()], Ty::Buf); + let what = format!("{c}_debug"); + let f = self.served_fn(&what, params(vec![recv.clone()]), Ty::Buf); let (pre, arg) = this(self, "this"); + let call = call(&f, &arg, &what, &|r| format!("_n.string({r})")); body.push(format!( - "\t/// Rust's `Debug` form.\n\tString debug() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pre}return _n.string({f}(_n.home, {arg}));\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + "\t/// Rust's `Debug` form.\n\tString debug() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pre}{call}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" )); } if object && t.clone { members.push("clone".to_string()); - let f = self.served_fn(&format!("{c}_clone"), vec![recv.clone()], recv.clone()); + let what = format!("{c}_clone"); + let f = self.served_fn(&what, params(vec![recv.clone()]), recv.clone()); + let call = call(&f, "a_.hold(this)", &what, &|r| format!("{name}._({r})")); body.push(format!( - "\t/// A new handle to a clone.\n\t{name} clone() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\treturn {name}._({f}(_n.home, a_.hold(this)));\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + "\t/// A new handle to a clone.\n\t{name} clone() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{call}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" )); } // A Dart enum compares by identity, as a plain Rust enum does, and may not override `==`. let plain = matches!(ty, Type::Enum(i) if self.abi.api.enums[*i].plain()); if t.eq && !plain { members.push("hashCode".to_string()); - let f = self.served_fn(&format!("{c}_eq"), vec![recv.clone(), recv.clone()], Ty::Bool); + let what = format!("{c}_eq"); + let f = self.served_fn(&what, params(vec![recv.clone(), recv.clone()]), Ty::Bool); let (pa, a) = this(self, "this"); let (pb, b) = this(self, "other"); + let call = call(&f, &format!("{a}, {b}"), &what, &|r| r.to_string()); body.push(format!( - "\t/// Rust's `PartialEq`.\n\t@override\n\tbool operator ==(Object other) {{\n\t\tif (other is! {name}) return false;\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pa}{pb}return {f}(_n.home, {a}, {b});\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n\n\t/// Rust has no `Hash` for it, so it keys no `Set` or `Map`.\n\t@override\n\tint get hashCode => throw UnsupportedError('{name} has no Rust Hash');\n" + "\t/// Rust's `PartialEq`.\n\t@override\n\tbool operator ==(Object other) {{\n\t\tif (other is! {name}) return false;\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pa}{pb}{call}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n\n\t/// Rust has no `Hash` for it, so it keys no `Set` or `Map`.\n\t@override\n\tint get hashCode => throw UnsupportedError('{name} has no Rust Hash');\n" )); } if t.default { @@ -1834,8 +1872,17 @@ impl<'a> Gen<'a> { name: c.clone(), constant: true, }; - let clone = self.served_fn(&format!("{c}_copy"), vec![handle.clone()], handle); - format!("_n.handOver({e}, {clone})") + // A closure's return has no caller to throw to, so a failed check aborts. + let mut params = vec![handle.clone()]; + let checked = abi.traits_status(*i); + if checked { + params.push(Ty::Out(Box::new(Ty::Status))); + } + let clone = self.served_fn(&format!("{c}_copy"), params, handle); + match checked { + true => format!("_n.handOver({e}, (h, p) => {clone}(h, p, ffi.nullptr))"), + false => format!("_n.handOver({e}, {clone})"), + } } Type::Option(inner) if matches!(**inner, Type::Object(_)) => { let given = self.returned(inner, "v"); @@ -2022,7 +2069,13 @@ impl<'a> Gen<'a> { } } } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); + // A call that fails a run-time check writes its status, which throws once it returns. + let status = export.call.status().is_some(); + let check = format!("_status(s_.value, '{c_name}');"); + if status { + pre.push("final s_ = a_()..value = 0;".to_string()); + } // A call this target lacks throws before any argument is lowered or given to Rust. if !abi.api.everywhere(self.on) { pre.insert(0, format!("_on(0x{:x}, '{c_name}');", self.on.0 & abi.api.all().0)); @@ -2076,7 +2129,14 @@ impl<'a> Gen<'a> { ); body.push("final ffi.Pointer f_;".to_string()); let mut inner = pre.clone(); - inner.push(format!("f_ = {call_fn}({});", args.join(", "))); + let mut call_args = args.clone(); + if status { + call_args.push("s_".to_string()); + } + inner.push(format!("f_ = {call_fn}({});", call_args.join(", "))); + if status { + inner.push(check.clone()); + } inner.extend(after.clone()); body.push(format!( "final a_ = _Arena();\ntry {{\n{}\n}} finally {{\n\ta_.end();\n}}", @@ -2091,10 +2151,19 @@ impl<'a> Gen<'a> { } let mut call_args = args.clone(); call_args.extend(outs.iter().map(|(n, _)| format!("o_{n}"))); + if status { + call_args.push("s_".to_string()); + } let call = format!("{call_fn}({})", call_args.join(", ")); + let checked = |inner: &mut Vec| { + if status { + inner.push(check.clone()); + } + }; match (&err, &ok) { (Some(err), ok) => { inner.push(format!("final ok_ = {call};")); + checked(&mut inner); inner.extend(writes); inner.extend(after.clone()); let ready = match ok { @@ -2111,18 +2180,21 @@ impl<'a> Gen<'a> { } (None, Type::Unit) => { inner.push(format!("{call};")); + checked(&mut inner); inner.extend(writes); inner.extend(after.clone()); } // A value C returns beside what Rust wrote back is its return, as with none. (None, ok) if outs.len() > writes.len() => { inner.push(format!("{call};")); + checked(&mut inner); inner.extend(writes); inner.extend(after.clone()); inner.push(format!("return {};", self.lift_outs(&function.ok, ok))); } (None, ok) => { inner.push(format!("final r_ = {call};")); + checked(&mut inner); inner.extend(writes); inner.extend(after.clone()); inner.push(format!("return {};", self.lift(ok, "r_"))); diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 6be1d20cf..5bcdc3da0 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -5,6 +5,7 @@ //! never frees, and lowered into memory the caller frees. A handle is borrowed as the //! receiver or parameter is, or moved out of when taken by value. +use std::cell::Cell; use std::fmt::Write; use std::path::Path; @@ -87,7 +88,7 @@ pub fn rs(ty: &Ty) -> String { Ty::SocketAddr => "rt::SocketAddr".to_string(), Ty::Struct(name) => name.clone(), // A C enum is an `int`. - Ty::Enum(_) | Ty::Poll => "i32".to_string(), + Ty::Enum(_) | Ty::Poll | Ty::Status => "i32".to_string(), Ty::Handle { .. } => "*const ::std::ffi::c_void".to_string(), Ty::Future(_) => "*mut rt::Future".to_string(), Ty::Waker => "rt::Waker".to_string(), @@ -224,6 +225,8 @@ impl Crate<'_> { lends: false, on: On::ALL, out: String::new(), + checking: Cell::new(false), + reserved: Cell::new(false), }; generator.lib(); crate::driver::write(&out.join("Cargo.toml"), &manifest)?; @@ -349,6 +352,12 @@ struct Gen<'a> { /// The targets of the item being written, which compile what names its Rust types. on: On, out: String, + /// Whether the code being written returns a failed check as an `Err`, with `?`, rather + /// than aborting, as a function with a `status` does. + checking: Cell, + /// Whether the argument being lifted had its moved handles reserved, so its lift moves + /// their values out unchecked. + reserved: Cell, } impl Gen<'_> { @@ -400,6 +409,59 @@ impl Gen<'_> { format!("::{}", self.dependency) + &path[1..].iter().map(|s| format!("::{s}")).collect::() } + /// `e`, a check's `Result`: its value, returning the failure from the function being + /// written if it takes a `status`, else aborting. + fn checked(&self, e: &str) -> String { + match self.checking.get() { + true => format!("{e}?"), + false => format!("rt::check({e}, w)"), + } + } + + /// Whether lifting a value of `ty` checks a handle that can fail. + fn checks(&self, ty: &Type) -> bool { + let mut handles = Vec::new(); + self.abi.api.handles(ty, &mut handles, &mut Vec::new()); + handles.iter().any(|&i| self.abi.checked[i]) + } + + /// Whether `ty` moves an object's value out of its handle, as passed by value. + fn moves(&self, ty: &Type) -> bool { + match ty { + Type::Object(i) => !self.abi.api.objects[*i].copy, + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => self.moves(inner), + Type::Tuple(elements) => elements.iter().any(|ty| self.moves(ty)), + _ => false, + } + } + + /// The statement reserving each value an argument `e` of type `ty` moves out, into + /// `takes`, so the move happens only once every check passed. + fn reserve(&self, ty: &Type, e: &str) -> String { + match ty { + Type::Object(i) => self.checked(&format!( + "unsafe {{ rt::Object::<{}>::reserve({e}, &mut takes, w) }}", + self.object_path(*i) + )) + ";", + Type::Box(inner) => self.reserve(inner, e), + Type::Option(inner) => match inner.base() { + Type::Object(_) => format!("if !{e}.is_null() {{ {} }}", self.reserve(inner, e)), + base if Abi::nullable(base) => format!("if !{e}.is_none() {{ {} }}", self.reserve(inner, e)), + _ => format!("if {e}.some {{ {} }}", self.reserve(inner, &format!("{e}.value"))), + }, + Type::List(_, inner) => format!( + "for c in unsafe {{ {e}.items(w) }} {{ {} }}", + self.reserve(inner, "(*c)") + ), + Type::Tuple(elements) => (elements.iter().enumerate()) + .filter(|(_, ty)| self.moves(ty)) + .map(|(n, ty)| self.reserve(ty, &format!("{e}._{n}"))) + .collect::>() + .join(" "), + _ => String::new(), + } + } + /// An object's Rust type: its lifetimes `'static`, as the handle holds it. fn object_path(&self, i: usize) -> String { self.rust(&Type::Object(i)) @@ -479,15 +541,25 @@ impl Gen<'_> { Type::SocketAddr(Family::V6) => format!("{e}.lift_v6(w)"), Type::Waker => format!("{e}.lift()"), Type::Future | Type::Reader => format!("{e}.lift(w)"), - Type::List(c, inner) => format!( - "unsafe {{ {e}.items(w) }}.iter().map(|c| {}).collect::<{}<{}>>()", - self.lift(inner, "(*c)"), - collection(*c), - match c { + Type::List(c, inner) => { + let args = match c { Collection::BTreeMap | Collection::JsonMap => "_, _", Collection::Vec | Collection::BTreeSet => "_", + }; + let lifted = self.lift(inner, "(*c)"); + // A failed check inside returns from the closure each item lifts in. + let fails = self.checks(inner) || (self.moves(inner) && !self.reserved.get()); + match self.checking.get() && fails { + true => format!( + "unsafe {{ {e}.items(w) }}.iter().map(|c| -> ::std::result::Result<_, rt::Status> {{ Ok({lifted}) }}).collect::<::std::result::Result<{}<{args}>, rt::Status>>()?", + collection(*c) + ), + false => format!( + "unsafe {{ {e}.items(w) }}.iter().map(|c| {lifted}).collect::<{}<{args}>>()", + collection(*c) + ), } - ), + } // A foreign enum's option converts into its filter, as `tracing::LevelFilter`. Type::Option(inner) if self.filtered(inner) => format!( "::std::convert::From::from(if {e}.some {{ Some({}) }} else {{ None }})", @@ -505,22 +577,34 @@ impl Gen<'_> { }, Type::Box(inner) => format!("::std::boxed::Box::new({})", self.lift(inner, e)), Type::Ref(_, inner) => self.lift(inner, e), - Type::Record(i) => format!("lift_{}(&{e}, w)", self.abi.record(*i)), - Type::Enum(i) => match self.abi.api.enums[*i].plain() { - true => format!("lift_{}({e}, w)", self.abi.enumeration(*i)), - false => format!("lift_{}(&{e}, w)", self.abi.enumeration(*i)), + Type::Record(i) => match self.checks(ty) { + true => self.checked(&format!("lift_{}(&{e}, w)", self.abi.record(*i))), + false => format!("lift_{}(&{e}, w)", self.abi.record(*i)), }, - Type::Object(i) if self.abi.api.objects[*i].opaque => { - format!("unsafe {{ rt::Object::::take({e}, w) }}.arc(w)") - } - Type::Object(i) => match self.abi.api.objects[*i].copy { - true => format!( - "*unsafe {{ rt::Object::<{}>::{}({e}, w) }}", - self.object_path(*i), - self.shared(*i) - ), - false => format!("unsafe {{ rt::Object::<{}>::take({e}, w) }}", self.object_path(*i)), + Type::Enum(i) => match (self.abi.api.enums[*i].plain(), self.checks(ty)) { + (true, _) => format!("lift_{}({e}, w)", self.abi.enumeration(*i)), + (false, true) => self.checked(&format!("lift_{}(&{e}, w)", self.abi.enumeration(*i))), + (false, false) => format!("lift_{}(&{e}, w)", self.abi.enumeration(*i)), }, + Type::Object(i) => { + let object = &self.abi.api.objects[*i]; + let path = match object.opaque { + true => "rt::Opaque".to_string(), + false => self.object_path(*i), + }; + let value = match (object.copy, self.reserved.get()) { + (true, _) => format!( + "*{}", + self.checked(&format!("unsafe {{ rt::Object::<{path}>::{}({e}, w) }}", self.shared(*i))) + ), + (false, true) => format!("unsafe {{ rt::Object::<{path}>::taken({e}) }}"), + (false, false) => self.checked(&format!("unsafe {{ rt::Object::<{path}>::take({e}, w) }}")), + }; + match object.opaque { + true => format!("{value}.arc(w)"), + false => value, + } + } Type::Callback(i, form) => abi::dyn_wrap(*form, &format!("Foreign_{}({e})", self.abi.callback(*i))), Type::Closure(closure) => self.closure(closure, e), Type::Range(_) => format!("{e}.lift()"), @@ -539,6 +623,14 @@ impl Gen<'_> { /// which lends each object the closure takes as a handle for that call. A kept one frees /// its data with `e_free` once dropped. fn closure(&self, closure: &Closure, e: &str) -> String { + // A failed check of what it returns has no caller to return to. + let checking = self.checking.replace(false); + let made = self.closure_body(closure, e); + self.checking.set(checking); + made + } + + fn closure_body(&self, closure: &Closure, e: &str) -> String { let typed: Vec = (closure.params.iter().enumerate()) .map(|(n, ty)| format!("c{n}: {}", self.rust(ty))) .collect(); @@ -806,6 +898,9 @@ impl Gen<'_> { self.line(format!( "#[unsafe(no_mangle)]\npub extern \"C\" fn {p}_target() -> u32 {{\n\tTARGET\n}}\n" )); + self.line(format!( + "#[unsafe(no_mangle)]\npub extern \"C\" fn {p}_status_message(status: i32) -> *const ::std::ffi::c_char {{\n\trt::Status::text(status).as_ptr()\n}}\n" + )); for (name, ty) in &self.abi.ranges { let (kind, int) = match ty { @@ -868,12 +963,21 @@ impl Gen<'_> { )); } let borrow = self.shared(i); - self.traits(&name, &path, object.traits, |e| { - format!("unsafe {{ rt::Object::<{path}>::{borrow}({e}, w) }}") - }); + // Each borrows its handles shared, which fails only as a call can. + let status = self.abi.traits_status(i).then_some("status"); + self.checking.set(status.is_some()); + let borrowing = |e: &str| self.checked(&format!("unsafe {{ rt::Object::<{path}>::{borrow}({e}, w) }}")); + let copy = borrowing("this"); + let borrowing: Vec = ["this", "a", "b"].into_iter().map(borrowing).collect(); + self.checking.set(false); + self.traits(&name, &path, object.traits, status, &borrowing); if self.abi.copies.contains(&i) { + let (param, body) = match status { + Some(status) => (format!(", {status}: *mut i32"), wrap(Some(status), "*const ::std::ffi::c_void", &format!("rt::Object::new(*{copy})"))), + None => (String::new(), format!("rt::Object::new(*{copy})")), + }; self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_copy(this: *const ::std::ffi::c_void) -> *const ::std::ffi::c_void {{\n\tlet w = \"{name}_copy\";\n\trt::Object::new(*unsafe {{ rt::Object::<{path}>::{borrow}(this, w) }})\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_copy(this: *const ::std::ffi::c_void{param}) -> *const ::std::ffi::c_void {{\n\tlet w = \"{name}_copy\";\n\t{body}\n}}\n" )); } let owner = Ty::Handle { @@ -889,32 +993,52 @@ impl Gen<'_> { } } - /// The functions a type's std traits export; `borrow` reads the receiver `e`. - fn traits(&mut self, name: &str, path: &str, traits: Traits, borrow: impl Fn(&str) -> String) { + /// The functions an object's std traits export, borrowing its handles `this`, `a`, + /// and `b` as `borrow` does, which takes `status` if a borrow can fail. + fn traits(&mut self, name: &str, path: &str, traits: Traits, status: Option<&str>, borrow: &[String]) { let handle = "*const ::std::ffi::c_void"; + let [this, a, b] = borrow else { + unreachable!("three handles") + }; + let param = status.map(|s| format!(", {s}: *mut i32")).unwrap_or_default(); if traits.clone { + let body = wrap( + status, + handle, + &format!("let this = {this};\n\trt::Object::new(<{path} as ::std::clone::Clone>::clone(&*this))"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_clone(this: {handle}) -> {handle} {{\n\tlet w = \"{name}_clone\";\n\tlet this = {};\n\trt::Object::new(<{path} as ::std::clone::Clone>::clone(&*this))\n}}\n", - borrow("this") + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_clone(this: {handle}{param}) -> {handle} {{\n\tlet w = \"{name}_clone\";\n\t{body}\n}}\n" )); } if traits.eq { + let body = wrap( + status, + "bool", + &format!("let a = {a};\n\tlet b = {b};\n\t<{path} as ::std::cmp::PartialEq>::eq(&*a, &*b)"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: {handle}, b: {handle}) -> bool {{\n\tlet w = \"{name}_eq\";\n\tlet a = {};\n\tlet b = {};\n\t<{path} as ::std::cmp::PartialEq>::eq(&*a, &*b)\n}}\n", - borrow("a"), - borrow("b") + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: {handle}, b: {handle}{param}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t{body}\n}}\n" )); } if traits.display { + let body = wrap( + status, + "rt::Buf", + &format!("let this = {this};\n\trt::Buf::string(::std::string::ToString::to_string(&*this))"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: {handle}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\tlet this = {};\n\trt::Buf::string(::std::string::ToString::to_string(&*this))\n}}\n", - borrow("this") + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: {handle}{param}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\t{body}\n}}\n" )); } if traits.debug { + let body = wrap( + status, + "rt::Buf", + &format!("let this = {this};\n\trt::Buf::string(format!(\"{{:?}}\", &*this))"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: {handle}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\tlet this = {};\n\trt::Buf::string(format!(\"{{:?}}\", &*this))\n}}\n", - borrow("this") + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: {handle}{param}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\t{body}\n}}\n" )); } if traits.default { @@ -973,12 +1097,16 @@ impl Gen<'_> { self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n{fields}}}\n" )); + // One holding a handle a check of can fail returns the failure. + let checks = self.checks(&ty); + self.checking.set(checks); let lifted: Vec = record .fields .iter() .enumerate() .map(|(n, f)| self.lift(&f.ty, &format!("c.f{n}"))) .collect(); + self.checking.set(false); let build = match record.tuple { true => format!("{path}({})", lifted.join(", ")), false => { @@ -991,8 +1119,12 @@ impl Gen<'_> { format!("{path} {{ {} }}", fields.join(", ")) } }; + let (ret, build) = match checks { + true => (format!("::std::result::Result<{path}, rt::Status>"), format!("Ok({build})")), + false => (path.clone(), build), + }; self.item(format!( - "fn lift_{name}(c: &{name}, w: &str) -> {path} {{\n\tlet _ = w;\n\t{build}\n}}\n" + "fn lift_{name}(c: &{name}, w: &str) -> {ret} {{\n\tlet _ = w;\n\t{build}\n}}\n" )); let bindings: Vec = (0..record.fields.len()).map(|n| format!("f{n}")).collect(); let pattern = match record.tuple { @@ -1039,6 +1171,8 @@ impl Gen<'_> { let name = self.abi.enumeration(i); let path = self.rust(&Type::Enum(i)); let ty = Type::Enum(i); + // One holding a handle a check of can fail returns the failure. + let checks = self.checks(&ty); let wildcard = match e.exhaustive { true => "", false => "\n\t\t_ => unreachable!(\"a variant this export does not know\"),", @@ -1130,12 +1264,14 @@ impl Gen<'_> { continue; } let payload = abi::payload(&name, &variant.name); + self.checking.set(checks); let lifted: Vec = variant .fields .iter() .enumerate() .map(|(k, f)| self.lift(&f.ty, &format!("p.f{k}"))) .collect(); + self.checking.set(false); let build = match variant.tuple { true => format!("{path}::{}({})", variant.name, lifted.join(", ")), false => { @@ -1148,6 +1284,10 @@ impl Gen<'_> { format!("{path}::{} {{ {} }}", variant.name, fields.join(", ")) } }; + let build = match checks { + true => format!("Ok({build})"), + false => build, + }; let _ = write!( lift, "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{build}\n\t\t}}" @@ -1177,8 +1317,12 @@ impl Gen<'_> { ); } } + let ret = match checks { + true => format!("::std::result::Result<{path}, rt::Status>"), + false => path.clone(), + }; self.item(format!( - "fn lift_{name}(c: &{name}, w: &str) -> {path} {{\n\tmatch c.tag {{{lift}\n\t\t_ => rt::fail(w, \"an enum tag is invalid\"),\n\t}}\n}}\n" + "fn lift_{name}(c: &{name}, w: &str) -> {ret} {{\n\tmatch c.tag {{{lift}\n\t\t_ => rt::fail(w, \"an enum tag is invalid\"),\n\t}}\n}}\n" )); self.item(format!( "fn lower_{name}(v: {path}) -> {name} {{\n\tmatch v {{{lower}{wildcard}\n\t}}\n}}\n" @@ -1290,10 +1434,22 @@ impl Gen<'_> { } } - /// An argument: the statement binding it from its C parameter `c` to `local`, and - /// the expression passing it. A future owns its arguments, so it borrows nothing - /// from the caller. - fn arg(&self, ty: &Type, c: &str, local: &str, future: bool) -> (String, String) { + /// An argument: the statement reserving each value it moves out of a handle, the + /// statement binding it from its C parameter `c` to `local`, and the expression passing + /// it. A future owns its arguments, so it borrows nothing from the caller. + fn arg(&self, ty: &Type, c: &str, local: &str, future: bool) -> (String, String, String) { + if self.moves(ty) { + let reserve = self.reserve(ty, c); + self.reserved.set(true); + let lifted = self.lift(ty, c); + self.reserved.set(false); + return (reserve, format!("let {local} = {lifted};"), local.to_string()); + } + let (stmt, pass) = self.arg_of(ty, c, local, future); + (String::new(), stmt, pass) + } + + fn arg_of(&self, ty: &Type, c: &str, local: &str, future: bool) -> (String, String) { match ty { Type::Ref(m, inner) => match (&**inner, m, future) { (Type::Object(i), _, _) => { @@ -1379,7 +1535,10 @@ impl Gen<'_> { }; ( binding, - format!("unsafe {{ rt::Object::<{}>::{borrow}({c}, w) }}", self.object_path(i)), + self.checked(&format!( + "unsafe {{ rt::Object::<{}>::{borrow}({c}, w) }}", + self.object_path(i) + )), ) } @@ -1452,9 +1611,30 @@ impl Gen<'_> { fn function_on(&mut self, function: &Function, owner: Option<(&Ty, Type)>) { let export = self.abi.export(function, owner.as_ref().map(|(ty, _)| *ty)); let name = export.call.name.clone(); + let status = export.call.status(); + self.checking.set(status.is_some()); // An eager call borrows its arguments only for the call, as a synchronous one does. let future = function.future && !function.eager; + // Each argument's statements: those holding no handle first, so what C gave Rust, + // such as a kept closure, drops if a check fails; then those that check a handle, + // then the reserved moves, once every check passed, so a call that fails a check + // changes nothing. + let mut checks = Vec::new(); + let mut reserves = Vec::new(); + let mut moves = Vec::new(); let mut stmts = Vec::new(); + let mut sort = |ty: &Type, (reserve, stmt, _): &(String, String, String)| { + let mut handles = Vec::new(); + self.abi.api.handles(ty, &mut handles, &mut Vec::new()); + match (reserve.is_empty(), handles.is_empty()) { + (false, _) => { + reserves.push(reserve.clone()); + moves.push(stmt.clone()); + } + (true, false) => checks.push(stmt.clone()), + (true, true) => stmts.push(stmt.clone()), + } + }; let mut args = Vec::new(); let mut params = Vec::new(); let mut c_params = export.call.params.iter(); @@ -1469,12 +1649,13 @@ impl Gen<'_> { Receiver::Mut => Type::Ref(Mutability::Mut, Box::new(Type::Object(*i))), Receiver::Owned => Type::Object(*i), }; - let (stmt, arg) = self.arg(&ty, "this", "a_self", future); - stmts.push(stmt); - args.push(arg); + let made = self.arg(&ty, "this", "a_self", future); + sort(&ty, &made); + args.push(made.2); } value => { - stmts.push(format!("let a_self = {};", self.lift(value, "unsafe { *this }"))); + let made = (String::new(), format!("let a_self = {};", self.lift(value, "unsafe { *this }")), String::new()); + sort(value, &made); args.push(match receiver { Receiver::Owned => "a_self".to_string(), _ => "&a_self".to_string(), @@ -1501,7 +1682,7 @@ impl Gen<'_> { params.push(format!("p{n}_free: {}", rs(free))); } } - let (stmt, arg) = match &function.task { + let made = match &function.task { // A context made of C's waker, lent for the call. Some(Task { at, @@ -1512,6 +1693,7 @@ impl Gen<'_> { Mutability::Not => ("let", format!("&a{n}")), }; ( + String::new(), format!( "let a{n}_waker = p{n}.lift();\n\t{binding} a{n} = ::std::task::Context::from_waker(&a{n}_waker);" ), @@ -1519,20 +1701,34 @@ impl Gen<'_> { ) } _ if param.interned => ( + String::new(), format!("let a{n} = rt::intern(unsafe {{ p{n}.str(w) }});"), format!("a{n}"), ), // A future made of a `Poll` owns a future C implements, and lends it pinned to // each poll. Some(_) if function.future && param.ty == Type::Future => ( + String::new(), format!("let mut a{n} = p{n}.lift(w);"), format!("::std::pin::Pin::new(&mut a{n})"), ), _ => self.arg(¶m.ty, &format!("p{n}"), &format!("a{n}"), future), }; - stmts.push(stmt); - args.push(arg); + sort(¶m.ty, &made); + args.push(made.2); } + drop(sort); + let mut stmts = { + let mut all = stmts; + all.extend(checks); + if !reserves.is_empty() { + all.insert(0, "let mut takes = rt::Takes::default();".to_string()); + all.extend(reserves); + all.push("takes.commit();".to_string()); + } + all.extend(moves); + all + }; // A future made of a `Poll` calls the function at each poll, passing the poll's // context, or a value made from its waker and kept until the next poll, so what that // poll registers stays registered. @@ -1642,10 +1838,18 @@ impl Gen<'_> { .enumerate() .map(|(n, out)| format!("let {out} = outs[{n}];\n\t\t\t")) .collect(); + if let Some(status) = status { + params.push(format!("{status}: *mut i32")); + } + let finish = self.finish(function, &ok, ("v", "rt::READY", "rt::ERROR")); + let made = format!( + "{body}{now}\n\trt::Future::new(async move {{\n\t\tlet result = {later};\n\t\t::std::boxed::Box::new(move |outs: &[*mut ::std::ffi::c_void], err: *mut ::std::ffi::c_void| -> i32 {{\n\t\t\tlet _ = (outs, err);\n\t\t\t{bind}{finish}\n\t\t}}) as rt::Done\n\t}})", + ); + self.checking.set(false); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> *mut rt::Future {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{body}{now}\n\trt::Future::new(async move {{\n\t\tlet result = {later};\n\t\t::std::boxed::Box::new(move |outs: &[*mut ::std::ffi::c_void], err: *mut ::std::ffi::c_void| -> i32 {{\n\t\t\tlet _ = (outs, err);\n\t\t\t{bind}{}\n\t\t}}) as rt::Done\n\t}})\n}}\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> *mut rt::Future {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{}\n}}\n", params.join(", "), - self.finish(function, &ok, ("v", "rt::READY", "rt::ERROR")) + wrap(status, "*mut rt::Future", &made) )); let poll_params: Vec = poll.params[2..] .iter() @@ -1681,18 +1885,38 @@ impl Gen<'_> { None => self.finish(function, &function.ok, ("", "", "")), }; self.lends = false; + let call = match function.collect { + // An iterator's items, all taken now. + true => format!("::std::iter::Iterator::collect::<::std::vec::Vec<_>>({call})"), + false => call, + }; + self.checking.set(false); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> {ret} {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{body}\n\tlet result = {};{writes}\n\t{finish}\n}}\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> {ret} {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{}\n}}\n", params.join(", "), - match function.collect { - // An iterator's items, all taken now. - true => format!("::std::iter::Iterator::collect::<::std::vec::Vec<_>>({call})"), - false => call, - }, + wrap(status, &ret, &format!("{body}\n\tlet result = {call};{writes}\n\t{finish}")) )); } } +/// The body of a C function returning `ret`, which runs `body`, statements then the value +/// returned: if it takes `status`, as a closure whose failed check `?` returns, written +/// through `status`. +fn wrap(status: Option<&str>, ret: &str, body: &str) -> String { + let Some(status) = status else { + return body.to_string(); + }; + format!( + "let checked = (|| -> ::std::result::Result<{ret}, rt::Status> {{\n\t{body_ok}\n\t}})();\n\tmatch checked {{\n\t\tOk(v) => v,\n\t\tErr(s) => unsafe {{ rt::failed({status}, s, w) }},\n\t}}", + body_ok = ok_tail(body) + ) +} + +/// `body`, statements then a value, with the value wrapped in `Ok`. +fn ok_tail(body: &str) -> String { + format!("Ok({{\n\t{body}\n\t}})") +} + /// What a function returns, owned: each reference at its top or in a returned tuple as /// its clone's type, with the expression cloning `v` into it, if any is one. fn owned(ok: &Type) -> (Type, Option) { diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 75bcc9f94..bb38a9fe5 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -317,6 +317,7 @@ const TAKEN: &[&str] = &[ "BoundExcluded", "Bytes", "NewBytes", + "ErrBorrowed", "ErrClosed", "Handle_", "NewHandle_", @@ -565,6 +566,12 @@ impl<'a> Gen<'a> { var target_ uint64\n\n\ // On_ panics unless the library's target is among those of on, as the item what is.\n\ func On_(on uint64, what string) {{\n\tif on>>target_&1 == 0 {{\n\t\tpanic(what + \": it is not on this library's target\")\n\t}}\n}}\n\n\ + // Supported_ is the error a call of what returns, matching errors.ErrUnsupported,\n\ + // unless the library's target is among those of on, as the item what is.\n\ + func Supported_(on uint64, what string) error {{\n\tif on>>target_&1 == 0 {{\n\t\treturn checkError_{{what, \"it is not on this library's target\", 0}}\n\t}}\n\treturn nil\n}}\n\n\ + // Status_ is the error a call of what returns, given the status it wrote: nil for\n\ + // none.\n\ + func Status_(status int32, what string) error {{\n\tif status == 0 {{\n\t\treturn nil\n\t}}\n\treturn checkError_{{what, C.GoString(C.{p}_status_message(C.{p}_status(status))), status}}\n}}\n\n\ //export {go}_call\nfunc {go}_call(data unsafe.Pointer) {{\n\tgiven_(data).(func())()\n}}\n\n\ //export {go}_release\nfunc {go}_release(data unsafe.Pointer) {{\n\trelease_(data)\n}}\n\n\ //export {go}_poll\nfunc {go}_poll(data unsafe.Pointer, waker cWaker) C.bool {{\n\treturn C.bool(given_(data).(*pollable_).poll(waker))\n}}\n\n\ @@ -1384,6 +1391,28 @@ impl<'a> Gen<'a> { results, body, }; + // A check each trait's borrow can fail: `String` and `GoString` return no error, so + // theirs aborts, as do C's. + let status = matches!(ty, Type::Object(i) if self.abi.traits_status(*i)); + let nil = match status { + true => ", nil", + false => "", + }; + let status_error = match status { + true => self.qualify(Packages::RT, "Status_"), + false => String::new(), + }; + // The call `call`, binding its result to `r_` then returning `failed` of its status's + // error, if it can fail a check. + let checked = |call: &str, name: &str, failed: &dyn Fn(&str) -> String| match status { + true => format!( + "var c_status_ C.{}\nr_ := {}, &c_status_)\nif c_status_ != 0 {{\n\treturn {}\n}}\n", + self.abi.rt("status"), + call.strip_suffix(')').expect("a call"), + failed(&format!("{status_error}(int32(c_status_), \"{name}\")")) + ), + false => format!("r_ := {call}\n"), + }; // fmt's String and GoString return no error, so a closed one reads as the error. let text = |e: &str| format!("{e}.Error()"); let mut methods = Vec::new(); @@ -1394,7 +1423,7 @@ impl<'a> Gen<'a> { "String", Vec::new(), vec!["string".to_string()], - format!("{open}return liftString_(C.{c}_to_string({this}))"), + format!("{open}return liftString_(C.{c}_to_string({this}{nil}))"), )); } if t.debug { @@ -1404,7 +1433,7 @@ impl<'a> Gen<'a> { "GoString", Vec::new(), vec!["string".to_string()], - format!("{open}return liftString_(C.{c}_debug({this}))"), + format!("{open}return liftString_(C.{c}_debug({this}{nil}))"), )); } if object && t.clone { @@ -1414,7 +1443,10 @@ impl<'a> Gen<'a> { "Clone", Vec::new(), vec![name.clone(), "error".to_string()], - format!("{open}return own_{c}(C.{c}_clone({this})), nil"), + format!( + "{open}{}return own_{c}(r_), nil", + checked(&format!("C.{c}_clone({this})"), &format!("{c}_clone"), &|e| format!("nil, {e}")) + ), )); } if t.eq { @@ -1429,8 +1461,9 @@ impl<'a> Gen<'a> { }; let body = match (object, arena) { (true, _) => format!( - "{open}o_ := other.Handle_.Hold()\nif o_ == nil {{\n\treturn {}\n}}\ndefer unhold_(other.Handle_, other)\nreturn bool(C.{c}_eq({this}, (*C.{c})(o_))){ok}", - failed("other.Handle_.Closed()") + "{open}o_ := other.Handle_.Hold()\nif o_ == nil {{\n\treturn {}\n}}\ndefer unhold_(other.Handle_, other)\n{}return bool(r_){ok}", + failed("other.Handle_.Closed()"), + checked(&format!("C.{c}_eq({this}, (*C.{c})(o_))"), &format!("{c}_eq"), &failed) ), (false, true) if closable => format!( "{open}c_other_ := lower_{c}(a_, other)\nif a_.err != nil {{\n\treturn {}\n}}\nreturn bool(C.{c}_eq({this}, &c_other_)){ok}", @@ -2004,7 +2037,12 @@ impl<'a> Gen<'a> { Type::Object(i) => { self.convert(ty); let c = self.abi.object(*i); - args.push(format!("own_{c}(C.{c}_copy({p}))")); + // A failed check aborts, as Rust's call into Go has nowhere to return it. + let nil = match self.abi.traits_status(*i) { + true => ", nil", + false => "", + }; + args.push(format!("own_{c}(C.{c}_copy({p}{nil}))")); } ty => args.push(self.lift_lent(ty, p)), } @@ -2084,10 +2122,15 @@ impl<'a> Gen<'a> { let c = self.abi.object(i); let name = self.qualified(&ty); let local = self.named(&ty).1; + // A failed check aborts, as Rust's closure has nowhere to return it. + let nil = match self.abi.traits_status(i) { + true => ", nil", + false => "", + }; self.helper(&format!("returned_{c}"), |_| { format!( "// returned_{c} is a new handle to a copy of v, which Rust frees once it copies v out of it.\n\ - func returned_{c}(v *{name}) *C.{c} {{\n\tif v == nil {{\n\t\tpanic(\"a nil {local} returned to Rust\")\n\t}}\n\th := v.Handle_.Hold()\n\tif h == nil {{\n\t\tpanic(v.Handle_.Closed())\n\t}}\n\tdefer unhold_(v.Handle_, v)\n\treturn C.{c}_copy((*C.{c})(h))\n}}\n\n\ + func returned_{c}(v *{name}) *C.{c} {{\n\tif v == nil {{\n\t\tpanic(\"a nil {local} returned to Rust\")\n\t}}\n\th := v.Handle_.Hold()\n\tif h == nil {{\n\t\tpanic(v.Handle_.Closed())\n\t}}\n\tdefer unhold_(v.Handle_, v)\n\treturn C.{c}_copy((*C.{c})(h){nil})\n}}\n\n\ func returnedOption_{c}(v *{name}) *C.{c} {{\n\tif v == nil {{\n\t\treturn nil\n\t}}\n\treturn returned_{c}(v)\n}}\n\n" ) }); @@ -2186,8 +2229,12 @@ impl<'a> Gen<'a> { let mut results: Vec = oks.iter().map(|ty| self.go(ty)).collect(); // A call that takes an object, as its receiver or held in any value it takes, // returns an error once that object is closed. + let status = export.call.status().is_some(); + let everywhere = abi.api.everywhere(function.on); let closable = receiver.is_some() && owner_ty.is_some_and(|ty| self.holds(ty)) - || function.params.iter().any(|p| self.holds(&p.ty)); + || function.params.iter().any(|p| self.holds(&p.ty)) + || status + || !everywhere; if function.err.is_some() || function.future || closable { results.push("error".to_string()); } @@ -2343,8 +2390,17 @@ impl<'a> Gen<'a> { } } } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); + // A failed check writes its status, whose error the call returns. + let failed = status.then(|| { + args.push("&c_status_".to_string()); + let error = format!("{}(int32(c_status_), \"{c_name}\")", self.qualify(Packages::RT, "Status_")); + format!("if c_status_ != 0 {{\n\t{}\n}}", fail(&error)) + }); let mut body = Vec::new(); + if status { + body.push(format!("var c_status_ C.{}", abi.rt("status"))); + } if arena { body.push("a_ := &arena_{}\ndefer a_.finish()".to_string()); } @@ -2361,6 +2417,7 @@ impl<'a> Gen<'a> { if let Some((_, poll, free)) = &export.future { body.push(format!("f_ := C.{c_name}({})", args.join(", "))); + body.extend(failed); body.push(format!("defer C.{}(f_)", free.name)); let mut decls = Vec::new(); for (k, ty) in oks.iter().enumerate() { @@ -2401,8 +2458,31 @@ impl<'a> Gen<'a> { body.push(self.out_decl(n, ty)); } let mut call_args = args.clone(); + // The status follows every out. + let status_arg = status.then(|| call_args.pop().expect("the status")); call_args.extend(outs.iter().map(|(n, _)| format!("&c_{n}_"))); + call_args.extend(status_arg); let mut call = format!("C.{c_name}({})", call_args.join(", ")); + // Made first, so a failed check returns before any result is read. + let made = failed.map(|failed| { + let made = match (&err, &export.call.ret) { + (Some(_), _) => { + body.push(format!("ok_ := {call}")); + "ok_".to_string() + } + (None, Ty::Void) => { + body.push(call.clone()); + String::new() + } + // Lifted from the out the result names, as a value written beside one is. + (None, _) => { + body.push(format!("c_out_ := {call}")); + String::new() + } + }; + body.push(failed); + made + }); // Each value passed for `&mut` takes what Rust wrote back, on every path. let writes: Vec = (function.params.iter().zip(&names)) .filter(|(param, _)| param.ty.written()) @@ -2411,7 +2491,10 @@ impl<'a> Gen<'a> { format!("*{name} = {}", self.lift(param.ty.base(), &format!("c_{out}_"))) }) .collect(); - if !writes.is_empty() { + if let Some(made) = made { + body.extend(writes); + call = made; + } else if !writes.is_empty() { let made = match &err { Some(_) => { body.push(format!("ok_ := {call}")); @@ -2518,8 +2601,10 @@ impl<'a> Gen<'a> { "\n\nIt blocks until Rust's future is ready, or ctx is done: then it cancels the future in\nRust, losing nothing, and returns ctx's error.", ); } - if let Some(check) = self.on(function.on, &c_name) { - body.insert(0, check); + if !everywhere { + let supported = self.qualify(Packages::RT, "Supported_"); + let error = format!("{supported}({}, \"{c_name}\")", function.on.0 & abi.api.all().0); + body.insert(0, format!("if err_ := {error}; err_ != nil {{\n\t{}\n}}", fail("err_"))); } Method { docs: text, diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index 05db62e09..f8210742e 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -22,6 +22,28 @@ pub struct Api { } impl Api { + /// Each object whose handle a call borrows or moves as it lifts a value of `ty`, into + /// `out`: its own, and those its records and enums hold. `seen` holds the records and + /// enums already walked, as `(is_enum, index)`. + pub fn handles(&self, ty: &Type, out: &mut Vec, seen: &mut Vec<(bool, usize)>) { + match ty { + Type::Object(i) => out.push(*i), + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(_, inner) => { + self.handles(inner, out, seen) + } + Type::Tuple(elements) => elements.iter().for_each(|ty| self.handles(ty, out, seen)), + Type::Record(i) if !seen.contains(&(false, *i)) => { + seen.push((false, *i)); + (self.records[*i].fields.iter()).for_each(|f| self.handles(&f.ty, out, seen)); + } + Type::Enum(i) if !seen.contains(&(true, *i)) => { + seen.push((true, *i)); + (self.enums[*i].variants.iter().flat_map(|v| &v.fields)).for_each(|f| self.handles(&f.ty, out, seen)); + } + _ => {} + } + } + /// Whether an item on `on` is on every target the API serves. pub fn everywhere(&self, on: On) -> bool { on.0 & self.all().0 == self.all().0 diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index b2cb436f3..5e3fdfc4e 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -832,7 +832,7 @@ impl<'a> Gen<'a> { arg.exprs.push(format!("Native.Pollable({e}, {context})")); arg.rust_args.push(format!("rt::future(env, {p})")); } - Ty::Void | Ty::Future(_) | Ty::Poll | Ty::Out(_) | Ty::Data => { + Ty::Void | Ty::Future(_) | Ty::Poll | Ty::Status | Ty::Out(_) | Ty::Data => { unreachable!("{c:?} is no parameter Kotlin passes") } } @@ -1015,12 +1015,21 @@ impl<'a> Gen<'a> { ) } }; + // Each borrows its handles shared, throwing a failed check. + let status = matches!(ty, Type::Object(i) if self.abi.traits_status(*i)); + let checked = |call: &str, name: &str, then: &str| match status { + true => format!( + "let mut status = 0;\n\t\tlet ret = {}, &mut status);\n\t\tif status != 0 {{\n\t\t\treturn rt::raise(env, status, \"{name}\");\n\t\t}}\n\t\t{then}", + call.strip_suffix(')').expect("a call") + ), + false => format!("let ret = {call};\n\t\t{then}"), + }; let unary = |g: &mut Self, suffix: &str, sig: &str| { g.native( &format!("{bare}_{suffix}"), &[(jni_self, native_self)], ("jbyteArray", "ByteArray?"), - &format!("rt::owned(env, crate::{c}_{suffix}({rust_self}))"), + &checked(&format!("crate::{c}_{suffix}({rust_self})"), &format!("{c}_{suffix}"), "rt::owned(env, ret)"), ); format!("\t{sig} = {open}\n\t\tNative.{bare}_{suffix}({arg})!!.decodeToString()\n\t}}\n") }; @@ -1043,7 +1052,7 @@ impl<'a> Gen<'a> { &format!("{bare}_copy"), &[("jlong", "Long")], ("jlong", "Long"), - &format!("crate::{c}_copy(p0 as usize as _) as usize as jlong"), + &checked(&format!("crate::{c}_copy(p0 as usize as _)"), &format!("{c}_copy"), "ret as usize as jlong"), ); } if object && t.clone { @@ -1051,7 +1060,7 @@ impl<'a> Gen<'a> { &format!("{bare}_clone"), &[("jlong", "Long")], ("jlong", "Long"), - &format!("crate::{c}_clone(p0 as usize as _) as usize as jlong"), + &checked(&format!("crate::{c}_clone(p0 as usize as _)"), &format!("{c}_clone"), "ret as usize as jlong"), ); body.push(format!( "\t/** A new handle to a clone. */\n\tfun clone(): {class} = handle.hold {{ h -> {class}(Native.{bare}_clone(h)) }}\n" @@ -1062,7 +1071,7 @@ impl<'a> Gen<'a> { &format!("{bare}_eq"), &[("jlong", "Long"), ("jlong", "Long")], ("jboolean", "Boolean"), - &format!("crate::{c}_eq(p0 as usize as _, p1 as usize as _)"), + &checked(&format!("crate::{c}_eq(p0 as usize as _, p1 as usize as _)"), &format!("{c}_eq"), "ret"), ); body.push(format!( "\toverride fun equals(other: Any?): Boolean =\n\t\tother is {class} && handle.hold {{ a -> other.handle.hold {{ b -> Native.{bare}_eq(a, b) }} }}\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): Int = 0\n" @@ -1834,7 +1843,13 @@ impl<'a> Gen<'a> { (&kt, None), ); } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); + // A failed check writes its status, which the glue throws once the call returns. + let status = export.call.status().is_some(); + let check = match status { + true => format!("\n\t\tif status != 0 {{\n\t\t\treturn rt::raise(env, status, \"{c_name}\");\n\t\t}}"), + false => String::new(), + }; let native_params: Vec<(&str, String)> = args .iter() @@ -1844,7 +1859,10 @@ impl<'a> Gen<'a> { // A future's call also takes the signal its waker parks, if its first poll pends. let signal = export.future.is_some().then(|| self.fresh("s")); exprs.extend(signal.clone()); - let rust_pre: Vec = args.iter().flat_map(|a| a.rust_pre.clone()).collect(); + let mut rust_pre: Vec = args.iter().flat_map(|a| a.rust_pre.clone()).collect(); + if status { + rust_pre.push("let mut status = 0;".to_string()); + } let rust_args: Vec = args.iter().flat_map(|a| a.rust_args.clone()).collect(); let after: Vec = args.iter().flat_map(|a| a.after.clone()).collect(); @@ -1865,8 +1883,9 @@ impl<'a> Gen<'a> { // The value the function returns, or, returning nothing, its statement. let value; // The C call, with the out pointers after its arguments. + let status_arg = status.then(|| "&mut status".to_string()); let rust_call = |outs: &[String]| { - let args: Vec<&String> = rust_args.iter().chain(outs).collect(); + let args: Vec<&String> = rust_args.iter().chain(outs).chain(&status_arg).collect(); let args: Vec<&str> = args.iter().map(|a| a.as_str()).collect(); format!("crate::{c_name}({})", args.join(", ")) }; @@ -1898,7 +1917,7 @@ impl<'a> Gen<'a> { ¶ms, ("jbyteArray", "ByteArray"), &format!( - "{}\n\t\tlet f = {};\n\t\trt::start(env, f.cast(), {p_signal}, jni_{}, jni_{})", + "{}\n\t\tlet f = {};{check}\n\t\trt::start(env, f.cast(), {p_signal}, jni_{}, jni_{})", rust_pre.join("\n\t\t"), rust_call(&[]), poll.name, @@ -1947,7 +1966,7 @@ impl<'a> Gen<'a> { ¶ms, ("jbyteArray", "ByteArray"), &format!( - "{}\n\t\t{decl}\n\t\t{ret_set}{};\n\t\tlet mut w = rt::Writer::default();\n\t\t{write}\n\t\tw.array(env)", + "{}\n\t\t{decl}\n\t\t{ret_set}{};{check}\n\t\tlet mut w = rt::Writer::default();\n\t\t{write}\n\t\tw.array(env)", rust_pre.join("\n\t\t"), rust_call(&ptrs) ), @@ -1997,7 +2016,7 @@ impl<'a> Gen<'a> { ¶ms, (jni, native), &format!( - "{}\n\t\tlet ret = {};\n\t\t{ret_expr}", + "{}\n\t\tlet ret = {};{check}\n\t\t{ret_expr}", rust_pre.join("\n\t\t"), rust_call(&[]) ), @@ -2287,7 +2306,7 @@ fn jni_type(c: &Ty, ty: &Type) -> (&'static str, &'static str) { Ty::Waker => ("jobject", "Waker"), Ty::ForeignReader => ("jobject", "java.nio.channels.SeekableByteChannel"), Ty::ForeignFuture => ("jobject", "Any"), - Ty::Poll | Ty::Out(_) | Ty::Data => unreachable!("{c:?} never crosses JNI"), + Ty::Poll | Ty::Status | Ty::Out(_) | Ty::Data => unreachable!("{c:?} never crosses JNI"), } } diff --git a/ffi/src/python.rs b/ffi/src/python.rs index 94419d55e..e2f4cce42 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -818,6 +818,16 @@ impl<'a> Gen<'a> { true => "this", false => "&this", }; + // An object whose run-time checks may fail passes each its status, which raises. + let checked = matches!(ty, Type::Object(i) if self.abi.traits_status(*i)); + let (status, arg) = match checked { + true => ("let mut status = 0;\n\t\t", ", &mut status"), + false => ("", ""), + }; + let check = |suffix: &str| match checked { + true => format!("\n\t\ts.status(status, \"{c}_{suffix}\")?;"), + false => String::new(), + }; let unary = |g: &mut Self, suffix: &str, member: &str, docs: &str, lower: String| { let d = Direct { member, @@ -831,7 +841,8 @@ impl<'a> Gen<'a> { &format!("{bare}_{suffix}"), d, &format!( - "let this = {this}?;\n\t\tlet ret = rt::call(|| crate::{c}_{suffix}({pass}));\n\t\ts.done();\n\t\t{lower}" + "let this = {this}?;\n\t\t{status}let ret = rt::call(|| crate::{c}_{suffix}({pass}{arg}));{}\n\t\ts.done();\n\t\t{lower}", + check(suffix) ), ); }; @@ -861,7 +872,8 @@ impl<'a> Gen<'a> { &format!("{bare}_eq"), 2, &format!( - "let this = {this}?;\n\t\tlet other = {other}?;\n\t\tlet ret = rt::call(|| crate::{c}_eq(this, other));\n\t\ts.done();\n\t\trt::bool_out(ret)" + "let this = {this}?;\n\t\tlet other = {other}?;\n\t\t{status}let ret = rt::call(|| crate::{c}_eq(this, other{arg}));{}\n\t\ts.done();\n\t\trt::bool_out(ret)", + check("eq") ), ); // Python makes a class that defines `__eq__` alone unhashable, as `Eq` without `Hash` is. @@ -1312,10 +1324,15 @@ impl<'a> Gen<'a> { let abi = self.abi; match ty { Type::String => format!("rt::str_given({o})"), + // A closure's return has no caller to raise to, so a failed check aborts. Type::Object(i) => format!( - "rt::hold({o}, {}, s, false).map(|h| crate::{}_copy(h))", + "rt::hold({o}, {}, s, false).map(|h| crate::{}_copy(h{}))", self.t(&abi.object(*i)), - abi.object(*i) + abi.object(*i), + match abi.traits_status(*i) { + true => ", std::ptr::null_mut()", + false => "", + } ), Type::Option(inner) if matches!(**inner, Type::Object(_)) => { let some = self.given(inner, o); @@ -1579,7 +1596,16 @@ impl<'a> Gen<'a> { exception.as_deref(), ); } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); + // A call that fails a run-time check writes its status, which raises once it returns. + let (status, check) = match export.call.status() { + Some(_) => ( + "let mut status = 0;\n\t\t".to_string(), + format!("\n\t\ts.status(status, \"{c_name}\")?;"), + ), + None => (String::new(), String::new()), + }; + let status_arg = export.call.status().map(|_| "&mut status".to_string()); // The outs follow the unstripped return: a borrowed tuple is one struct, not its elements. let ok = strip_refs(&function.ok); @@ -1603,8 +1629,8 @@ impl<'a> Gen<'a> { free.name, ); stmts.push(format!( - "let f = rt::call(|| crate::{c_name}({}));\n\t\ts.done();\n\t\trt::start(f.cast(), poll_{c_name}, free_{c_name}, s, {})", - c_args.join(", "), + "{status}let f = rt::call(|| crate::{c_name}({}));{check}\n\t\ts.done();\n\t\trt::start(f.cast(), poll_{c_name}, free_{c_name}, s, {})", + c_args.iter().chain(&status_arg).cloned().collect::>().join(", "), function.eager )); } else { @@ -1615,9 +1641,9 @@ impl<'a> Gen<'a> { } false => self.outs(&outs, &written, &function.ok, err.as_ref(), exception.as_deref(), "ret"), }; - let args: Vec = c_args.iter().cloned().chain(ptrs).collect(); + let args: Vec = c_args.iter().cloned().chain(ptrs).chain(status_arg).collect(); stmts.push(format!( - "{decl}\n\t\tlet ret = rt::call(|| crate::{c_name}({}));\n\t\ts.done();\n\t\t{value}", + "{decl}\n\t\t{status}let ret = rt::call(|| crate::{c_name}({}));{check}\n\t\ts.done();\n\t\t{value}", args.join(", ") )); } diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index aee38ffb1..f86a8cdb1 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -88,8 +88,12 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result Swift.Bool {{\n\t\t{p}_contract() == 0x{contract:016x} && {upper}_CONTRACT == 0x{contract:016x}\n\t}}\n\n\ \t/// The index of the library's target among those this file serves.\n\ \tstatic let target = Swift.UInt64({p}_target())\n\n\ - \t/// Traps unless the library's target is among those of `on`, as the item `what` is.\n\ - \tstatic func on(_ on: Swift.UInt64, _ what: Swift.String) {{\n\t\tif (on >> target) & 1 == 0 {{\n\t\t\tfatalError(\"\\(what): it is not on this library's target\")\n\t\t}}\n\t}}\n\n\ + \t/// Throws unless the library's target is among those of `on`, as the item `what` is.\n\ + \tstatic func on(_ on: Swift.UInt64, _ what: Swift.String) throws {{\n\t\tif (on >> target) & 1 == 0 {{\n\t\t\tthrow CheckError(function: what, message: \"it is not on this library's target\")\n\t\t}}\n\t}}\n\n\ + \t/// Traps unless the library's target is among those of `on`, where nothing can throw.\n\ + \tstatic func onOrTrap(_ on: Swift.UInt64, _ what: Swift.String) {{\n\t\tif (on >> target) & 1 == 0 {{\n\t\t\tfatalError(\"\\(what): it is not on this library's target\")\n\t\t}}\n\t}}\n\n\ + \t/// Throws what `status`, written by a call of `what`, says it failed, if anything.\n\ + \t@inline(__always)\n\tstatic func status(_ status: Swift.Int32, _ what: Swift.StaticString) throws {{\n\t\tif status != 0 {{\n\t\t\tthrow CheckError(function: \"\\(what)\", message: Swift.String(cString: {p}_status_message(status)))\n\t\t}}\n\t}}\n\n\ \tstatic func free(_ buf: Buf) {{\n\t\t{p}_buf_free(buf)\n\t}}\n\n\ \t/// A buffer Rust owns, holding a copy of what `view` lends.\n\ \tstatic func copy(_ view: Buf) -> Buf {{\n\t\t{p}_buf_copy(view)\n\t}}\n\n\ @@ -138,6 +142,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result { impl<'a> Gen<'a> { /// The statement that traps unless the library's target is among those of `on`, as the /// item `what` is, then `then`; or `then` alone if it is on every target. + /// The check that the library's target is among those of `on`, as the item `what` is, + /// which traps, followed by `then`. fn on(&self, on: On, what: &str, then: &str) -> String { let api = self.abi.api; match api.everywhere(on) { true => then.to_string(), - false => format!("Native.on({}, \"{what}\"){then}", on.0 & api.all().0), + false => format!("Native.onOrTrap({}, \"{what}\"){then}", on.0 & api.all().0), + } + } + + /// The status a trait function of object `i` takes, if any: none, so a failed check + /// aborts, where nothing can throw. + fn nil(&self, i: usize) -> &'static str { + match self.abi.traits_status(i) { + true => ", nil", + false => "", } } @@ -737,6 +753,11 @@ impl<'a> Gen<'a> { let name = self.sw(ty); let object = matches!(ty, Type::Object(_)); let unique = self.unique(ty); + // A failed check aborts with its message, as none of these can throw. + let nil = match ty { + Type::Object(i) if self.abi.traits_status(*i) => ", nil", + _ => "", + }; // The receiver: a struct's handle, a class's held, or the value lowered. let (open, this, close) = match object { true if unique => (String::new(), "self.handle", ""), @@ -766,7 +787,7 @@ impl<'a> Gen<'a> { members.push("description".to_string()); body.push(format!( "\tpublic var description: Swift.String {{\n\t\t{}\n\t}}\n", - text(&open, &format!("{c}_to_string({this})")) + text(&open, &format!("{c}_to_string({this}{nil})")) )); } if t.debug { @@ -774,14 +795,14 @@ impl<'a> Gen<'a> { members.push("debugDescription".to_string()); body.push(format!( "\t/// Rust's `Debug` form.\n\tpublic var debugDescription: Swift.String {{\n\t\t{}\n\t}}\n", - text(&open, &format!("{c}_debug({this})")) + text(&open, &format!("{c}_debug({this}{nil})")) )); } if object && t.clone { members.push("clone".to_string()); let raw = match unique { - true => format!("{c}_clone(self.handle)"), - false => format!("self.handle.with {{ {c}_clone($0) }}"), + true => format!("{c}_clone(self.handle{nil})"), + false => format!("self.handle.with {{ {c}_clone($0{nil}) }}"), }; body.push(format!( "\t/// A new handle to a clone.\n\tpublic func clone() -> {name} {{\n\t\t{name}(raw: {raw})\n\t}}\n" @@ -790,8 +811,8 @@ impl<'a> Gen<'a> { if t.eq { protocols.push("Equatable"); let compare = match object { - true if unique => format!("{c}_eq(a.handle, b.handle)"), - true => format!("a.handle.with {{ a_ in b.handle.with {{ b_ in {c}_eq(a_, b_) }} }}"), + true if unique => format!("{c}_eq(a.handle, b.handle{nil})"), + true => format!("a.handle.with {{ a_ in b.handle.with {{ b_ in {c}_eq(a_, b_{nil}) }} }}"), false => { let arena = match self.arena(ty) { true => "let a_ = Native.Arena()\n\t\tdefer { a_.finish() }\n\t\t", @@ -1171,7 +1192,7 @@ impl<'a> Gen<'a> { /// How the call passes `param`, the C parameter `c`, whose Swift value is `e`, the /// `k`th, which `function` takes. - fn arg(&mut self, ty: &Type, c: &Ty, e: &str, k: usize, tr: &str) -> (Arg, bool) { + fn arg(&mut self, ty: &Type, c: &Ty, e: &str, k: usize, tr: &str, what: &str) -> (Arg, bool) { let abi = self.abi; let mut arg = Arg::default(); let mut arena = false; @@ -1219,10 +1240,10 @@ impl<'a> Gen<'a> { (ty, Ty::Handle { .. }) => { let h = format!("h{k}_"); let open = match ty.base() { - Type::Option(_) => format!("{tr}Native.with({e}?.handle) {{ {h} in"), - _ => format!("{tr}{e}.handle.with {{ {h} in"), + Type::Option(_) => format!("{tr}Native.with({what}{e}?.handle, {{ {h} in"), + _ => format!("{tr}{e}.handle.with({what}{{ {h} in"), }; - arg.open = Some((open, "}".to_string())); + arg.open = Some((open, "})".to_string())); arg.exprs.push(h); } (ty, _) => { @@ -1276,7 +1297,12 @@ impl<'a> Gen<'a> { args.push(l); } // A `Copy` object, which Swift's class holds a copy of. - Type::Object(i) => args.push(format!("{}(raw: {}_copy({p}))", self.sw(ty), self.abi.object(*i))), + Type::Object(i) => args.push(format!( + "{}(raw: {}_copy({p}{}))", + self.sw(ty), + self.abi.object(*i), + self.nil(*i) + )), ty => args.push(self.lift_lent(ty, p)), } } @@ -1331,14 +1357,16 @@ impl<'a> Gen<'a> { // Kept alive while its handle is copied: once `.handle` is read, ARC may release // the object, whose deinit would free the handle first. Type::Object(i) => format!( - "withExtendedLifetime({e}) {{ r_ in r_.handle.with {{ {}_copy($0) }} }}", - abi.object(*i) + "withExtendedLifetime({e}) {{ r_ in r_.handle.with {{ {}_copy($0{}) }} }}", + abi.object(*i), + self.nil(*i) ), Type::Option(inner) => match &**inner { Type::Str | Type::String => format!("({e}.map {{ Native.owned($0) }} ?? Native.none)"), Type::Object(i) => format!( - "withExtendedLifetime({e}) {{ r_ in r_?.handle.with {{ {}_copy($0) }} ?? nil }}", - abi.object(*i) + "withExtendedLifetime({e}) {{ r_ in r_?.handle.with {{ {}_copy($0{}) }} ?? nil }}", + abi.object(*i), + self.nil(*i) ), _ => self.lower(ty, e), }, @@ -1422,18 +1450,26 @@ impl<'a> Gen<'a> { Some(Receiver::Mut) if unique => "mutating ", _ => "", }; - // Only a future or a `Result` throws, and a future only once it is awaited. - let throws = function.future || function.err.is_some(); - let tr = match function.err.is_some() && !function.future { + // A call that can fail a check throws, as do a future and a `Result`, a future's + // error only once it is awaited. + let status = export.call.status().is_some(); + let everywhere = abi.api.everywhere(function.on); + let throws = function.future || function.err.is_some() || status || !everywhere; + let tr = match (function.err.is_some() && !function.future) || status { true => "try ", false => "", }; + // A class's handle closed throws where the call can. + let what = match status { + true => format!("\"{c_name}\", "), + false => String::new(), + }; let mut prelude = Vec::new(); if !object { prelude.push("Native.check()".to_string()); } - if !abi.api.everywhere(function.on) { - prelude.push(self.on(function.on, &c_name, "")); + if !everywhere { + prelude.push(format!("try Native.on({}, \"{c_name}\")", function.on.0 & abi.api.all().0)); } let mut arena = false; let mut args: Vec = Vec::new(); @@ -1445,7 +1481,7 @@ impl<'a> Gen<'a> { match c { Ty::Handle { .. } if unique => arg.exprs.push("self.handle".to_string()), Ty::Handle { .. } => { - arg.open = Some((format!("{tr}self.handle.with {{ h_ in"), "}".to_string())); + arg.open = Some((format!("{tr}self.handle.with({what}{{ h_ in"), "})".to_string())); arg.exprs.push("h_".to_string()); let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); if receiver == Receiver::Owned && !copy { @@ -1500,11 +1536,11 @@ impl<'a> Gen<'a> { (false, _) => self.ownership(¶m.ty), }; params.push(format!("_ {name}: {ownership}{}", self.sw(¶m.ty))); - let (arg, needs) = self.arg(¶m.ty, c, name, k, tr); + let (arg, needs) = self.arg(¶m.ty, c, name, k, tr, &what); arena |= needs; args.push(arg); } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); // Each value passed for `&mut` takes what Rust wrote back, once the call's own // access to it ends, on every path, a throw included. let mut written = Vec::new(); @@ -1540,9 +1576,28 @@ impl<'a> Gen<'a> { let thrown = err.as_ref().map(|e| self.thrown(e)); let mut inner = Vec::new(); + // A failed check writes its status, which throws once the call returns. + let (status_arg, check) = match status { + true => { + inner.push("var c_status_: Swift.Int32 = 0".to_string()); + ( + vec!["&c_status_".to_string()], + Some(format!("try Native.status(c_status_, \"{c_name}\")")), + ) + } + false => (Vec::new(), None), + }; let tail; if let Some((_, poll, free)) = &export.future { - inner.push(format!("{c_name}({})", exprs.join(", "))); + let call = format!("{c_name}({})", exprs.iter().chain(&status_arg).cloned().collect::>().join(", ")); + match &check { + Some(check) => { + inner.push(format!("let f_ = {call}")); + inner.push(check.clone()); + inner.push("return f_".to_string()); + } + None => inner.push(call), + } let poll_outs: Vec<(String, Ty)> = poll.params[2..].to_vec(); let mut body: Vec = poll_outs.iter().map(|(n, ty)| self.out_decl(n, ty)).collect(); let mut call_args = vec!["f_".to_string(), "w_".to_string()]; @@ -1580,6 +1635,7 @@ impl<'a> Gen<'a> { } let mut call_args = exprs.clone(); call_args.extend(outs.iter().map(|(n, _)| format!("&c_{n}_"))); + call_args.extend(status_arg); let call = format!("{c_name}({})", call_args.join(", ")); match (&err, &ok) { (Some(err), ok) => { @@ -1587,21 +1643,33 @@ impl<'a> Gen<'a> { Type::Unit => "return".to_string(), _ => format!("return {}", self.lift_outs(&function.ok, ok)), }; - inner.push(format!("if {call} {{\n\t{ready}\n}}")); + match &check { + Some(check) => { + inner.push(format!("let c_ok_ = {call}")); + inner.push(check.clone()); + inner.push(format!("if c_ok_ {{\n\t{ready}\n}}")); + } + None => inner.push(format!("if {call} {{\n\t{ready}\n}}")), + } let lifted = self.lift(err, "c_err_"); inner.push(format!( "throw {}", thrown_value(thrown.as_ref().expect("an error throws"), &lifted) )); } - (None, Type::Unit) => inner.push(call), + (None, Type::Unit) => { + inner.push(call); + inner.extend(check); + } // A value C returns beside what Rust wrote back is its return, as with none. (None, ok) if outs.iter().any(|(n, _)| !written.contains(n)) => { inner.push(call); + inner.extend(check); inner.push(format!("return {}", self.lift_outs(&function.ok, ok))); } (None, ok) => { inner.push(format!("let c_ret_ = {call}")); + inner.extend(check); inner.push(format!("return {}", self.lift(ok, "c_ret_"))); } } @@ -1621,6 +1689,11 @@ impl<'a> Gen<'a> { } let mut body = prelude; match tail { + // Made by a call of several lines, unwrapped, which binds `f_` itself. + Some(tail) if code.starts_with("var c_status_") => { + body.push(code.trim_end_matches("\nreturn f_").to_string()); + body.push(tail); + } Some(tail) => { let create = match code.starts_with("return ") { true => code["return ".len()..].to_string(), @@ -1676,8 +1749,14 @@ impl<'a> Gen<'a> { }; let body = body.join("\n"); let var = function.params.is_empty() && !throws; + // A field read stays a property though a check of its handle may fail, which throws + // from its getter, unless Swift cannot copy its value, whose getter cannot throw. + let getter = + throws && !function.future && function.err.is_none() && everywhere && !self.unique(&function.ok); let var = match (function.access, receiver) { - (Access::Field { set: false, .. }, Some(_)) => var && mutation.is_empty(), + (Access::Field { set: false, .. }, Some(_)) => { + function.params.is_empty() && (!throws || getter) && mutation.is_empty() + } (Access::Const, None) => { if var { member = member.trim_matches('`').to_uppercase(); @@ -1708,6 +1787,10 @@ impl<'a> Gen<'a> { params.join(", ") ), }; + let body = match var && throws { + true => format!("get throws {{\n{}\n}}", indent(&body)), + false => body, + }; let code = format!("{}{head} {{\n{}\n}}\n", docs(text.trim(), ""), indent(&body)); (code, full(is_static, &member, labels)) } diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 3354b6ff0..9415254b5 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -137,12 +137,12 @@ static api_buf made_times(void *data, uint32_t n) { /* reissue's closures: a new ticket, and a copy of one C keeps, each Rust's to free. */ static api_Ticket *reissue_next(void *data, const api_Ticket *ticket) { (void)data; - return api_Ticket_new(api_Ticket_number(ticket) + 1); + return api_Ticket_new(api_Ticket_number(ticket, NULL) + 1); } static api_Ticket *reissue_kept(void *data, const api_Ticket *ticket) { (void)ticket; - return api_Ticket_copy(data); + return api_Ticket_copy(data, NULL); } static uint32_t spawned_task(void *data) { @@ -173,13 +173,13 @@ static bool drive_poll(void *data, const api_Instant *now, const api_Waiter *wai driver *d = data; d->calls++; if (d->calls == 1) { - *out = api_later(now, (api_duration){.secs = 0, .nanos = 5000000}); + *out = api_later(now, (api_duration){.secs = 0, .nanos = 5000000}, NULL); return true; } if (d->calls == 2) { - api_duration since = api_since(now, d->start); + api_duration since = api_since(now, d->start, NULL); printf("drive %llu\n", (unsigned long long)(since.secs * 1000 + since.nanos / 1000000)); - api_waker returned = api_Waiter_waker(waiter); + api_waker returned = api_Waiter_waker(waiter, NULL); returned.wake(returned.data); returned.drop(returned.data); *out = NULL; @@ -282,9 +282,9 @@ static bool closures(void) { /* A closure taking and returning `Copy` objects copies them, never running their `Clone`. */ api_Ticket *kept = api_Ticket_new(9); api_Ticket *seven = api_Ticket_new(7); - api_Ticket *given = api_reissue(seven, reissue_next, NULL); - api_Ticket *held = api_reissue(seven, reissue_kept, kept); - printf("reissued %u %u, cloned %u\n", (unsigned)api_Ticket_number(given), (unsigned)api_Ticket_number(held), + api_Ticket *given = api_reissue(seven, reissue_next, NULL, NULL); + api_Ticket *held = api_reissue(seven, reissue_kept, kept, NULL); + printf("reissued %u %u, cloned %u\n", (unsigned)api_Ticket_number(given, NULL), (unsigned)api_Ticket_number(held, NULL), (unsigned)api_Ticket_clones()); api_Ticket_free(held); api_Ticket_free(given); @@ -294,7 +294,7 @@ static bool closures(void) { /* A kept closure, called on another thread after the call that took it returned. */ bool spawned_freed = false; api_Spawned *spawned = api_Spawned_new(spawned_task, &spawned_freed, spawned_free); - printf("spawned %u\n", (unsigned)api_Spawned_join(spawned)); + printf("spawned %u\n", (unsigned)api_Spawned_join(spawned, NULL)); api_Spawned_free(spawned); if (!spawned_freed) { return false; @@ -303,7 +303,7 @@ static bool closures(void) { api_waker waker = {.wake = wake, .drop = drop, .data = NULL}; api_Instant *start = api_now(); driver d = {.start = start}; - api_drive_future *drive = api_drive(start, drive_poll, &d, drive_free); + api_drive_future *drive = api_drive(start, drive_poll, &d, drive_free, NULL); api_buf stopped; int woken = wakes; while (api_drive_poll(drive, waker, &stopped) == API_POLL_PENDING) { @@ -314,7 +314,7 @@ static bool closures(void) { /* Dropped before it finishes, it frees the closure it kept. */ driver idle = {.start = start}; - drive = api_drive(start, drive_idle, &idle, drive_free); + drive = api_drive(start, drive_idle, &idle, drive_free, NULL); ok = ok && api_drive_poll(drive, waker, &stopped) == API_POLL_PENDING && !idle.freed; api_drive_future_free(drive); ok = ok && idle.freed; @@ -350,8 +350,8 @@ static void log_free(void *data) { /* A lent closure: adds data's amount to the counter lent for the call. */ static bool edit(void *data, api_Counter *counter) { - api_Counter_add(counter, *(const uint64_t *)data, NULL, NULL); - return api_Counter_edited(counter) && api_Counter_get(counter) == 6; + api_Counter_add(counter, *(const uint64_t *)data, NULL, NULL, NULL); + return api_Counter_edited(counter, NULL) && api_Counter_get(counter, NULL) == 6; } /* Lent closures borrowing their object shared: one `Sync`, one not. */ @@ -368,7 +368,7 @@ static uint32_t triple(void *data, uint32_t side) { static uint64_t visit(void *data, const api_Cursor *cursor) { (void)data; - return api_Cursor_at(cursor) * 2; + return api_Cursor_at(cursor, NULL) * 2; } static void print_visit(void *data, uint32_t at) { @@ -378,7 +378,7 @@ static void print_visit(void *data, uint32_t at) { static uint32_t peek_tally(void *data, const api_Tally *tally) { (void)data; - return api_Tally_bump(tally); + return api_Tally_bump(tally, NULL); } /* A lent closure, which Rust calls on another thread: keeps levels above data's. */ @@ -396,7 +396,7 @@ static bool free_lent(void *data, api_Counter *counter) { static bool move_lent(void *data, api_Counter *counter) { (void)data; - api_Label label = api_Counter_into_label(counter, (api_option_Point){0}); + api_Label label = api_Counter_into_label(counter, (api_option_Point){0}, NULL); api_Label_free(&label); return false; } @@ -471,33 +471,33 @@ static int script(void) { printf("default %d %d\n", d.x, d.y); api_Counter *c = api_Counter_new(str("c"), 5); - printf("get %llu\n", (unsigned long long)api_Counter_get(c)); - print("name ", api_Counter_name(c), "\n"); + printf("get %llu\n", (unsigned long long)api_Counter_get(c, NULL)); + print("name ", api_Counter_name(c, NULL), "\n"); uint64_t v; api_Error err; - if (api_Counter_add(c, 10, &v, &err)) { + if (api_Counter_add(c, 10, &v, &err, NULL)) { printf("add ok %llu\n", (unsigned long long)v); } - if (!api_Counter_add(c, 2000, &v, &err)) { + if (!api_Counter_add(c, 2000, &v, &err, NULL)) { print("add err ", api_Error_to_string(&err), " "); print("", api_Error_debug(&err), "\n"); api_Error_free(&err); } - if (!api_Counter_sub(c, 20, &v, &err)) { + if (!api_Counter_sub(c, 20, &v, &err, NULL)) { printf("sub err underflow by %llu ", (unsigned long long)err.underflow.by); print("", api_Error_debug(&err), "\n"); api_Error_free(&err); } - api_Counter_rename(c, str("d")); - print("name ", api_Counter_name(c), "\n"); - printf("is d %s\n", api_Counter_is(c, str("d")) ? "true" : "false"); - api_Counter *copy = api_Counter_clone(c); - api_Counter_add(copy, 1, NULL, NULL); - printf("clone %llu original %llu\n", (unsigned long long)api_Counter_get(copy), - (unsigned long long)api_Counter_get(c)); + api_Counter_rename(c, str("d"), NULL); + print("name ", api_Counter_name(c, NULL), "\n"); + printf("is d %s\n", api_Counter_is(c, str("d"), NULL) ? "true" : "false"); + api_Counter *copy = api_Counter_clone(c, NULL); + api_Counter_add(copy, 1, NULL, NULL, NULL); + printf("clone %llu original %llu\n", (unsigned long long)api_Counter_get(copy, NULL), + (unsigned long long)api_Counter_get(c, NULL)); api_Counter_free(copy); - print("debug ", api_Counter_debug(c), "\n"); - api_Label label = api_Counter_into_label(c, (api_option_Point){.some = true, .value = p}); + print("debug ", api_Counter_debug(c, NULL), "\n"); + api_Label label = api_Counter_into_label(c, (api_option_Point){.some = true, .value = p}, NULL); api_Counter_free(c); print("label ", api_Label_debug(&label), "\n"); api_Label_free(&label); @@ -508,20 +508,20 @@ static int script(void) { /* A tuple returns through one out pointer per element. */ api_Counter *whole = api_Counter_new(str("s"), 7); api_Counter *below, *above; - api_Counter_split(whole, 5, &below, &above); + api_Counter_split(whole, 5, &below, &above, NULL); api_Counter_free(whole); - print("split ", api_Counter_name(below), " "); - printf("%llu ", (unsigned long long)api_Counter_get(below)); - print("", api_Counter_name(above), " "); - printf("%llu\n", (unsigned long long)api_Counter_get(above)); + print("split ", api_Counter_name(below, NULL), " "); + printf("%llu ", (unsigned long long)api_Counter_get(below, NULL)); + print("", api_Counter_name(above, NULL), " "); + printf("%llu\n", (unsigned long long)api_Counter_get(above, NULL)); api_Counter_free(below); api_Counter_free(above); api_Counter *one = api_Counter_new(str("o"), 3); api_Counter *only; - api_only(one, &only); + api_only(one, &only, NULL); api_Counter_free(one); - print("only ", api_Counter_name(only), " "); - printf("%llu\n", (unsigned long long)api_Counter_get(only)); + print("only ", api_Counter_name(only, NULL), " "); + printf("%llu\n", (unsigned long long)api_Counter_get(only, NULL)); api_Counter_free(only); uint8_t w8; int16_t w16; @@ -575,15 +575,15 @@ static int script(void) { {._0 = str("low"), ._1 = api_Counter_new(str("l"), 1)}, {._0 = str("high"), ._1 = api_Counter_new(str("h"), 9)}, }; - api_Board *board = api_Board_new((api_vec_tuple_string_Counter){.ptr = meters, .len = 2}); + api_Board *board = api_Board_new((api_vec_tuple_string_Counter){.ptr = meters, .len = 2}, NULL); api_Counter_free(meters[0]._1); api_Counter_free(meters[1]._1); api_vec_tuple_string_Counter ranked = api_Board_ranked(board); printf("ranked"); for (size_t i = 0; i < ranked.len; i++) { printf("%s%.*s ", i == 0 ? " " : ", ", (int)ranked.ptr[i]._0.len, (const char *)ranked.ptr[i]._0.ptr); - print("", api_Counter_name(ranked.ptr[i]._1), " "); - printf("%llu", (unsigned long long)api_Counter_get(ranked.ptr[i]._1)); + print("", api_Counter_name(ranked.ptr[i]._1, NULL), " "); + printf("%llu", (unsigned long long)api_Counter_get(ranked.ptr[i]._1, NULL)); } printf("\n"); api_vec_tuple_string_Counter_free(ranked); @@ -675,35 +675,35 @@ static int script(void) { print("named Some((\"", named.value._0, "\",))\n"); /* Each lent counter is a new handle to a clone. */ - api_Registry *registry = api_Registry_new(api_Counter_new(str("first"), 1)); - printf("last %s\n", api_Registry_last(registry) != NULL ? "true" : "false"); - api_Registry_push(registry, api_Counter_new(str("last"), 2)); - api_Counter *first = api_Registry_first_get(registry); - api_Counter_add(first, 10, NULL, NULL); - api_Counter *lent = api_Registry_first_get(registry); - printf("first %llu lent %llu\n", (unsigned long long)api_Counter_get(lent), - (unsigned long long)api_Counter_get(first)); + api_Registry *registry = api_Registry_new(api_Counter_new(str("first"), 1), NULL); + printf("last %s\n", api_Registry_last(registry, NULL) != NULL ? "true" : "false"); + api_Registry_push(registry, api_Counter_new(str("last"), 2), NULL); + api_Counter *first = api_Registry_first_get(registry, NULL); + api_Counter_add(first, 10, NULL, NULL, NULL); + api_Counter *lent = api_Registry_first_get(registry, NULL); + printf("first %llu lent %llu\n", (unsigned long long)api_Counter_get(lent, NULL), + (unsigned long long)api_Counter_get(first, NULL)); api_Counter_free(lent); api_Counter_free(first); - api_Counter *last = api_Registry_last(registry); - api_Counter_add(last, 10, NULL, NULL); - lent = api_Registry_last(registry); - printf("last %llu lent %llu\n", (unsigned long long)api_Counter_get(lent), - (unsigned long long)api_Counter_get(last)); + api_Counter *last = api_Registry_last(registry, NULL); + api_Counter_add(last, 10, NULL, NULL, NULL); + lent = api_Registry_last(registry, NULL); + printf("last %llu lent %llu\n", (unsigned long long)api_Counter_get(lent, NULL), + (unsigned long long)api_Counter_get(last, NULL)); api_Counter_free(lent); api_Counter_free(last); uint64_t by = 5; - bool edited = api_Registry_edit(registry, edit, &by); - first = api_Registry_first_get(registry); - printf("edit %s %llu\n", edited ? "true" : "false", (unsigned long long)api_Counter_get(first)); + bool edited = api_Registry_edit(registry, edit, &by, NULL); + first = api_Registry_first_get(registry, NULL); + printf("edit %s %llu\n", edited ? "true" : "false", (unsigned long long)api_Counter_get(first, NULL)); api_Counter_free(first); /* A setter moves an object in, as assignment does, leaving the handle to be freed. */ api_Counter *second = api_Counter_new(str("second"), 3); - api_Registry_first_set(registry, second); + api_Registry_first_set(registry, second, NULL); api_Counter_free(second); - first = api_Registry_first_get(registry); - printf("set first %llu ", (unsigned long long)api_Counter_get(first)); - print("", api_Counter_name(first), "\n"); + first = api_Registry_first_get(registry, NULL); + printf("set first %llu ", (unsigned long long)api_Counter_get(first, NULL)); + print("", api_Counter_name(first, NULL), "\n"); api_Counter_free(first); printf("walk %llu\n", (unsigned long long)api_walk(3, visit, NULL)); printf("each %u\n", api_each(2, print_visit, NULL)); @@ -714,33 +714,33 @@ static int script(void) { /* A type generic only over lifetimes is a handle to its 'static form. */ api_Name *name = api_Name_owned(str("a")); api_Name *b = api_Name_owned(str("b")); - api_Name *joined = api_Name_join(name, b); + api_Name *joined = api_Name_join(name, b, NULL); api_Name_free(b); - print("joined ", api_Name_as_str(joined), " "); - print("", api_Name_debug(joined), " "); - printf("%s\n", api_Name_eq(joined, name) ? "true" : "false"); + print("joined ", api_Name_as_str(joined, NULL), " "); + print("", api_Name_debug(joined, NULL), " "); + printf("%s\n", api_Name_eq(joined, name, NULL) ? "true" : "false"); api_Name_free(name); char text[] = "c"; - api_Name_rename(joined, str(text)); + api_Name_rename(joined, str(text), NULL); /* The name took a copy: the caller's buffer is its own again. */ text[0] = 'x'; - print("renamed ", api_Name_as_str(joined), "\n"); + print("renamed ", api_Name_as_str(joined, NULL), "\n"); api_Name *dd = api_Name_owned(str("dd")); - api_Name *longest = api_longer(joined, dd); - print("longer ", api_Name_as_str(longest), "\n"); + api_Name *longest = api_longer(joined, dd, NULL); + print("longer ", api_Name_as_str(longest, NULL), "\n"); api_Name_free(longest); api_Name_free(dd); api_Name_free(joined); api_Name *unnamed = api_unnamed(); - print("unnamed [", api_Name_as_str(unnamed), "]\n"); + print("unnamed [", api_Name_as_str(unnamed, NULL), "]\n"); api_Name_free(unnamed); /* Two buffers of equal text, interned into one string. */ char lit_text[] = "lit", again_text[] = "lit"; api_Name *lit = api_Name_borrowed(str(lit_text)); api_Name *again = api_Name_borrowed(str(again_text)); api_Name *other = api_Name_borrowed(str("other")); - print("borrowed ", api_Name_as_str(lit), " "); - printf("%s %s\n", api_Name_shares(lit, again) ? "true" : "false", api_Name_shares(lit, other) ? "true" : "false"); + print("borrowed ", api_Name_as_str(lit, NULL), " "); + printf("%s %s\n", api_Name_shares(lit, again, NULL) ? "true" : "false", api_Name_shares(lit, other, NULL) ? "true" : "false"); api_Name_free(lit); api_Name_free(again); api_Name_free(other); @@ -767,9 +767,9 @@ static int script(void) { return 1; } api_Digest *digest = api_Digest_new(); - api_Digest_sum_set(digest, sum); - api_array_u8_4 got = api_Digest_sum_get(digest); - api_array_u8_4 masked = api_Digest_flipped(digest, (api_array_u8_4){{0xff, 0, 0xff, 0}}); + api_Digest_sum_set(digest, sum, NULL); + api_array_u8_4 got = api_Digest_sum_get(digest, NULL); + api_array_u8_4 masked = api_Digest_flipped(digest, (api_array_u8_4){{0xff, 0, 0xff, 0}}, NULL); printf("digest %02x%02x%02x%02x %02x%02x%02x%02x", got.items[0], got.items[1], got.items[2], got.items[3], masked.items[0], masked.items[1], masked.items[2], masked.items[3]); print(" ", unparsed_err, "\n"); @@ -777,17 +777,17 @@ static int script(void) { api_Batch *batch = api_Batch_new(); unsigned accepted = 0; for (uint32_t v = 1; v <= 5; v++) { - accepted += api_Batch_push(batch, v); + accepted += api_Batch_push(batch, v, NULL); } api_Batch_2 *batch2 = api_pair(); unsigned paired = 0; for (uint32_t v = 1; v <= 3; v++) { - paired += api_Batch_2_push(batch2, v); + paired += api_Batch_2_push(batch2, v, NULL); } - unsigned long long emptied = api_drain_4(batch); - printf("batch %zu %u %llu %llu %zu %u %llu\n", api_Batch_capacity(batch), accepted, emptied, - (unsigned long long)api_drain_4(batch), api_Batch_2_capacity(batch2), paired, (unsigned long long)api_drain_2(batch2)); - printf("capacities %zu %zu\n", api_capacities_4_2(batch, batch2), api_capacities_2_2(batch2, batch2)); + unsigned long long emptied = api_drain_4(batch, NULL); + printf("batch %zu %u %llu %llu %zu %u %llu\n", api_Batch_capacity(batch, NULL), accepted, emptied, + (unsigned long long)api_drain_4(batch, NULL), api_Batch_2_capacity(batch2, NULL), paired, (unsigned long long)api_drain_2(batch2, NULL)); + printf("capacities %zu %zu\n", api_capacities_4_2(batch, batch2, NULL), api_capacities_2_2(batch2, batch2, NULL)); api_Batch_2_free(batch2); api_Batch_free(batch); api_range_u64 wide = api_widen((api_range_u64){.start = 2, .end = 5}, 1); @@ -864,23 +864,23 @@ static int script(void) { printf("secs %llu\n", (unsigned long long)api_Stamp_secs(stamp)); api_Message *message = api_Message_new(stamp, str("body")); api_Stamp_free(stamp); - print("payload ", api_Message_payload_get(message), "\n"); - api_Stamp *read = api_Message_stamp_get(message); + print("payload ", api_Message_payload_get(message, NULL), "\n"); + api_Stamp *read = api_Message_stamp_get(message, NULL); print("stamp ", api_Stamp_debug(read), "\n"); api_Stamp_free(read); - printf("sent %s\n", api_Message_sent(message) ? "true" : "false"); + printf("sent %s\n", api_Message_sent(message, NULL) ? "true" : "false"); /* Setters assign in place: the bytes are copied in, and a `Copy` handle copied. */ api_Stamp *reset = api_Stamp_from_millis(4000); - api_Message_stamp_set(message, reset); - api_Message_payload_set(message, str("edited")); - print("payload ", api_Message_payload_get(message), "\n"); - read = api_Message_stamp_get(message); + api_Message_stamp_set(message, reset, NULL); + api_Message_payload_set(message, str("edited"), NULL); + print("payload ", api_Message_payload_get(message, NULL), "\n"); + read = api_Message_stamp_get(message, NULL); print("stamp ", api_Stamp_debug(read), "\n"); api_Stamp_free(read); printf("secs %llu\n", (unsigned long long)api_Stamp_secs(reset)); api_Stamp_free(reset); - api_Message_stamp_set(message, NULL); - printf("stamp %s\n", api_Message_stamp_get(message) != NULL ? "true" : "false"); + api_Message_stamp_set(message, NULL, NULL); + printf("stamp %s\n", api_Message_stamp_get(message, NULL) != NULL ? "true" : "false"); api_Message_free(message); static const api_Log_vtable print_log = {.free = log_free, .log = log_line}; @@ -896,17 +896,17 @@ static int script(void) { api_Channel *channel = api_Channel_new(); api_waker waker = {.wake = wake, .drop = drop, .data = NULL}; - api_Channel_recv_future *recv = api_Channel_recv(channel); + api_Channel_recv_future *recv = api_Channel_recv(channel, NULL); api_buf payload; printf("recv pending %s\n", api_Channel_recv_poll(recv, waker, &payload) == API_POLL_PENDING ? "true" : "false"); - api_Channel_send(channel, str("hi")); + api_Channel_send(channel, str("hi"), NULL); printf("woken %d\n", wakes); if (api_Channel_recv_poll(recv, waker, &payload) == API_POLL_READY) { print("recv ", payload, "\n"); } api_Channel_recv_future_free(recv); - api_Channel_send(channel, str("")); - api_Channel_recv_nonempty_future *nonempty = api_Channel_recv_nonempty(channel); + api_Channel_send(channel, str(""), NULL); + api_Channel_recv_nonempty_future *nonempty = api_Channel_recv_nonempty(channel, NULL); switch (api_Channel_recv_nonempty_poll(nonempty, waker, &payload, &err)) { case API_POLL_READY: printf("nonempty %zu\n", payload.len); @@ -922,8 +922,8 @@ static int script(void) { api_Channel_recv_nonempty_future_free(nonempty); api_Batch *fill4 = api_Batch_new(); api_Batch_2 *fill2 = api_pair(); - api_fill_4_future *filling4 = api_fill_4(fill4, 9); - api_fill_2_future *filling2 = api_fill_2(fill2, 1); + api_fill_4_future *filling4 = api_fill_4(fill4, 9, NULL); + api_fill_2_future *filling2 = api_fill_2(fill2, 1, NULL); api_vec_u32 filled4, filled2; if (api_fill_4_poll(filling4, waker, &filled4) == API_POLL_READY && api_fill_2_poll(filling2, waker, &filled2) == API_POLL_READY) { printf("fill"); @@ -943,14 +943,14 @@ static int script(void) { api_Batch_free(fill4); /* The send happens at the call: the future is dropped unpolled. */ - api_Channel_send_later_future_free(api_Channel_send_later(channel, str("eager"))); - recv = api_Channel_recv(channel); + api_Channel_send_later_future_free(api_Channel_send_later(channel, str("eager"), NULL)); + recv = api_Channel_recv(channel, NULL); if (api_Channel_recv_poll(recv, waker, &payload) == API_POLL_READY) { print("recv ", payload, "\n"); } api_Channel_recv_future_free(recv); - api_Channel_send(channel, str("sized")); - api_Channel_recv_sized_future *sized = api_Channel_recv_sized(channel); + api_Channel_send(channel, str("sized"), NULL); + api_Channel_recv_sized_future *sized = api_Channel_recv_sized(channel, NULL); size_t len; if (api_Channel_recv_sized_poll(sized, waker, &payload, &len) == API_POLL_READY) { print("sized ", payload, " "); @@ -958,49 +958,49 @@ static int script(void) { } api_Channel_recv_sized_future_free(sized); /* The waker moves into Rust, which drops it once it wakes. */ - api_Channel_notify(channel, waker); - api_Channel_send(channel, str("rung")); + api_Channel_notify(channel, waker, NULL); + api_Channel_send(channel, str("rung"), NULL); printf("woken %d\n", wakes); /* A poll with no async twin is a future: it takes what the last send left. */ - api_Channel_poll_take_future *take = api_Channel_poll_take(channel); + api_Channel_poll_take_future *take = api_Channel_poll_take(channel, NULL); if (api_Channel_poll_take_poll(take, waker, &payload) == API_POLL_READY) { print("take ", payload, "\n"); } api_Channel_poll_take_future_free(take); - api_Channel_send(channel, str("measured")); - api_Channel_poll_measure_future *measure = api_Channel_poll_measure(channel, 2); + api_Channel_send(channel, str("measured"), NULL); + api_Channel_poll_measure_future *measure = api_Channel_poll_measure(channel, 2, NULL); if (api_Channel_poll_measure_poll(measure, waker, &len, &err) == API_POLL_READY) { printf("measure %zu\n", len); } api_Channel_poll_measure_future_free(measure); /* A context is made of the waker, which Rust keeps until the send wakes it. */ - api_Channel_listen(channel, waker); - api_Channel_send(channel, str("heard")); + api_Channel_listen(channel, waker, NULL); + api_Channel_send(channel, str("heard"), NULL); printf("woken %d\n", wakes); /* A returned waker is the caller's: it wakes the waiter's task until dropped. */ api_Waiter *waiter = api_Waiter_new(waker); - api_waker returned = api_Waiter_waker(waiter); + api_waker returned = api_Waiter_waker(waiter, NULL); returned.wake(returned.data); returned.drop(returned.data); - returned = api_Waiter_context(waiter); + returned = api_Waiter_context(waiter, NULL); returned.wake(returned.data); printf("woken %d\n", wakes); /* Passed back, it moves into Rust as any waker does. */ int woken = wakes; - api_Channel_notify(channel, returned); + api_Channel_notify(channel, returned, NULL); api_Waiter_free(waiter); - api_Channel_send(channel, str("again")); + api_Channel_send(channel, str("again"), NULL); if (wakes != woken + 1) { return 1; } - api_Channel_clear(channel); + api_Channel_clear(channel, NULL); /* The waiter a pending poll registers lives until the next poll, so the send wakes it. */ - take = api_Channel_poll_take(channel); + take = api_Channel_poll_take(channel, NULL); if (api_Channel_poll_take_poll(take, waker, &payload) != API_POLL_PENDING) { return 1; } woken = wakes; - api_Channel_send(channel, str("later")); + api_Channel_send(channel, str("later"), NULL); if (wakes != woken + 1 || api_Channel_poll_take_poll(take, waker, &payload) != API_POLL_READY) { return 1; } @@ -1009,17 +1009,17 @@ static int script(void) { api_Channel_free(channel); /* A value that holds itself is an object. */ - api_Node *list = api_Node_new(1, NULL); - api_Node_push(list, 2); - printf("node %llu\n", (unsigned long long)api_Node_sum(list)); + api_Node *list = api_Node_new(1, NULL, NULL); + api_Node_push(list, 2, NULL); + printf("node %llu\n", (unsigned long long)api_Node_sum(list, NULL)); api_Node_free(list); api_Tally *tally = api_Tally_new(); - api_Tally_bump(tally); - uint32_t count = api_Tally_bump(tally); - printf("tally %u %s\n", count, api_Tally_eq(tally, tally) ? "true" : "false"); - printf("both %u\n", api_bump_both(tally, tally)); - printf("peek %u\n", api_peek(tally, peek_tally, NULL)); + api_Tally_bump(tally, NULL); + uint32_t count = api_Tally_bump(tally, NULL); + printf("tally %u %s\n", count, api_Tally_eq(tally, tally, NULL) ? "true" : "false"); + printf("both %u\n", api_bump_both(tally, tally, NULL)); + printf("peek %u\n", api_peek(tally, peek_tally, NULL, NULL)); api_Tally_free(tally); /* Another export's types, which this export's function takes and returns. */ @@ -1053,27 +1053,27 @@ static int script(void) { const char *names[] = {"a", "b"}; for (int i = 0; i < 2; i++) { api_Counter *pushed = api_Counter_new(str(names[i]), (uint64_t)i + 1); - api_Stack_push(stack, pushed); + api_Stack_push(stack, pushed, NULL); api_Counter_free(pushed); } - api_Counter *top = api_Stack_pop(stack); - printf("stack %zu Some(%llu)", api_Stack_depth(stack), (unsigned long long)api_Counter_get(top)); + api_Counter *top = api_Stack_pop(stack, NULL); + printf("stack %zu Some(%llu)", api_Stack_depth(stack, NULL), (unsigned long long)api_Counter_get(top, NULL)); api_Counter_free(top); - api_Counter *next = api_Stack_pop(stack); + api_Counter *next = api_Stack_pop(stack, NULL); printf(" %s", next != NULL ? "true" : "false"); api_Counter_free(next); - printf(" %s\n", api_Stack_pop(stack) == NULL ? "true" : "false"); + printf(" %s\n", api_Stack_pop(stack, NULL) == NULL ? "true" : "false"); /* Generic functions and methods, at each implementation of the trait bounding them, by reference, which leaves C its handle. */ api_Counter *from_point = api_Counter_seeded_Point(str("p"), (api_Point){.x = -3, .y = 4}); api_Counter *from_size = api_Counter_seeded_Size(str("s"), (api_Size){.width = 5, .height = 6}); - api_Counter *from_counter = api_Counter_seeded_Counter(str("c"), from_point); + api_Counter *from_counter = api_Counter_seeded_Counter(str("c"), from_point, NULL); /* Two instances whose arguments share a last segment: one takes its module too. */ api_Counter *from_polar = api_Counter_seeded_polar_Point(str("q"), (api_polar_Point){.turns = 1, .distance = 2}); - printf("seeded %llu %llu %llu %llu past %llu seeds %llu\n", (unsigned long long)api_Counter_get(from_point), - (unsigned long long)api_Counter_get(from_size), (unsigned long long)api_Counter_get(from_counter), - (unsigned long long)api_Counter_get(from_polar), - (unsigned long long)api_Stack_depth_past_Point(stack, (api_Point){.x = 1, .y = 2}), + printf("seeded %llu %llu %llu %llu past %llu seeds %llu\n", (unsigned long long)api_Counter_get(from_point, NULL), + (unsigned long long)api_Counter_get(from_size, NULL), (unsigned long long)api_Counter_get(from_counter, NULL), + (unsigned long long)api_Counter_get(from_polar, NULL), + (unsigned long long)api_Stack_depth_past_Point(stack, (api_Point){.x = 1, .y = 2}, NULL), (unsigned long long)api_seeds_Point_Size((api_Size){.width = 2, .height = 3})); api_Counter_free(from_point); api_Counter_free(from_size); @@ -1127,17 +1127,17 @@ static int script(void) { /* Its Drop runs when C frees the handle, not before. */ api_Token *token = api_Token_new(7); - printf("token %u dropped %u\n", api_Token_id_get(token), api_dropped()); + printf("token %u dropped %u\n", api_Token_id_get(token, NULL), api_dropped()); api_Token_free(token); printf("dropped %u\n", api_dropped()); /* A field and the method of its name, each its own function. */ api_Retry *retry = api_Retry_new((api_option_u32){.some = true, .value = 5}); - api_option_u32 limit = api_Retry_limit_get(retry); - printf("retry %d %u %u %u\n", limit.some, limit.value, api_Retry_limit(retry), api_Retry_delay(retry)); + api_option_u32 limit = api_Retry_limit_get(retry, NULL); + printf("retry %d %u %u %u\n", limit.some, limit.value, api_Retry_limit(retry, NULL), api_Retry_delay(retry, NULL)); /* The renamed field's setter, named as its getter. */ - api_Retry_limit_set(retry, (api_option_u32){0}); - printf("retry %u\n", api_Retry_limit(retry)); + api_Retry_limit_set(retry, (api_option_u32){0}, NULL); + printf("retry %u\n", api_Retry_limit(retry, NULL)); api_Retry_free(retry); api_Slot_u8 *slot = api_Slot_u8_new(200); printf("slot %u\n", api_Slot_u8_value(slot)); @@ -1148,8 +1148,8 @@ static int script(void) { if (!api_Url_parse(str("https://example.com/a/b"), &base, &parse_err)) { return 1; } - print("url ", api_Url_to_string(base), " host "); - api_buf host = api_host(base); + print("url ", api_Url_to_string(base, NULL), " host "); + api_buf host = api_host(base, NULL); if (host.ptr != NULL) { print("Some(\"", host, "\")\n"); } else { @@ -1157,10 +1157,10 @@ static int script(void) { } api_Url *resolved; api_buf resolve_err; - api_Url *base_clone = api_Url_clone(base); - if (api_resolve(base_clone, str("c?d"), &resolved, &resolve_err)) { - print("resolve ", api_Url_to_string(resolved), " "); - printf("%s\n", api_Url_eq(resolved, base) ? "true" : "false"); + api_Url *base_clone = api_Url_clone(base, NULL); + if (api_resolve(base_clone, str("c?d"), &resolved, &resolve_err, NULL)) { + print("resolve ", api_Url_to_string(resolved, NULL), " "); + printf("%s\n", api_Url_eq(resolved, base, NULL) ? "true" : "false"); api_Url_free(resolved); } else { print("resolve err ", resolve_err, "\n"); @@ -1169,7 +1169,7 @@ static int script(void) { api_Url_free(base); api_Url *bad; if (api_Url_parse(str("no scheme"), &bad, &parse_err)) { - print("parse ", api_Url_to_string(bad), "\n"); + print("parse ", api_Url_to_string(bad, NULL), "\n"); api_Url_free(bad); } else { print("parse err ", parse_err, "\n"); @@ -1248,12 +1248,12 @@ static int script(void) { api_coordinates((api_option_Point){0})); api_Counter *two = api_Counter_new(str("two"), 2); api_Counter *three = api_Counter_new(str("three"), 3); - printf("plus %llu %llu\n", (unsigned long long)api_Counter_plus(two, three), - (unsigned long long)api_Counter_plus(two, NULL)); - bool added = api_Counter_add_to(two, three); - bool added_none = api_Counter_add_to(two, NULL); + printf("plus %llu %llu\n", (unsigned long long)api_Counter_plus(two, three, NULL), + (unsigned long long)api_Counter_plus(two, NULL, NULL)); + bool added = api_Counter_add_to(two, three, NULL); + bool added_none = api_Counter_add_to(two, NULL, NULL); printf("add_to %s %s %llu\n", added ? "true" : "false", added_none ? "true" : "false", - (unsigned long long)api_Counter_get(three)); + (unsigned long long)api_Counter_get(three, NULL)); api_Counter_free(two); api_Counter_free(three); @@ -1286,33 +1286,33 @@ static int script(void) { /* An Arc of another crate's type: an opaque handle, taken back by value or lent. */ api_tls_Config *config = api_secure(str("h3")); - print("secure ", api_alpn_lent(config), " "); - api_tls_Config *config_copy = api_tls_Config_clone(config); - print("", api_alpn(config_copy), "\n"); + print("secure ", api_alpn_lent(config, NULL), " "); + api_tls_Config *config_copy = api_tls_Config_clone(config, NULL); + print("", api_alpn(config_copy, NULL), "\n"); api_tls_Config_free(config_copy); api_tls_Config_free(config); /* A borrow of a meter that is not Clone, lent from its owner's handle. */ { api_Meters *lender = api_Meters_new(); - api_Meter *first = api_Meters_first(lender); - api_Meter_add(first, 2); - unsigned long long added = api_Meter_add(first, 3); - api_Meter *again = api_Meters_first(lender); - printf("lent %llu %llu\n", added, (unsigned long long)api_Meter_count(again)); + api_Meter *first = api_Meters_first(lender, NULL); + api_Meter_add(first, 2, NULL); + unsigned long long added = api_Meter_add(first, 3, NULL); + api_Meter *again = api_Meters_first(lender, NULL); + printf("lent %llu %llu\n", added, (unsigned long long)api_Meter_count(again, NULL)); api_Meter_free(again); api_Meter_free(first); - api_Meters_clear(lender); - again = api_Meters_first(lender); - printf("cleared %llu\n", (unsigned long long)api_Meter_count(again)); + api_Meters_clear(lender, NULL); + again = api_Meters_first(lender, NULL); + printf("cleared %llu\n", (unsigned long long)api_Meter_count(again, NULL)); api_Meter_free(again); api_Meters_free(lender); /* One lent from a handle that is not Sync, on one thread at a time. */ api_Tallies *tallies = api_Tallies_new(); - api_Tally *tallied = api_Tallies_first(tallies); - api_Tally_bump(tallied); + api_Tally *tallied = api_Tallies_first(tallies, NULL); + api_Tally_bump(tallied, NULL); api_Tally_free(tallied); - tallied = api_Tallies_first(tallies); - printf("tallied %u\n", api_Tally_bump(tallied)); + tallied = api_Tallies_first(tallies, NULL); + printf("tallied %u\n", api_Tally_bump(tallied, NULL)); api_Tally_free(tallied); api_Tallies_free(tallies); } @@ -1333,19 +1333,19 @@ static int script(void) { api_vec_Counter counters = api_counters((api_vec_string){.ptr = counter_names, .len = 2}); printf("counters"); for (size_t i = 0; i < counters.len; i++) { - print(i == 0 ? " " : ", ", api_Counter_name(counters.ptr[i]), " "); - printf("%llu", (unsigned long long)api_Counter_get(counters.ptr[i])); + print(i == 0 ? " " : ", ", api_Counter_name(counters.ptr[i], NULL), " "); + printf("%llu", (unsigned long long)api_Counter_get(counters.ptr[i], NULL)); } printf("\n"); /* Each counter moves in, leaving its handle only to free. */ - uint64_t worth = api_worth((api_vec_Counter){.ptr = counters.ptr, .len = 1}, (api_vec_Counter){.ptr = counters.ptr + 1, .len = 1}); - uint64_t none = api_worth((api_vec_Counter){0}, (api_vec_Counter){.ptr = counters.ptr, .len = 0}); + uint64_t worth = api_worth((api_vec_Counter){.ptr = counters.ptr, .len = 1}, (api_vec_Counter){.ptr = counters.ptr + 1, .len = 1}, NULL); + uint64_t none = api_worth((api_vec_Counter){0}, (api_vec_Counter){.ptr = counters.ptr, .len = 0}, NULL); printf("worth %llu %llu\n", (unsigned long long)worth, (unsigned long long)none); api_vec_Counter_free(counters); api_Counter *held = api_Counter_new(str("h"), 4); api_option_u64 counts[] = { - api_held_count((api_option_tuple_Counter){.some = true, .value = {._0 = held}}), - api_held_count((api_option_tuple_Counter){0}), + api_held_count((api_option_tuple_Counter){.some = true, .value = {._0 = held}}, NULL), + api_held_count((api_option_tuple_Counter){0}, NULL), }; api_Counter_free(held); printf("held"); @@ -1358,9 +1358,9 @@ static int script(void) { } printf("\n"); api_Counter *boxed = api_boxed_counter(str("b")); - print("boxed Some(\"", api_boxed_name(boxed), "\") "); + print("boxed Some(\"", api_boxed_name(boxed, NULL), "\") "); api_Counter_free(boxed); - printf("%s %s\n", api_boxed_name(NULL).ptr ? "Some" : "None", api_boxed_counter((api_buf){0}) ? "false" : "true"); + printf("%s %s\n", api_boxed_name(NULL, NULL).ptr ? "Some" : "None", api_boxed_counter((api_buf){0}) ? "false" : "true"); printf("less %u\n", api_less(5, 2)); uint32_t row0[] = {1, 2}; @@ -1396,11 +1396,11 @@ static int script(void) { if (!api_Json_parse(str("{\"b\": [1, 2.5, null], \"a\": \"\\u00e9\"}"), &doc, &parse_err)) { return 1; } - print("json ", api_encode(doc), "\n"); - print("timed text ", api_timed_text(doc), "\n"); - api_Json *tagged = api_tagged(doc, str("t")); + print("json ", api_encode(doc, NULL), "\n"); + print("timed text ", api_timed_text(doc, NULL), "\n"); + api_Json *tagged = api_tagged(doc, str("t"), NULL); api_Json_free(doc); - print("tagged ", api_Json_to_text(tagged), "\n"); + print("tagged ", api_Json_to_text(tagged, NULL), "\n"); api_Json_free(tagged); /* A JSON object's entries cross as pairs of a key and its document, each moved in. */ api_tuple_string_Json json_pairs[2] = {{._0 = str("b")}, {._0 = str("a")}}; @@ -1408,19 +1408,19 @@ static int script(void) { !api_Json_parse(str("2"), &json_pairs[1]._1, &parse_err)) { return 1; } - api_vec_tuple_string_Json json_entries = api_entries((api_vec_tuple_string_Json){.ptr = json_pairs, .len = 2}, str("t")); + api_vec_tuple_string_Json json_entries = api_entries((api_vec_tuple_string_Json){.ptr = json_pairs, .len = 2}, str("t"), NULL); api_Json_free(json_pairs[0]._1); api_Json_free(json_pairs[1]._1); printf("entries"); for (size_t i = 0; i < json_entries.len; i++) { printf("%s%.*s ", i == 0 ? " " : ", ", (int)json_entries.ptr[i]._0.len, (const char *)json_entries.ptr[i]._0.ptr); - print("", api_Json_to_text(json_entries.ptr[i]._1), ""); + print("", api_Json_to_text(json_entries.ptr[i]._1, NULL), ""); } printf("\n"); api_vec_tuple_string_Json_free(json_entries); api_Json *broken; if (api_Json_parse(str("{\"a\":"), &broken, &parse_err)) { - print("json parse ", api_Json_to_text(broken), "\n"); + print("json parse ", api_Json_to_text(broken, NULL), "\n"); api_Json_free(broken); } else { print("json parse err ", parse_err, "\n"); @@ -1430,11 +1430,11 @@ static int script(void) { if (!api_Url_parse(str("https://example.com/a/b"), &home, &parse_err)) { return 1; } - api_Site *site = api_Site_new(home); - api_Url *home0 = api_Site_home_get(site); - api_Url *home1 = api_Site_home_get(site); - print("site ", api_Url_to_string(home0), " "); - print("", api_Url_to_string(home1), "\n"); + api_Site *site = api_Site_new(home, NULL); + api_Url *home0 = api_Site_home_get(site, NULL); + api_Url *home1 = api_Site_home_get(site, NULL); + print("site ", api_Url_to_string(home0, NULL), " "); + print("", api_Url_to_string(home1, NULL), "\n"); api_Url_free(home0); api_Url_free(home1); api_Site_free(site); @@ -1458,13 +1458,13 @@ static int script(void) { api_buf trail_names[] = {str("a"), str("b")}; api_Trail *trail = api_Trail_new((api_vec_Point){.ptr = trail_points, .len = 2}, (api_vec_string){.ptr = trail_names, .len = 2}); - api_vec_Point points = api_Trail_points(trail); + api_vec_Point points = api_Trail_points(trail, NULL); printf("trail ["); for (size_t i = 0; i < points.len; i++) { print(i == 0 ? "" : ", ", api_Point_debug(&points.ptr[i]), ""); } api_vec_Point_free(points); - api_vec_Label labels = api_Trail_labels(trail); + api_vec_Label labels = api_Trail_labels(trail, NULL); printf("] ["); for (size_t i = 0; i < labels.len; i++) { print(i == 0 ? "" : ", ", api_Label_debug(&labels.ptr[i]), ""); @@ -1472,32 +1472,32 @@ static int script(void) { printf("]\n"); api_vec_Label_free(labels); /* Returned iterators, each collected at the call: drained labels are gone after. */ - api_vec_Point iter_points = api_Trail_iter(trail); + api_vec_Point iter_points = api_Trail_iter(trail, NULL); printf("iterated ["); for (size_t i = 0; i < iter_points.len; i++) { print(i == 0 ? "" : ", ", api_Point_debug(&iter_points.ptr[i]), ""); } api_vec_Point_free(iter_points); - api_vec_string iter_names = api_Trail_names(trail); + api_vec_string iter_names = api_Trail_names(trail, NULL); printf("] ["); for (size_t i = 0; i < iter_names.len; i++) { printf("%s\"%.*s\"", i == 0 ? "" : ", ", (int)iter_names.ptr[i].len, (const char *)iter_names.ptr[i].ptr); } api_vec_string_free(iter_names); - api_buf iter_bytes = api_Trail_bytes(trail); + api_buf iter_bytes = api_Trail_bytes(trail, NULL); printf("] ["); for (size_t i = 0; i < iter_bytes.len; i++) { printf("%s%u", i == 0 ? "" : ", ", iter_bytes.ptr[i]); } printf("]\n"); api_buf_free(iter_bytes); - api_vec_Label drained = api_Trail_drain(trail); + api_vec_Label drained = api_Trail_drain(trail, NULL); printf("drained ["); for (size_t i = 0; i < drained.len; i++) { print(i == 0 ? "" : ", ", api_Label_debug(&drained.ptr[i]), ""); } api_vec_Label_free(drained); - api_vec_Label left = api_Trail_labels(trail); + api_vec_Label left = api_Trail_labels(trail, NULL); printf("] then ["); for (size_t i = 0; i < left.len; i++) { print(i == 0 ? "" : ", ", api_Label_debug(&left.ptr[i]), ""); @@ -1505,7 +1505,7 @@ static int script(void) { printf("]\n"); api_vec_Label_free(left); /* A filter, collected at the call. */ - api_vec_Point beyond = api_Trail_beyond(trail, 1); + api_vec_Point beyond = api_Trail_beyond(trail, 1, NULL); printf("beyond"); for (size_t i = 0; i < beyond.len; i++) { printf("%s%d %d", i == 0 ? " " : ", ", beyond.ptr[i].x, beyond.ptr[i].y); @@ -1517,8 +1517,8 @@ static int script(void) { api_vec_Counter numbered = api_numbered((api_vec_string){.ptr = numbered_names, .len = 2}); printf("numbered"); for (size_t i = 0; i < numbered.len; i++) { - print(i == 0 ? " " : ", ", api_Counter_name(numbered.ptr[i]), " "); - printf("%llu", (unsigned long long)api_Counter_get(numbered.ptr[i])); + print(i == 0 ? " " : ", ", api_Counter_name(numbered.ptr[i], NULL), " "); + printf("%llu", (unsigned long long)api_Counter_get(numbered.ptr[i], NULL)); } printf("\n"); api_vec_Counter_free(numbered); @@ -1535,46 +1535,46 @@ static int script(void) { !api_Json_parse(str("2"), §ion_pairs[1]._1, &parse_err)) { return 1; } - api_vec_tuple_string_Json sections = api_sections((api_vec_tuple_string_Json){.ptr = section_pairs, .len = 2}); + api_vec_tuple_string_Json sections = api_sections((api_vec_tuple_string_Json){.ptr = section_pairs, .len = 2}, NULL); api_Json_free(section_pairs[0]._1); api_Json_free(section_pairs[1]._1); printf("sections"); for (size_t i = 0; i < sections.len; i++) { printf("%s%.*s ", i == 0 ? " " : ", ", (int)sections.ptr[i]._0.len, (const char *)sections.ptr[i]._0.ptr); - print("", api_Json_to_text(sections.ptr[i]._1), ""); + print("", api_Json_to_text(sections.ptr[i]._1, NULL), ""); } printf("\n"); api_vec_tuple_string_Json_free(sections); /* An instant only Rust makes, which C copies into each call and compares. */ api_Instant *began = api_now(); - api_Instant *ended = api_later(began, (api_duration){.secs = 1, .nanos = 500000000}); - api_Instant *same = api_later(began, (api_duration){.secs = 0, .nanos = 0}); - api_duration forward = api_since(ended, began); - api_duration backward = api_since(began, ended); + api_Instant *ended = api_later(began, (api_duration){.secs = 1, .nanos = 500000000}, NULL); + api_Instant *same = api_later(began, (api_duration){.secs = 0, .nanos = 0}, NULL); + api_duration forward = api_since(ended, began, NULL); + api_duration backward = api_since(began, ended, NULL); printf("instants %llu %llu %s %s\n", (unsigned long long)(forward.secs * 1000 + forward.nanos / 1000000), - (unsigned long long)(backward.secs * 1000 + backward.nanos / 1000000), api_Instant_eq(began, same) ? "true" : "false", - api_Instant_eq(began, ended) ? "true" : "false"); + (unsigned long long)(backward.secs * 1000 + backward.nanos / 1000000), api_Instant_eq(began, same, NULL) ? "true" : "false", + api_Instant_eq(began, ended, NULL) ? "true" : "false"); api_Instant_free(same); api_Instant_free(ended); api_Instant_free(began); /* tracing's level, and its filter as an optional level whose none is `OFF`. */ api_Verbosity *verbosity = api_Verbosity_new(API_LEVEL_WARN); - print("verbosity ", api_spell(api_Verbosity_filter(verbosity)), " "); - print("", api_spell(api_louder(api_Verbosity_filter(verbosity))), "\n"); - api_Verbosity_level_set(verbosity, API_LEVEL_TRACE); - printf("verbosity %s ", api_Verbosity_level_get(verbosity) == API_LEVEL_TRACE ? "true" : "false"); - print("", api_spell(api_Verbosity_filter(verbosity)), " "); - print("", api_spell(api_louder(api_Verbosity_filter(verbosity))), " "); + print("verbosity ", api_spell(api_Verbosity_filter(verbosity, NULL)), " "); + print("", api_spell(api_louder(api_Verbosity_filter(verbosity, NULL))), "\n"); + api_Verbosity_level_set(verbosity, API_LEVEL_TRACE, NULL); + printf("verbosity %s ", api_Verbosity_level_get(verbosity, NULL) == API_LEVEL_TRACE ? "true" : "false"); + print("", api_spell(api_Verbosity_filter(verbosity, NULL)), " "); + print("", api_spell(api_louder(api_Verbosity_filter(verbosity, NULL))), " "); print("", api_spell(api_louder((api_option_Level){.some = false})), "\n"); api_Verbosity_free(verbosity); /* A returned `Vec<&Token>` holds clones, which its free drops. */ api_Token *twice = api_Token_new(9); uint32_t before = api_dropped(); - api_vec_Token tokens = api_Token_twice(twice); - if (tokens.len != 2 || api_Token_id_get(tokens.ptr[1]) != 9) { + api_vec_Token tokens = api_Token_twice(twice, NULL); + if (tokens.len != 2 || api_Token_id_get(tokens.ptr[1], NULL) != 9) { return 1; } api_vec_Token_free(tokens); @@ -1594,21 +1594,21 @@ static int script(void) { static int broken(const char *which) { if (strcmp(which, "moved") == 0) { api_Counter *c = api_Counter_new(str("c"), 0); - api_Label label = api_Counter_into_label(c, (api_option_Point){0}); + api_Label label = api_Counter_into_label(c, (api_option_Point){0}, NULL); api_Label_free(&label); - api_Counter_get(c); + api_Counter_get(c, NULL); } else if (strcmp(which, "borrowed") == 0) { api_Channel *channel = api_Channel_new(); - api_Channel_recv_future *recv = api_Channel_recv(channel); + api_Channel_recv_future *recv = api_Channel_recv(channel, NULL); (void)recv; - api_Channel_clear(channel); + api_Channel_clear(channel, NULL); } else if (strcmp(which, "utf8") == 0) { api_buf bad = {.ptr = (const uint8_t *)"\xff", .len = 1}; api_greet(bad, API_SHAPE_CIRCLE); } else if (strcmp(which, "finished") == 0) { api_Channel *channel = api_Channel_new(); - api_Channel_send(channel, str("x")); - api_Channel_recv_future *recv = api_Channel_recv(channel); + api_Channel_send(channel, str("x"), NULL); + api_Channel_recv_future *recv = api_Channel_recv(channel, NULL); api_waker waker = {0}; api_buf payload; api_Channel_recv_poll(recv, waker, &payload); @@ -1621,19 +1621,19 @@ static int broken(const char *which) { } else if (strcmp(which, "enum") == 0) { api_greet(str("x"), (api_Shape)2); } else if (strcmp(which, "lent") == 0 || strcmp(which, "lent moved") == 0) { - api_Registry *registry = api_Registry_new(api_Counter_new(str("c"), 0)); - api_Registry_edit(registry, strcmp(which, "lent") == 0 ? free_lent : move_lent, NULL); + api_Registry *registry = api_Registry_new(api_Counter_new(str("c"), 0), NULL); + api_Registry_edit(registry, strcmp(which, "lent") == 0 ? free_lent : move_lent, NULL, NULL); } else if (strcmp(which, "outlived") == 0) { api_Meters *meters = api_Meters_new(); - api_Meter *first = api_Meters_first(meters); - api_Meters_clear(meters); - api_Meter_count(first); + api_Meter *first = api_Meters_first(meters, NULL); + api_Meters_clear(meters, NULL); + api_Meter_count(first, NULL); } else if (strcmp(which, "lent shared") == 0) { api_Meters *meters = api_Meters_new(); - api_Meter_reset(api_Meters_first(meters)); + api_Meter_reset(api_Meters_first(meters, NULL), NULL); } else if (strcmp(which, "null") == 0) { - api_Registry *registry = api_Registry_new(api_Counter_new(str("c"), 0)); - api_Registry_edit(registry, NULL, NULL); + api_Registry *registry = api_Registry_new(api_Counter_new(str("c"), 0), NULL); + api_Registry_edit(registry, NULL, NULL, NULL); } else if (strcmp(which, "returned") == 0) { api_made(1, made_view, NULL); } else if (strcmp(which, "reader") == 0) { diff --git a/ffi/tests/api.go b/ffi/tests/api.go index ecd169d46..2c0c75657 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -594,9 +594,9 @@ func script() { return escaped(*p) }), ",", ""), "[]")) // A path that is no UTF-8 is a Go string all the same. - check(api.Stray() == "\xff", "a path that is not UTF-8 crosses as its bytes") + check(must(api.Stray()) == "\xff", "a path that is not UTF-8 crosses as its bytes") var lent string - api.VisitStray(func(path string) { lent = path }) + ok(api.VisitStray(func(path string) { lent = path })) check(lent == "\xff", "a path Rust lends that is not UTF-8 crosses as its bytes") // An error Rust returns only boxed is its type. _, err = api.Refuse("why") @@ -1420,15 +1420,19 @@ func batched() []rt.Bytes { // broken breaks a contract, which must fail with a message naming it. func broken(which string) { switch which { + // A failed check returns an error, which the case raises once it checked its kind. case "outlived": meters := api.NewMeters() first := must(meters.First()) ok(meters.Clear()) - _ = must(first.Count()) + _, err := first.Count() + check(errors.Is(err, rt.ErrClosed), "a handle that outlived its lender is closed") + panic(err) case "lent moved": must(api.NewRegistry(api.NewCounter("c", 0))).Edit(func(counter *api.Counter) bool { - counter.IntoLabel(nil) - return false + _, err := counter.IntoLabel(nil) + check(errors.Is(err, rt.ErrBorrowed), "a lent handle moved out is borrowed") + panic(err) }) // An IPv6 address where Rust takes `SocketAddrV4`. case "family": @@ -1444,7 +1448,9 @@ func broken(which string) { api.Extended(math.MaxInt64, 1) // The packages serve wasm32 too, whose items the host's library lacks. case "target": - api.WasmOnly() + _, err := api.WasmOnly() + check(errors.Is(err, errors.ErrUnsupported), "an item off the target is unsupported") + panic(err) case "variant": api.PointerBits(api.PlatformWasm{Bits: 32}) case "plain variant": diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index 93b3fcca3..b529f5ef9 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -170,8 +170,8 @@ func script() { print("default \(d.x) \(d.y)") var c = Counter.new("c", 5) - print("get \(c.get())") - print("name \(c.name())") + print("get \(try! c.get())") + print("name \(try! c.name())") print("add ok \(try! c.add(10))") do { _ = try c.add(2000) @@ -187,22 +187,22 @@ func script() { } catch { check(false, "sub throws an Error") } - c.rename("d") - print("name \(c.name())") - print("is d \(c.`is`("d"))") + try! c.rename("d") + print("name \(try! c.name())") + print("is d \(try! c.`is`("d"))") var copy = c.clone() _ = try! copy.add(1) - print("clone \(copy.get()) original \(c.get())") + print("clone \(try! copy.get()) original \(try! c.get())") print("debug \(c.debugDescription)") - let label = c.intoLabel(p) + let label = try! c.intoLabel(p) print("label \(debug(label))") print("note \(hasText(Note(text: ""))) \(hasText(Note(text: nil)))") // A tuple holding a handle is a struct of its elements. - let split = Counter.new("s", 7).split(5) - print("split \(split._0.name()) \(split._0.get()) \(split._1.name()) \(split._1.get())") - let one = only(Counter.new("o", 3)) - print("only \(one.name()) \(one.get())") + let split = try! Counter.new("s", 7).split(5) + print("split \(try! split._0.name()) \(try! split._0.get()) \(try! split._1.name()) \(try! split._1.get())") + let one = try! only(Counter.new("o", 3)) + print("only \(try! one.name()) \(try! one.get())") let (w8, w16, w32, w64) = widths(98305) print("widths \(w8) \(w16) \(w32) \(w64)") for b: UInt64 in [2, 0] { @@ -234,9 +234,9 @@ func script() { var ranks = UniqueArray() ranks.append(TupleStringCounter("low", Counter.new("l", 1))) ranks.append(TupleStringCounter("high", Counter.new("h", 9))) - let board = Board.new(ranks) + let board = try! Board.new(ranks) let ranked = board.ranked() - print("ranked \(listed(ranked) { "\($0._0) \($0._1.name()) \($0._1.get())" })") + print("ranked \(listed(ranked) { "\($0._0) \(try! $0._1.name()) \(try! $0._1.get())" })") // A map crosses as its pairs and a set as its items, which Rust collects in order, each // key once, the last kept. let atlas = Atlas(points: [("b", Point(x: 1, y: 1)), ("ab", Point(x: 2, y: 3)), ("b", Point(x: 4, y: 4))], ids: [3, 1, 3]) @@ -270,40 +270,40 @@ func script() { print("title Some((\"\(title(true)!)\",)) \(title(false) == nil ? "None" : "Some") named Some((\"\(named("x")!)\",))") // Each lent counter is a new handle to a clone. - var registry = Registry.new(Counter.new("first", 1)) - print("last \(registry.last() != nil)") - registry.push(Counter.new("last", 2)) - var first = registry.firstGet + var registry = try! Registry.new(Counter.new("first", 1)) + print("last \(try! registry.last() != nil)") + try! registry.push(Counter.new("last", 2)) + var first = try! registry.firstGet() _ = try! first.add(10) - print("first \(registry.firstGet.get()) lent \(first.get())") - var last = registry.last()! + print("first \(try! registry.firstGet().get()) lent \(try! first.get())") + var last = try! registry.last()! _ = try! last.add(10) - print("last \(registry.last()!.get()) lent \(last.get())") + print("last \(try! registry.last()!.get()) lent \(try! last.get())") // A closure lent for the call edits the first counter through a handle. let by: UInt64 = 5 - let edited = registry.edit { counter in + let edited = try! registry.edit { counter in _ = try! counter.add(by) - return counter.edited() && counter.get() == 6 + return try! counter.edited() && try! counter.get() == 6 } - print("edit \(edited) \(registry.firstGet.get())") + print("edit \(edited) \(try! registry.firstGet().get())") // A setter moves an object in, as assignment does, closing its handle. - registry.firstSet(Counter.new("second", 3)) - print("set first \(registry.firstGet.get()) \(registry.firstGet.name())") - print("walk \(walk(3) { cursor in cursor.at() * 2 })") + try! registry.firstSet(Counter.new("second", 3)) + print("set first \(try! registry.firstGet().get()) \(try! registry.firstGet().name())") + print("walk \(walk(3) { cursor in try! cursor.at() * 2 })") print("each \(each(2) { at in print("visit \(at)") })") print("offset \(offset({ 7 }, 3))") print("shift \(shift({ 7 }, 4))") let name = Name.owned("a") - var joined = name.join(Name.owned("b")) - print("joined \(joined.asStr()) \(joined.debugDescription) \(joined == name)") - joined.rename("c") - print("renamed \(joined.asStr())") - let long = longer(joined, Name.owned("dd")) - print("longer \(long.asStr())") - print("unnamed [\(unnamed().asStr())]") + var joined = try! name.join(Name.owned("b")) + print("joined \(try! joined.asStr()) \(joined.debugDescription) \(joined == name)") + try! joined.rename("c") + print("renamed \(try! joined.asStr())") + let long = try! longer(joined, Name.owned("dd")) + print("longer \(try! long.asStr())") + print("unnamed [\(try! unnamed().asStr())]") let lit = Name.borrowed("lit") - print("borrowed \(lit.asStr()) \(lit.shares(Name.borrowed("lit"))) \(lit.shares(Name.borrowed("other")))") + print("borrowed \(try! lit.asStr()) \(try! lit.shares(Name.borrowed("lit"))) \(try! lit.shares(Name.borrowed("other")))") print("total \(total(RangeU64(start: 2, end: 5)))") print("reach \(reach(2, 5))") @@ -316,7 +316,7 @@ func script() { check(U128(high: .max, low: .max).description == "340282366920938463463374607431768211455", "a u128's decimal digits") let sumHex = { (sum: ArrayU8x4) in sum.map { Swift.String(format: "%02x", $0) }.joined() } var digest = Digest.new() - digest.sumSet(try! fingerprint("deadbeef")) + try! digest.sumSet(try! fingerprint("deadbeef")) var unparsed = "" do { _ = try fingerprint("zz") @@ -325,14 +325,14 @@ func script() { } catch { check(false, "fingerprint throws its message") } - print("digest \(sumHex(digest.sumGet)) \(sumHex(digest.flipped(ArrayU8x4([0xff, 0, 0xff, 0])!))) \(unparsed)") + print("digest \(sumHex(try! digest.sumGet)) \(sumHex(try! digest.flipped(ArrayU8x4([0xff, 0, 0xff, 0])!))) \(unparsed)") check(ArrayU8x4([1, 2, 3]) == nil, "3 bytes are no [u8; 4]") var batch = Batch.new(), batch2 = pair() - let accepted = (1...5).filter { batch.push($0) }.count - let paired = (1...3).filter { batch2.push($0) }.count - let emptied = drain4(&batch) - print("batch \(batch.capacity()) \(accepted) \(emptied) \(drain4(&batch)) \(batch2.capacity()) \(paired) \(drain2(&batch2))") - print("capacities \(capacities42(batch, batch2)) \(capacities22(batch2, batch2))") + let accepted = (1...5).filter { try! batch.push($0) }.count + let paired = (1...3).filter { try! batch2.push($0) }.count + let emptied = try! drain4(&batch) + print("batch \(try! batch.capacity()) \(accepted) \(emptied) \(try! drain4(&batch)) \(try! batch2.capacity()) \(paired) \(try! drain2(&batch2))") + print("capacities \(try! capacities42(batch, batch2)) \(try! capacities22(batch2, batch2))") let wide = widen(RangeU64(start: 2, end: 5), 1) print("widen \(wide.start) \(wide.end)") let counts = [ @@ -386,18 +386,18 @@ func script() { print("later \(Stamp.fromMillis(3000).later(stamp))") print("secs \(stamp.secs())") var message = Message.new(stamp, Data("body".utf8)) - print("payload \(text(message.payloadGet))") - print("stamp \(debug(message.stampGet!))") - print("sent \(message.sent())") + print("payload \(text(try! message.payloadGet))") + print("stamp \(debug(try! message.stampGet!))") + print("sent \(try! message.sent())") // Setters assign in place: the bytes are copied in, and a `Copy` handle copied. let reset = Stamp.fromMillis(4000) - message.stampSet(reset) - message.payloadSet(Data("edited".utf8)) - print("payload \(text(message.payloadGet))") - print("stamp \(debug(message.stampGet!))") + try! message.stampSet(reset) + try! message.payloadSet(Data("edited".utf8)) + print("payload \(text(try! message.payloadGet))") + print("stamp \(debug(try! message.stampGet!))") print("secs \(reset.secs())") - message.stampSet(nil) - print("stamp \(message.stampGet != nil)") + try! message.stampSet(nil) + print("stamp \(try! message.stampGet != nil)") // Bound first, so Swift's reference is gone before the print. let kept = emit(PrintLog(), Data([1, 2, 3, 4])) @@ -424,13 +424,13 @@ func script() { q.launch { got.value = try await channel.recv() } q.drain() print("recv pending \(got.value == nil)") - channel.send(Data("hi".utf8)) + try! channel.send(Data("hi".utf8)) wakes.value += q.size print("woken \(wakes.value)") q.drain() print("recv \(text(got.value!))") block { - channel.send(Data()) + try! channel.send(Data()) do { print("nonempty \(try await channel.recvNonempty().count)") } catch { @@ -442,43 +442,43 @@ func script() { // The send happens at the call, before the future is first polled. try await channel.sendLater(Data("eager".utf8)) print("recv \(text(try await channel.recv()))") - channel.send(Data("sized".utf8)) + try! channel.send(Data("sized".utf8)) let (payload, size) = try await channel.recvSized() print("sized \(text(payload)) \(size)") } // The waker moves into Rust, which keeps it until the send wakes it. - channel.notify { wakes.value += 1 } - channel.send(Data("rung".utf8)) + try! channel.notify { wakes.value += 1 } + try! channel.send(Data("rung".utf8)) print("woken \(wakes.value)") block { // A poll with no async twin is a future. print("take \(text(try await channel.pollTake()))") // A `&mut self` poll needs a binding of its own, which a closure cannot capture. var measured = Channel.new() - measured.send(Data("measured".utf8)) + try! measured.send(Data("measured".utf8)) print("measure \(try await measured.pollMeasure(2))") } // A context is made of the waker, which Rust keeps until the send wakes it. - channel.listen { wakes.value += 1 } - channel.send(Data("heard".utf8)) + try! channel.listen { wakes.value += 1 } + try! channel.send(Data("heard".utf8)) print("woken \(wakes.value)") // A returned waker wakes the waiter's task through Rust until released. do { let waiter = Waiter.new { wakes.value += 1 } - waiter.waker()() - waiter.context()() + try! waiter.waker()() + try! waiter.context()() } print("woken \(wakes.value)") - var list = Node.new(1, nil) - list.push(2) - print("node \(list.sum())") + var list = try! Node.new(1, nil) + try! list.push(2) + print("node \(try! list.sum())") let tally = Tally.new() - _ = tally.bump() - print("tally \(tally.bump()) \(tally == tally)") - print("both \(bumpBoth(tally, tally))") - print("peek \(peek(tally) { lent in lent.bump() })") + _ = try! tally.bump() + print("tally \(try! tally.bump()) \(tally == tally)") + print("both \(try! bumpBoth(tally, tally))") + print("peek \(try! peek(tally) { lent in try! lent.bump() })") let grown = grow(Area.new(Size(width: 2, height: 3)), 1) let size = grown.size() @@ -498,19 +498,19 @@ func script() { // At its default, named as the type itself. var stack = Stack.new() for (i, n) in ["a", "b"].enumerated() { - stack.push(Counter.new(n, UInt64(i) + 1)) + try! stack.push(Counter.new(n, UInt64(i) + 1)) } - let top = stack.pop()!.get() - print("stack \(stack.depth()) Some(\(top)) \(stack.pop() != nil) \(stack.pop() == nil)") + let top = try! stack.pop()!.get() + print("stack \(try! stack.depth()) Some(\(top)) \(try! stack.pop() != nil) \(try! stack.pop() == nil)") // Generic functions and methods, at each implementation of the trait bounding them, by // reference, which leaves Swift its handle. let fromPoint = Counter.seededPoint("p", Point(x: -3, y: 4)) let fromSize = Counter.seededSize("s", Size(width: 5, height: 6)) - let fromCounter = Counter.seededCounter("c", fromPoint) + let fromCounter = try! Counter.seededCounter("c", fromPoint) let fromPolar = Counter.seededPolarPoint("q", PolarPoint(turns: 1, distance: 2)) print( - "seeded \(fromPoint.get()) \(fromSize.get()) \(fromCounter.get()) \(fromPolar.get()) " - + "past \(stack.depthPastPoint(Point(x: 1, y: 2))) seeds \(seedsPointSize(Size(width: 2, height: 3)))") + "seeded \(try! fromPoint.get()) \(try! fromSize.get()) \(try! fromCounter.get()) \(try! fromPolar.get()) " + + "past \(try! stack.depthPastPoint(Point(x: 1, y: 2))) seeds \(seedsPointSize(Size(width: 2, height: 3)))") // A trait implemented for `Point` and `&Point` apart: one instance. print("weigh \(weighPoint(Point(x: -1, y: 2)))") @@ -549,22 +549,22 @@ func script() { // Its Drop runs when Swift releases the last reference, not before. do { let token = Token.new(7) - withExtendedLifetime(token) { print("token \(token.idGet) dropped \(dropped())") } + withExtendedLifetime(token) { print("token \(try! token.idGet) dropped \(dropped())") } } print("dropped \(dropped())") // A field and the method of its name, each its own member. var retry = Retry.new(5) - let limit = retry.limitGet - print("retry \(limit != nil ? 1 : 0) \(limit ?? 0) \(retry.limit()) \(retry.delay())") + let limit = try! retry.limitGet + print("retry \(limit != nil ? 1 : 0) \(limit ?? 0) \(try! retry.limit()) \(try! retry.delay())") // The renamed field's setter, named as its getter. - retry.limitSet(nil) - print("retry \(retry.limit())") + try! retry.limitSet(nil) + print("retry \(try! retry.limit())") print("slot \(SlotU8.new(200).value())") // Foreign types: a URL parsed from text, socket addresses, OS paths, and `Vec`s. let base = try! Url.parse("https://example.com/a/b") - print("url \(base.description) host \(host(base).map { "Some(\"\($0)\")" } ?? "None")") + print("url \(base.description) host \(try! host(base).map { "Some(\"\($0)\")" } ?? "None")") let url = try! resolve(base.clone(), "c?d") print("resolve \(url.description) \(url == base)") do { @@ -596,23 +596,23 @@ func script() { let two = Counter.new("two", 2) var three: Counter? = Counter.new("three", 3) var none: Counter? = nil - print("plus \(two.plus(other: three!)) \(two.plus())") + print("plus \(try! two.plus(other: three!)) \(try! two.plus())") // A list and an option holding a struct beside a class, whose handle each moves. var pairs = UniqueArray() pairs.append(TupleCounterRefusal(Counter.new("a", 1), refusal("x"))) check( - reasons(pairs, TupleCounterRefusal(Counter.new("b", 2), refusal("y"))) + try! reasons(pairs, TupleCounterRefusal(Counter.new("b", 2), refusal("y"))) == "a refused: x, b refused: y", "reasons moves a struct and a class out of a list and an option" ) // Each optional borrowed parameter is an overload with and without its label. let one = Counter.new("one", 1) check( - two.clamped() == 2 && two.clamped(low: three!) == 3 && two.clamped(high: one) == 1 - && two.clamped(low: one, high: three!) == 2, + try! two.clamped() == 2 && try! two.clamped(low: three!) == 3 && try! two.clamped(high: one) == 1 + && try! two.clamped(low: one, high: three!) == 2, "clamped overloads each optional borrow" ) - print("add_to \(two.addTo(&three)) \(two.addTo(&none)) \(three!.get())") + print("add_to \(try! two.addTo(&three)) \(try! two.addTo(&none)) \(try! three!.get())") } do { var encoded = Data("0,".utf8) @@ -633,28 +633,28 @@ func script() { } do { let config = secure("h3") - print("secure \(alpnLent(config)) \(alpn(config.clone()))") + print("secure \(try! alpnLent(config)) \(try! alpn(config.clone()))") var meters = Meters.new() - let first = meters.first() - _ = first.add(2) - print("lent \(first.add(3)) \(meters.first().count())") - meters.clear() - print("cleared \(meters.first().count())") + let first = try! meters.first() + _ = try! first.add(2) + print("lent \(try! first.add(3)) \(try! meters.first().count())") + try! meters.clear() + print("cleared \(try! meters.first().count())") let tallies = Tallies.new() - _ = tallies.first().bump() - print("tallied \(tallies.first().bump())") + _ = try! tallies.first().bump() + print("tallied \(try! tallies.first().bump())") } print("chunks \(chunks(Data("abcde".utf8), 2).map { Array($0) })") var counters = counters(["x", "y"]) - print("counters \(listed(counters) { "\($0.name()) \($0.get())" })") + print("counters \(listed(counters) { "\(try! $0.name()) \(try! $0.get())" })") // Each counter moves in. var some = UniqueArray() some.append(counters.remove(at: 0)) var boxed = UniqueArray() boxed.append(counters.remove(at: 0)) - print("worth \(worth(some, boxed)) \(worth(nil, UniqueArray()))") - print("held \([heldCount(Counter.new("h", 4)), heldCount(nil)].map { $0.map { "Some(\($0))" } ?? "None" }.joined(separator: " "))") - print("boxed \(boxedName(boxedCounter("b")).map { "Some(\"\($0)\")" } ?? "None") \(boxedName(nil) ?? "None") \(boxedCounter(nil) == nil)") + print("worth \(try! worth(some, boxed)) \(try! worth(nil, UniqueArray()))") + print("held \([try! heldCount(Counter.new("h", 4)), try! heldCount(nil)].map { $0.map { "Some(\($0))" } ?? "None" }.joined(separator: " "))") + print("boxed \(try! boxedName(boxedCounter("b")).map { "Some(\"\($0)\")" } ?? "None") \(try! boxedName(nil) ?? "None") \(boxedCounter(nil) == nil)") print("less \(less(5, 2))") print("sums \(sums([[1, 2], [], [3]]))") let sum = summed([Point(x: 1, y: 2), Point(x: -4, y: 6)]) @@ -670,16 +670,16 @@ func script() { let parts = String(decoding: Api.joined([Data("ab".utf8), Data(), Data("c".utf8)]), as: UTF8.self) print("joined \(parts) \(Api.joined([]).count) read \(readLen(Data("hello".utf8)))") let doc = try! Json.parse(#"{"b": [1, 2.5, null], "a": "é"}"#) - print("json \(encode(doc))") - print("timed text \(timedText(doc))") - let tagged = tagged(doc, "t") - print("tagged \(tagged.toText())") + print("json \(try! encode(doc))") + print("timed text \(try! timedText(doc))") + let tagged = try! tagged(doc, "t") + print("tagged \(try! tagged.toText())") // A JSON object's entries cross as pairs of a key and its document, each moved in. var pairs = UniqueArray() pairs.append(TupleStringJson("b", try! Json.parse("[1]"))) pairs.append(TupleStringJson("a", try! Json.parse("2"))) - let jsonEntries = entries(pairs, "t") - print("entries \(listed(jsonEntries) { "\($0._0) \($0._1.toText())" })") + let jsonEntries = try! entries(pairs, "t") + print("entries \(listed(jsonEntries) { "\($0._0) \(try! $0._1.toText())" })") do { _ = try Json.parse("{\"a\":") } catch let e as StringError { @@ -687,20 +687,20 @@ func script() { } catch { check(false, "parse throws its message") } - let site = Site.new(base) + let site = try! Site.new(base) print("site \(site.homeGet.description) \(site.homeGet.description)") // Slices, passed in and returned as clones, and the socket address of each family. print("shapes \(Shape.ALL) greetings \(GREETINGS)") var trail = Trail.new([Point(x: 1, y: 2), Point(x: 3, y: 4)], ["a", "b"]) - print("trail \(trail.points()) \(trail.labels())") + print("trail \(try! trail.points()) \(try! trail.labels())") // Returned iterators, each collected at the call: drained labels are gone after. - print("iterated \(trail.iter()) \(trail.names()) \(Array(trail.bytes()))") - print("drained \(trail.drain()) then \(trail.labels())") + print("iterated \(try! trail.iter()) \(try! trail.names()) \(Array(try! trail.bytes()))") + print("drained \(try! trail.drain()) then \(try! trail.labels())") // A filter, collected at the call. - print("beyond \(trail.beyond(1).map { "\($0.x) \($0.y)" }.joined(separator: ", "))") + print("beyond \(try! trail.beyond(1).map { "\($0.x) \($0.y)" }.joined(separator: ", "))") let numbered = numbered(["p", "q"]) - print("numbered \(listed(numbered) { "\($0.name()) \($0.get())" })") + print("numbered \(listed(numbered) { "\(try! $0.name()) \(try! $0.get())" })") let (next4, next6) = nexts(SocketAddr(ip: [127, 0, 0, 1], port: 8080), v6) print("nexts \(next4.ip.map(String.init).joined(separator: ".")) \(next4.port) \(next6.port) \(next6.scopeId)") @@ -708,27 +708,27 @@ func script() { var sectionPairs = UniqueArray() sectionPairs.append(TupleStringJson("b", try! Json.parse("[1]"))) sectionPairs.append(TupleStringJson("a", try! Json.parse("2"))) - let jsonSections = sections(sectionPairs) - print("sections \(listed(jsonSections) { "\($0._0) \($0._1.toText())" })") + let jsonSections = try! sections(sectionPairs) + print("sections \(listed(jsonSections) { "\($0._0) \(try! $0._1.toText())" })") // An instant only Rust makes, which each call copies out of its handle, and compares. let millis = { (d: Swift.Duration) in d.components.seconds * 1000 + d.components.attoseconds / 1_000_000_000_000_000 } let began = now() - let ended = later(began, .milliseconds(1500)) - print("instants \(millis(since(ended, began))) \(millis(since(began, ended))) \(began == later(began, .zero)) \(began == ended)") + let ended = try! later(began, .milliseconds(1500)) + print("instants \(millis(try! since(ended, began))) \(millis(try! since(began, ended))) \(began == try! later(began, .zero)) \(began == ended)") // tracing's level, and its filter as an optional level whose nil is `OFF`. var verbosity = Verbosity.new(.warn) - print("verbosity \(spell(verbosity.filter())) \(spell(louder(verbosity.filter())))") - verbosity.levelSet(.trace) - print("verbosity \(verbosity.levelGet == .trace) \(spell(verbosity.filter())) \(spell(louder(verbosity.filter()))) \(spell(louder(nil)))") + print("verbosity \(spell(try! verbosity.filter())) \(spell(louder(try! verbosity.filter())))") + try! verbosity.levelSet(.trace) + print("verbosity \(try! verbosity.levelGet == .trace) \(spell(try! verbosity.filter())) \(spell(louder(try! verbosity.filter()))) \(spell(louder(nil)))") print("made \(made(3) { "\($0) times" })") // A closure taking and returning `Copy` objects copies them, never running their `Clone`. let nine = Ticket.new(9) - let given = reissue(Ticket.new(7)) { Ticket.new($0.number() + 1) }.number() - let held = reissue(Ticket.new(7)) { _ in nine }.number() + let given = try! reissue(Ticket.new(7)) { Ticket.new(try! $0.number() + 1) }.number() + let held = try! reissue(Ticket.new(7)) { _ in nine }.number() print("reissued \(given) \(held), cloned \(Ticket.clones())") // A kept closure, called on another thread after the call that took it returned. - print("spawned \(Spawned.new { 7 }.join())") + print("spawned \(try! Spawned.new { 7 }.join())") // A kept closure: called again at the instant it asks for, then once its waiter wakes. let start = now() let calls = Cell(0) @@ -736,10 +736,10 @@ func script() { try await drive(start) { now, waiter in calls.value += 1 switch calls.value { - case 1: return later(now, .milliseconds(5)) + case 1: return try! later(now, .milliseconds(5)) case 2: - print("drive \(millis(since(now, start)))") - waiter.waker()() + print("drive \(millis(try! since(now, start)))") + try! waiter.waker()() return nil default: throw StringError(error: "stopped after \(calls.value)") } @@ -850,7 +850,7 @@ func futures() { // The send lands from another thread while the receive waits. let payload = block { - Thread.detachNewThread { channel.send(Data("blocked".utf8)) } + Thread.detachNewThread { try! channel.send(Data("blocked".utf8)) } return try await channel.recv() } check(text(payload) == "blocked", "recv returns the payload another thread sent") @@ -863,7 +863,7 @@ func futures() { q.launch { got.value = text(try await channel.recv()) } q.drain() check(got.value == nil, "recv waits for a send") - channel.send(Data("then".utf8)) + try! channel.send(Data("then".utf8)) check(got.value == nil, "recv never resumes inside the wake") q.drain() check(got.value == "then", "recv resumes with the payload") @@ -901,7 +901,7 @@ func futures() { } } q.drain() - channel.send(Data("kept".utf8)) + try! channel.send(Data("kept".utf8)) task.cancel() q.drain() check(!called.value && threw.value, "a cancelled receive throws CancellationError") @@ -912,7 +912,7 @@ func futures() { // A task cancelled before its call takes nothing, even a payload ready at the first poll. do { let threw = Cell(false) - channel.send(Data("ready".utf8)) + try! channel.send(Data("ready".utf8)) let task = q.launch { do { _ = try await channel.recv() @@ -955,7 +955,7 @@ func futures() { q.launch { got.value = text(try await other.recv()) } q.drain() } - sender.send(Data("held".utf8)) + try! sender.send(Data("held".utf8)) q.drain() check(got.value == "held", "the call awaiting on an object holds it") } @@ -1048,7 +1048,7 @@ func futures() { _ = held return 1 } - check(spawned.join() == 1, "a spawned closure runs") + check(try! spawned.join() == 1, "a spawned closure runs") until { freed.value } check(freed.value, "Rust releases a kept closure once it drops it") } @@ -1093,7 +1093,7 @@ func owned() { check(dropped() == before + 1, "releasing returned bytes frees Rust's") do { let token = Token.new(2) - check(token.idGet == 2, "a token reads back") + check(try! token.idGet == 2, "a token reads back") } check(dropped() == before + 2, "releasing an object frees its handle") do { @@ -1154,28 +1154,28 @@ func broken(_ which: String) { // A lent return outliving its owner's change, which Rust checks. case "outlived": var meters = Meters.new() - let first = meters.first() - meters.clear() - _ = first.count() + let first = try! meters.first() + try! meters.clear() + _ = try! first.count() // An IPv6 address where Rust takes `SocketAddrV4`. case "family": let any = SocketAddr(ip: Array(repeating: 0, count: 16), port: 1) _ = nexts(any, any) // No String holds a path that is not UTF-8. case "stray": - _ = stray() + _ = try! stray() case "lent stray": - visitStray { _ in } + try! visitStray { _ in } // A class moved into Rust, which Swift still holds: the call closes its handle. case "refusal moved": let kept = refusal("z") var pairs = UniqueArray() pairs.append(TupleCounterRefusal(Counter.new("a", 1), kept)) - _ = reasons(pairs, nil) + _ = try! reasons(pairs, nil) _ = kept.description // The file serves wasm32 too, whose items the host's library lacks. case "target": - _ = wasmOnly() + _ = try! wasmOnly() case "variant": _ = pointerBits(.wasm(bits: 32)) case "plain variant": diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index a348ea731..788a61798 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -195,7 +195,7 @@ fn api_c() { ("lent", "api_Registry_edit: a lent handle was freed"), ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("null", "api_Registry_edit: a function is null"), ("returned", "api_made: a returned buffer is not one buf_copy made"), @@ -203,7 +203,7 @@ fn api_c() { ("outlived", OUTLIVED), ( "lent shared", - "api_Meter_reset: a handle a call lent shared is borrowed exclusively", + "api_Meter_reset: a handle a call lent is borrowed exclusively or moved out", ), ("target", "api_wasm_only: it is not on this library's target"), ( @@ -260,7 +260,7 @@ fn api_cpp() { ), ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("lent kept", "a lent object was moved out of"), ("overread", "api_tail: a reader read more than it was asked"), @@ -585,7 +585,7 @@ fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { ("lent kept", "Counter is closed"), ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("callback threw", "api::Log::log: a call into Kotlin threw"), ("closure threw", "api_made: a call into Kotlin threw"), @@ -642,7 +642,7 @@ fn api_python() { ("lent held", "Counter: a call still holds it when its lend ends"), ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("callback threw", "api::Log::log: a call into Python raised"), ("foreign cancelled", "a future Rust polls raised"), @@ -862,7 +862,7 @@ fn api_dart() { ("lent kept", "Bad state: Counter is closed"), ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("callback threw", "api::Log::log: a call into Dart threw"), ( @@ -1068,7 +1068,7 @@ fn api_go() { for (case, message) in [ ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("nil reader", "a nil io.ReadSeeker"), ("returned closed", "Instant is closed"), From 62749c84e404d3258ac097965091326951d90e32 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 20:03:10 -0800 Subject: [PATCH 03/30] wip(ffi): uncounted borrows for unchecked types, docs, quest Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- ffi/README.md | 50 ++++++++++++---- ffi/runtime/rt.rs | 24 ++++++++ ffi/src/abi.rs | 9 ++- ffi/src/glue.rs | 12 +++- ffi/tests/api.c | 60 +++++++++++++++++++ ffi/tests/api.swift | 10 ++-- ffi/tests/fixtures.rs | 14 +++-- quest/m1/ffi-export/checks-raise.md | 91 +++++++++++++---------------- 9 files changed, 195 insertions(+), 77 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 31b52b4c0..3e1c8e08a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -5,7 +5,7 @@ Each project lives in its own top-level directory; the [quest tree](quest/README - An unsupported construct or malformed input is a compile error with a source span, never a warning, a silent fallback, or a runtime failure. Generated code panics only where the Rust source does. One exception is an `unsafe` pointer from elsewhere whose origin translation can't see, such as a parameter's: a loud run-time check may stand in for the refusal, under m3's [unsafe policy](quest/m3/README.md), since refusing every such pointer would refuse pointer arithmetic on any parameter. - The other is a binding, which checks at run time only the Rust rules its language cannot express: borrow exclusivity, use after a move or close, and the thread of a value that is not `Send`. A failed check raises the language's own error, or aborts where it has none; a Rust panic aborts. + The other is a binding, which checks at run time only the Rust rules its language cannot express: borrow exclusivity, use after a move or close, the thread of a value that is not `Send`, and a call of an item the library's target lacks. A failed check raises the language's own error, or aborts where it has none; a Rust panic aborts. - Generated output must match the source's semantics exactly: integer widths and overflow, drop order, laziness, and error paths. Prove it with differential tests against native Rust, not by inspection. - A binding's types are lossless: every value of the Rust type crosses and returns exactly. A native type stands in only where it is exact; otherwise the runtime's own type crosses, with checked conversions to and from the idiomatic one. Speed ranks above ergonomics. - Everything is configless, with opinionated names and defaults. Translators take stock crates as they are: no annotations, and no config beyond true platform boundaries. Each config key or source change a consumer needs is a gap to close. diff --git a/ffi/README.md b/ffi/README.md index 60386f2f6..735d4ed2d 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -158,8 +158,9 @@ item that target compiles works exactly, and no other does: - A function, method, or constructor only some targets have is declared everywhere. On another target, the library defines it to fail, and the binding checks the library's target before calling it, raising the language's own error, as for a closed object: - `UnsupportedOperationException` in Kotlin, `NotImplementedError` in Python, a trap in - Swift, a panic in Go, and `UnsupportedError` in Dart. C and C++ abort with the message. + `UnsupportedOperationException` in Kotlin, `NotImplementedError` in Python, a + `CheckError` thrown in Swift, an error matching `errors.ErrUnsupported` returned in Go, + and `UnsupportedError` in Dart. C and C++ abort with the message. - So does a variant only some targets have, when a value holding it crosses into a target without it. A record's fields cross whole, so one whose fields differ between targets is refused, as below. @@ -190,13 +191,19 @@ dropped for the other target's span where the file differs. does, checked at run time: shared for `&T`, exclusive for `&mut T`, and moved out for `T`, after which the handle is only freed. A `Copy` value is copied out instead, and a value that is not `Sync` is shared on one thread at a time, and held exclusively by a - future, which may be polled on any thread. A borrow Rust would - refuse at compile time aborts, naming the function, as does a null or moved-out handle: - there is no Rust caller to return an error to. A setter borrows as `&mut T` and takes + future, which may be polled on any thread. A borrow Rust would refuse at compile time, + or a moved-out handle, fails the call before any Rust code runs, so it changes nothing: + a function whose handles can fail a check takes a trailing `_status *status`, + through which it writes the `_STATUS_*` that failed, returning a zeroed value, + and `_status_message` names it. A NULL `status` aborts with the message, naming + the function, as C++ passes; each other binding raises its language's own error, as + below. A null handle aborts. Only a type some export can misuse is checked: one that is + not `Sync`, or that an export borrows as `&mut`, moves out, or lends a closure; a call + borrowing none of those takes no `status`. A setter borrows as `&mut T` and takes its value as `T`, so it writes the value the handle holds, or the one Rust lent it, while a getter's copy is C's own: a nested field is got, set, and set back. A type whose C++ copies share one value has `_borrowed`, true while a call or future borrows - it, so moving it out would abort. + it, so moving it out would fail. - **In and out**: a `&mut` of a value is two parameters, the value passed in as one taken by value is, and a `_out` pointer, through which Rust writes what the call left in it on every path, its errors included. C keeps what it passed, and frees what Rust wrote @@ -204,9 +211,9 @@ dropped for the other target's span where the file differs. assigned what Rust wrote; Kotlin, Python, and Dart take a `Mut` holding the value, whose `value` Rust's write replaces, before any error raises. - **Lent handles** borrow from the handle a call lent them from: each use holds that one - shared and aborts, naming the function, once it was since borrowed exclusively, moved - out, or freed, which Rust's borrow checker would refuse. Borrowing a lent handle - exclusively aborts too, as Rust lent it shared. + shared and fails with `_STATUS_OUTLIVED` once it was since borrowed exclusively, + moved out, or freed, which Rust's borrow checker would refuse. Borrowing a lent handle + exclusively, or moving it out, fails with `_STATUS_LENT`, as Rust lent it. - **Interned strings**: a `&str` a function needs for `'static`, as `Path::from_static` does, is leaked once per distinct content and reused for every later equal one, so a caller passing ever-new strings grows the set without bound. @@ -321,6 +328,8 @@ exceptions and RTTI on or off: it throws nothing and reads no type information. run-time check, since Clang does not track a returned handle class. No export keeps a view or reference past its call, so none carries `[[clang::lifetime_capture_by]]`; the first that does adds it. +- **Checks**: a call passes no `status`, so a borrow or move Rust's rules refuse aborts + with its message, naming the function, as C's does with a NULL one. - **Sharing** stands in for `clone` where no call can tell the two apart but by counting an `Arc`'s or `Rc`'s clones, which the exporter decides at compile time. A type's copies share when it is `Clone`; `Sync`, since one that is not is borrowed on one thread at a time, which copies on two threads @@ -409,6 +418,9 @@ in as a `ByteArray`, bytes returned as their owner, and anything else encoded in A field is a `val`, `firstGet`, and its setter a function, `firstSet(value)`, named as the C ones, since setting an object moves it in and closes its handle, which an assignment would hide. +- **Checks**: a borrow or move Rust's rules refuse, such as a `&mut` borrow of an object + another call borrows, or a move out of one a closure was lent, throws an + `IllegalStateException` naming the function and the rule, before any Rust code runs, so the call changed nothing. - **Records** are data classes, compared by their fields, and **enums** an `enum class`, or for one with data a sealed class of a data class per variant. `Display` is `toString()`, `Debug` `debug()`, and `Default` a companion's `default()`. @@ -498,6 +510,9 @@ caller imports it with no ignores. after which a call raises `ValueError`. `Clone` is `clone()`, and `PartialEq` is `==`, unhashable. A field is a property, `first_get`, and its setter a method, `first_set(value)`, named as the C ones, since setting an object moves it in and closes it, which an assignment would hide. +- **Checks**: a borrow or move Rust's rules refuse raises, naming the function and the rule, + before any Rust code runs, so the call changed nothing: `ValueError` for a value moved out, or a lent object that outlived the one it + borrows from, as for a closed object, and `RuntimeError` for any other. - **Records** are slotted dataclasses, compared by their fields, and **enums** an `Enum` of upper-case members, or for one with data a class of a dataclass per variant, `Event.Moved(p)`. `Display` is `str()`, `Debug` `repr()`, and `Default` a static @@ -587,10 +602,17 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod `Equatable`; `Display` is `description` and `Debug` `debugDescription`, which Swift conforms no noncopyable type to yet. An object that is `Copy`, or thrown as an error, which `Swift.Error` requires be copyable, is a final class instead, whose handle a call - holds until it returns, and moving its value out closes it, after which a call traps. A + holds until it returns, and moving its value out closes it, after which a call throws. A field is a computed property, `firstGet`, and its setter a function, `firstSet(value)`, named as the C ones, since setting an object moves it in, which an assignment would hide. An object is `Sendable`, since Rust checks each borrow at run time. +- **Checks**: a borrow or move Rust's rules refuse at run time, such as a borrow of a value + that is not `Sync` on a second thread, throws a `CheckError`, holding the function and + the rule, before any Rust code runs, so the call changed nothing. So every function whose objects can fail a check `throws`, and a + field read stays a property whose getter throws, except one of a noncopyable value, + whose getter Swift cannot let throw, which is a method, `firstGet()`. `description`, + `debugDescription`, `==`, `clone()`, and a copy a closure returns, which cannot throw, + trap instead. - **Records** are structs and **enums** Swift enums, with a variant's data as associated values, all `Sendable`. `PartialEq` is `==`, through Rust's; `Display` is `description`, `Debug` `debugDescription`, and `Default` a static `default()`. @@ -688,6 +710,12 @@ reads a go.mod. and `GoString()`, which return none, return its text. A `self` method, or passing the object by value, closes it once the call returns. `Clone` is `Clone()`, and a `pub` field a getter, `FirstGet()`, and a setter, `FirstSet(v)`. +- **Checks**: a borrow or move Rust's rules refuse returns an error naming the function + and the rule, before any Rust code runs, so the call changed nothing: one matching `rt.ErrClosed` for a value moved out, or a lent + object that outlived the one it borrows from, and `rt.ErrBorrowed` for any other. So + every function whose objects can fail a check returns an `error`. `String()`, + `GoString()`, and what a closure returns have none to return, so they abort with the + message. - **Records** are structs, and **plain enums** an `int32` type with a constant per variant (`ShapeSquare`). An enum with data is an interface each variant's struct implements (`EventMoved{V0: p}`), sealed so only the package's do; its methods are the @@ -789,6 +817,8 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass send an object to another, so no `close()` races a call. A `self` method, or passing the object by value, closes it once the call returns. `Clone` is `clone()`, and a `pub` field a getter and a setter. +- **Checks**: a borrow or move Rust's rules refuse throws a `StateError` naming the + function and the rule, before any Rust code runs, so the call changed nothing. - **Records** are final classes with a named, required parameter per field, and positional fields are `$0`, `$1`, and so on. **Plain enums** are Dart enums, and an enum with data a sealed class with a final class per variant (`EventMoved`), so a `switch` diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index 15a37fab4..c993d3194 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -826,6 +826,20 @@ impl Object { Ok(Shared { object }) } + /// Borrow the value shared for a call, uncounted: no export borrows a value of its type + /// exclusively, moves it out, or lends it, and it is `Sync`, so no borrow can conflict + /// with this one, which costs no atomic operation. + /// + /// # Safety + /// As [`Self::borrow`], for such a type alone. + pub unsafe fn peek<'a>(handle: *const c_void, what: &str) -> Peek<'a, T> + where + T: Sync, + { + // SAFETY: the caller's contract. + Peek(unsafe { &*Self::get(handle, what).value() }) + } + /// Borrow a value that is not `Sync` shared for a call: any number of times on one /// thread, as Rust allows, and on no other thread meanwhile. /// @@ -1075,6 +1089,16 @@ impl Drop for Shared<'_, T> { } } +/// [`Object::peek`]'s borrow, which ends with nothing to release. +pub struct Peek<'a, T>(&'a T); + +impl Deref for Peek<'_, T> { + type Target = T; + fn deref(&self) -> &T { + self.0 + } +} + pub struct Local<'a, T> { object: &'a Object, } diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index 0bc32c154..3fc511155 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -146,8 +146,8 @@ pub const STATUSES: [(&str, &str); 6] = [ /// value is not `Sync`, so a borrow on another thread fails; an export borrows it /// exclusively or moves it out, so a borrow beside another call's fails; or a handle to /// it is lent, by a call returning a borrow or to a closure or trait for a call, so a use -/// past the lend fails. Every other object's handles are only ever borrowed shared, which -/// never fails. +/// past the lend fails, as may a trait's method returning it. Every other object's handles +/// are only ever borrowed shared, which never fails, so a call borrows them uncounted. fn checked(api: &Api) -> Vec { /// Mark each object a parameter of type `ty` borrows exclusively, moves, or lends, /// `owned` when `ty` is passed by value rather than behind a shared reference. @@ -164,6 +164,8 @@ fn checked(api: &Api) -> Vec { Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => param(api, inner, owned, checked), Type::Tuple(elements) => elements.iter().for_each(|ty| param(api, ty, owned, checked)), Type::Closure(closure) => closure.params.iter().for_each(|ty| lent(api, ty, checked)), + // A record or data enum passed by value moves each object it holds. + Type::Record(_) | Type::Enum(_) if owned => lent(api, ty, checked), _ => {} } } @@ -190,8 +192,11 @@ fn checked(api: &Api) -> Vec { lent(api, &f.ok, &mut checked); } } + // What an implementation is lent, and what it hands Rust, which moves it out. for method in api.callbacks.iter().flat_map(|c| &c.methods) { method.params.iter().for_each(|p| lent(api, &p.ty, &mut checked)); + lent(api, &method.ok, &mut checked); + method.err.iter().for_each(|err| lent(api, err, &mut checked)); } checked } diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 5bcdc3da0..25500dc70 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -966,7 +966,10 @@ impl Gen<'_> { // Each borrows its handles shared, which fails only as a call can. let status = self.abi.traits_status(i).then_some("status"); self.checking.set(status.is_some()); - let borrowing = |e: &str| self.checked(&format!("unsafe {{ rt::Object::<{path}>::{borrow}({e}, w) }}")); + let borrowing = |e: &str| match status { + Some(_) => self.checked(&format!("unsafe {{ rt::Object::<{path}>::{borrow}({e}, w) }}")), + None => format!("unsafe {{ rt::Object::<{path}>::peek({e}, w) }}"), + }; let copy = borrowing("this"); let borrowing: Vec = ["this", "a", "b"].into_iter().map(borrowing).collect(); self.checking.set(false); @@ -1533,6 +1536,13 @@ impl Gen<'_> { true => "let mut", false => "let", }; + // A type no export can misuse is borrowed shared for a call uncounted. + if !exclusive && !future && !self.abi.checked[i] { + return ( + binding, + format!("unsafe {{ rt::Object::<{}>::peek({c}, w) }}", self.object_path(i)), + ); + } ( binding, self.checked(&format!( diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 9415254b5..27c620fc2 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -1591,7 +1591,67 @@ static int script(void) { } /* Each breaks a contract, which must abort with a message naming the function. */ +/* A counter a closure was lent, which it fails to move out, then reads as it was. */ +static bool move_lent_status(void *data, api_Counter *counter) { + api_status *status = data; + api_Counter_into_label(counter, (api_option_Point){0}, status); + return *status == API_STATUS_LENT && api_Counter_get(counter, NULL) == 0; +} + +/* Each check a call fails writes its status through a non-null one, and changes nothing. */ +static int statuses(void) { + int failed = 0; + api_status status = 0; + api_Channel *channel = api_Channel_new(); + api_Channel_send(channel, str("x"), NULL); + api_Channel_recv_future *recv = api_Channel_recv(channel, NULL); + api_Channel_clear(channel, &status); + if (status != API_STATUS_BORROWED || strcmp(api_status_message(status), "a handle is already borrowed") != 0) { + fprintf(stderr, "a clear during a recv wrote %d\n", status); + failed = 1; + } + /* The recv still has what was sent, and once it ends the clear passes. */ + api_waker waker = {0}; + api_buf payload; + if (api_Channel_recv_poll(recv, waker, &payload) != API_POLL_READY || payload.len != 1) { + fprintf(stderr, "a failed clear changed the channel\n"); + failed = 1; + } + api_buf_free(payload); + api_Channel_recv_future_free(recv); + status = 0; + api_Channel_clear(channel, &status); + if (status != 0) { + fprintf(stderr, "a clear after the recv wrote %d\n", status); + failed = 1; + } + api_Channel_free(channel); + + status = 0; + api_Meters *meters = api_Meters_new(); + api_Meter *first = api_Meters_first(meters, NULL); + api_Meters_clear(meters, NULL); + if (api_Meter_count(first, &status) != 0 || status != API_STATUS_OUTLIVED) { + fprintf(stderr, "a meter that outlived its meters wrote %d\n", status); + failed = 1; + } + api_Meter_free(first); + api_Meters_free(meters); + + status = 0; + api_Registry *registry = api_Registry_new(api_Counter_new(str("c"), 0), NULL); + if (!api_Registry_edit(registry, move_lent_status, &status, NULL)) { + fprintf(stderr, "a lent counter moved out wrote %d\n", status); + failed = 1; + } + api_Registry_free(registry); + return failed; +} + static int broken(const char *which) { + if (strcmp(which, "statuses") == 0) { + return statuses(); + } if (strcmp(which, "moved") == 0) { api_Counter *c = api_Counter_new(str("c"), 0); api_Label label = api_Counter_into_label(c, (api_option_Point){0}, NULL); diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index b529f5ef9..1db34d4a0 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -283,7 +283,7 @@ func script() { let by: UInt64 = 5 let edited = try! registry.edit { counter in _ = try! counter.add(by) - return try! counter.edited() && try! counter.get() == 6 + return try! counter.edited() && counter.get() == 6 } print("edit \(edited) \(try! registry.firstGet().get())") // A setter moves an object in, as assignment does, closing its handle. @@ -608,8 +608,8 @@ func script() { // Each optional borrowed parameter is an overload with and without its label. let one = Counter.new("one", 1) check( - try! two.clamped() == 2 && try! two.clamped(low: three!) == 3 && try! two.clamped(high: one) == 1 - && try! two.clamped(low: one, high: three!) == 2, + try! two.clamped() == 2 && two.clamped(low: three!) == 3 && two.clamped(high: one) == 1 + && two.clamped(low: one, high: three!) == 2, "clamped overloads each optional borrow" ) print("add_to \(try! two.addTo(&three)) \(try! two.addTo(&none)) \(try! three!.get())") @@ -688,7 +688,7 @@ func script() { check(false, "parse throws its message") } let site = try! Site.new(base) - print("site \(site.homeGet.description) \(site.homeGet.description)") + print("site \(try! site.homeGet().description) \(try! site.homeGet().description)") // Slices, passed in and returned as clones, and the socket address of each family. print("shapes \(Shape.ALL) greetings \(GREETINGS)") @@ -714,7 +714,7 @@ func script() { let millis = { (d: Swift.Duration) in d.components.seconds * 1000 + d.components.attoseconds / 1_000_000_000_000_000 } let began = now() let ended = try! later(began, .milliseconds(1500)) - print("instants \(millis(try! since(ended, began))) \(millis(try! since(began, ended))) \(began == try! later(began, .zero)) \(began == ended)") + print("instants \(millis(try! since(ended, began))) \(millis(try! since(began, ended))) \(try! began == later(began, .zero)) \(began == ended)") // tracing's level, and its filter as an optional level whose nil is `OFF`. var verbosity = Verbosity.new(.warn) print("verbosity \(spell(try! verbosity.filter())) \(spell(louder(try! verbosity.filter())))") diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 788a61798..609cec4d2 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -176,6 +176,8 @@ fn api_c() { api.transcript, "the C transcript differs from the native one" ); + // A non-null status takes each failed check, which changed nothing. + run(Command::new(&exe).arg("statuses")); // Each broken contract aborts, naming the function and what broke. for (case, message) in [ @@ -585,13 +587,13 @@ fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { ("lent kept", "Counter is closed"), ( "lent moved", - "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", + "IllegalStateException: api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("callback threw", "api::Log::log: a call into Kotlin threw"), ("closure threw", "api_made: a call into Kotlin threw"), ("closure failed", "api_drive: a call into Kotlin threw"), ("reader threw", "SeekableByteChannel.read: a call into Kotlin threw"), - ("outlived", OUTLIVED), + ("outlived", "IllegalStateException: api_Meter_count: a handle lent from another outlived it"), ("stray", "MalformedInputException"), ("family", "an IPv6 address has no Rust SocketAddrV4"), ] { @@ -642,11 +644,11 @@ fn api_python() { ("lent held", "Counter: a call still holds it when its lend ends"), ( "lent moved", - "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", + "RuntimeError: api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("callback threw", "api::Log::log: a call into Python raised"), ("foreign cancelled", "a future Rust polls raised"), - ("outlived", OUTLIVED), + ("outlived", "ValueError: api_Meter_count: a handle lent from another outlived it"), ("family", "ValueError: an IPv6 address has no Rust SocketAddrV4"), ("kept threw", "api_Spawned_new: a call into Python raised"), ("returned", "api_made: a call into Python raised"), @@ -862,7 +864,7 @@ fn api_dart() { ("lent kept", "Bad state: Counter is closed"), ( "lent moved", - "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", + "Bad state: api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("callback threw", "api::Log::log: a call into Dart threw"), ( @@ -873,7 +875,7 @@ fn api_dart() { "reader threw", "a reader's seek: a call into Dart threw Bad state: a seek failed", ), - ("outlived", OUTLIVED), + ("outlived", "Bad state: api_Meter_count: a handle lent from another outlived it"), ("family", "an IPv6 address has no Rust SocketAddrV4"), ("stray", "a path from Rust is not UTF-8"), ("lent stray", "a path from Rust is not UTF-8"), diff --git a/quest/m1/ffi-export/checks-raise.md b/quest/m1/ffi-export/checks-raise.md index 37c96cf12..248ca2597 100644 --- a/quest/m1/ffi-export/checks-raise.md +++ b/quest/m1/ffi-export/checks-raise.md @@ -1,62 +1,49 @@ -# [L] Failed checks raise +# [M] Failed checks raise ## Goal -A run-time check a [rust2-ffi](/ffi/README.md) binding fails, for a rule its language -cannot express, raises the language's own error and leaves the process running: a borrow -Rust would refuse (a `&mut self` call while another call holds the handle), a call on a -moved-out or closed handle, an object lent for a call used past it, or an item called on a -target that lacks it. Kotlin, Python, Dart, and Swift throw, and Go returns an `error`. C -and C++ abort with the message, and a Rust panic aborts everywhere. Each handle's borrows -are counted once per call, in Rust, and a type no export can misuse pays for no check. +A run-time check a [rust2-ffi](/ffi/README.md) binding fails raises the language's own +error and leaves the process running, everywhere the binding has a caller to raise to, and +each handle's borrows are counted once per call, in Rust: the bindings keep no hold count +of their own beside the runtime's. ## Plan -Decided with the maintainer on 2026-11-09, in the quest audit: ✅ checks raise and panics -abort. Rejected: raising panics too, which costs unwind tables and landing pads in every -export and a poisoned state for an object that panicked mid-call; aborting on every failed -check, as today. The check runs before any Rust code does, so nothing is corrupted when it -fails. AGENTS.md carries the rule. - -Decided with the maintainer on 2026-11-12, in the quest audit: - -- ✅ The one accounting is the runtime's, in `ffi/runtime/rt.rs`: a failed check returns a - status and the binding raises from it. Rejected: the accounting in each binding, as this - quest first planned, which ports the runtime's state machine (shared, exclusive, moved, - other thread, lent) to five runtimes and m3's three, and needs unchecked C entry points, - since Swift and Go call the C header directly; building both in Go first to measure. -- ✅ The exporter decides at export time which types need a check. A type no export - borrows exclusively or moves skips the accounting on its owned handles; lent handles - keep it. `ffi/src/ir.rs` already computes most of this for an object's `shared` flag. - -What the code does today: - -- `rt.rs` aborts through `fail` at 34 sites, reachable from safe multithreaded Kotlin, Go, - and Python. Every handle argument pays a compare-and-swap in and a `fetch_sub` out, which - is the whole of C's 10 ns u64 getter against Rust's 0.4 - ([the binding benchmark](/bench/ffi/README.md)). -- Kotlin and Go keep a hold count of their own beside the runtime's, so a call costs four - atomic operations per handle. Swift keeps one only for its classes since - [#697](https://github.com/moq-dev/rust2/pull/697), and traps on a closed handle - (`rt.swift`'s `hold`). The bindings' counts go; the runtime's status replaces them. -- [#700](https://github.com/moq-dev/rust2/pull/700) added a fourth check, an item called - on a target the library is not built for, which traps in Swift (`ffi/src/swift.rs`) and - panics in Go (`ffi/src/go.rs`). AGENTS.md lists three; add it there with this change. -- Swift's `async throws` and throwing functions already exist for `Result`; a call that - can fail only a check becomes throwing too, which changes its signature. Go already - returns `rt.ErrClosed` from every call that takes a handle. -- A foreign exception inside a callback or lent closure still aborts, since Rust has - nowhere to unwind it to. -- `ffi/README.md` says the C++ header throws nothing, so C++ aborts as C does. - -Measure a call before and after on the benchmark's handle call and u64 getter rows. A -checked call may keep the runtime's two atomics; a type that needs no check should reach -Rust's own cost plus the crossing. - -Public API: which generated functions throw or return an error (breaking in Swift), the -error types the runtimes add, and the status the C functions return. +Decided with the maintainer on 2026-11-09 and 2026-11-12, in the quest audits: checks +raise and panics abort; the one accounting is the runtime's, in `ffi/runtime/rt.rs`, whose +failed check returns a status the binding raises from; and the exporter decides at export +time which types need a check. + +What the code does now: + +- A function whose handles can fail a check takes a trailing `_status *status`; + NULL aborts with the message, as C++ passes. Kotlin throws `IllegalStateException`, + Python `ValueError` or `RuntimeError`, Dart `StateError`, Swift `CheckError`, and Go + returns an error matching `rt.ErrClosed` or `rt.ErrBorrowed`. An item off the target + throws in Swift and returns `errors.ErrUnsupported` in Go. A type no export borrows + exclusively, moves, or lends, and that is `Sync`, is borrowed uncounted (`Object::peek`). +- Still aborting where a caller exists: Swift's `description`, `debugDescription`, `==`, + and `clone()` of a checked object, which its protocols cannot let throw; Go's `String()` + and `GoString()`; a record's or data enum's trait functions (`_eq`, `_to_string`, + `_debug`) whose value holds a checked handle; and Swift's `Arena.hold`, which traps on a + closed class handle. +- Kotlin, Go, Python, and Dart keep a hold count of their own beside the runtime's, so + close waits for running calls; a checked call costs four atomic operations per handle. + Dropping them needs the Rust allocation freed only once the binding's GC releases the + object, with close dropping the value instead: a moved-out state the runtime already + has. + +Measure the bench's handle call and u64 getter rows before and after, on an idle host. + +Public API: the bindings' close semantics, if close stops waiting for a running call. + +## Waiting + +- Maintainer: whether close in Kotlin, Go, Python, and Dart drops the value at once and leaves the + handle's memory to the GC, failing a running call's later uses with `moved`, so the + bindings' hold counts go. Recommended: yes; it is the runtime's existing `TAKEN` state. ## Related -- [Copy types cross by value](/quest/m1/ffi-export/copy-values.md) - landing it first leaves only thrown errors as Swift classes, so fewer signatures turn throwing +- [Copy types cross by value](/quest/m1/ffi-export/copy-values.md) - landing it first leaves only thrown errors as Swift classes - [Cheaper returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) - rewrites the same handle word, so its baselines move with this From ac9e2456e51174635444ee5ccf6fc2ce786e1415 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 21:25:12 -0800 Subject: [PATCH 04/30] wip(ffi): fixture expectations, clippy, fmt Co-Authored-By: Claude Opus 5.5 --- ffi/src/abi.rs | 5 ++++- ffi/src/cpp.rs | 14 +++++++++----- ffi/src/glue.rs | 35 +++++++++++++++++++++++++++-------- ffi/src/go.rs | 9 +++++++-- ffi/src/kotlin.rs | 24 ++++++++++++++++++++---- ffi/src/swift.rs | 16 +++++++++++----- ffi/tests/api.swift | 29 +++++++++++++++++++---------- ffi/tests/fixtures.rs | 15 ++++++++++++--- 8 files changed, 109 insertions(+), 38 deletions(-) diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index 3fc511155..ff9c35075 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -111,7 +111,10 @@ pub struct Sig { impl Sig { /// The `status` parameter it takes last, if a call can fail a check of a handle. pub fn status(&self) -> Option<&str> { - self.params.last().filter(|(_, ty)| is_status(ty)).map(|(name, _)| name.as_str()) + self.params + .last() + .filter(|(_, ty)| is_status(ty)) + .map(|(name, _)| name.as_str()) } } diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index ed96357ac..25c07962c 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -669,7 +669,7 @@ impl<'a> Header<'a> { "\tfriend bool operator==(const {name} &a, const {name} &b) {{\n\t\treturn ::{c}_eq(a.raw_.get(), b.raw_.get(){status});\n\t}}\n\tfriend bool operator!=(const {name} &a, const {name} &b) {{\n\t\treturn !(a == b);\n\t}}" )); } - self.traits(&name, &c, t, None, status, &mut body, &mut members); + self.traits(&name, &c, t, Ok(status), &mut body, &mut members); let owner = Ty::Handle { name: c.clone(), constant: false, @@ -689,19 +689,23 @@ impl<'a> Header<'a> { } /// The members a type's std traits export beyond its operators: an object's, which - /// holds a handle, or a record's, `value`, which is lowered. + /// holds a handle and passes each call `Ok` of its status argument, or a record's, of + /// type `Err`, which is lowered. fn traits( &mut self, name: &str, c: &str, t: Traits, - value: Option<&Type>, - status: &str, + of: Result<&str, &Type>, body: &mut Vec, members: &mut Vec, ) { let ns = self.ns.clone(); let d = self.d(); + let (value, status) = match of { + Ok(status) => (None, status), + Err(ty) => (Some(ty), ""), + }; let object = value.is_none(); let callable = match object { true => "RUST2_FFI_CALLABLE ", @@ -875,7 +879,7 @@ impl<'a> Header<'a> { self.lower(ty, "other"), )); } - self.traits(&name, &c, t, Some(ty), "", &mut body, &mut members); + self.traits(&name, &c, t, Err(ty), &mut body, &mut members); let owner = Ty::Struct(c.clone()); let mut taken = fields.clone(); taken.push(name.clone()); diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 25500dc70..79676276a 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -439,10 +439,12 @@ impl Gen<'_> { /// `takes`, so the move happens only once every check passed. fn reserve(&self, ty: &Type, e: &str) -> String { match ty { - Type::Object(i) => self.checked(&format!( - "unsafe {{ rt::Object::<{}>::reserve({e}, &mut takes, w) }}", - self.object_path(*i) - )) + ";", + Type::Object(i) => { + self.checked(&format!( + "unsafe {{ rt::Object::<{}>::reserve({e}, &mut takes, w) }}", + self.object_path(*i) + )) + ";" + } Type::Box(inner) => self.reserve(inner, e), Type::Option(inner) => match inner.base() { Type::Object(_) => format!("if !{e}.is_null() {{ {} }}", self.reserve(inner, e)), @@ -595,7 +597,10 @@ impl Gen<'_> { let value = match (object.copy, self.reserved.get()) { (true, _) => format!( "*{}", - self.checked(&format!("unsafe {{ rt::Object::<{path}>::{}({e}, w) }}", self.shared(*i))) + self.checked(&format!( + "unsafe {{ rt::Object::<{path}>::{}({e}, w) }}", + self.shared(*i) + )) ), (false, true) => format!("unsafe {{ rt::Object::<{path}>::taken({e}) }}"), (false, false) => self.checked(&format!("unsafe {{ rt::Object::<{path}>::take({e}, w) }}")), @@ -976,7 +981,14 @@ impl Gen<'_> { self.traits(&name, &path, object.traits, status, &borrowing); if self.abi.copies.contains(&i) { let (param, body) = match status { - Some(status) => (format!(", {status}: *mut i32"), wrap(Some(status), "*const ::std::ffi::c_void", &format!("rt::Object::new(*{copy})"))), + Some(status) => ( + format!(", {status}: *mut i32"), + wrap( + Some(status), + "*const ::std::ffi::c_void", + &format!("rt::Object::new(*{copy})"), + ), + ), None => (String::new(), format!("rt::Object::new(*{copy})")), }; self.export(format!( @@ -1123,7 +1135,10 @@ impl Gen<'_> { } }; let (ret, build) = match checks { - true => (format!("::std::result::Result<{path}, rt::Status>"), format!("Ok({build})")), + true => ( + format!("::std::result::Result<{path}, rt::Status>"), + format!("Ok({build})"), + ), false => (path.clone(), build), }; self.item(format!( @@ -1664,7 +1679,11 @@ impl Gen<'_> { args.push(made.2); } value => { - let made = (String::new(), format!("let a_self = {};", self.lift(value, "unsafe { *this }")), String::new()); + let made = ( + String::new(), + format!("let a_self = {};", self.lift(value, "unsafe { *this }")), + String::new(), + ); sort(value, &made); args.push(match receiver { Receiver::Owned => "a_self".to_string(), diff --git a/ffi/src/go.rs b/ffi/src/go.rs index bb38a9fe5..3a3fc690b 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -1445,7 +1445,9 @@ impl<'a> Gen<'a> { vec![name.clone(), "error".to_string()], format!( "{open}{}return own_{c}(r_), nil", - checked(&format!("C.{c}_clone({this})"), &format!("{c}_clone"), &|e| format!("nil, {e}")) + checked(&format!("C.{c}_clone({this})"), &format!("{c}_clone"), &|e| format!( + "nil, {e}" + )) ), )); } @@ -2394,7 +2396,10 @@ impl<'a> Gen<'a> { // A failed check writes its status, whose error the call returns. let failed = status.then(|| { args.push("&c_status_".to_string()); - let error = format!("{}(int32(c_status_), \"{c_name}\")", self.qualify(Packages::RT, "Status_")); + let error = format!( + "{}(int32(c_status_), \"{c_name}\")", + self.qualify(Packages::RT, "Status_") + ); format!("if c_status_ != 0 {{\n\t{}\n}}", fail(&error)) }); let mut body = Vec::new(); diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 5e3fdfc4e..7c8ece475 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -1029,7 +1029,11 @@ impl<'a> Gen<'a> { &format!("{bare}_{suffix}"), &[(jni_self, native_self)], ("jbyteArray", "ByteArray?"), - &checked(&format!("crate::{c}_{suffix}({rust_self})"), &format!("{c}_{suffix}"), "rt::owned(env, ret)"), + &checked( + &format!("crate::{c}_{suffix}({rust_self})"), + &format!("{c}_{suffix}"), + "rt::owned(env, ret)", + ), ); format!("\t{sig} = {open}\n\t\tNative.{bare}_{suffix}({arg})!!.decodeToString()\n\t}}\n") }; @@ -1052,7 +1056,11 @@ impl<'a> Gen<'a> { &format!("{bare}_copy"), &[("jlong", "Long")], ("jlong", "Long"), - &checked(&format!("crate::{c}_copy(p0 as usize as _)"), &format!("{c}_copy"), "ret as usize as jlong"), + &checked( + &format!("crate::{c}_copy(p0 as usize as _)"), + &format!("{c}_copy"), + "ret as usize as jlong", + ), ); } if object && t.clone { @@ -1060,7 +1068,11 @@ impl<'a> Gen<'a> { &format!("{bare}_clone"), &[("jlong", "Long")], ("jlong", "Long"), - &checked(&format!("crate::{c}_clone(p0 as usize as _)"), &format!("{c}_clone"), "ret as usize as jlong"), + &checked( + &format!("crate::{c}_clone(p0 as usize as _)"), + &format!("{c}_clone"), + "ret as usize as jlong", + ), ); body.push(format!( "\t/** A new handle to a clone. */\n\tfun clone(): {class} = handle.hold {{ h -> {class}(Native.{bare}_clone(h)) }}\n" @@ -1071,7 +1083,11 @@ impl<'a> Gen<'a> { &format!("{bare}_eq"), &[("jlong", "Long"), ("jlong", "Long")], ("jboolean", "Boolean"), - &checked(&format!("crate::{c}_eq(p0 as usize as _, p1 as usize as _)"), &format!("{c}_eq"), "ret"), + &checked( + &format!("crate::{c}_eq(p0 as usize as _, p1 as usize as _)"), + &format!("{c}_eq"), + "ret", + ), ); body.push(format!( "\toverride fun equals(other: Any?): Boolean =\n\t\tother is {class} && handle.hold {{ a -> other.handle.hold {{ b -> Native.{bare}_eq(a, b) }} }}\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): Int = 0\n" diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index f86a8cdb1..4dce27bed 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -1469,7 +1469,10 @@ impl<'a> Gen<'a> { prelude.push("Native.check()".to_string()); } if !everywhere { - prelude.push(format!("try Native.on({}, \"{c_name}\")", function.on.0 & abi.api.all().0)); + prelude.push(format!( + "try Native.on({}, \"{c_name}\")", + function.on.0 & abi.api.all().0 + )); } let mut arena = false; let mut args: Vec = Vec::new(); @@ -1589,7 +1592,10 @@ impl<'a> Gen<'a> { }; let tail; if let Some((_, poll, free)) = &export.future { - let call = format!("{c_name}({})", exprs.iter().chain(&status_arg).cloned().collect::>().join(", ")); + let call = format!( + "{c_name}({})", + exprs.iter().chain(&status_arg).cloned().collect::>().join(", ") + ); match &check { Some(check) => { inner.push(format!("let f_ = {call}")); @@ -1751,8 +1757,7 @@ impl<'a> Gen<'a> { let var = function.params.is_empty() && !throws; // A field read stays a property though a check of its handle may fail, which throws // from its getter, unless Swift cannot copy its value, whose getter cannot throw. - let getter = - throws && !function.future && function.err.is_none() && everywhere && !self.unique(&function.ok); + let getter = throws && !function.future && function.err.is_none() && everywhere && !self.unique(&function.ok); let var = match (function.access, receiver) { (Access::Field { set: false, .. }, Some(_)) => { function.params.is_empty() && (!throws || getter) && mutation.is_empty() @@ -2154,7 +2159,8 @@ mod tests { let decl = decl.trim_end_matches('{').trim(); let (name, conforms) = decl.split_once(':').unwrap_or((decl, "")); let names = std::iter::once(name).chain(conforms.split(',')); - for name in names.map(str::trim).filter(|n| !n.is_empty() && !n.starts_with('@')) { + // A qualified name, such as `Swift.Error`, cannot clash. + for name in (names.map(str::trim)).filter(|n| !n.is_empty() && !n.starts_with('@') && !n.contains('.')) { assert!(TAKEN.contains(&name), "the runtime's `{name}` is not in TAKEN"); } } diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index 1db34d4a0..d95ad0345 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -1110,24 +1110,24 @@ func owned() { #if MOVED func moved() { let c = Counter.new("c", 0) - _ = c.intoLabel(nil) - _ = c.get() + _ = try! c.intoLabel(nil) + _ = try! c.get() } #endif #if BOXED_MOVED func boxedMoved() { - let tail = Node.new(1, nil) - _ = Node.new(2, tail) - _ = tail.sum() + let tail = try! Node.new(1, nil) + _ = try! Node.new(2, tail) + _ = try! tail.sum() } #endif #if LENT_MOVED func lentMoved() { - var registry = Registry.new(Counter.new("c", 0)) - _ = registry.edit { counter in - _ = counter.intoLabel(nil) + var registry = try! Registry.new(Counter.new("c", 0)) + _ = try! registry.edit { counter in + _ = try! counter.intoLabel(nil) return false } } @@ -1141,7 +1141,7 @@ final class Kept: @unchecked Sendable { func lentKept() { let kept = Kept() - _ = peek(Tally.new()) { lent in + _ = try! peek(Tally.new()) { lent in kept.tally = lent return 0 } @@ -1156,7 +1156,16 @@ func broken(_ which: String) { var meters = Meters.new() let first = try! meters.first() try! meters.clear() - _ = try! first.count() + do { + _ = try first.count() + } catch let e as CheckError { + // Rust ran nothing, so its meters still clear. + check(e.function == "api_Meter_count", "a check names its function") + try! meters.clear() + fatalError(e.description) + } catch { + check(false, "a failed check throws a CheckError") + } // An IPv6 address where Rust takes `SocketAddrV4`. case "family": let any = SocketAddr(ip: Array(repeating: 0, count: 16), port: 1) diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 609cec4d2..48b2b9b34 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -593,7 +593,10 @@ fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { ("closure threw", "api_made: a call into Kotlin threw"), ("closure failed", "api_drive: a call into Kotlin threw"), ("reader threw", "SeekableByteChannel.read: a call into Kotlin threw"), - ("outlived", "IllegalStateException: api_Meter_count: a handle lent from another outlived it"), + ( + "outlived", + "IllegalStateException: api_Meter_count: a handle lent from another outlived it", + ), ("stray", "MalformedInputException"), ("family", "an IPv6 address has no Rust SocketAddrV4"), ] { @@ -648,7 +651,10 @@ fn api_python() { ), ("callback threw", "api::Log::log: a call into Python raised"), ("foreign cancelled", "a future Rust polls raised"), - ("outlived", "ValueError: api_Meter_count: a handle lent from another outlived it"), + ( + "outlived", + "ValueError: api_Meter_count: a handle lent from another outlived it", + ), ("family", "ValueError: an IPv6 address has no Rust SocketAddrV4"), ("kept threw", "api_Spawned_new: a call into Python raised"), ("returned", "api_made: a call into Python raised"), @@ -875,7 +881,10 @@ fn api_dart() { "reader threw", "a reader's seek: a call into Dart threw Bad state: a seek failed", ), - ("outlived", "Bad state: api_Meter_count: a handle lent from another outlived it"), + ( + "outlived", + "Bad state: api_Meter_count: a handle lent from another outlived it", + ), ("family", "an IPv6 address has no Rust SocketAddrV4"), ("stray", "a path from Rust is not UTF-8"), ("lent stray", "a path from Rust is not UTF-8"), From d99836164339bc52137fe1ba1f16f0b3135554a3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 21:38:19 -0800 Subject: [PATCH 05/30] test(ffi): Swift's within(0) waits, so Rust's first poll pends The empty async closure ran on another thread and could return before Rust's first poll, as a ready Rust future does, so the test failed about 4 runs in 10. Co-Authored-By: Claude Opus 5.5 --- ffi/tests/api.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index d95ad0345..45adb779d 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -976,16 +976,20 @@ func futures() { check(got.value == 10, "an async closure's end wakes Rust") } do { + // Its task runs on another thread, so only a closure that waits is sure to pend at + // Rust's first poll: an empty one may return first, as a ready Rust future does. + let (stream, gate) = AsyncStream.makeStream() let threw = Cell(false) q.launch { do { - try await within(0) {} + try await within(0) { for await _ in stream { break } } } catch is Api.Error { threw.value = true } } q.drain() check(threw.value, "Rust gives up on an async closure") + gate.finish() } do { let started = Cell(false) From 82c6901ad0ff422af97e51e82ef85b14beb5e287 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 00:05:42 -0800 Subject: [PATCH 06/30] wip(ffi): close takes the value through Rust's TAKEN in Kotlin and Python Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/python.rs | 71 ++++++------------- ffi/runtime/rt.kt | 70 +++++++++++++----- ffi/runtime/rt.rs | 28 ++++++++ ffi/src/abi.rs | 24 +++++++ ffi/src/c.rs | 38 ++++++++-- ffi/src/dart.rs | 2 +- ffi/src/glue.rs | 35 +++++++-- ffi/src/go.rs | 2 +- ffi/src/kotlin.rs | 161 +++++++++++++++++++++++------------------- ffi/src/python.rs | 82 ++++++++------------- ffi/tests/api.kt | 77 +++++++++++++------- ffi/tests/api.py | 2 +- ffi/tests/fixtures.rs | 14 ++-- 13 files changed, 369 insertions(+), 237 deletions(-) diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 15b8a7d96..a8f76f62a 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -361,15 +361,14 @@ impl Key { } /// What a call borrows from Python until it returns: the objects and copies whose memory -/// it reads, the handles it holds and those it moves out of, and what it -/// gives Rust, taken back if the call never happens. +/// it reads, the lent handles it holds, and what it gives Rust, taken back if the call +/// never happens. #[derive(Default)] pub struct Scope { keep: Vec, arrays: Vec>, - /// The handles held, each with whether the call moves its value out. Inline, as a - /// call holds few, so holding one allocates nothing. - held: smallvec::SmallVec<[(Obj, bool); 4]>, + /// The lent handles held. Inline, as a call holds few, so holding one allocates nothing. + held: smallvec::SmallVec<[Obj; 4]>, /// The objects Rust lends a callback, ended once it returns. lent: Vec, /// Python implementations and wakers given to Rust, taken back unless the call @@ -388,15 +387,8 @@ impl Scope { ptr } - /// Note that the call happened: what it moved out of is closed, and what it was given - /// is Rust's. + /// Note that the call happened: what it was given is Rust's. pub fn done(&mut self) { - for (held, moved) in &self.held { - if *moved { - // SAFETY: a handle this scope holds, so live. - unsafe { close(held.ptr().cast()) }; - } - } self.given(); } @@ -428,7 +420,7 @@ impl Scope { /// The handles held, which a future keeps until it ends. pub fn take_held(&mut self) -> Vec { - self.held.drain(..).map(|(held, _)| held).collect() + self.held.drain(..).collect() } } @@ -456,7 +448,7 @@ impl Drop for Scope { // SAFETY: an object [`lent`] made. unsafe { end(lent.ptr().cast()) }; } - while let Some((held, _)) = self.held.pop() { + while let Some(held) = self.held.pop() { // SAFETY: a handle this scope holds. unsafe { release(held.ptr().cast()) }; } @@ -1436,14 +1428,16 @@ pub unsafe extern "C" fn free_callback(data: *mut c_void) { } /// An object owning a handle Rust returned, or one Rust lends for a call: the instance -/// of a class of the generated Python, which subclasses `Object`. +/// of a class of the generated Python, which subclasses `Object`. An owned one is freed +/// once its last reference goes, which no call outlives: Rust's own check fails a use +/// after its value moved out. A lent one counts its calls, so its lend ends with none. #[repr(C)] pub struct Handle { ob_base: PyObject, raw: *const c_void, /// What frees the handle: none for one Rust lends, or once freed. free: Option, - /// The calls holding it, each of which it outlives. + /// The calls holding a lent one, each of which it outlives. holds: usize, closed: bool, } @@ -1490,59 +1484,40 @@ fn handle(raw: *const c_void, index: usize, free: Option R<*const c_void> { +/// The handle `obj` owns, an object of the class registered at `index`, for the call, +/// which the caller's reference keeps; a lent one held until the call returns. +pub fn hold(obj: *mut PyObject, index: usize, s: &mut Scope) -> R<*const c_void> { check(obj, index)?; let handle = obj.cast::(); // SAFETY: an object of a class that subclasses `Object`, and the GIL is held. unsafe { + if (*handle).free.is_some() { + return Ok((*handle).raw); + } if (*handle).closed { return value_error(&format!("{} is closed", type_name(ty(index)))); } (*handle).holds += 1; - s.held.push((Obj::borrowed(obj), moved)); + s.held.push(Obj::borrowed(obj)); Ok((*handle).raw) } } /// [`hold`], or a null handle for `None`. -pub fn hold_opt(obj: *mut PyObject, index: usize, s: &mut Scope, moved: bool) -> R<*const c_void> { +pub fn hold_opt(obj: *mut PyObject, index: usize, s: &mut Scope) -> R<*const c_void> { match is_none(obj) { true => Ok(null()), - false => hold(obj, index, s, moved), + false => hold(obj, index, s), } } -/// Let go of a call's hold, freeing the handle if it was closed meanwhile. +/// Let go of a call's hold of a lent handle. /// /// # Safety /// `handle` is live, and held. unsafe fn release(handle: *mut Handle) { // SAFETY: the caller's contract. - unsafe { - (*handle).holds -= 1; - if (*handle).closed && (*handle).holds == 0 { - free_now(handle); - } - } -} - -/// Close a handle: freed now, or once the last call holding it returns. -/// -/// # Safety -/// `handle` is live. -pub unsafe fn close(handle: *mut Handle) { - // SAFETY: the caller's contract. - unsafe { - if (*handle).closed { - return; - } - (*handle).closed = true; - if (*handle).holds == 0 { - free_now(handle); - } - } + unsafe { (*handle).holds -= 1 }; } /// End a lend, closing its handle, which Rust frees next: a call still holding it would @@ -1557,7 +1532,7 @@ unsafe fn end(handle: *mut Handle) { let name = type_name(Py_TYPE(handle.cast()).cast()); rt::fail(&name, "a call still holds it when its lend ends"); } - close(handle); + (*handle).closed = true; } } diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index 78e87442b..b02af0ff4 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -119,14 +119,19 @@ class Writer { private var held: java.util.ArrayList? = null - /** An object's handle, held until [release], or 0 for none. */ - fun handle(v: Handle?) { + /** The owners of the handles encoded, which the call passes its JNI function to keep. */ + var kept: java.util.ArrayList? = null + private set + + /** The handle of the object [owner], held until [release], or 0 for none. */ + fun handle(v: Handle?, owner: Any?) { if (v == null) { i64(0L) return } i64(v.hold()) (held ?: java.util.ArrayList().also { held = it }).add(v) + (kept ?: java.util.ArrayList().also { kept = it }).add(owner!!) } fun finish(): ByteArray = b.array().copyOf(b.position()) @@ -272,14 +277,23 @@ abstract class Cleanup(owner: Any?) : java.lang.ref.PhantomReference(owner, } /** - * A handle [owner] owns, or is lent for a call: each call holds it, so [close] frees it - * only once no call does, and a call after [close] throws. An owned one is closed once - * the GC collects [owner], if nothing closed it. + * A handle [owner] owns, or is lent for a call. One of a value Rust checks, which [drop] + * drops, is Rust's alone to account: [close] drops the value as Rust's own drop does, + * failing while a call borrows it, a call after it throws as Rust's check of a moved value + * fails, and the handle is freed once the GC collects [owner], which each call passes to + * its JNI function so the GC cannot collect it meanwhile. Any other counts its calls: [close] + * frees it then, throwing while a call holds it, and a call after it throws. An owned one + * is freed once the GC collects [owner], if nothing closed it. */ -class Handle(owner: Any, internal val raw: Long, private val free: Drop?, private val type: String) : - Cleanup(if (free == null) null else owner) { - // The holds, one for the owner until closed and one per call, shifted past a bit set - // once closed: one word, so a handle allocates nothing beside itself. +class Handle( + owner: Any, + internal val raw: Long, + private val free: Drop?, + private val type: String, + private val drop: Close? = null, +) : Cleanup(if (free == null) null else owner) { + // The holds of a counted one, one for the owner until closed and one per call, shifted + // past a bit set once closed: one word, so a handle allocates nothing beside itself. @Volatile @JvmField internal var state = HELD init { @@ -287,6 +301,7 @@ class Handle(owner: Any, internal val raw: Long, private val free: Drop?, privat } fun hold(): Long { + if (drop != null) return raw while (true) { val n = state kotlin.check(n and CLOSED == 0L) { "$type is closed" } @@ -294,13 +309,18 @@ class Handle(owner: Any, internal val raw: Long, private val free: Drop?, privat } } - fun release() = freed(STATE.addAndGet(this, -HELD)) + fun release() { + if (drop == null) freed(STATE.addAndGet(this, -HELD)) + } - fun close() { + /** Close it as [Handle] says, passing [owner] to keep it reachable; again, it does nothing. */ + fun close(owner: Any) { + if (drop != null) return drop.close(raw, owner) while (true) { val n = state if (n and CLOSED != 0L) return - if (STATE.compareAndSet(this, n, (n or CLOSED) - HELD)) return freed((n or CLOSED) - HELD) + kotlin.check(n == HELD) { "$type: a handle is already borrowed" } + if (STATE.compareAndSet(this, n, CLOSED)) return freed(CLOSED) } } @@ -309,17 +329,30 @@ class Handle(owner: Any, internal val raw: Long, private val free: Drop?, privat * reach the freed handle, so it throws, as Rust's own check fails once a call borrowed. */ fun end() { - close() + abandon() kotlin.check(state == CLOSED) { "$type: a call still holds it when its lend ends" } } + /** Give up the owner's hold of a counted one, freeing it once no call holds one. */ + fun abandon() { + while (true) { + val n = state + if (n and CLOSED != 0L) return + if (STATE.compareAndSet(this, n, (n or CLOSED) - HELD)) return freed((n or CLOSED) - HELD) + } + } + // Free it once no hold is left. private fun freed(n: Long) { if (n < HELD && free != null && untrack()) free.drop(raw) } - // A call still holding it frees it once it returns. - override fun collected() = close() + // Nothing reaches the owner, so no call of a checked one runs; a counted one's call + // still holding it frees it once it returns. + override fun collected() = when (drop) { + null -> abandon() + else -> if (untrack()) free!!.drop(raw) else Unit + } /** Run [block] with the raw handle, held until it returns. */ inline fun hold(block: (Long) -> R): R { @@ -345,7 +378,7 @@ class Returned(raw: Long) : Waker, AutoCloseable { override fun wake() = handle.hold { __waker_wake(it) } - override fun close() = handle.close() + override fun close() = handle.abandon() } /** Frees a raw handle: a `fun interface`, so the handle crosses unboxed. */ @@ -353,6 +386,11 @@ fun interface Drop { fun drop(raw: Long) } +/** Drops the value of a raw handle, passing its [owner] to keep it reachable meanwhile. */ +fun interface Close { + fun close(raw: Long, owner: Any) +} + /** * Rust's buffer, which [owner] holds, freed once the GC collects this. Each read passes it * to the JNI function reading the buffer, as JNI keeps an argument reachable until the call diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index c993d3194..47445a549 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -1000,6 +1000,34 @@ impl Object { unsafe { (*object.value.get()).assume_init_read() } } + /// Drop the value now, as a move out would, leaving the handle to be freed: what a + /// binding's close does, so a later use fails as moved, as Rust refuses a use after a + /// drop. It fails, changing nothing, while a call or future borrows the value, as Rust + /// refuses a drop under a borrow, and does nothing once the value is gone. A handle a + /// call returned lent from another's value only stops lending it; one lent to C for a + /// call fails, as Rust lent it. + /// + /// # Safety + /// As [`Self::borrow`]. + pub unsafe fn close(handle: *const c_void, what: &str) -> Result<(), Status> { + // SAFETY: the caller's contract. + let object = unsafe { Self::get(handle, what) }; + if !object.lent.is_null() && object.lender.is_none() { + return Err(Status::Lent); + } + match object.state.compare_exchange(0, TAKEN, Ordering::Acquire, Ordering::Relaxed) { + Ok(_) => {} + Err(TAKEN) => return Ok(()), + Err(_) => return Err(Status::Borrowed), + } + object.epoch.fetch_add(1, Ordering::Release); + if object.lent.is_null() { + // SAFETY: TAKEN excludes every other access, and `Drop` skips a taken value. + unsafe { (*object.value.get()).assume_init_drop() }; + } + Ok(()) + } + /// The value of a handle a closure C implements returned Rust, which copies it out and /// frees the handle. /// diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index ff9c35075..f9688465c 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -942,6 +942,22 @@ impl<'a> Abi<'a> { self.checked[i] } + /// Whether a record's or enum's `_eq`, `_to_string`, and `_debug` take a `status`, + /// as each borrows the handles it holds shared. + pub fn value_status(&self, ty: &Type) -> bool { + let mut handles = Vec::new(); + self.api.handles(ty, &mut handles, &mut Vec::new()); + handles.iter().any(|&i| self.checked[i]) + } + + /// Whether the std trait functions of `ty`, an object, record, or enum, take a `status`. + pub fn traits_status_of(&self, ty: &Type) -> bool { + match ty { + Type::Object(i) => self.traits_status(*i), + ty => self.value_status(ty), + } + } + /// The out pointer Rust writes back the value C passed for `&mut` as parameter `name` /// through, named apart from every parameter of `function`. pub fn written(function: &Function, name: &str) -> String { @@ -1115,6 +1131,14 @@ impl<'a> Abi<'a> { object.span, Self::made(&item, &name), ); + if self.checked[i] { + self.claim( + format!("{name}_drop"), + what.clone(), + object.span, + Self::made(&item, &name), + ); + } if object.shared { self.claim( format!("{name}_borrowed"), diff --git a/ffi/src/c.rs b/ffi/src/c.rs index 08558512a..67123ad8d 100644 --- a/ffi/src/c.rs +++ b/ffi/src/c.rs @@ -211,7 +211,13 @@ impl Header<'_> { for (i, record) in api.records.iter().enumerate() { let name = abi.record(i); self.line(format!("/* {} */", abi.api.rust(&record.path, &record.args)).trim_end()); - self.value_functions(&name, Ty::Struct(name.clone()), record.traits, true); + self.value_functions( + &name, + Ty::Struct(name.clone()), + record.traits, + true, + abi.value_status(&Type::Record(i)), + ); let owner = Ty::Struct(name.clone()); for method in &record.methods { self.function(method, Some(&owner)); @@ -225,7 +231,13 @@ impl Header<'_> { continue; } self.line(format!("/* {} */", abi.api.rust(&e.path, &e.args))); - self.value_functions(&name, ty.clone(), e.traits, !e.plain()); + self.value_functions( + &name, + ty.clone(), + e.traits, + !e.plain(), + abi.value_status(&Type::Enum(i)), + ); for method in &e.methods { self.function(method, Some(&ty)); } @@ -236,6 +248,16 @@ impl Header<'_> { let docs = format!("{}\n\n{}", abi.api.rust(&object.path, &object.args), object.docs); self.line(comment(docs.trim(), "").trim_end()); self.line(format!("void {name}_free({name} *self);")); + if abi.checked[i] { + self.line( + comment( + "Drop the value now, as a move out would, leaving the handle to free: a later call fails as moved, and a second drop does nothing. Fails while a call or future borrows the value.", + "", + ) + .trim_end(), + ); + self.line(format!("void {name}_drop({name} *self, {p}_status *status);")); + } if object.shared { self.line(format!("bool {name}_borrowed(const {name} *self);")); } @@ -401,8 +423,12 @@ impl Header<'_> { } /// The functions a value type's std traits export, and its `free` if it owns memory. - fn value_functions(&mut self, name: &str, ty: Ty, traits: Traits, free: bool) { + fn value_functions(&mut self, name: &str, ty: Ty, traits: Traits, free: bool, status: bool) { let p = &self.abi.prefix; + let status = match status { + true => format!(", {p}_status *status"), + false => String::new(), + }; let this = self.decl(&Ty::In(Box::new(ty.clone())), "self"); let a = self.decl(&Ty::In(Box::new(ty.clone())), "a"); let b = self.decl(&Ty::In(Box::new(ty.clone())), "b"); @@ -410,13 +436,13 @@ impl Header<'_> { self.line(format!("void {name}_free({name} *self);")); } if traits.eq { - self.line(format!("bool {name}_eq({a}, {b});")); + self.line(format!("bool {name}_eq({a}, {b}{status});")); } if traits.display { - self.line(format!("{p}_buf {name}_to_string({this});")); + self.line(format!("{p}_buf {name}_to_string({this}{status});")); } if traits.debug { - self.line(format!("{p}_buf {name}_debug({this});")); + self.line(format!("{p}_buf {name}_debug({this}{status});")); } if traits.default { self.line(format!("{} {name}_default(void);", self.decl(&ty, ""))); diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 03bd148b3..57821006b 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -1342,7 +1342,7 @@ impl<'a> Gen<'a> { false => Ty::In(Box::new(c_ty.clone())), }; // An object whose run-time checks may fail passes each its status, which throws. - let status = matches!(ty, Type::Object(i) if self.abi.traits_status(*i)); + let status = self.abi.traits_status_of(ty); let params = |args: Vec| -> Vec { let mut args = args; if status { diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 79676276a..c5de42acc 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -420,9 +420,7 @@ impl Gen<'_> { /// Whether lifting a value of `ty` checks a handle that can fail. fn checks(&self, ty: &Type) -> bool { - let mut handles = Vec::new(); - self.abi.api.handles(ty, &mut handles, &mut Vec::new()); - handles.iter().any(|&i| self.abi.checked[i]) + self.abi.value_status(ty) } /// Whether `ty` moves an object's value out of its handle, as passed by value. @@ -962,6 +960,11 @@ impl Gen<'_> { self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_free(this: *const ::std::ffi::c_void) {{\n\tunsafe {{ rt::Object::<{path}>::free(this) }}\n}}\n" )); + if self.abi.checked[i] { + self.export(format!( + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_drop(this: *const ::std::ffi::c_void, status: *mut i32) {{\n\tlet w = \"{name}_drop\";\n\tif let Err(s) = unsafe {{ rt::Object::<{path}>::close(this, w) }} {{\n\t\tunsafe {{ rt::failed::<()>(status, s, w) }}\n\t}}\n}}\n" + )); + } if object.shared { self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_borrowed(this: *const ::std::ffi::c_void) -> bool {{\n\tunsafe {{ rt::Object::<{path}>::borrowed(this, \"{name}_borrowed\") }}\n}}\n" @@ -1066,23 +1069,43 @@ impl Gen<'_> { /// The functions every value type has: its std traits, read through a pointer, and /// `free`. fn value_traits(&mut self, name: &str, path: &str, ty: &Type, traits: Traits) { + // One holding a handle a check of can fail returns the failure through a `status`. + let status = self.checks(ty).then_some("status"); + self.checking.set(status.is_some()); let lift = self.lift(ty, "unsafe { *this }"); let lift_a = self.lift(ty, "unsafe { *a }"); let lift_b = self.lift(ty, "unsafe { *b }"); + self.checking.set(false); + let param = status.map(|s| format!(", {s}: *mut i32")).unwrap_or_default(); let c = rs(&self.abi.ty(ty)); if traits.eq { + let body = wrap( + status, + "bool", + &format!("<{path} as ::std::cmp::PartialEq>::eq(&{lift_a}, &{lift_b})"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: *const {c}, b: *const {c}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t<{path} as ::std::cmp::PartialEq>::eq(&{lift_a}, &{lift_b})\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: *const {c}, b: *const {c}{param}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t{body}\n}}\n" )); } if traits.display { + let body = wrap( + status, + "rt::Buf", + &format!("rt::Buf::string(::std::string::ToString::to_string(&{lift}))"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: *const {c}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\trt::Buf::string(::std::string::ToString::to_string(&{lift}))\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: *const {c}{param}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\t{body}\n}}\n" )); } if traits.debug { + let body = wrap( + status, + "rt::Buf", + &format!("rt::Buf::string(format!(\"{{:?}}\", &{lift}))"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: *const {c}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\trt::Buf::string(format!(\"{{:?}}\", &{lift}))\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: *const {c}{param}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\t{body}\n}}\n" )); } if traits.default { diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 3a3fc690b..b417f93f2 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -1393,7 +1393,7 @@ impl<'a> Gen<'a> { }; // A check each trait's borrow can fail: `String` and `GoString` return no error, so // theirs aborts, as do C's. - let status = matches!(ty, Type::Object(i) if self.abi.traits_status(*i)); + let status = self.abi.traits_status_of(ty); let nil = match status { true => ", nil", false => "", diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 7c8ece475..9db5b0aea 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -25,7 +25,7 @@ use std::hash::Hasher; use rustc_data_structures::stable_hash::StableHasher; use crate::abi::{self, Abi, Ty, pascal}; -use crate::ir::{Access, Family, Function, Int, Literal, Mutability, On, Param, Receiver, Type}; +use crate::ir::{Access, Family, Function, Int, Literal, Mutability, On, Param, Type}; const RUNTIME: &str = include_str!("../runtime/rt.kt"); pub const GLUE_RUNTIME: &str = include_str!("../runtime/jni.rs"); @@ -121,7 +121,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result Gen<'a> { format!("{e}.let {{ {l} -> {w}.i32({l}.size); for ({v} in {l}) {{ {item} }} }}") } Type::Option(inner) => match inner.base() { - Type::Object(_) => format!("{w}.handle({e}?.handle)"), + Type::Object(_) => format!("{w}.handle({e}?.handle, {e})"), base if Abi::is_buf(base) => self.write_value(base, w, e), // None is a length of -1. Type::List(..) => { @@ -573,7 +573,7 @@ impl<'a> Gen<'a> { true => format!("{w}.i32({e}.value)"), false => format!("w_{}({w}, {e})", abi.enumeration(*i)), }, - Type::Object(_) => format!("{w}.handle({e}.handle)"), + Type::Object(_) => format!("{w}.handle({e}.handle, {e})"), Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => format!("w_{}({w}, {e})", abi.range(ty)), Type::Tuple(elements) => { let t = self.fresh("t"); @@ -696,21 +696,39 @@ impl<'a> Gen<'a> { /// The Kotlin expression passing `e` of type `ty` back to Rust as the C type `c`, as a /// callback or closure returns it: a scalar, a string's bytes, or a new handle to a /// copy of a `Copy` object, made without its `Clone`, or 0 for none, which Rust frees. - fn lower_ret(&self, c: &Ty, ty: &Type, e: &str) -> String { + fn lower_ret(&mut self, c: &Ty, ty: &Type, e: &str) -> String { match c { Ty::Int(int) => to_signed(*int, e), Ty::Buf => format!("{e}.encodeToByteArray()"), Ty::Handle { name, .. } => { let clone = format!("Native.{}_copy", self.bare(name)); + let r = self.fresh("r"); + let kept = match self.abi.value_status(ty) { + true => format!(", {r}"), + false => String::new(), + }; match ty { - Type::Option(_) => format!("{e}?.handle?.hold {{ h -> {clone}(h) }} ?: 0L"), - _ => format!("{e}.handle.hold {{ h -> {clone}(h) }}"), + Type::Option(_) => { + format!("{e}?.let {{ {r} -> {r}.handle.hold {{ h -> {clone}(h{kept}) }} }} ?: 0L") + } + _ => format!("{e}.let {{ {r} -> {r}.handle.hold {{ h -> {clone}(h{kept}) }} }}"), } } _ => e.to_string(), } } + /// Pass `e` to the native method as one more parameter the glue ignores, which JNI + /// keeps reachable until the call returns: the owner of a checked object's handle, or + /// a list of them, whose collection would free the handle while Rust reads it. A fence + /// on every JVM and Android level, where `Reference.reachabilityFence` needs API 28. + fn keep(&mut self, arg: &mut Arg, e: &str, n: &mut usize) { + *n += 1; + arg.jni.push("jobject"); + arg.native.push("Any?".to_string()); + arg.exprs.push(e.to_string()); + } + /// How Kotlin passes the value `e` of IR type `ty` as the C parameter `c`, named /// `name` in C, and how the glue takes it. `callback` names the parameter's closure /// trampoline, if one. @@ -750,15 +768,13 @@ impl<'a> Gen<'a> { true => format!("Native.hold({e}?.handle) {{ {h} ->"), false => format!("{e}.handle.hold {{ {h} ->"), }); - // Taken by value, the value moves out, leaving the handle only to free. - if moved(abi, ty) { - arg.after.push(match option { - true => format!("{e}?.close()"), - false => format!("{e}.close()"), - }); - } arg.exprs.push(h); arg.rust_args.push(format!("{p} as usize as _")); + // Taken by value, the value moves out, which Rust's check accounts. One of a + // checked object passes its owner too, kept reachable through the call. + if abi.value_status(ty) { + self.keep(&mut arg, e, n); + } } Ty::Buf => { let option = matches!(ty.base(), Type::Option(_)); @@ -778,11 +794,6 @@ impl<'a> Gen<'a> { } Ty::U128 | Ty::Duration | Ty::SocketAddr | Ty::Struct(_) | Ty::In(_) => { let w = self.fresh("w"); - // Taken by value, each object in a list moves out, as one passed alone does. - if moved(abi, ty) { - let close = self.close(ty, e); - arg.after.push(close); - } arg.open = Some(format!("Native.Writer().use {{ {w} ->")); let value = self.write_value(ty, &w, e); arg.inside.push(value); @@ -794,6 +805,10 @@ impl<'a> Gen<'a> { Ty::In(_) => format!("&a{p}"), _ => format!("a{p}"), }); + // The owners of the checked objects encoded, kept reachable through the call. + if abi.value_status(ty) { + self.keep(&mut arg, &format!("{w}.kept"), n); + } } Ty::Fn(..) => { let trampoline = trampoline.expect("a closure has its trampoline"); @@ -1016,7 +1031,7 @@ impl<'a> Gen<'a> { } }; // Each borrows its handles shared, throwing a failed check. - let status = matches!(ty, Type::Object(i) if self.abi.traits_status(*i)); + let status = self.abi.traits_status_of(ty); let checked = |call: &str, name: &str, then: &str| match status { true => format!( "let mut status = 0;\n\t\tlet ret = {}, &mut status);\n\t\tif status != 0 {{\n\t\t\treturn rt::raise(env, status, \"{name}\");\n\t\t}}\n\t\t{then}", @@ -1024,10 +1039,18 @@ impl<'a> Gen<'a> { ), false => format!("let ret = {call};\n\t\t{then}"), }; + // What keeps the owners of its checked handles reachable through the call. + let (keep, kept): (&[(&str, &str)], &str) = match (status, object) { + (false, _) => (&[], ""), + (true, true) => (&[("jobject", "Any?")], ", this"), + (true, false) => (&[("jobject", "Any?")], ", w.kept"), + }; + let mut unary_params = vec![(jni_self, native_self)]; + unary_params.extend_from_slice(keep); let unary = |g: &mut Self, suffix: &str, sig: &str| { g.native( &format!("{bare}_{suffix}"), - &[(jni_self, native_self)], + &unary_params, ("jbyteArray", "ByteArray?"), &checked( &format!("crate::{c}_{suffix}({rust_self})"), @@ -1035,7 +1058,7 @@ impl<'a> Gen<'a> { "rt::owned(env, ret)", ), ); - format!("\t{sig} = {open}\n\t\tNative.{bare}_{suffix}({arg})!!.decodeToString()\n\t}}\n") + format!("\t{sig} = {open}\n\t\tNative.{bare}_{suffix}({arg}{kept})!!.decodeToString()\n\t}}\n") }; if t.display { // Final, so a data class variant of a sealed class keeps it rather than making @@ -1054,7 +1077,7 @@ impl<'a> Gen<'a> { { self.native( &format!("{bare}_copy"), - &[("jlong", "Long")], + &[&[("jlong", "Long")], keep].concat(), ("jlong", "Long"), &checked( &format!("crate::{c}_copy(p0 as usize as _)"), @@ -1066,7 +1089,7 @@ impl<'a> Gen<'a> { if object && t.clone { self.native( &format!("{bare}_clone"), - &[("jlong", "Long")], + &[&[("jlong", "Long")], keep].concat(), ("jlong", "Long"), &checked( &format!("crate::{c}_clone(p0 as usize as _)"), @@ -1075,22 +1098,35 @@ impl<'a> Gen<'a> { ), ); body.push(format!( - "\t/** A new handle to a clone. */\n\tfun clone(): {class} = handle.hold {{ h -> {class}(Native.{bare}_clone(h)) }}\n" + "\t/** A new handle to a clone. */\n\tfun clone(): {class} = handle.hold {{ h -> {class}(Native.{bare}_clone(h{kept})) }}\n" )); } if object && t.eq { + let (params, b, other) = match status { + true => ( + vec![ + ("jlong", "Long"), + ("jobject", "Any?"), + ("jlong", "Long"), + ("jobject", "Any?"), + ], + "p2", + ", other", + ), + false => (vec![("jlong", "Long"), ("jlong", "Long")], "p1", ""), + }; self.native( &format!("{bare}_eq"), - &[("jlong", "Long"), ("jlong", "Long")], + ¶ms, ("jboolean", "Boolean"), &checked( - &format!("crate::{c}_eq(p0 as usize as _, p1 as usize as _)"), + &format!("crate::{c}_eq(p0 as usize as _, {b} as usize as _)"), &format!("{c}_eq"), "ret", ), ); body.push(format!( - "\toverride fun equals(other: Any?): Boolean =\n\t\tother is {class} && handle.hold {{ a -> other.handle.hold {{ b -> Native.{bare}_eq(a, b) }} }}\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): Int = 0\n" + "\toverride fun equals(other: Any?): Boolean =\n\t\tother is {class} && handle.hold {{ a -> other.handle.hold {{ b -> Native.{bare}_eq(a{kept}, b{other}) }} }}\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): Int = 0\n" )); } if t.default { @@ -1403,9 +1439,24 @@ impl<'a> Gen<'a> { ("()", "Unit"), &format!("crate::{c}_free(p0 as usize as _)"), ); - let mut body = vec![format!( - "\tinternal val handle = Native.Handle(this, raw, if (lent) null else Native.Drop {{ Native.{bare}_free(it) }}, \"{name}\")\n\n\t/** Free the handle once no call holds it: a call after this throws. */\n\toverride fun close() = handle.close()\n" - )]; + let mut body = vec![match abi.checked[i] { + true => { + self.native( + &format!("{bare}_drop"), + &[("jlong", "Long"), ("jobject", "Any?")], + ("()", "Unit"), + &format!( + "let mut status = 0;\n\t\tcrate::{c}_drop(p0 as usize as _, &mut status);\n\t\tif status != 0 {{\n\t\t\treturn rt::raise(env, status, \"{c}_drop\");\n\t\t}}" + ), + ); + format!( + "\tinternal val handle = Native.Handle(this, raw, if (lent) null else Native.Drop {{ Native.{bare}_free(it) }}, \"{name}\", if (lent) null else Native.Close {{ h, o -> Native.{bare}_drop(h, o) }})\n\n\t/** Drop the value now, as Rust's drop does: a call after this throws, and one still running makes this throw. */\n\toverride fun close() = handle.close(this)\n" + ) + } + false => format!( + "\tinternal val handle = Native.Handle(this, raw, if (lent) null else Native.Drop {{ Native.{bare}_free(it) }}, \"{name}\")\n\n\t/** Free the handle now: a call after this throws, and one still running makes this throw. */\n\toverride fun close() = handle.close(this)\n" + ), + }]; let mut statics = Vec::new(); self.traits(&ty, &c, object.traits, &mut body, &mut statics); let owner = Ty::Handle { @@ -1787,7 +1838,7 @@ impl<'a> Gen<'a> { let mut c_params = export.call.params.iter(); self.suspends = export.future.is_some(); let receiver = function.receiver.filter(|_| owner.is_some()); - if let Some(receiver) = receiver { + if receiver.is_some() { let (_, c) = c_params.next().expect("a method has its receiver"); let ty = owner_ty.expect("a method has its type").clone(); let mut arg = match c { @@ -1802,15 +1853,15 @@ impl<'a> Gen<'a> { arg.open = Some(format!("this.handle.hold {{ {h} ->")); arg.exprs.push(h); arg.rust_args.push(format!("{p} as usize as _")); - let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); - if receiver == Receiver::Owned && !copy { - arg.after.push("this.close()".to_string()); + // A `self` method moves the value out, which Rust's check accounts. + if abi.value_status(&ty) { + self.keep(&mut arg, "this", &mut n); } arg } _ => self.arg(&ty, c, "this", &mut n, None), }; - arg.native.truncate(1); + arg.native.truncate(arg.jni.len()); args.push(arg); } let mut kt_params = Vec::new(); @@ -2129,33 +2180,6 @@ impl<'a> Gen<'a> { Some(indent_by(&code, indent_level)) } - /// The Kotlin statement closing each object the value `e` of type `ty` holds, once a - /// call moved them out. - fn close(&mut self, ty: &Type, e: &str) -> String { - match ty { - Type::Option(inner) => { - let v = self.fresh("v"); - let close = self.close(inner, &v); - format!("{e}?.let {{ {v} -> {close} }}") - } - Type::Box(inner) => self.close(inner, e), - Type::Tuple(elements) => { - let t = self.fresh("t"); - let closes: Vec = (elements.iter().enumerate()) - .filter(|(_, ty)| moved(self.abi, ty)) - .map(|(k, ty)| self.close(ty, &format!("{t}.{}", ordinal(k)))) - .collect(); - format!("{e}.let {{ {t} -> {} }}", closes.join("; ")) - } - Type::List(_, inner) => { - let v = self.fresh("v"); - let close = self.close(inner, &v); - format!("{e}.forEach {{ {v} -> {close} }}") - } - _ => format!("{e}.close()"), - } - } - /// The glue's out values for `outs`, of a function returning `ok` or `err`, the /// pointers the C call takes after its parameters, and the statements writing them, /// `ok` saying whether it returned ok. @@ -2268,17 +2292,6 @@ fn bytes(ty: &Type) -> bool { } } -/// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, -/// unless `Copy`, and never through a reference. -fn moved(abi: &Abi<'_>, ty: &Type) -> bool { - match ty { - Type::Object(i) => !abi.api.objects[*i].copy, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), - Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), - _ => false, - } -} - /// Whether `ty` crosses as a buffer of UTF-8 text, or none. fn string(ty: &Type) -> bool { decode(ty).is_some() diff --git a/ffi/src/python.rs b/ffi/src/python.rs index e2f4cce42..8da3e74ee 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -477,9 +477,8 @@ impl<'a> Gen<'a> { } /// The glue's expression, an `R`, reading the Python object `o` as the C value of - /// `ty`, with what it borrows held in the scope `s`; `m` is the glue's `bool`, whether - /// the position moves an object's value out, closing it. - fn lift(&mut self, ty: &Type, o: &str, m: &str) -> String { + /// `ty`, with what it borrows held in the scope `s`. + fn lift(&mut self, ty: &Type, o: &str) -> String { let abi = self.abi; match ty { Type::Const(_) => unreachable!("a const argument is no value"), @@ -498,27 +497,21 @@ impl<'a> Gen<'a> { Type::U128 => format!("rt::u128_in({o})"), Type::Duration => format!("rt::duration_in({o})"), Type::SocketAddr(family) => format!("rt::addr_in({o}, {})", family_of(*family)), - Type::List(..) => format!("r_{}({o}, s, {m})", self.c_struct(ty)), + Type::List(..) => format!("r_{}({o}, s)", self.c_struct(ty)), Type::Option(inner) => match inner.base() { - Type::Object(i) => { - let m = match abi.api.objects[*i].copy || matches!(**inner, Type::Ref(..)) { - true => "false", - false => m, - }; - format!("rt::hold_opt({o}, {}, s, {m})", self.t(&abi.object(*i))) - } + Type::Object(i) => format!("rt::hold_opt({o}, {}, s)", self.t(&abi.object(*i))), base if Abi::is_buf(base) => { - let some = self.lift(inner, o, m); + let some = self.lift(inner, o); format!("(if rt::is_none({o}) {{ Ok(crate::rt::Buf::NONE) }} else {{ {some} }})") } Type::List(..) => { - let some = self.lift(inner, o, m); + let some = self.lift(inner, o); format!("(if rt::is_none({o}) {{ Ok(crate::rt::List::NONE) }} else {{ {some} }})") } - _ => format!("r_{}({o}, s, {m})", self.c_struct(ty)), + _ => format!("r_{}({o}, s)", self.c_struct(ty)), }, - Type::Box(inner) => self.lift(inner, o, m), - Type::Ref(_, inner) => self.lift(inner, o, "false"), + Type::Box(inner) => self.lift(inner, o), + Type::Ref(_, inner) => self.lift(inner, o), Type::Record(_) => format!("r_{}({o}, s)", self.c_struct(ty)), Type::Enum(i) if abi.api.enums[*i].variants.iter().any(|v| !abi.api.everywhere(v.on)) => { // Checks the member a value is against the library's target. @@ -532,13 +525,7 @@ impl<'a> Gen<'a> { true => format!("rt::member_in({o}, {})", self.t(&abi.enumeration(*i))), false => format!("r_{}({o}, s)", abi.enumeration(*i)), }, - Type::Object(i) => { - let m = match abi.api.objects[*i].copy { - true => "false", - false => m, - }; - format!("rt::hold({o}, {}, s, {m})", self.t(&abi.object(*i))) - } + Type::Object(i) => format!("rt::hold({o}, {}, s)", self.t(&abi.object(*i))), Type::Callback(i, _) => { let c = abi.callback(*i); format!( @@ -551,7 +538,7 @@ impl<'a> Gen<'a> { Type::Future => format!("rt::future_in({o}, s)"), Type::Range(int) => format!("rt::range_in::<{}>({o})", int.rust()), Type::Bound(_) | Type::Bounds(_) => format!("r_{}({o}, s)", abi.range(ty)), - Type::Tuple(_) => format!("r_{}({o}, s, {m})", self.c_struct(ty)), + Type::Tuple(_) => format!("r_{}({o}, s)", self.c_struct(ty)), Type::Unit | Type::Closure(..) => unreachable!("{ty:?} is no value Python passes"), } } @@ -758,11 +745,11 @@ impl<'a> Gen<'a> { /// The glue of an option struct. fn option(&mut self, c: &str, inner: &Type) { - let read = self.lift(inner, "o", "m"); + let read = self.lift(inner, "o"); let write = self.lower(inner, "v.value"); let _ = write!( self.glue, - "fn r_{c}(o: *mut PyObject, s: &mut rt::Scope, m: bool) -> R {{\n\tmatch rt::is_none(o) {{\n\t\ttrue => Ok(crate::{c} {{ some: false, value: unsafe {{ std::mem::zeroed() }} }}),\n\t\tfalse => Ok(crate::{c} {{ some: true, value: {}? }}),\n\t}}\n}}\n\nfn w_{c}(v: crate::{c}) -> R {{\n\tmatch v.some {{\n\t\ttrue => {write},\n\t\tfalse => rt::none(),\n\t}}\n}}\n\n", + "fn r_{c}(o: *mut PyObject, s: &mut rt::Scope) -> R {{\n\tmatch rt::is_none(o) {{\n\t\ttrue => Ok(crate::{c} {{ some: false, value: unsafe {{ std::mem::zeroed() }} }}),\n\t\tfalse => Ok(crate::{c} {{ some: true, value: {}? }}),\n\t}}\n}}\n\nfn w_{c}(v: crate::{c}) -> R {{\n\tmatch v.some {{\n\t\ttrue => {write},\n\t\tfalse => rt::none(),\n\t}}\n}}\n\n", read ); } @@ -774,14 +761,14 @@ impl<'a> Gen<'a> { let mut fields = Vec::new(); let mut writes = Vec::new(); for (k, ty) in elements.iter().enumerate() { - let lift = self.lift(ty, &format!("x{k}"), "m"); + let lift = self.lift(ty, &format!("x{k}")); reads.push(format!("let x{k} = rt::tuple_item(o, {k}, {n})?;")); fields.push(format!("_{k}: {lift}?")); writes.push(self.lower(ty, &format!("v._{k}"))); } let _ = write!( self.glue, - "fn r_{c}(o: *mut PyObject, s: &mut rt::Scope, m: bool) -> R {{\n\t{}\n\tOk(crate::{c} {{ {} }})\n}}\n\nfn w_{c}(v: crate::{c}) -> R {{\n\trt::tuple(vec![{}])\n}}\n\n", + "fn r_{c}(o: *mut PyObject, s: &mut rt::Scope) -> R {{\n\t{}\n\tOk(crate::{c} {{ {} }})\n}}\n\nfn w_{c}(v: crate::{c}) -> R {{\n\trt::tuple(vec![{}])\n}}\n\n", reads.join("\n\t"), fields.join(", "), writes.join(", ") @@ -790,11 +777,11 @@ impl<'a> Gen<'a> { /// The glue of a `Vec`'s array. fn list(&mut self, c: &str, elem: &Type) { - let read = self.lift(elem, "o", "m"); + let read = self.lift(elem, "o"); let write = self.lower(elem, "e"); let _ = write!( self.glue, - "fn r_{c}(o: *mut PyObject, s: &mut rt::Scope, m: bool) -> R {{\n\trt::list_in(o, s, |o, s| {read})\n}}\n\nfn w_{c}(v: crate::{c}) -> R {{\n\trt::list_out(v, |e| {write})\n}}\n\n" + "fn r_{c}(o: *mut PyObject, s: &mut rt::Scope) -> R {{\n\trt::list_in(o, s, |o, s| {read})\n}}\n\nfn w_{c}(v: crate::{c}) -> R {{\n\trt::list_out(v, |e| {write})\n}}\n\n" ); } @@ -807,19 +794,15 @@ impl<'a> Gen<'a> { let object = matches!(ty, Type::Object(_)); // The receiver as the glue reads it: a handle, held for the call, or a value. let this = match object { - true => self.lift( - &Type::Ref(crate::ir::Mutability::Not, Box::new(ty.clone())), - "a[0]", - "false", - ), - false => self.lift(ty, "a[0]", "false"), + true => self.lift(&Type::Ref(crate::ir::Mutability::Not, Box::new(ty.clone())), "a[0]"), + false => self.lift(ty, "a[0]"), }; let pass = match object { true => "this", false => "&this", }; // An object whose run-time checks may fail passes each its status, which raises. - let checked = matches!(ty, Type::Object(i) if self.abi.traits_status(*i)); + let checked = self.abi.traits_status_of(ty); let (status, arg) = match checked { true => ("let mut status = 0;\n\t\t", ", &mut status"), false => ("", ""), @@ -863,11 +846,7 @@ impl<'a> Gen<'a> { )); } if object && t.eq { - let other = self.lift( - &Type::Ref(crate::ir::Mutability::Not, Box::new(ty.clone())), - "a[1]", - "false", - ); + let other = self.lift(&Type::Ref(crate::ir::Mutability::Not, Box::new(ty.clone())), "a[1]"); self.native( &format!("{bare}_eq"), 2, @@ -999,7 +978,7 @@ impl<'a> Gen<'a> { _ => format!("rt::attr(o, {key}, s)?"), }; reads.push(format!("let x{n} = {read};")); - fields.push(format!("f{n}: {}?", self.lift(&field.ty, &format!("x{n}"), "false"))); + fields.push(format!("f{n}: {}?", self.lift(&field.ty, &format!("x{n}")))); writes.push(format!("({key}, {})", self.lower(&field.ty, &format!("v.f{n}")))); } if record.fields.is_empty() { @@ -1128,7 +1107,7 @@ impl<'a> Gen<'a> { for (k, f) in variant.fields.iter().enumerate() { let key = self.key(&field_name(&f.name)); binds.push(format!("let x{k} = rt::attr(o, {key}, s)?;")); - fields.push(format!("f{k}: {}?", self.lift(&f.ty, &format!("x{k}"), "false"))); + fields.push(format!("f{k}: {}?", self.lift(&f.ty, &format!("x{k}")))); lowers.push(format!("({key}, {})", self.lower(&f.ty, &format!("p.f{k}")))); } reads.push(format!( @@ -1326,7 +1305,7 @@ impl<'a> Gen<'a> { Type::String => format!("rt::str_given({o})"), // A closure's return has no caller to raise to, so a failed check aborts. Type::Object(i) => format!( - "rt::hold({o}, {}, s, false).map(|h| crate::{}_copy(h{}))", + "rt::hold({o}, {}, s).map(|h| crate::{}_copy(h{}))", self.t(&abi.object(*i)), abi.object(*i), match abi.traits_status(*i) { @@ -1338,7 +1317,7 @@ impl<'a> Gen<'a> { let some = self.given(inner, o); format!("(if rt::is_none({o}) {{ Ok(std::ptr::null()) }} else {{ {some} }})") } - _ => self.lift(ty, o, "false"), + _ => self.lift(ty, o), } } @@ -1523,11 +1502,11 @@ impl<'a> Gen<'a> { Receiver::Ref => Type::Ref(crate::ir::Mutability::Not, Box::new(ty)), Receiver::Mut => Type::Ref(crate::ir::Mutability::Mut, Box::new(ty)), }; - stmts.push(format!("let p{n} = {}?;", self.lift(&ty, &format!("a[{n}]"), "true"))); + stmts.push(format!("let p{n} = {}?;", self.lift(&ty, &format!("a[{n}]")))); c_args.push(format!("p{n}")); } _ => { - stmts.push(format!("let p{n} = {}?;", self.lift(&ty, &format!("a[{n}]"), "false"))); + stmts.push(format!("let p{n} = {}?;", self.lift(&ty, &format!("a[{n}]")))); c_args.push(format!("&p{n}")); } } @@ -1550,7 +1529,7 @@ impl<'a> Gen<'a> { (Type::Ref(_, inner), _) if param.ty.written() => { stmts.push(format!( "let h{n} = rt::mut_value(a[{n}], s)?;\n\t\tlet p{n} = {}?;", - self.lift(inner, &format!("h{n}"), "true") + self.lift(inner, &format!("h{n}")) )); c_args.push(format!("p{n}")); written.push((n, (**inner).clone())); @@ -1576,10 +1555,7 @@ impl<'a> Gen<'a> { } } _ => { - stmts.push(format!( - "let p{n} = {}?;", - self.lift(¶m.ty, &format!("a[{n}]"), "true") - )); + stmts.push(format!("let p{n} = {}?;", self.lift(¶m.ty, &format!("a[{n}]")))); c_args.push(format!("p{n}")); } } diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index df02a065f..f264c810e 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -750,17 +750,22 @@ fun futures() { check(payload.text() == "kept", "a cancelled receive loses nothing") } - // A handle closed while a call awaits on it is freed once the call ends. + // Closing a handle a call awaiting on it borrows throws, as Rust refuses a drop under a + // borrow, and changes nothing: the call ends as it would, and then the close passes. run { val other = Channel.new() var got: String? = null scope.launch { got = other.recv().text() } q.drain() val sender = other.clone() - other.close() + val refused = runCatching { other.close() }.exceptionOrNull() + check(refused is IllegalStateException && refused.message == "api_Channel_drop: a handle is already borrowed", "a close under an awaiting call throws: $refused") sender.send("held".encodeToByteArray()) q.drain() - check(got == "held", "a closed handle stays held by the call awaiting on it") + check(got == "held", "the awaiting call ends as it would") + other.close() + val moved = runCatching { other.clear() }.exceptionOrNull() + check(moved is IllegalStateException && moved.message == "api_Channel_clear: a handle's value was moved out", "a call after close throws: $moved") sender.close() } @@ -978,7 +983,7 @@ fun owned() { check(dropped() == before + 1u, "closing frees at once") token.close() check(dropped() == before + 1u, "closing again frees nothing") - check(runCatching { token.idGet }.exceptionOrNull()?.message == "Token is closed", "a call after close throws") + check(runCatching { token.idGet }.exceptionOrNull()?.message == "api_Token_id_get: a handle's value was moved out", "a call after close throws") // The file serves wasm32 too: what only wasm32 has throws on the host's library. check(pointerBits(platform()) == 64u && pointerBits(Platform.Native(64u)) == 64u, "the host is native") @@ -1059,30 +1064,13 @@ fun sliced(id: UInt): Bytes = tokenBytes(id, 1025u).slice(1015) // Frees racing calls: a close racing calls on other threads, and the GC freeing what a // call reads while it runs. fun raced() { - // Each call either runs, holding the handle so Rust's value outlives it, or throws. + // A latch's calls only borrow it, so Kotlin counts them; a token's Rust checks. var before = dropped() - val token = Token.new(5u) - val ran = java.util.concurrent.atomic.AtomicInteger() - val failed = java.util.concurrent.atomic.AtomicInteger() - val callers = List(8) { - thread { - while (true) { - val id = try { - token.idGet - } catch (e: IllegalStateException) { - check(e.message == "Token is closed", "a call racing close throws") - failed.incrementAndGet() - return@thread - } - check(id == 5u, "a call racing close reads the live value") - ran.incrementAndGet() - } - } - } - while (ran.get() < 100) Thread.yield() - token.close() - callers.forEach { it.join() } - check(failed.get() == 8 && dropped() == before + 1u, "close racing calls drops Rust's value once") + val latch = Latch.default() + latch.set(5u) + race(latch, { it.get() == 5u }, "Latch is closed", "Latch: a handle is already borrowed") + race(Token.new(5u), { it.idGet == 5u }, "api_Token_id_get: a handle's value was moved out", "api_Token_drop: a handle is already borrowed") + check(dropped() == before + 1u, "close racing calls drops Rust's value once") // Calls on values nothing else reaches, while another thread collects: each reads what // it was given, never a buffer freed under it and reused by the next, which holds @@ -1109,6 +1097,41 @@ fun raced() { collected(before + 3u * rounds, "the GC frees what each call raced") } +// Close [made] while 8 threads call [call] on it: each call runs on the live value or, once +// it is closed, throws [closed]; a close a running call beats throws [borrowed], as Rust +// refuses a drop under a borrow, and changes nothing, so the next tries again. +fun race(made: T, call: (T) -> Boolean, closed: String, borrowed: String) { + val ran = java.util.concurrent.atomic.AtomicInteger() + val failed = java.util.concurrent.atomic.AtomicInteger() + val callers = List(8) { + thread { + while (true) { + val live = try { + call(made) + } catch (e: IllegalStateException) { + check(e.message == closed, "a call racing close throws: ${e.message}") + failed.incrementAndGet() + return@thread + } + check(live, "a call racing close reads the live value") + ran.incrementAndGet() + } + } + } + while (ran.get() < 100) Thread.yield() + while (true) { + try { + made.close() + break + } catch (e: IllegalStateException) { + check(e.message == borrowed, "a close racing a call throws: ${e.message}") + } + } + callers.forEach { it.join() } + check(failed.get() == 8, "every call after close throws") + made.close() +} + // At most 64 MiB of Rust's bytes are lent at once, freed or not, since the GC sees none of // them: past that, bytes cross copied, freed at the call, until the GC frees what was lent. // First, so no bytes an earlier step lent count against it. diff --git a/ffi/tests/api.py b/ffi/tests/api.py index caf7fe791..85f161c95 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -556,7 +556,7 @@ async def polls(): print(f"chunks {[list(data(chunk)) for chunk in api.chunks(b'abcde', 2)]}") counters = api.counters(["x", "y"]) print("counters " + ", ".join(f"{c.name()} {c.get()}" for c in counters)) - # Each counter moves in, so Python closes it. + # Each counter moves in, so a later call raises. print(f"worth {api.worth([counters[0]], [counters[1]])} {api.worth(None, [])}") for counter in counters: try: diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 48b2b9b34..4de94eead 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -582,8 +582,14 @@ fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { run(&mut vm(&["futures"])); run(&mut vm(&["owned"])); for (case, message) in [ - ("moved", "Counter is closed"), - ("boxed moved", "Node is closed"), + ( + "moved", + "IllegalStateException: api_Counter_get: a handle's value was moved out", + ), + ( + "boxed moved", + "IllegalStateException: api_Node_sum: a handle's value was moved out", + ), ("lent kept", "Counter is closed"), ( "lent moved", @@ -641,8 +647,8 @@ fn api_python() { run(&mut python(&dir, &[case])); } for (case, message) in [ - ("moved", "ValueError: Counter is closed"), - ("boxed moved", "ValueError: Node is closed"), + ("moved", "ValueError: api_Counter_get: a handle's value was moved out"), + ("boxed moved", "ValueError: api_Node_sum: a handle's value was moved out"), ("lent kept", "ValueError: Counter is closed"), ("lent held", "Counter: a call still holds it when its lend ends"), ( From 68750d25db797876410f47adce3fd2a26e65cc42 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 00:27:04 -0800 Subject: [PATCH 07/30] wip(ffi): Go's Close drops the value through Rust and returns an error Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.go | 70 +++++++++++++++++++----- ffi/src/go.rs | 120 ++++++++++++++++-------------------------- ffi/tests/api.go | 102 ++++++++++++++++++++++------------- ffi/tests/fixtures.rs | 7 ++- 4 files changed, 172 insertions(+), 127 deletions(-) diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index afc8dacb2..a09b579cf 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -58,28 +58,38 @@ func (e closedError_) Is(target error) bool { return target == ErrClosed } -// Handle_ is what an object owns, or is lent for a call: each call holds it, so Close frees -// it only once no call does, and a call after Close returns ErrClosed. For the generated -// packages, which share it: use none of it. +// Handle_ is what an object owns, or is lent for a call. One of a value Rust checks, which +// drop drops, is Rust's alone to account: Close drops the value as Rust's own drop does, +// failing while a call borrows it, a call after it fails as Rust's check of a moved value +// does, and the handle is freed once the GC collects the object, which each call keeps +// reachable. Any other counts its calls: Close frees it then, failing while a call holds +// it, and a call after it returns ErrClosed. For the generated packages, which share it: +// use none of it. type Handle_ struct { raw unsafe.Pointer state atomic.Int64 // Nil for a handle Rust lends, which it frees itself. free func(unsafe.Pointer) + // Non-nil for an owned one of a value Rust checks. + drop func(unsafe.Pointer) error name string - // What closes an owned handle the GC finds unclosed. + // What frees an owned handle once the GC collects its object. Cleanup runtime.Cleanup } -// NewHandle_ is a handle of raw, which free frees, or nil for one Rust lends. -func NewHandle_(raw unsafe.Pointer, free func(unsafe.Pointer), name string) *Handle_ { - h := &Handle_{raw: raw, free: free, name: name} +// NewHandle_ is a handle of raw, which free frees, or nil for one Rust lends, and drop +// drops the value of, or nil for one Go counts. +func NewHandle_(raw unsafe.Pointer, free func(unsafe.Pointer), drop func(unsafe.Pointer) error, name string) *Handle_ { + h := &Handle_{raw: raw, free: free, drop: drop, name: name} h.state.Store(held_) return h } // Hold is the handle, held until Release, or nil once closed. func (h *Handle_) Hold() unsafe.Pointer { + if h.drop != nil { + return h.raw + } for { n := h.state.Load() if n&closed_ != 0 { @@ -98,14 +108,48 @@ func (h *Handle_) Closed() error { // Release gives up a call's hold. func (h *Handle_) Release() { - if h.state.Add(-held_) < held_ && h.free != nil { + if h.drop == nil && h.state.Add(-held_) < held_ && h.free != nil { h.free(h.raw) } } -// Close gives up the owner's hold, freeing the handle once no call holds it; again, it -// does nothing. -func (h *Handle_) Close() { +// Close closes h as Handle_ says; again, it does nothing. +func (h *Handle_) Close() error { + if h.drop != nil { + return h.drop(h.raw) + } + for { + n := h.state.Load() + if n&closed_ != 0 { + return nil + } + if n != held_ { + return checkError_{h.name, "a handle is already borrowed", 3} + } + if h.state.CompareAndSwap(n, closed_) { + // A cleanup left registered until the GC slows each one registered after it. + h.Cleanup.Stop() + if h.free != nil { + h.free(h.raw) + } + return nil + } + } +} + +// Collected frees an owned handle once the GC collects its object: at once if Rust +// accounts it, since no call runs on an object nothing reaches, or else once no call holds +// it. +func (h *Handle_) Collected() { + if h.drop != nil { + h.free(h.raw) + return + } + h.abandon() +} + +// abandon gives up the owner's hold of a counted one, freeing it once no call holds it. +func (h *Handle_) abandon() { for { n := h.state.Load() if n&closed_ != 0 { @@ -113,8 +157,6 @@ func (h *Handle_) Close() { } next := (n | closed_) - held_ if h.state.CompareAndSwap(n, next) { - // A cleanup left registered until the GC slows each one registered after it. - h.Cleanup.Stop() if next < held_ && h.free != nil { h.free(h.raw) } @@ -127,7 +169,7 @@ func (h *Handle_) Close() { // would reach the freed handle, so it panics, as Rust's own check fails once a call // borrowed. func (h *Handle_) End() { - h.Close() + h.abandon() if h.state.Load() != closed_ { panic(h.name + ": a call still holds it when its lend ends") } diff --git a/ffi/src/go.rs b/ffi/src/go.rs index b417f93f2..2f7223f32 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -38,7 +38,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::fmt::Write; use crate::abi::{self, Abi, Ty, pascal}; -use crate::ir::{Access, Closure, Family, Function, Home, Int, Literal, On, Param, Receiver, Type}; +use crate::ir::{Access, Closure, Family, Function, Home, Int, Literal, On, Param, Type}; const RUNTIME: &str = include_str!("../runtime/rt.go"); const CONVERSIONS: &str = include_str!("../runtime/ffi_cgo.go"); @@ -1391,13 +1391,8 @@ impl<'a> Gen<'a> { results, body, }; - // A check each trait's borrow can fail: `String` and `GoString` return no error, so - // theirs aborts, as do C's. + // A check each trait's borrow can fail. let status = self.abi.traits_status_of(ty); - let nil = match status { - true => ", nil", - false => "", - }; let status_error = match status { true => self.qualify(Packages::RT, "Status_"), false => String::new(), @@ -1413,7 +1408,8 @@ impl<'a> Gen<'a> { ), false => format!("r_ := {call}\n"), }; - // fmt's String and GoString return no error, so a closed one reads as the error. + // fmt's String and GoString return no error, so a closed one, or a failed check, reads + // as the error. let text = |e: &str| format!("{e}.Error()"); let mut methods = Vec::new(); if t.display { @@ -1423,7 +1419,10 @@ impl<'a> Gen<'a> { "String", Vec::new(), vec!["string".to_string()], - format!("{open}return liftString_(C.{c}_to_string({this}{nil}))"), + format!( + "{open}{}return liftString_(r_)", + checked(&format!("C.{c}_to_string({this})"), &format!("{c}_to_string"), &text) + ), )); } if t.debug { @@ -1433,7 +1432,10 @@ impl<'a> Gen<'a> { "GoString", Vec::new(), vec!["string".to_string()], - format!("{open}return liftString_(C.{c}_debug({this}{nil}))"), + format!( + "{open}{}return liftString_(r_)", + checked(&format!("C.{c}_debug({this})"), &format!("{c}_debug"), &text) + ), )); } if object && t.clone { @@ -1468,8 +1470,9 @@ impl<'a> Gen<'a> { checked(&format!("C.{c}_eq({this}, (*C.{c})(o_))"), &format!("{c}_eq"), &failed) ), (false, true) if closable => format!( - "{open}c_other_ := lower_{c}(a_, other)\nif a_.err != nil {{\n\treturn {}\n}}\nreturn bool(C.{c}_eq({this}, &c_other_)){ok}", - failed("a_.err") + "{open}c_other_ := lower_{c}(a_, other)\nif a_.err != nil {{\n\treturn {}\n}}\n{}return bool(r_){ok}", + failed("a_.err"), + checked(&format!("C.{c}_eq({this}, &c_other_)"), &format!("{c}_eq"), &failed) ), (false, true) => { format!("{open}c_other_ := lower_{c}(a_, other)\nreturn bool(C.{c}_eq({this}, &c_other_))") @@ -1847,16 +1850,23 @@ impl<'a> Gen<'a> { let name = self.named(&ty).1; let rust = abi.api.rust(&object.path, &object.args); let handle = self.qualify(Packages::RT, "Handle_"); - let text = format!( - "{name} is `{rust}`, a handle Close frees, or else the GC once it collects it.\n\n{}", - object.docs - ); + let (text, close) = match abi.checked[i] { + true => ( + "a handle the GC frees once it collects it", + "// Close drops the value now, as Rust's drop does: a call after it fails, and Close fails\n\ + // while a call borrows it, changing nothing.\n", + ), + false => ( + "a handle Close frees, or else the GC once it collects it", + "// Close frees the handle now, rather than once the GC collects it: a call after it fails,\n\ + // and Close fails while a call holds it, changing nothing.\n", + ), + }; + let text = format!("{name} is `{rust}`, {text}.\n\n{}", object.docs); let _ = write!( self.types, "{}type {name} struct {{\n\t// Handle_ is the handle it owns, for the generated packages: use none of it.\n\tHandle_ *{handle}\n}}\n\n\ - // Close frees the handle once no call holds it, rather than once the GC collects it: a\n\ - // call after it panics.\n\ - func (self *{name}) Close() {{\n\tself.Handle_.Close()\n}}\n\n", + {close}func (self *{name}) Close() error {{\n\terr := self.Handle_.Close()\n\truntime.KeepAlive(self)\n\treturn err\n}}\n\n", docs(text.trim(), "") ); let mut members = vec!["Close".to_string(), "Handle_".to_string()]; @@ -1877,12 +1887,25 @@ impl<'a> Gen<'a> { let name = self.qualified(&ty); let handle = self.qualify(Packages::RT, "Handle_"); let new = self.qualify(Packages::RT, "NewHandle_"); + let (drop, dropping) = match self.abi.checked[i] { + true => ( + format!( + "// drop_{c} drops the value of the handle p, which Rust checks.\n\ + func drop_{c}(p unsafe.Pointer) error {{\n\tvar s C.{status}\n\tC.{c}_drop((*C.{c})(p), &s)\n\treturn {status_}(int32(s), \"{c}_drop\")\n}}\n\n", + status = self.abi.rt("status"), + status_ = self.qualify(Packages::RT, "Status_"), + ), + format!("drop_{c}"), + ), + false => (String::new(), "nil".to_string()), + }; format!( "func free_{c}(p unsafe.Pointer) {{\n\tC.{c}_free((*C.{c})(p))\n}}\n\n\ + {drop}\ // own_{c} owns a handle Rust returned, or is nil for none.\n\ - func own_{c}(raw *C.{c}) *{name} {{\n\tif raw == nil {{\n\t\treturn nil\n\t}}\n\tv := &{name}{{Handle_: {new}(unsafe.Pointer(raw), free_{c}, \"{local}\")}}\n\tv.Handle_.Cleanup = runtime.AddCleanup(v, (*{handle}).Close, v.Handle_)\n\treturn v\n}}\n\n\ + func own_{c}(raw *C.{c}) *{name} {{\n\tif raw == nil {{\n\t\treturn nil\n\t}}\n\tv := &{name}{{Handle_: {new}(unsafe.Pointer(raw), free_{c}, {dropping}, \"{local}\")}}\n\tv.Handle_.Cleanup = runtime.AddCleanup(v, (*{handle}).Collected, v.Handle_)\n\treturn v\n}}\n\n\ // lend_{c} is a handle Rust lends for a call, which it frees itself.\n\ - func lend_{c}(raw *C.{c}) *{name} {{\n\treturn &{name}{{Handle_: {new}(unsafe.Pointer(raw), nil, \"{local}\")}}\n}}\n\n\ + func lend_{c}(raw *C.{c}) *{name} {{\n\treturn &{name}{{Handle_: {new}(unsafe.Pointer(raw), nil, nil, \"{local}\")}}\n}}\n\n\ // holdOption_{c} is v's handle, held until the call returns, or nil for none.\n\ func holdOption_{c}(a_ *arena_, v *{name}) *C.{c} {{\n\tif v == nil {{\n\t\treturn nil\n\t}}\n\treturn (*C.{c})(a_.hold(v.Handle_, v))\n}}\n\n" ) @@ -2138,34 +2161,6 @@ impl<'a> Gen<'a> { }); } - /// The Go statement closing each object the value `e` of type `ty` holds, once a call - /// moved them out. - fn close(&self, ty: &Type, e: &str, depth: usize) -> String { - match ty { - Type::Option(inner) => match inner.base() { - Type::Object(_) => format!("if {e} != nil {{ {e}.Close() }}"), - _ => { - let v = format!("v{depth}_"); - format!("if {e} != nil {{ {v} := *{e}; {} }}", self.close(inner, &v, depth + 1)) - } - }, - Type::Box(inner) => self.close(inner, e, depth), - Type::Tuple(elements) if elements.len() == 1 => self.close(&elements[0], e, depth), - Type::Tuple(elements) => { - let closes: Vec = (elements.iter().enumerate()) - .filter(|(_, ty)| moved(self.abi, ty)) - .map(|(k, ty)| self.close(ty, &format!("{e}.V{k}"), depth)) - .collect(); - closes.join("; ") - } - Type::List(_, inner) => { - let v = format!("v{depth}_"); - format!("for _, {v} := range {e} {{ {} }}", self.close(inner, &v, depth + 1)) - } - _ => format!("{e}.Close()"), - } - } - /// `function`'s Go form: a method of `owner` (whose IR type is `owner_ty`) if it has one /// and a receiver, or else a function of the package. fn function(&mut self, function: &Function, owner: Option<&Ty>, owner_ty: Option<&Type>) -> Method { @@ -2250,13 +2245,12 @@ impl<'a> Gen<'a> { }; let mut prelude: Vec = Vec::new(); - let mut after: Vec = Vec::new(); let mut checked = false; let mut arena = false; let mut args: Vec = Vec::new(); let mut c_params = export.call.params.iter(); let mut go_receiver = None; - if let Some(receiver) = receiver { + if receiver.is_some() { let (_, c) = c_params.next().expect("a method has its receiver"); let ty = owner_ty.expect("a method has its type").clone(); let go = self.go(&ty); @@ -2268,10 +2262,6 @@ impl<'a> Gen<'a> { fail("self.Handle_.Closed()") )); args.push(format!("(*C.{name})(h_)")); - let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); - if receiver == Receiver::Owned && !copy { - after.push("self.Close()".to_string()); - } } _ => { let c = match abi.ty(&ty) { @@ -2313,9 +2303,6 @@ impl<'a> Gen<'a> { continue; } params.push((name.clone(), self.go_param(¶m.ty))); - if moved(abi, ¶m.ty) { - after.push(self.close(¶m.ty, &name, 0)); - } match (¶m.ty, c) { (Type::Closure(closure), _) => { let trampoline = self.trampoline(¶m.ty, closure); @@ -2413,12 +2400,6 @@ impl<'a> Gen<'a> { if checked { body.push(format!("if a_.err != nil {{\n\t{}\n}}", fail("a_.err"))); } - if !after.is_empty() { - match &after[..] { - [one] if one.ends_with("Close()") && !one.contains(' ') => body.push(format!("defer {one}")), - _ => body.push(format!("defer func() {{\n\t{}\n}}()", after.join("\n\t"))), - } - } if let Some((_, poll, free)) = &export.future { body.push(format!("f_ := C.{c_name}({})", args.join(", "))); @@ -2663,17 +2644,6 @@ fn poll_error(abi: &Abi<'_>) -> String { format!("C.{}_POLL_ERROR", abi.prefix.to_uppercase()) } -/// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, -/// a list, or a tuple, unless `Copy`, and never through a reference. -fn moved(abi: &Abi<'_>, ty: &Type) -> bool { - match ty { - Type::Object(i) => !abi.api.objects[*i].copy, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), - Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), - _ => false, - } -} - /// `ty` returned: a borrow is returned as a clone, so owned. fn strip_refs(ty: &Type) -> Type { match ty { diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 2c0c75657..3a3061bce 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -1014,7 +1014,8 @@ func futures() { check(errors.Is(err, context.DeadlineExceeded), "a receive past its deadline returns its error") } - // A handle closed while a call waits on it stays alive until the call ends. + // Closing a handle a call waiting on it borrows fails, as Rust refuses a drop under a + // borrow, and changes nothing: the call ends as it would, and then Close passes. { other := api.NewChannel() watching := watch(ctx) @@ -1022,9 +1023,13 @@ func futures() { go func() { got <- string(must(other.Recv(watching)).Copy()) }() <-watching.waiting sender := must(other.Clone()) - other.Close() + err := other.Close() + check(errors.Is(err, rt.ErrBorrowed) && err.Error() == "api_Channel_drop: a handle is already borrowed", "Close under a waiting call fails") sender.Send([]byte("held")) - check(<-got == "held", "a closed handle stays alive in the call waiting on it") + check(<-got == "held", "the waiting call ends as it would") + check(other.Close() == nil, "Close passes once no call borrows it") + _, err = other.Recv(ctx) + check(errors.Is(err, rt.ErrClosed) && err.Error() == "api_Channel_recv: a handle's value was moved out", "a call after Close fails") sender.Close() } @@ -1195,33 +1200,36 @@ func uncollected(n uint32, what string) { } // closed checks that every call taking a closed object returns ErrClosed, however it was -// closed, and that a second Close does nothing. +// closed, and that a second Close does nothing: as Rust's check of a moved value for one +// Rust checks, and as Go's own for one whose calls only borrow it. func closed() { ctx := context.Background() - isClosed := func(err error, name, what string) { - check(errors.Is(err, rt.ErrClosed) && err.Error() == name+" is closed", what) + isClosed := func(err error, want, what string) { + check(errors.Is(err, rt.ErrClosed) && err.Error() == want, fmt.Sprintf("%s: %v", what, err)) + } + moved := func(function string) string { + return function + ": a handle's value was moved out" } // A method, a trait's, and a field's accessors. c := api.NewCounter("c", 1) - c.Close() - c.Close() + check(c.Close() == nil && c.Close() == nil, "Close, then again, passes") _, err := c.Get() - isClosed(err, "Counter", "a method of a closed object") + isClosed(err, moved("api_Counter_get"), "a method of a closed object") _, err = c.Clone() - isClosed(err, "Counter", "Clone of a closed object") - check(c.GoString() == "Counter is closed", "GoString of a closed object is the error") + isClosed(err, moved("api_Counter_clone"), "Clone of a closed object") + check(c.GoString() == moved("api_Counter_debug"), "GoString of a closed object is the error") tally := api.NewTally() tally.Close() _, err = api.NewTally().Equal(tally) - isClosed(err, "Tally", "Equal with a closed object") + isClosed(err, moved("api_Tally_eq"), "Equal with a closed object") registry := must(api.NewRegistry(api.NewCounter("first", 1))) registry.Close() _, err = registry.FirstGet() - isClosed(err, "Registry", "a field's getter of a closed object") + isClosed(err, moved("api_Registry_first_get"), "a field's getter of a closed object") // A call that fails moves nothing in. second := api.NewCounter("second", 2) - isClosed(registry.FirstSet(second), "Registry", "a field's setter of a closed object") + isClosed(registry.FirstSet(second), moved("api_Registry_first_set"), "a field's setter of a closed object") check(must(second.Get()) == 2, "a failed call leaves what it would move") // A record's method, whose receiver holds an object. @@ -1230,44 +1238,60 @@ func closed() { check(must(stamped.Total()) == 5, "a record's method reads the object it holds") stamp.Close() _, err = stamped.Total() - isClosed(err, "Stamp", "a record's method whose object is closed") + isClosed(err, "Stamp is closed", "a record's method whose object is closed") // An argument, alone, or nested where the call holds it through its arena. _, err = api.BumpBoth(api.NewTally(), tally) - isClosed(err, "Tally", "a closed argument") + isClosed(err, moved("api_bump_both"), "a closed argument") open := api.NewCounter("open", 3) _, err = api.Worth(&[]*api.Counter{open, c}, nil) - isClosed(err, "Counter", "a closed object in a list") + isClosed(err, moved("api_worth"), "a closed object in a list") check(must(open.Get()) == 3, "a failed call leaves the rest of a list") // An async call. channel := api.NewChannel() channel.Close() _, err = channel.Recv(ctx) - isClosed(err, "Channel", "an async call of a closed object") - - // Closed by a call that moved it, plain or boxed, or once the callback it was lent to - // returned. - moved := api.NewCounter("m", 0) - must(moved.IntoLabel(nil)) - _, err = moved.Get() - isClosed(err, "Counter", "a method of a moved object") + isClosed(err, moved("api_Channel_recv"), "an async call of a closed object") + + // Moved by a call, plain or boxed, or closed once the callback it was lent to returned. + taken := api.NewCounter("m", 0) + must(taken.IntoLabel(nil)) + _, err = taken.Get() + isClosed(err, moved("api_Counter_get"), "a method of a moved object") tail := must(api.NewNode(1, nil)) must(api.NewNode(2, tail)) _, err = tail.Sum() - isClosed(err, "Node", "a method of a boxed moved object") + isClosed(err, moved("api_Node_sum"), "a method of a boxed moved object") var kept *api.Counter must(must(api.NewRegistry(api.NewCounter("c", 0))).Edit(func(counter *api.Counter) bool { kept = counter return false })) _, err = kept.Get() - isClosed(err, "Counter", "a method of a lent object kept past its callback") - - // Close racing calls on other goroutines: each call either runs, holding the handle - // so Rust's value outlives it, or returns ErrClosed; Rust's is dropped once. + isClosed(err, "Counter is closed", "a method of a lent object kept past its callback") + + // Close racing calls on other goroutines, of a value Go counts and of one Rust checks. + latch := api.LatchDefault() + must(latch.Set(5)) + race(latch, func() (bool, error) { + v, err := latch.Get() + return err == nil && *v == 5, err + }, "Latch is closed", "Latch: a handle is already borrowed") before := api.Dropped() token := api.NewToken(5) + race(token, func() (bool, error) { + id, err := token.IdGet() + return id == 5, err + }, moved("api_Token_id_get"), "api_Token_drop: a handle is already borrowed") + check(api.Dropped() == before+1, "Close racing calls drops Rust's value once") + fmt.Println("closed ok") +} + +// race closes c while 8 goroutines call call on it: each call runs on the live value or, +// once c is closed, fails with closed; a Close a running call beats fails with borrowed, as +// Rust refuses a drop under a borrow, and changes nothing, so the next tries again. +func race(c interface{ Close() error }, call func() (bool, error), closed, borrowed string) { var wg sync.WaitGroup var ran, failed atomic.Int64 for range 8 { @@ -1275,13 +1299,13 @@ func closed() { go func() { defer wg.Done() for { - id, err := token.IdGet() + live, err := call() if err != nil { - isClosed(err, "Token", "a call racing Close") + check(errors.Is(err, rt.ErrClosed) && err.Error() == closed, fmt.Sprintf("a call racing Close: %v", err)) failed.Add(1) return } - check(id == 5, "a call racing Close reads the live value") + check(live, "a call racing Close reads the live value") ran.Add(1) } }() @@ -1289,10 +1313,16 @@ func closed() { for ran.Load() < 100 { runtime.Gosched() } - token.Close() + for { + err := c.Close() + if err == nil { + break + } + check(errors.Is(err, rt.ErrBorrowed) && err.Error() == borrowed, fmt.Sprintf("Close racing a call: %v", err)) + } wg.Wait() - check(failed.Load() == 8 && api.Dropped() == before+1, "Close racing calls drops Rust's value once") - fmt.Println("closed ok") + check(failed.Load() == 8, "every call after Close fails") + check(c.Close() == nil, "Close again passes") } // bytes checks that Bytes Rust returns read Rust's buffer in place, kept alive by any view diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 4de94eead..191029bd6 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -648,7 +648,10 @@ fn api_python() { } for (case, message) in [ ("moved", "ValueError: api_Counter_get: a handle's value was moved out"), - ("boxed moved", "ValueError: api_Node_sum: a handle's value was moved out"), + ( + "boxed moved", + "ValueError: api_Node_sum: a handle's value was moved out", + ), ("lent kept", "ValueError: Counter is closed"), ("lent held", "Counter: a call still holds it when its lend ends"), ( @@ -1088,7 +1091,7 @@ fn api_go() { "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("nil reader", "a nil io.ReadSeeker"), - ("returned closed", "Instant is closed"), + ("returned closed", "api_Instant_copy: a handle's value was moved out"), ("family", "an address that is not IPv4 has no Rust SocketAddrV4"), ("outlived", OUTLIVED), ("text", "api_greet: a string is not UTF-8"), From 2c9f87f31b1ec9049d65d2f8f95cdb79d95ed099 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 01:14:11 -0800 Subject: [PATCH 08/30] wip(ffi): Dart's close drops the value through Rust Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.dart | 26 ++++++++++++----- ffi/src/dart.rs | 63 +++++++-------------------------------- ffi/tests/api.dart | 68 ++++++++++++++++++++++++++++--------------- ffi/tests/fixtures.rs | 4 +-- 4 files changed, 76 insertions(+), 85 deletions(-) diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index af74e1d08..abcac25f7 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -337,40 +337,52 @@ final class _Callback extends ffi.Struct { external ffi.Pointer data; } -/// What an object owns: its handle, freed once closed and no call holds it, or once the -/// garbage collector finds it unreachable. A lent one's Rust frees. +/// What an object owns: its handle, or one Rust lends for a call, which Rust frees. An owned +/// one of a value Rust checks is Rust's alone to account: [close] drops the value as Rust's +/// own drop does, failing while a call borrows it, a call after it throws as Rust's check +/// of a moved value does, and the handle is freed once the garbage collector finds this +/// unreachable. Any other counts its calls: [close] frees it then, throwing while a call +/// holds it, and a call after it throws. abstract base class _Object implements ffi.Finalizable { final ffi.Pointer _raw; - /// The owner's hold until closed, and one for each call. + /// The owner's hold until closed, and one for each call, of a counted one. int _holds = 1; bool _closed = false; final bool _owned; + /// Whether it counts its calls, rather than Rust. + final bool _counted; + /// The garbage collector's hold of an owned one's handle, until freed. ffi.Pointer _own = ffi.nullptr; - _Object(this._raw, this._owned); + _Object(this._raw, bool owned, bool checked) : _owned = owned, _counted = !(owned && checked); String get _type; /// Free the handle and detach its finalizer. void _free(); + /// Drop the value of one Rust checks, throwing [StateError] if a call borrows it. + void _drop() {} + ffi.Pointer _hold() { + if (!_counted) return _raw; if (_closed) throw StateError('$_type is closed'); _holds++; return _raw; } void _release() { - if (--_holds == 0 && _owned) _free(); + if (_counted && --_holds == 0 && _owned) _free(); } - /// Free the handle once no call holds it, rather than once the garbage collector finds - /// this unreachable: a call after it throws [StateError]. + /// Close it as [_Object] says; again, it does nothing. void close() { + if (!_counted) return _drop(); if (_closed) return; + if (_holds != 1) throw StateError('$_type: a handle is already borrowed'); _closed = true; _release(); } diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 57821006b..1a553e6c5 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -27,7 +27,7 @@ use std::fmt::Write; use crate::abi::{self, Abi, Sig, Ty, pascal}; use crate::glue::rs; -use crate::ir::{Access, Closure, Family, Function, Int, Literal, On, Receiver, Type}; +use crate::ir::{Access, Closure, Family, Function, Int, Literal, On, Type}; use crate::kotlin::strip_refs; const RUNTIME: &str = include_str!("../runtime/rt.dart"); @@ -1602,9 +1602,17 @@ impl<'a> Gen<'a> { }; let (own, free) = self.owned(&c, &owner); let mut members = vec!["close".to_string()]; + let checked = abi.checked[i]; let mut body = vec![format!( - "\t{name}._(ffi.Pointer raw) : super(raw, true) {{\n\t\t_own = {own}(this, raw);\n\t}}\n\n\t{name}._lent(ffi.Pointer raw) : super(raw, false);\n\n\t@override\n\tString get _type => '{name}';\n\n\t@override\n\tvoid _free() => {free}(_n.home, _own, this, _raw);\n" + "\t{name}._(ffi.Pointer raw) : super(raw, true, {checked}) {{\n\t\t_own = {own}(this, raw);\n\t}}\n\n\t{name}._lent(ffi.Pointer raw) : super(raw, false, {checked});\n\n\t@override\n\tString get _type => '{name}';\n\n\t@override\n\tvoid _free() => {free}(_n.home, _own, this, _raw);\n" )]; + if checked { + let what = format!("{c}_drop"); + let f = self.served_fn(&what, vec![owner.clone(), Ty::Out(Box::new(Ty::Status))], Ty::Void); + body.push(format!( + "\t@override\n\tvoid _drop() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\tfinal s_ = a_()..value = 0;\n\t\t\t{f}(_n.home, _raw, s_);\n\t\t\t_status(s_.value, '{what}');\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + )); + } self.traits(&ty, &c, object.traits, &mut body, &mut members); self.methods(&object.methods, &owner, &ty, &mut body, &mut members); self.distinct(&rust, &members); @@ -1892,30 +1900,6 @@ impl<'a> Gen<'a> { } } - /// The Dart statements closing each object the value `e` of type `ty` holds, once a - /// call moved them out. - fn close(&mut self, ty: &Type, e: &str) -> String { - match ty { - Type::Option(inner) => { - let close = self.close(inner, "v"); - format!("{{ final v = {e}; if (v != null) {{ {close} }} }}") - } - Type::Box(inner) => self.close(inner, e), - Type::Tuple(elements) => { - let closes: Vec = (elements.iter().enumerate()) - .filter(|(_, ty)| moved(self.abi, ty)) - .map(|(k, ty)| self.close(ty, &format!("{e}.${}", k + 1))) - .collect(); - closes.join(" ") - } - Type::List(_, inner) => { - let close = self.close(inner, "v"); - format!("for (final v in {e}) {{ {close} }}") - } - _ => format!("{e}.close();"), - } - } - /// `function`'s Dart form and its member name, a member of `owner` (whose IR type is /// `owner_ty`) if it has one. fn function( @@ -1966,19 +1950,13 @@ impl<'a> Gen<'a> { let receiver = function.receiver.filter(|_| owner.is_some()); let mut pre = Vec::new(); let mut args: Vec = Vec::new(); - let mut after = Vec::new(); let mut c_params = export.call.params.iter(); - if let Some(receiver) = receiver { + if receiver.is_some() { let (_, c) = c_params.next().expect("a method has its receiver"); let ty = owner_ty.expect("a method has its type").clone(); match c { Ty::Handle { .. } => { args.push("a_.hold(this)".to_string()); - let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); - if receiver == Receiver::Owned && !copy { - // Qualified, apart from a parameter named `close`. - after.push("this.close();".to_string()); - } } Ty::In(inner) => { let (stmts, ptr) = self.value_ptr(&ty, inner, "this"); @@ -2018,9 +1996,6 @@ impl<'a> Gen<'a> { continue; } params.push(format!("{} {name}", self.dt(¶m.ty))); - if moved(abi, ¶m.ty) { - after.push(self.close(¶m.ty, &name)); - } match (¶m.ty, c) { (Type::Closure(closure_), Ty::Fn(..)) => { c_params.next().expect("a closure has its data"); @@ -2137,7 +2112,6 @@ impl<'a> Gen<'a> { if status { inner.push(check.clone()); } - inner.extend(after.clone()); body.push(format!( "final a_ = _Arena();\ntry {{\n{}\n}} finally {{\n\ta_.end();\n}}", indent(&inner.join("\n")) @@ -2165,7 +2139,6 @@ impl<'a> Gen<'a> { inner.push(format!("final ok_ = {call};")); checked(&mut inner); inner.extend(writes); - inner.extend(after.clone()); let ready = match ok { Type::Unit => "return;".to_string(), _ => format!("return {};", self.lift_outs(&function.ok, ok)), @@ -2182,21 +2155,18 @@ impl<'a> Gen<'a> { inner.push(format!("{call};")); checked(&mut inner); inner.extend(writes); - inner.extend(after.clone()); } // A value C returns beside what Rust wrote back is its return, as with none. (None, ok) if outs.len() > writes.len() => { inner.push(format!("{call};")); checked(&mut inner); inner.extend(writes); - inner.extend(after.clone()); inner.push(format!("return {};", self.lift_outs(&function.ok, ok))); } (None, ok) => { inner.push(format!("final r_ = {call};")); checked(&mut inner); inner.extend(writes); - inner.extend(after.clone()); inner.push(format!("return {};", self.lift(ok, "r_"))); } } @@ -2378,17 +2348,6 @@ fn thrown_value(thrown: &Thrown, lifted: &str) -> String { } } -/// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, -/// a list, or a tuple, unless `Copy`, and never through a reference. -fn moved(abi: &Abi<'_>, ty: &Type) -> bool { - match ty { - Type::Object(i) => !abi.api.objects[*i].copy, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), - Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), - _ => false, - } -} - /// The `dart:ffi` type of a C integer. fn native_int(int: Int) -> &'static str { match int { diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 2d7d0d4ef..c78f17f0e 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -76,12 +76,25 @@ void throws(void Function() f, String what) { check(false, what); } -/// `f` throws the StateError of a closed `type`. +/// `f` throws the StateError of a closed `type`: Dart's own, for a value whose calls only +/// borrow it, or else Rust's check of a moved value, naming the function. void closed(void Function() f, String type, String what) { try { f(); } on StateError catch (e) { - check(e.message == '$type is closed', '$what: $e'); + final moved = e.message.startsWith('api_') && e.message.endsWith(": a handle's value was moved out"); + check(e.message == '$type is closed' || moved, '$what: $e'); + return; + } + check(false, what); +} + +/// `f` throws the StateError `message` of a close under a borrow. +void borrowed(void Function() f, String message, String what) { + try { + f(); + } on StateError catch (e) { + check(e.message == message, '$what: $e'); return; } check(false, what); @@ -551,7 +564,7 @@ Future script() async { print('chunks ${api.chunks(utf8.encode('abcde'), 2)}'); final counters = api.counters(['x', 'y']); print('counters ${counters.map((c) => '${c.name()} ${c.get()}').join(', ')}'); - // Each counter moves in, so Dart closes it. + // Each counter moves in, so a later call throws. print('worth ${api.worth([counters[0]], [counters[1]])} ${api.worth(null, [])}'); for (final counter in counters) { closed(counter.get, 'Counter', 'a counter moved in is closed'); @@ -858,16 +871,21 @@ Future futures() async { done.cancel(); check(await done == 2, 'a finished future cancelled keeps its value'); - // A handle closed while a call awaits on it stays alive until the call ends. + // Closing a handle a call awaiting on it borrows throws, as Rust refuses a drop under a + // borrow, and changes nothing: the call ends as it would, and then the close passes. final other = api.Channel(); final held = other.recv(); final sender = other.clone(); - other.close(); + try { + other.close(); + check(false, 'a close under an awaiting call throws'); + } on StateError catch (e) { + check(e.message == 'api_Channel_drop: a handle is already borrowed', 'a close under an awaiting call: $e'); + } sender.send(utf8.encode('held')); - check( - text(await within(held, 'a receive on a closed handle')) == 'held', - 'a closed handle stays alive in the call awaiting on it', - ); + check(text(await within(held, 'a receive the close left')) == 'held', 'the awaiting call ends as it would'); + other.close(); + closed(other.clear, 'Channel', 'a call after the close'); sender.close(); // A future holding an implementation drops it as it finishes, closing it. @@ -1045,8 +1063,9 @@ void closedObjects() { closed(() => api.Board([('open', open), ('closed', c)]), 'Counter', 'a closed object in a tuple'); check(open.get() == 3, 'a call that throws moves none of its other arguments'); - // Nor gives Rust an implementation beside an argument that throws, which would keep it - // unclosed and force each later call through a thread of its own. + // Nor keeps an implementation beside an argument that throws, which would keep it unclosed + // and force each later call through a thread of its own: one Dart refuses Rust never + // takes, and one Rust's check refuses Rust drops, as it takes what it was given first. check(api.onFirstThread(), 'a call while Rust holds no implementation'); final spared = CountLog(); throws(() => api.ArrayU8x4([1, 2, 3]), '3 bytes are no [u8; 4]'); @@ -1054,12 +1073,12 @@ void closedObjects() { throws(() => api.logCounts(spared, 256, open, []), 'an integer out of range after an implementation'); closed(() => api.logCounts(spared, 0, c, []), 'Counter', 'a closed object after an implementation'); closed(() => api.logCounts(spared, 0, open, [c]), 'Counter', 'a closed object in a list after an implementation'); - check(spared.closes == 0, 'a call that throws gives Rust no implementation'); + check(spared.closes == 2, 'a call whose check throws drops the implementation it gave Rust'); check(api.onFirstThread(), "a call after one that threw runs on the isolate's thread"); lacks(() => api.wasmLog(spared), 'api_wasm_log'); check(api.onFirstThread(), "a call after one this target lacks runs on the isolate's thread"); check(api.logCounts(spared, 0, open, []) == 1, 'an implementation a call that threw gave none of'); - check(spared.lines == 1 && spared.closes == 1, 'an implementation given once is closed once'); + check(spared.lines == 1 && spared.closes == 3, 'an implementation given once is closed once'); // An async call. final channel = api.Channel(); @@ -1081,26 +1100,27 @@ void closedObjects() { }); closed(kept.get, 'Counter', 'a lent object kept past its closure'); - // Closed while a call holds it, from the closure the call lends, the object stays alive - // until the call returns, and is freed then. + // Closed from the closure a call lends while the call borrows it, an object throws, as + // Rust refuses a drop under a borrow, and changes nothing; closed after, it closes. final editing = api.Registry(api.Counter('e', 4)); final edited = editing.edit((counter) { - editing.close(); - closed(() => editing.firstGet, 'Registry', 'an object closed in a closure refuses calls'); + borrowed(editing.close, 'api_Registry_drop: a handle is already borrowed', 'a close in a closure the call lends'); return counter.get() == 4; }); - check(edited, 'the call holding a closed object still reaches it'); - closed(() => editing.firstGet, 'Registry', 'an object closed in a closure stays closed'); - // As the drop of what it holds shows: once, after the call. + check(edited, 'the call whose object a close failed on runs on'); + editing.close(); + closed(() => editing.firstGet, 'Registry', 'an object closed after the call'); + // The same for one Dart counts, as the drop of what it holds shows: once, at the close. late api.Logger logger; late CountLog log; log = CountLog(() { - logger.close(); - check(log.closes == 0, 'an object closed during a call it is held by outlives the call'); + borrowed(logger.close, 'Logger: a handle is already borrowed', 'a close during a call it is held by'); }); logger = api.Logger(log); - check(logger.write('x'), 'the call holding a closed object returns'); - check(log.closes == 1, 'an object closed during a call it is held by is freed as it returns'); + check(logger.write('x'), 'the call whose object a close failed on returns'); + check(log.closes == 0, 'a close that failed frees nothing'); + logger.close(); + check(log.closes == 1, 'a close after the call frees it'); logger.close(); check(log.closes == 1, 'closing it again frees nothing'); print('closed ok'); diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 191029bd6..23564e4e0 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -874,8 +874,8 @@ fn api_dart() { run(&mut dart_run(&exe, &[case])); } for (case, message) in [ - ("moved", "Bad state: Counter is closed"), - ("boxed moved", "Bad state: Node is closed"), + ("moved", "Bad state: api_Counter_get: a handle's value was moved out"), + ("boxed moved", "Bad state: api_Node_sum: a handle's value was moved out"), ("lent kept", "Bad state: Counter is closed"), ( "lent moved", From eef1898e9bdfb67dec70912963e9ee2af67bd89b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 02:04:03 -0800 Subject: [PATCH 09/30] wip(ffi): Swift's description, ==, clone and a closed class nested in an argument throw Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.swift | 24 ++++- ffi/src/swift.rs | 207 ++++++++++++++++++++++++++++++------------ ffi/tests/api.swift | 53 +++++++---- ffi/tests/fixtures.rs | 1 - 4 files changed, 209 insertions(+), 76 deletions(-) diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index 8d835cfb7..b3eec529b 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -504,11 +504,16 @@ extension Native { return try handle.with(what, body) } - /// What a call lowers nested values into, kept until it returns: copied bytes, arrays, - /// and the handles it holds. + /// What a call of `what` lowers nested values into, kept until it returns: copied + /// bytes, arrays, and the handles it holds. final class Arena { private var blocks: [UnsafeMutableRawPointer] = [] private var held: [Handle] = [] + private let what: StaticString + + init(_ what: StaticString) { + self.what = what + } func copy(_ bytes: UnsafeRawBufferPointer) -> Buf { guard bytes.count > 0 else { @@ -537,6 +542,7 @@ extension Native { text.map { buf($0) } ?? none } + /// The handle of a `Copy` object, which nothing closes, held until the call returns. func hold(_ handle: Handle) -> OpaquePointer? { let raw = handle.hold() held.append(handle) @@ -547,6 +553,20 @@ extension Native { handle.map { hold($0) } ?? nil } + /// The handle of an object a move may have closed, held until the call returns, or + /// a throw for the call if it is closed. + func held(_ handle: Handle) throws -> OpaquePointer? { + guard handle.held() else { + throw CheckError(function: "\(what)", message: "\(handle.type) is closed") + } + held.append(handle) + return handle.raw + } + + func held(_ handle: Handle?) throws -> OpaquePointer? { + try handle.map { try held($0) } ?? nil + } + /// `items` as a C array, each lowered by `lower`: never null, so an empty one is /// told apart from a missing one. func list(_ items: [T], _ lower: (T) throws -> C) rethrows -> (UnsafePointer, Int) { diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 4dce27bed..421c917ef 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -417,6 +417,27 @@ impl<'a> Gen<'a> { } } + /// Whether lowering a `ty` nested in another value can throw: it holds a class whose + /// value a move takes, closing its handle. + fn throwing(&self, ty: &Type) -> bool { + let mut handles = Vec::new(); + self.abi.api.handles(ty, &mut handles, &mut Vec::new()); + handles.into_iter().any(|i| self.held(i)) + } + + /// Whether object `i` is a class a move may close, whose hold for a call can fail. + fn held(&self, i: usize) -> bool { + self.abi.traits_status(i) && !self.unique(&Type::Object(i)) + } + + /// `lowered`, a lowering in a function that cannot throw, trapping instead. + fn trapping(lowered: String) -> String { + match lowered.contains("try ") { + true => format!("try! {lowered}"), + false => lowered, + } + } + /// The arena argument a value's `_lower` takes, if it needs one. fn arena_arg(&self, ty: &Type) -> &'static str { match self.arena(ty) { @@ -452,8 +473,13 @@ impl<'a> Gen<'a> { true => "unique", false => "list", }; + // A hold that throws makes the list's lowering throw. + let t = match item.contains("try ") { + true => "try ", + false => "", + }; format!( - "Native.vec(a_.{list}({e}) {{ {x} in {item} }}, {}.init(ptr:len:owner:))", + "Native.vec({t}a_.{list}({e}) {{ {x} in {item} }}, {}.init(ptr:len:owner:))", abi.list(inner) ) } @@ -471,26 +497,40 @@ impl<'a> Gen<'a> { ) } }; - format!("Native.option({e}, {{ {v} in {some} }}, {none})") + let t = match some.contains("try ") { + true => "try ", + false => "", + }; + format!("{t}Native.option({e}, {{ {v} in {some} }}, {none})") } Type::Option(inner) => match inner.base() { + Type::Object(i) if self.held(*i) => format!("try a_.held({e}?.handle)"), Type::Object(_) => format!("a_.hold({e}?.handle)"), _ if Abi::is_buf(inner) => format!("a_.buf({e})"), Type::List(_, elem) => { let v = self.fresh("v"); let some = self.lower(inner, &v); - format!("({e}.map {{ {v} in {some} }} ?? {}())", abi.list(elem)) + let t = if some.contains("try ") { "try " } else { "" }; + format!("({t}{e}.map {{ {v} in {some} }} ?? {}())", abi.list(elem)) } _ => { let c = abi.option(inner); let v = self.fresh("v"); let some = self.lower(inner, &v); - format!("({e}.map {{ {v} in {c}(some: true, value: {some}) }} ?? {c}())") + let t = if some.contains("try ") { "try " } else { "" }; + format!("({t}{e}.map {{ {v} in {c}(some: true, value: {some}) }} ?? {c}())") } }, Type::Box(inner) | Type::Ref(_, inner) => self.lower(inner, e), - Type::Record(_) | Type::Enum(_) => format!("{e}._lower({})", self.arena_arg(ty)), + Type::Record(_) | Type::Enum(_) => { + let t = match self.throwing(ty) { + true => "try ", + false => "", + }; + format!("{t}{e}._lower({})", self.arena_arg(ty)) + } Type::Object(_) if self.unique(ty) => format!("{e}.handle"), + Type::Object(i) if self.held(*i) => format!("try a_.held({e}.handle)"), Type::Object(_) => format!("a_.hold({e}.handle)"), Type::Callback(i, _) => format!("Native.lower_{}({e})", abi.callback(*i)), Type::Tuple(elements) => { @@ -753,84 +793,125 @@ impl<'a> Gen<'a> { let name = self.sw(ty); let object = matches!(ty, Type::Object(_)); let unique = self.unique(ty); - // A failed check aborts with its message, as none of these can throw. - let nil = match ty { - Type::Object(i) if self.abi.traits_status(*i) => ", nil", - _ => "", + // One whose checks can fail throws, so conforms to no protocol of these, whose + // requirements cannot throw; any other's checks never fail. + let status = self.abi.traits_status_of(ty); + let on = self.on(self.on, c, "\n\t\t"); + // The statements lowering a value receiver for the function `what`. + let value = |what: &str| { + let arena = match self.arena(ty) { + true => format!("let a_ = Native.Arena(\"{what}\")\n\t\tdefer {{ a_.finish() }}\n\t\t"), + false => String::new(), + }; + format!("{on}Native.check()\n\t\t{arena}") }; - // The receiver: a struct's handle, a class's held, or the value lowered. - let (open, this, close) = match object { - true if unique => (String::new(), "self.handle", ""), - true => ("self.handle.with { h_ in ".to_string(), "h_", " }"), - false => { - let arena = match self.arena(ty) { - true => "let a_ = Native.Arena()\n\t\tdefer { a_.finish() }\n\t\t", - false => "", - }; - ( + let lower = match self.throwing(ty) { + true => format!("try self._lower({})", self.arena_arg(ty)), + false => format!("self._lower({})", self.arena_arg(ty)), + }; + // The statements calling `call`, the C call with its arguments, unclosed, of the + // function `what`, returning `lift` of its result. + let call = |call: &str, what: &str, lift: &dyn Fn(&str) -> String| match status { + true => format!( + "var s_: Swift.Int32 = 0\n\t\tlet r_ = {call}, &s_)\n\t\ttry Native.status(s_, \"{what}\")\n\t\treturn {}", + lift("r_") + ), + false => format!("return {}", lift(&format!("{call})"))), + }; + // The body calling `{c}_` of the receiver, returning `lift` of its result. + let unary = |suffix: &str, lift: &dyn Fn(&str) -> String| { + let what = format!("{c}_{suffix}"); + match (object, unique) { + (true, true) => call(&format!("{what}(self.handle"), &what, lift), + (true, false) => { + let with = match status { + true => format!("try self.handle.with(\"{what}\")"), + false => "self.handle.with".to_string(), + }; format!( - "{}Native.check()\n\t\t{arena}var c_ = self._lower({})\n\t\treturn ", - self.on(self.on, c, "\n\t\t"), - self.arena_arg(ty) - ), - "&c_", - "", - ) + "return {with} {{ h_ in\n\t\t\t{}\n\t\t}}", + call(&format!("{what}(h_"), &what, lift).replace("\n\t\t", "\n\t\t\t") + ) + } + (false, _) => format!( + "{}var c_ = {lower}\n\t\t{}", + value(&what), + call(&format!("{what}(&c_"), &what, lift) + ), } }; - let text = |open: &str, call: &str| match object { - true => format!("Native.string({open}{call}{close})"), - false => format!("{open}Native.string({call})"), + let throws = match status { + true => " throws", + false => "", }; + // A property's body, in a getter that throws if the call can. + let getter = |body: String| match status { + true => format!("get throws {{\n\t\t\t{}\n\t\t}}", body.replace("\n\t\t", "\n\t\t\t")), + false => body, + }; + let text = |r: &str| format!("Native.string({r})"); if t.display { - protocols.push("CustomStringConvertible"); + if !status { + protocols.push("CustomStringConvertible"); + } members.push("description".to_string()); body.push(format!( "\tpublic var description: Swift.String {{\n\t\t{}\n\t}}\n", - text(&open, &format!("{c}_to_string({this}{nil})")) + getter(unary("to_string", &text)) )); } if t.debug { - protocols.push("CustomDebugStringConvertible"); + if !status { + protocols.push("CustomDebugStringConvertible"); + } members.push("debugDescription".to_string()); body.push(format!( "\t/// Rust's `Debug` form.\n\tpublic var debugDescription: Swift.String {{\n\t\t{}\n\t}}\n", - text(&open, &format!("{c}_debug({this}{nil})")) + getter(unary("debug", &text)) )); } if object && t.clone { members.push("clone".to_string()); - let raw = match unique { - true => format!("{c}_clone(self.handle{nil})"), - false => format!("self.handle.with {{ {c}_clone($0{nil}) }}"), - }; + let lift = |r: &str| format!("{name}(raw: {r})"); body.push(format!( - "\t/// A new handle to a clone.\n\tpublic func clone() -> {name} {{\n\t\t{name}(raw: {raw})\n\t}}\n" + "\t/// A new handle to a clone.\n\tpublic func clone(){throws} -> {name} {{\n\t\t{}\n\t}}\n", + unary("clone", &lift) )); } if t.eq { - protocols.push("Equatable"); - let compare = match object { - true if unique => format!("{c}_eq(a.handle, b.handle{nil})"), - true => format!("a.handle.with {{ a_ in b.handle.with {{ b_ in {c}_eq(a_, b_{nil}) }} }}"), - false => { - let arena = match self.arena(ty) { - true => "let a_ = Native.Arena()\n\t\tdefer { a_.finish() }\n\t\t", - false => "", + if !status { + protocols.push("Equatable"); + } + let what = format!("{c}_eq"); + let same = |r: &str| r.to_string(); + let compare = match (object, unique) { + (true, true) => call(&format!("{what}(a.handle, b.handle"), &what, &same), + (true, false) => { + let with = |h: &str| match status { + true => format!("try {h}.handle.with(\"{what}\")"), + false => format!("{h}.handle.with"), }; - let arg = self.arena_arg(ty); format!( - "{}Native.check()\n\t\t{arena}var ca_ = a._lower({arg})\n\t\tvar cb_ = b._lower({arg})\n\t\treturn {c}_eq(&ca_, &cb_)", - self.on(self.on, c, "\n\t\t") + "return {} {{ a_ in {} {{ b_ in\n\t\t\t{}\n\t\t}} }}", + with("a"), + with("b"), + call(&format!("{what}(a_, b_"), &what, &same).replace("\n\t\t", "\n\t\t\t") ) } + (false, _) => format!( + "{}var ca_ = {}\n\t\tvar cb_ = {}\n\t\t{}", + value(&what), + lower.replacen("self.", "a.", 1), + lower.replacen("self.", "b.", 1), + call(&format!("{what}(&ca_, &cb_"), &what, &same) + ), }; let borrowing = match unique { true => "borrowing ", false => "", }; body.push(format!( - "\tpublic static func == (a: {borrowing}{name}, b: {borrowing}{name}) -> Swift.Bool {{\n\t\t{compare}\n\t}}\n" + "\tpublic static func == (a: {borrowing}{name}, b: {borrowing}{name}){throws} -> Swift.Bool {{\n\t\t{compare}\n\t}}\n" )); } if t.default { @@ -916,8 +997,12 @@ impl<'a> Gen<'a> { "\tstatic func _lift(_ c: {c}) -> {name} {{\n\t\t{name}({})\n\t}}\n", lifted.join(", ") )); + let throws = match self.throwing(&ty) { + true => " throws", + false => "", + }; body.push(format!( - "\tfunc _lower({arena}) -> {c} {{\n\t\t{c}({})\n\t}}\n", + "\tfunc _lower({arena}){throws} -> {c} {{\n\t\t{c}({})\n\t}}\n", lowered.join(", ") )); let text = format!("{rust}\n\n{}", record.docs); @@ -1023,7 +1108,11 @@ impl<'a> Gen<'a> { lowers.join("\n") ), false => format!( - "\tfunc _lower({arena}) -> {c} {{\n\t\tvar c = {c}()\n\t\tswitch self {{\n{}\n\t\t}}\n\t\treturn c\n\t}}\n", + "\tfunc _lower({arena}){} -> {c} {{\n\t\tvar c = {c}()\n\t\tswitch self {{\n{}\n\t\t}}\n\t\treturn c\n\t}}\n", + match self.throwing(&ty) { + true => " throws", + false => "", + }, lowers.join("\n") ), }); @@ -1121,7 +1210,7 @@ impl<'a> Gen<'a> { let call = format!("Box.unbox(data).{member}({})", args.join(", ")); let call = match method.ok { Type::Unit => call, - _ => self.lower(&method.ok, &call), + _ => Self::trapping(self.lower(&method.ok, &call)), }; let mut closure_params = vec!["data".to_string()]; closure_params.extend(ps); @@ -1368,9 +1457,9 @@ impl<'a> Gen<'a> { abi.object(*i), self.nil(*i) ), - _ => self.lower(ty, e), + _ => Self::trapping(self.lower(ty, e)), }, - ty => self.lower(ty, e), + ty => Self::trapping(self.lower(ty, e)), } } @@ -1493,7 +1582,11 @@ impl<'a> Gen<'a> { } _ => { arena |= self.arena(&ty); - prelude.push(format!("var c_self_ = self._lower({})", self.arena_arg(&ty))); + let t = match self.throwing(&ty) { + true => "try ", + false => "", + }; + prelude.push(format!("var c_self_ = {t}self._lower({})", self.arena_arg(&ty))); arg.exprs.push("&c_self_".to_string()); } } @@ -1561,7 +1654,7 @@ impl<'a> Gen<'a> { if arena { prelude.insert( 1.min(prelude.len()), - "let a_ = Native.Arena()\ndefer { a_.finish() }".to_string(), + format!("let a_ = Native.Arena(\"{c_name}\")\ndefer {{ a_.finish() }}"), ); } let after: Vec = args.iter().flat_map(|a| a.after.clone()).collect(); diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index 45adb779d..ba07489d5 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -37,6 +37,18 @@ func text(_ data: Data) -> String { String(decoding: data, as: UTF8.self) } +/// Check that `body` throws for the call `function` of a Refusal a move closed. +func closed(_ function: String, _ body: () throws -> Void) { + do { + try body() + check(false, "\(function) of a closed Refusal throws") + } catch let e as CheckError { + check(e.function == function && e.message == "Refusal is closed", "\(function) threw \(e)") + } catch { + check(false, "\(function) threw \(error)") + } +} + /// The refusal `refuse` throws for `reason`: a class, since Swift throws it. func refusal(_ reason: String) -> Refusal { do { @@ -190,10 +202,10 @@ func script() { try! c.rename("d") print("name \(try! c.name())") print("is d \(try! c.`is`("d"))") - var copy = c.clone() + var copy = try! c.clone() _ = try! copy.add(1) print("clone \(try! copy.get()) original \(try! c.get())") - print("debug \(c.debugDescription)") + print("debug \(try! c.debugDescription)") let label = try! c.intoLabel(p) print("label \(debug(label))") print("note \(hasText(Note(text: ""))) \(hasText(Note(text: nil)))") @@ -296,7 +308,7 @@ func script() { let name = Name.owned("a") var joined = try! name.join(Name.owned("b")) - print("joined \(try! joined.asStr()) \(joined.debugDescription) \(joined == name)") + print("joined \(try! joined.asStr()) \(try! joined.debugDescription) \(try! joined == name)") try! joined.rename("c") print("renamed \(try! joined.asStr())") let long = try! longer(joined, Name.owned("dd")) @@ -476,7 +488,7 @@ func script() { let tally = Tally.new() _ = try! tally.bump() - print("tally \(try! tally.bump()) \(tally == tally)") + print("tally \(try! tally.bump()) \(try! tally == tally)") print("both \(try! bumpBoth(tally, tally))") print("peek \(try! peek(tally) { lent in try! lent.bump() })") @@ -564,9 +576,9 @@ func script() { // Foreign types: a URL parsed from text, socket addresses, OS paths, and `Vec`s. let base = try! Url.parse("https://example.com/a/b") - print("url \(base.description) host \(try! host(base).map { "Some(\"\($0)\")" } ?? "None")") - let url = try! resolve(base.clone(), "c?d") - print("resolve \(url.description) \(url == base)") + print("url \(try! base.description) host \(try! host(base).map { "Some(\"\($0)\")" } ?? "None")") + let url = try! resolve(try! base.clone(), "c?d") + print("resolve \(try! url.description) \(try! url == base)") do { _ = try Url.parse("no scheme") } catch let e as StringError { @@ -605,6 +617,22 @@ func script() { == "a refused: x, b refused: y", "reasons moves a struct and a class out of a list and an option" ) + // A class a move closed throws for each later call, as a receiver or nested. + let kept = refusal("z") + var moved = UniqueArray() + moved.append(TupleCounterRefusal(Counter.new("c", 3), kept)) + _ = try! reasons(moved, nil) + closed("api_Refusal_to_string") { _ = try kept.description } + var again = UniqueArray() + again.append(TupleCounterRefusal(Counter.new("d", 4), kept)) + do { + _ = try reasons(again, nil) + check(false, "api_reasons of a closed Refusal throws") + } catch let e as CheckError { + check(e.function == "api_reasons" && e.message == "Refusal is closed", "api_reasons threw \(e)") + } catch { + check(false, "api_reasons threw \(error)") + } // Each optional borrowed parameter is an overload with and without its label. let one = Counter.new("one", 1) check( @@ -714,7 +742,7 @@ func script() { let millis = { (d: Swift.Duration) in d.components.seconds * 1000 + d.components.attoseconds / 1_000_000_000_000_000 } let began = now() let ended = try! later(began, .milliseconds(1500)) - print("instants \(millis(try! since(ended, began))) \(millis(try! since(began, ended))) \(try! began == later(began, .zero)) \(began == ended)") + print("instants \(millis(try! since(ended, began))) \(millis(try! since(began, ended))) \(try! began == later(began, .zero)) \(try! began == ended)") // tracing's level, and its filter as an optional level whose nil is `OFF`. var verbosity = Verbosity.new(.warn) print("verbosity \(spell(try! verbosity.filter())) \(spell(louder(try! verbosity.filter())))") @@ -951,7 +979,7 @@ func futures() { let sender: Channel do { let other = Channel.new() - sender = other.clone() + sender = try! other.clone() q.launch { got.value = text(try await other.recv()) } q.drain() } @@ -1179,13 +1207,6 @@ func broken(_ which: String) { _ = try! stray() case "lent stray": try! visitStray { _ in } - // A class moved into Rust, which Swift still holds: the call closes its handle. - case "refusal moved": - let kept = refusal("z") - var pairs = UniqueArray() - pairs.append(TupleCounterRefusal(Counter.new("a", 1), kept)) - _ = try! reasons(pairs, nil) - _ = kept.description // The file serves wasm32 too, whose items the host's library lacks. case "target": _ = try! wasmOnly() diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 23564e4e0..b0ae1793a 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -807,7 +807,6 @@ fn api_swift() { ("outlived", OUTLIVED), ("stray", "a path from Rust is not UTF-8"), ("lent stray", "a path from Rust is not UTF-8"), - ("refusal moved", "Refusal is closed"), ("target", "api_wasm_only: it is not on this library's target"), ("variant", "api_Platform.Wasm: it is not on this library's target"), ("plain variant", "api_Family.Wasm: it is not on this library's target"), From 6992b6197bc24c5629057db06403f84ddd588726 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 02:16:13 -0800 Subject: [PATCH 10/30] test(ffi): C's drop is refused under a recv, then moves the value Co-Authored-By: Claude Opus 5.5 --- ffi/tests/api.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 27c620fc2..84584ad56 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -1627,6 +1627,37 @@ static int statuses(void) { } api_Channel_free(channel); + /* A drop under a running call is refused; once it ends the drop takes the value, which + * a later call then finds moved, and free frees only the handle. */ + channel = api_Channel_new(); + api_Channel_send(channel, str("y"), NULL); + recv = api_Channel_recv(channel, NULL); + status = 0; + api_Channel_drop(channel, &status); + if (status != API_STATUS_BORROWED) { + fprintf(stderr, "a drop during a recv wrote %d\n", status); + failed = 1; + } + if (api_Channel_recv_poll(recv, waker, &payload) != API_POLL_READY || payload.len != 1) { + fprintf(stderr, "a refused drop changed the channel\n"); + failed = 1; + } + api_buf_free(payload); + api_Channel_recv_future_free(recv); + status = 0; + api_Channel_drop(channel, &status); + api_Channel_drop(channel, &status); + if (status != 0) { + fprintf(stderr, "a drop after the recv wrote %d\n", status); + failed = 1; + } + api_Channel_clear(channel, &status); + if (status != API_STATUS_MOVED) { + fprintf(stderr, "a clear after a drop wrote %d\n", status); + failed = 1; + } + api_Channel_free(channel); + status = 0; api_Meters *meters = api_Meters_new(); api_Meter *first = api_Meters_first(meters, NULL); From 2e743753eead084e5d1320711fcacdf633af1300 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 02:28:52 -0800 Subject: [PATCH 11/30] fix(ffi): an unchecked Copy object lifts uncounted, so a checking list of them needs no ? The moq export's record traits lifted a list of Version, a Copy object no export misuses, with a ? inside the item closure. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 84 +++++++++++++++++++++++++++++++------------------ ffi/src/glue.rs | 2 ++ 2 files changed, 55 insertions(+), 31 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 735d4ed2d..54b4e9339 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -199,7 +199,12 @@ dropped for the other target's span where the file differs. the function, as C++ passes; each other binding raises its language's own error, as below. A null handle aborts. Only a type some export can misuse is checked: one that is not `Sync`, or that an export borrows as `&mut`, moves out, or lends a closure; a call - borrowing none of those takes no `status`. A setter borrows as `&mut T` and takes + borrowing none of those takes no `status`, and neither does `_free`. A checked type also + has `_drop`, which drops the value at once, as a move out would, and leaves the handle to + `_free`: a later call fails with `_STATUS_MOVED`, a second drop does nothing, + and one while a call or future borrows the value fails with + `_STATUS_BORROWED`. A record's or data enum's `_eq`, `_to_string`, and `_debug` + take a `status` too when it holds a checked handle. A setter borrows as `&mut T` and takes its value as `T`, so it writes the value the handle holds, or the one Rust lent it, while a getter's copy is C's own: a nested field is got, set, and set back. A type whose C++ copies share one value has `_borrowed`, true while a call or future borrows @@ -411,10 +416,15 @@ in as a `ByteArray`, bytes returned as their owner, and anything else encoded in such as a function's or an exception's, takes a trailing `_` where a type, the runtime, or an earlier name has it. A keyword is quoted in backticks, and a member named as one Kotlin gives its type takes a trailing `_`. A constructor such as `new` stays a companion function: `Counter.new(1)`. -- **Objects** are classes owning their handle, `AutoCloseable`, which `close()` frees once - no call holds it; a call after it throws. One never closed is closed once the GC - collects it, on a daemon thread a phantom reference queue feeds. A `self` method, or passing the object by - value, closes it once the call returns. `Clone` is `clone()`, and `PartialEq` is `equals`. +- **Objects** are classes owning their handle, `AutoCloseable`. Of a checked type, + `close()` drops the value through Rust's own `_drop`, as Rust's `drop` would: a call + after it throws as for a value moved out, and one while a call borrows the value throws + instead, as Rust refuses that drop, leaving the value to a later `close()`. Of any other + type, `close()` frees the handle, and throws while a call holds it; a call after it + throws. Either way the handle itself is freed once the GC collects the object, on a + daemon thread a phantom reference queue feeds, so each call passes its object to keep + it reachable. A `self` method, or passing the object by value, moves its value out, + after which a call throws. `Clone` is `clone()`, and `PartialEq` is `equals`. A field is a `val`, `firstGet`, and its setter a function, `firstSet(value)`, named as the C ones, since setting an object moves it in and closes its handle, which an assignment would hide. @@ -505,9 +515,9 @@ caller imports it with no ignores. method named as a field takes a trailing `_`. Constructors such as `new` stay static methods: `Counter.new(1)`. - **Objects** are classes owning their handle, freed once their last reference goes, - which a call holds until it returns, so there is no `close()`. A `self` method, or - passing the object by value, moves its value out and closes it once the call returns, - after which a call raises `ValueError`. `Clone` is `clone()`, and `PartialEq` is `==`, unhashable. A field is a + which the caller's reference keeps through a call, so there is no `close()`. A `self` + method, or passing the object by value, moves its value out, after which a call raises + `ValueError`. `Clone` is `clone()`, and `PartialEq` is `==`, unhashable. A field is a property, `first_get`, and its setter a method, `first_set(value)`, named as the C ones, since setting an object moves it in and closes it, which an assignment would hide. - **Checks**: a borrow or move Rust's rules refuse raises, naming the function and the rule, @@ -599,8 +609,8 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod is an overload with and without it, labelled by its name, as a defaulted argument is: `clamped()`, `clamped(low:)`, `clamped(high:)` and `clamped(low:high:)`, up to two per function; `Option<&mut T>` is `inout T?`. `Clone` is `clone()`, and `PartialEq` is `==`, which Swift 6.4 makes - `Equatable`; `Display` is `description` and `Debug` `debugDescription`, which Swift - conforms no noncopyable type to yet. An object that is `Copy`, or thrown as an error, + `Equatable` unless it throws, as below; `Display` is `description` and `Debug` + `debugDescription`, which Swift conforms no noncopyable type to yet. An object that is `Copy`, or thrown as an error, which `Swift.Error` requires be copyable, is a final class instead, whose handle a call holds until it returns, and moving its value out closes it, after which a call throws. A field is a computed property, `firstGet`, and its setter a function, `firstSet(value)`, @@ -610,12 +620,16 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod that is not `Sync` on a second thread, throws a `CheckError`, holding the function and the rule, before any Rust code runs, so the call changed nothing. So every function whose objects can fail a check `throws`, and a field read stays a property whose getter throws, except one of a noncopyable value, - whose getter Swift cannot let throw, which is a method, `firstGet()`. `description`, - `debugDescription`, `==`, `clone()`, and a copy a closure returns, which cannot throw, - trap instead. + whose getter Swift cannot let throw, which is a method, `firstGet()`. Of a type whose + checks can fail, or a record or enum holding one, `description` and `debugDescription` + are getters that throw, and `clone()` and `==` throw, so it conforms to none of + `CustomStringConvertible`, `CustomDebugStringConvertible`, and `Equatable`, whose + requirements cannot throw. A closed class anywhere in a call's arguments throws too. A + copy a closure returns, which has no caller to throw to, traps instead. - **Records** are structs and **enums** Swift enums, with a variant's data as associated values, all `Sendable`. `PartialEq` is `==`, through Rust's; `Display` is `description`, - `Debug` `debugDescription`, and `Default` a static `default()`. + `Debug` `debugDescription`, each throwing as an object's does when it holds a checked + one, and `Default` a static `default()`. - **Bytes** are `Data`. Passed in at a call's top level, Rust reads Swift's own; nested in another value, they are copied in. Bytes Rust returns lend its buffer until Swift releases the `Data`, though `Data` copies a few bytes inline, freeing Rust's at once. @@ -702,20 +716,25 @@ reads a go.mod. named as a Go keyword, a predeclared name, or a package the code uses, take a trailing `_` too, as does a variant's or error's name or any other in a package that another there, or the runtime's, takes. -- **Objects** are pointers to structs owning their handle. `Close()` frees it once no call - holds it, and a second `Close()` does nothing; one never closed is closed once the GC - collects it, through `runtime.AddCleanup`. A call that meets a closed object, as its - receiver or anywhere in its arguments, returns an error matching `rt.ErrClosed`, even when - `Close` races it on another goroutine, so every such call returns an `error`; `String()` - and `GoString()`, which return none, return its text. A `self` method, or passing the object by value, closes - it once the call returns. `Clone` is `Clone()`, and a `pub` field a getter, `FirstGet()`, +- **Objects** are pointers to structs owning their handle, each an `io.Closer`. Of a + checked type, `Close()` drops the value through Rust's own `_drop`, as Rust's `drop` + would: a call after it returns an error matching `rt.ErrClosed`, as for a value moved + out, and a `Close()` while a call on another goroutine borrows the value returns one + matching `rt.ErrBorrowed` instead, as Rust refuses that drop, leaving the value to a + later `Close()`. Of any other type, `Close()` frees the handle, and returns an error + matching `rt.ErrBorrowed` while a call holds it. A second `Close()` does nothing, and the + handle itself is freed once the GC collects the object, through `runtime.AddCleanup`. A + call that meets a closed object, as its receiver or anywhere in its arguments, returns an + error matching `rt.ErrClosed`, so every such call returns an `error`; `String()` and + `GoString()`, which return none, return the error's text. A `self` method, or passing + the object by value, moves its value out. `Clone` is `Clone()`, and a `pub` field a getter, `FirstGet()`, and a setter, `FirstSet(v)`. - **Checks**: a borrow or move Rust's rules refuse returns an error naming the function and the rule, before any Rust code runs, so the call changed nothing: one matching `rt.ErrClosed` for a value moved out, or a lent object that outlived the one it borrows from, and `rt.ErrBorrowed` for any other. So - every function whose objects can fail a check returns an `error`. `String()`, - `GoString()`, and what a closure returns have none to return, so they abort with the - message. + every function whose objects can fail a check returns an `error`. `String()` and + `GoString()` return a failed check's text, having no error to return; what a closure + returns has no caller to return one to, so it aborts with the message. - **Records** are structs, and **plain enums** an `int32` type with a constant per variant (`ShapeSquare`). An enum with data is an interface each variant's struct implements (`EventMoved{V0: p}`), sealed so only the package's do; its methods are the @@ -809,13 +828,16 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass a trailing `_` (`Point.default_()`), and names that clash, such as one the runtime declares or a data enum's variant class, are kept apart as Kotlin's are. The glue's C names are `_dart_`, with a `_` more for each C name of the export already there. -- **Objects** are final classes owning their handle. `close()` frees it once no call - holds it, and a second does nothing; one never closed is freed once the garbage - collector finds it unreachable, through a finalizable handle of Dart's C API. Any call that meets a - closed object, as its receiver or anywhere in its arguments, throws a `StateError` - before Rust sees it, and moves none of the call's other arguments. An isolate cannot - send an object to another, so no `close()` races a call. A `self` method, or passing the - object by value, closes it once the call returns. `Clone` is `clone()`, and a `pub` +- **Objects** are final classes owning their handle. Of a checked type, `close()` drops + the value through Rust's own `_drop`, as Rust's `drop` would: a call after it throws a + `StateError` as for a value moved out, and a `close()` while a call borrows the value, + from a closure that call runs, throws one instead, as Rust refuses that drop. Of any + other type, `close()` frees the handle, and throws while a call holds it. A second + `close()` does nothing, and the handle itself is freed once the garbage collector finds + the object unreachable, through a finalizable handle of Dart's C API. Any call that meets + a closed object, as its receiver or anywhere in its arguments, throws a `StateError` + before Rust sees it. A `self` method, or passing the object by value, moves its value + out. `Clone` is `clone()`, and a `pub` field a getter and a setter. - **Checks**: a borrow or move Rust's rules refuse throws a `StateError` naming the function and the rule, before any Rust code runs, so the call changed nothing. diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index c5de42acc..f229ae2c8 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -593,6 +593,8 @@ impl Gen<'_> { false => self.object_path(*i), }; let value = match (object.copy, self.reserved.get()) { + // One no export can misuse is copied out uncounted, as it is borrowed. + (true, _) if !self.abi.checked[*i] => format!("*unsafe {{ rt::Object::<{path}>::peek({e}, w) }}"), (true, _) => format!( "*{}", self.checked(&format!( From 0c16210115d13e994b48b6d8cecf56e4acbe5ce5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 03:30:19 -0800 Subject: [PATCH 12/30] chore(quest): swift-gives-up is finished; object-ownership profiles from the Rust-like close The Swift within(0) check also waits for the error's resumption, the second race swift-gives-up names. Co-Authored-By: Claude Opus 5.5 --- ffi/tests/api.swift | 3 ++- quest/m0/swift-gives-up.md | 28 -------------------------- quest/m1/ffi-speed/object-ownership.md | 4 +++- 3 files changed, 5 insertions(+), 30 deletions(-) delete mode 100644 quest/m0/swift-gives-up.md diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index c35c9a32d..af2f7186f 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -1020,7 +1020,8 @@ func futures() { threw.value = true } } - q.drain() + // The task's resumption with the error may reach the queue after one drain ends. + until { q.drain(); return threw.value } check(threw.value, "Rust gives up on an async closure") gate.finish() } diff --git a/quest/m0/swift-gives-up.md b/quest/m0/swift-gives-up.md deleted file mode 100644 index dd5650f5a..000000000 --- a/quest/m0/swift-gives-up.md +++ /dev/null @@ -1,28 +0,0 @@ -# [XS] api_swift waits for the error an async closure's timeout raises - -## Goal - -`api_swift` in `ffi/tests/fixtures.rs` passes every run: its "Rust gives up on an async -closure" check in `ffi/tests/api.swift` no longer fails intermittently. - -## Plan - -Found while validating [#740](https://github.com/kixel-ai/rust2/pull/740) on 2026-11-13: -`just ffi test --test fixtures` failed `api_swift` there in 2 of 4 runs, and at `main` -`7008d1d3` in 1 of 5, each time with `"futures" failed: Rust gives up on an async closure`. -The test passes alone. - -Two races fit, and the fix covers both: - -- The closure is empty, so it can finish before the budget runs out: `Pollable.poll` in - `ffi/runtime/rt.swift` starts the closure's Task and then loads `done` again, and - `Within::poll` in `ffi/fixtures/api/src/lib.rs` polls the inner future before checking - its budget. When the Task finishes in that window, `within(0) {}` returns `Ok(())` and - nothing throws. The closure must stay pending, such as by awaiting a stream nothing - yields to, as the check above it does. -- The check reads `threw` after a single `q.drain()`, so it also fails if the task's - resumption with the `Overflow` error reaches the queue after `drain` returned. The check - above it waits with `until { q.drain(); return got.value != 0 }`; wait the same way. - -Confirm which one fails before fixing, such as by delaying the closure's Task or the -resumption, and fix the test's timing rather than retrying it. diff --git a/quest/m1/ffi-speed/object-ownership.md b/quest/m1/ffi-speed/object-ownership.md index c5c3e6b4d..aba533f9c 100644 --- a/quest/m1/ffi-speed/object-ownership.md +++ b/quest/m1/ffi-speed/object-ownership.md @@ -17,7 +17,9 @@ Ownership stays automatic: a dropped object is still freed without a call from t (`ffi/src/go.rs`'s `own_`). So take candidates from the profile. One left: allocate the wrapper and its handle together, so a return allocates once. - Explicit `Close` stays, and the cleanup stays as the backstop; dropping it is refused, - since a missed `Close` would leak. + since a missed `Close` would leak. Since checks-raise (#730), a checked type's `Close` + drops the value through Rust's `_drop` and keeps no hold count of its own, so the + cleanup alone frees the handle; profile from there. - Re-measure the bench's object return and stamped write. ## Related From 527edd16f62b5d87eb4a85c88027223d1c5622c3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 04:25:16 -0800 Subject: [PATCH 13/30] fix(ffi): C++ passes a record's trait status; the fixture's Seat covers each binding A record or data enum holding a checked handle takes a status in _eq, _to_string, and _debug, which C++'s operators left out. Seat, holding a Ticket, runs both through every binding against Rust's transcript. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/examples/transcript.rs | 3 +++ ffi/fixtures/api/src/lib.rs | 13 +++++++++ ffi/moq/bench/export.c | 36 ++++++++++++------------- ffi/src/cpp.rs | 19 ++++++++++--- ffi/tests/api.c | 8 ++++++ ffi/tests/api.cpp | 6 +++++ ffi/tests/api.dart | 4 +++ ffi/tests/api.go | 3 +++ ffi/tests/api.kt | 4 +++ ffi/tests/api.py | 3 +++ ffi/tests/api.swift | 4 +++ 11 files changed, 81 insertions(+), 22 deletions(-) diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 1edb74dd6..524482ab9 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -735,6 +735,9 @@ fn main() { let given = api::reissue(api::Ticket::new(7), |t| api::Ticket::new(t.number() + 1)).number(); let held = api::reissue(api::Ticket::new(7), |_| kept).number(); println!("reissued {given} {held}, cloned {}", api::Ticket::clones()); + // A record's traits borrow the checked handle it holds. + let (front, back) = (api::seat(kept, 1), api::seat(kept, 2)); + println!("seat {front:?} {} {}", front == front.clone(), front == back); // A kept closure, called on another thread after the call that took it returned. println!("spawned {}", api::Spawned::new(|| 7).join()); // A kept closure: called again at the instant it asks for, then once its waiter wakes. diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 5faf446d3..56d032615 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -725,6 +725,19 @@ pub fn reissue_lent(ticket: &Ticket, swap: impl Fn(&Ticket) -> Ticket) -> Ticket swap(ticket) } +/// A record holding a `Copy` object a closure is lent, so its `PartialEq` and `Debug` +/// check that object's handle. +#[derive(Clone, Debug, PartialEq)] +pub struct Seat { + pub ticket: Ticket, + pub row: u32, +} + +/// The seat in `row` for `ticket`. +pub fn seat(ticket: Ticket, row: u32) -> Seat { + Seat { ticket, row } +} + /// What `check` returns, a closure with no value but its error. pub fn attempt(check: impl FnOnce() -> Result<(), String>) -> Result<(), String> { check() diff --git a/ffi/moq/bench/export.c b/ffi/moq/bench/export.c index 01e679f46..bc0f59222 100644 --- a/ffi/moq/bench/export.c +++ b/ffi/moq/bench/export.c @@ -55,7 +55,7 @@ static const moq_waker waker = {0}; static volatile unsigned sink; static moq_group_Consumer *recv_group(moq_track_Subscriber *subscriber) { - moq_track_Subscriber_recv_group_future *receiving = moq_track_Subscriber_recv_group(subscriber); + moq_track_Subscriber_recv_group_future *receiving = moq_track_Subscriber_recv_group(subscriber, NULL); moq_group_Consumer *reader = NULL; moq_Error *err = NULL; if (moq_track_Subscriber_recv_group_poll(receiving, waker, &reader, &err) != MOQ_POLL_READY || reader == NULL) { @@ -66,7 +66,7 @@ static moq_group_Consumer *recv_group(moq_track_Subscriber *subscriber) { } static moq_frame_Frame *read_frame(moq_group_Consumer *reader) { - moq_group_Consumer_read_frame_future *reading = moq_group_Consumer_read_frame(reader); + moq_group_Consumer_read_frame_future *reading = moq_group_Consumer_read_frame(reader, NULL); moq_frame_Frame *read = NULL; moq_Error *err = NULL; if (moq_group_Consumer_read_frame_poll(reading, waker, &read, &err) != MOQ_POLL_READY || read == NULL) { @@ -79,7 +79,7 @@ static moq_frame_Frame *read_frame(moq_group_Consumer *reader) { static moq_group_Producer *append_group(moq_track_Producer *track) { moq_group_Producer *group = NULL; moq_Error *err = NULL; - if (!moq_track_Producer_append_group(track, &group, &err)) { + if (!moq_track_Producer_append_group(track, &group, &err, NULL)) { fail("append_group"); } return group; @@ -87,14 +87,14 @@ static moq_group_Producer *append_group(moq_track_Producer *track) { static void write_frame(moq_group_Producer *group, const moq_Timestamp *at, moq_buf frame) { moq_Error *err = NULL; - if (!moq_group_Producer_write_frame(group, at, frame, &err)) { + if (!moq_group_Producer_write_frame(group, at, frame, &err, NULL)) { fail("write_frame"); } } static void finish(moq_group_Producer *group) { moq_Error *err = NULL; - if (!moq_group_Producer_finish(group, &err)) { + if (!moq_group_Producer_finish(group, &err, NULL)) { fail("finish"); } moq_group_Producer_free(group); @@ -114,7 +114,7 @@ static span throughput(moq_track_Producer *track, moq_track_Subscriber *subscrib for (int f = 0; f < FRAMES; f++) { write_frame(group, at, frame); moq_frame_Frame *read = read_frame(reader); - moq_buf bytes = moq_frame_Frame_payload_get(read); + moq_buf bytes = moq_frame_Frame_payload_get(read, NULL); *sum += bytes.ptr[0]; moq_buf_free(bytes); moq_frame_Frame_free(read); @@ -129,16 +129,16 @@ static span throughput(moq_track_Producer *track, moq_track_Subscriber *subscrib static void round_(int print) { moq_broadcast_Info *binfo = moq_broadcast_Info_new(); - moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(binfo); + moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(binfo, NULL); moq_broadcast_Info_free(binfo); moq_Timescale *micro = moq_Timescale_MICRO(); /* Builders take the handle's value, leaving the handle to free. */ moq_track_Info *defaults = moq_track_Info_default(); - moq_track_Info *info = moq_track_Info_with_timescale(defaults, micro); + moq_track_Info *info = moq_track_Info_with_timescale(defaults, micro, NULL); moq_track_Info_free(defaults); moq_track_Producer *track = NULL; moq_Error *err = NULL; - if (!moq_broadcast_Producer_create_track(broadcast, str("bench"), info, &track, &err)) { + if (!moq_broadcast_Producer_create_track(broadcast, str("bench"), info, &track, &err, NULL)) { fail("create_track"); } moq_track_Info_free(info); @@ -150,15 +150,15 @@ static void round_(int print) { start = now(); for (int i = 0; i < N; i++) { - sum += moq_track_Producer_is_closed(track); + sum += moq_track_Producer_is_closed(track, NULL); } span call = per(start, N); moq_group_Producer *demanded = append_group(track); - moq_group_Demand *demand = moq_group_Producer_demand(demanded); + moq_group_Demand *demand = moq_group_Producer_demand(demanded, NULL); start = now(); for (int i = 0; i < N; i++) { - sum += (unsigned)moq_group_Demand_sequence(demand); + sum += (unsigned)moq_group_Demand_sequence(demand, NULL); } span getter = per(start, N); moq_group_Demand_free(demand); @@ -166,13 +166,13 @@ static void round_(int print) { start = now(); for (int i = 0; i < N; i++) { - sum += moq_track_Producer_largest(track).some; + sum += moq_track_Producer_largest(track, NULL).some; } span record = per(start, N); start = now(); for (int i = 0; i < N; i++) { - moq_track_Info_free(moq_track_Producer_info(track)); + moq_track_Info_free(moq_track_Producer_info(track, NULL)); } span object = per(start, N); @@ -209,8 +209,8 @@ static void round_(int print) { } span write = per(start, N); - moq_track_Consumer *consumer = moq_track_Producer_consume(track); - moq_track_Consumer_subscribe_future *subscribing = moq_track_Consumer_subscribe(consumer, NULL); + moq_track_Consumer *consumer = moq_track_Producer_consume(track, NULL); + moq_track_Consumer_subscribe_future *subscribing = moq_track_Consumer_subscribe(consumer, NULL, NULL); moq_track_Subscriber *subscriber = NULL; if (moq_track_Consumer_subscribe_poll(subscribing, waker, &subscriber, &err) != MOQ_POLL_READY) { fail("subscribe"); @@ -242,7 +242,7 @@ static void round_(int print) { moq_group_Producer *group = append_group(track); moq_group_Consumer *reader = recv_group(subscriber); for (int f = 0; f < FRAMES; f++) { - moq_group_Consumer_read_frame_future *reading = moq_group_Consumer_read_frame(reader); + moq_group_Consumer_read_frame_future *reading = moq_group_Consumer_read_frame(reader, NULL); moq_frame_Frame *read = NULL; if (moq_group_Consumer_read_frame_poll(reading, waker, &read, &err) != MOQ_POLL_PENDING) { fail("woken read_frame ready"); @@ -262,7 +262,7 @@ static void round_(int print) { /* The last frame's 100 bytes, owned without a copy, then freed. */ start = now(); for (int i = 0; i < N; i++) { - moq_buf bytes = moq_frame_Frame_payload_get(last); + moq_buf bytes = moq_frame_Frame_payload_get(last, NULL); sum += bytes.ptr[0]; moq_buf_free(bytes); } diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 7b082fa7c..8aa8101ff 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -230,6 +230,15 @@ impl<'a> Header<'a> { } /// The arena a body lowering each of `tys` declares, if one needs it. + /// The status a record's or enum's trait function takes, if a handle in it is checked: + /// null, so a failed check aborts, as each other call of C++'s does. + fn value_status(&self, ty: &Type) -> &'static str { + match self.abi.value_status(ty) { + true => ", nullptr", + false => "", + } + } + fn scope<'t>(&self, tys: impl IntoIterator) -> String { match tys.into_iter().any(|ty| self.arena(ty)) { true => format!( @@ -704,7 +713,7 @@ impl<'a> Header<'a> { let d = self.d(); let (value, status) = match of { Ok(status) => (None, status), - Err(ty) => (Some(ty), ""), + Err(ty) => (Some(ty), self.value_status(ty)), }; let object = value.is_none(); let callable = match object { @@ -873,10 +882,11 @@ impl<'a> Header<'a> { "\tbool operator==(const {name} &other) const;\n\tbool operator!=(const {name} &other) const {{\n\t\treturn !(*this == other);\n\t}}" )); self.def(format!( - "inline bool {name}::operator==(const {name} &other) const {{\n{}\tauto c_a_ = {};\n\tauto c_b_ = {};\n\treturn ::{c}_eq(&c_a_, &c_b_);\n}}\n", + "inline bool {name}::operator==(const {name} &other) const {{\n{}\tauto c_a_ = {};\n\tauto c_b_ = {};\n\treturn ::{c}_eq(&c_a_, &c_b_{});\n}}\n", self.scope([ty]), self.lower(ty, "*this"), self.lower(ty, "other"), + self.value_status(ty), )); } self.traits(&name, &c, t, Err(ty), &mut body, &mut members); @@ -935,16 +945,17 @@ impl<'a> Header<'a> { let (a, b, this) = (lower("a"), lower("b"), lower("self")); let scope = self.scope([&ty]); let t = e.traits; + let status = self.value_status(&ty); if t.eq { let (a_, b_) = (declare(¶m, "a"), declare(¶m, "b")); self.def(format!( - "inline bool operator==({a_}, {b_}) {{\n{scope}\tauto c_a_ = {a};\n\tauto c_b_ = {b};\n\treturn ::{c}_eq(&c_a_, &c_b_);\n}}\n\ninline bool operator!=({a_}, {b_}) {{\n\treturn !(a == b);\n}}\n" + "inline bool operator==({a_}, {b_}) {{\n{scope}\tauto c_a_ = {a};\n\tauto c_b_ = {b};\n\treturn ::{c}_eq(&c_a_, &c_b_{status});\n}}\n\ninline bool operator!=({a_}, {b_}) {{\n\treturn !(a == b);\n}}\n" )); } for (on, trait_name) in [(t.display, "to_string"), (t.debug, "debug")] { if on { self.def(format!( - "inline ::{ns}::buf {name}_{trait_name}({}) {{\n{scope}\tauto c_self_ = {this};\n\treturn {d}::access::adopt(::{c}_{trait_name}(&c_self_));\n}}\n", + "inline ::{ns}::buf {name}_{trait_name}({}) {{\n{scope}\tauto c_self_ = {this};\n\treturn {d}::access::adopt(::{c}_{trait_name}(&c_self_{status}));\n}}\n", declare(¶m, "self"), d = self.d() )); diff --git a/ffi/tests/api.c b/ffi/tests/api.c index ce5872e23..6fd2619e9 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -286,6 +286,14 @@ static bool closures(void) { api_Ticket *held = api_reissue(seven, reissue_kept, kept, NULL); printf("reissued %u %u, cloned %u\n", (unsigned)api_Ticket_number(given, NULL), (unsigned)api_Ticket_number(held, NULL), (unsigned)api_Ticket_clones()); + /* A record's traits borrow the checked handle it holds. */ + api_Seat front = api_seat(kept, 1, NULL); + api_Seat back = api_seat(kept, 2, NULL); + print("seat ", api_Seat_debug(&front, NULL), ""); + printf(" %s %s\n", api_Seat_eq(&front, &front, NULL) ? "true" : "false", + api_Seat_eq(&front, &back, NULL) ? "true" : "false"); + api_Seat_free(&back); + api_Seat_free(&front); api_Ticket_free(held); api_Ticket_free(given); api_Ticket_free(seven); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 84c3f8f34..ad54b0516 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -1055,6 +1055,12 @@ int script() { }).number(); std::printf("reissued %u %u, cloned %u\n", static_cast(given), static_cast(held), static_cast(api::Ticket::clones())); + // A record's traits borrow the checked handle it holds. + api::Ticket nine = api::Ticket::new_(9); + api::Seat front = api::seat(nine, 1); + api::Seat back = api::seat(nine, 2); + print("seat ", front.debug(), ""); + std::printf(" %s %s\n", front == front ? "true" : "false", front == back ? "true" : "false"); } // A kept closure, called on another thread after the call that took it returned. std::printf("spawned %u\n", static_cast(api::Spawned::new_([] { return 7; }).join())); diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index f13390da9..8066df419 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -663,6 +663,10 @@ Future script() async { final renewed = api.reissue(api.Ticket(7), (t) => api.Ticket(t.number() + 1)).number(); final copied = api.reissue(api.Ticket(7), (_) => nine).number(); print('reissued $renewed $copied, cloned ${api.Ticket.clones()}'); + // A record's traits borrow the checked handle it holds. + final front = api.seat(nine, 1); + final back = api.seat(nine, 2); + print('seat ${front.debug()} ${front == api.seat(nine, 1)} ${front == back}'); // A kept closure, called on another thread after the call that took it returned. final spawned = api.Spawned(() => 7); print('spawned ${spawned.join()}'); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 8920b64ba..128564775 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -757,6 +757,9 @@ func script() { renewed := must(api.Reissue(seven, func(t *api.Ticket) *api.Ticket { return api.NewTicket(must(t.Number()) + 1) })) copied := must(api.Reissue(seven, func(*api.Ticket) *api.Ticket { return nine })) fmt.Printf("reissued %d %d, cloned %d\n", must(renewed.Number()), must(copied.Number()), api.TicketClones()) + // A record's traits borrow the checked handle it holds. + front, back := must(api.NewSeat(nine, 1)), must(api.NewSeat(nine, 2)) + fmt.Printf("seat %s %t %t\n", front.GoString(), must(front.Equal(front)), must(front.Equal(back))) for _, ticket := range []*api.Ticket{copied, renewed, seven, nine} { ticket.Close() } diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 0ffe126e6..44772b998 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -555,6 +555,10 @@ fun script() { val given = reissue(Ticket.new(7u)) { t -> Ticket.new(t.number() + 1u) }.number() val held = reissue(Ticket.new(7u)) { nine }.number() println("reissued $given $held, cloned ${Ticket.clones()}") + // A record's traits borrow the checked handle it holds. + val front = seat(nine, 1u) + val back = seat(nine, 2u) + println("seat ${front.debug()} ${front == front.copy()} ${front == back}") // A kept closure, called on another thread after the call that took it returned. println("spawned ${Spawned.new { 7u }.join()}") // A kept closure: called again at the instant it asks for, then once its waiter wakes. diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 76bdbb15b..9c8d87903 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -633,6 +633,9 @@ async def polls(): given = api.reissue(api.Ticket.new(7), lambda t: api.Ticket.new(t.number() + 1)).number() held = api.reissue(api.Ticket.new(7), lambda _: kept).number() print(f"reissued {given} {held}, cloned {api.Ticket.clones()}") + # A record's traits borrow the checked handle it holds. + front, back = api.seat(kept, 1), api.seat(kept, 2) + print(f"seat {front!r} {str(front == api.seat(kept, 1)).lower()} {str(front == back).lower()}") # A kept closure, called on another thread after the call that took it returned. print(f"spawned {api.Spawned.new(lambda: 7).join()}") # A kept closure: called again at the instant it asks for, then once its waiter wakes. diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index af2f7186f..fd768c2e4 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -755,6 +755,10 @@ func script() { let given = try! reissue(Ticket.new(7)) { Ticket.new(try! $0.number() + 1) }.number() let held = try! reissue(Ticket.new(7)) { _ in nine }.number() print("reissued \(given) \(held), cloned \(Ticket.clones())") + // A record's traits borrow the checked handle it holds. + let front = try! seat(nine, 1) + let back = try! seat(nine, 2) + print("seat \(try! front.debugDescription) \(try! front == front) \(try! front == back)") // A kept closure, called on another thread after the call that took it returned. print("spawned \(try! Spawned.new { 7 }.join())") // A kept closure: called again at the instant it asks for, then once its waiter wakes. From b1854f4a85e5c257f96cb03ea0cc3d8f39e18013 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 04:32:09 -0800 Subject: [PATCH 14/30] fix(ffi): the moq bench passes C's statuses and tries Swift's throwing calls Its C program missed the status each checked call now takes, and Swift's calls on checked handles now throw. Co-Authored-By: Claude Opus 5.5 --- ffi/moq/bench/export.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ffi/moq/bench/export.swift b/ffi/moq/bench/export.swift index e95a8935e..2a659d408 100644 --- a/ffi/moq/bench/export.swift +++ b/ffi/moq/bench/export.swift @@ -80,35 +80,35 @@ func throughput(_ track: TrackProducer, _ subscriber: inout TrackSubscriber, _ a func round() throws -> ([(String, Span)], Int) { var out: [(String, Span)] = [] - let broadcast = BroadcastInfo.new().produce() + let broadcast = try BroadcastInfo.new().produce() let track = try broadcast.createTrack("bench", TrackInfo.default().withTimescale(Timescale.MICRO)) let frame = Data(count: 100) var sum = 0 var start = now() - for _ in 0.. ([(String, Span)], Int) { // The last frame's 100 bytes, copied into a Data, and one of them read. start = now() for _ in 0.. Date: Fri, 13 Nov 2026 04:57:02 -0800 Subject: [PATCH 15/30] perf(ffi): Dart's calls share one status cell per isolate The status each call allocated from its arena cost a checked call about 4 ns in the bench's handle call and u64 getter. Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.dart | 4 ++++ ffi/src/dart.rs | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index abcac25f7..43df8aa59 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -534,6 +534,10 @@ final _n = _Native(); final class _Native { late final _Stack stack = _Stack(); + + /// Where a call that can fail a check writes the failure: one for the isolate, since a + /// call reads it as it returns, before any other can run, and a failure clears it. + late final ffi.Pointer status = _dartAlloc(4).cast()..value = 0; late final isolate.RawReceivePort _port; late final ffi.Pointer home; final _Waker idle = ffi.Struct.create<_Waker>(); diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 4d3e635f4..ba7fde6a2 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -119,7 +119,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result>> _target) & 1 == 0) {{\n\t\tthrow UnsupportedError(\"$what: it is not on this library's target\");\n\t}}\n}}\n\n\ /// Throws unless [status], which the call [what] wrote, is zero: a run-time check failed.\n\ - void _status(int status, String what) {{\n\tif (status == 0) return;\n\tfinal m = _statusMessage(status);\n\tvar n = 0;\n\twhile (m[n] != 0) n++;\n\tthrow StateError('$what: ${{const convert.Utf8Decoder().convert(m.asTypedList(n))}}');\n}}\n\n" + void _status(int status, String what) {{\n\tif (status == 0) return;\n\t_n.status.value = 0;\n\tfinal m = _statusMessage(status);\n\tvar n = 0;\n\twhile (m[n] != 0) n++;\n\tthrow StateError('$what: ${{const convert.Utf8Decoder().convert(m.asTypedList(n))}}');\n}}\n\n" ); for (name, params, ret, dart_params, dart_ret) in [ ("contract", "", "ffi.Uint64", "", "int"), @@ -1354,7 +1354,7 @@ impl<'a> Gen<'a> { let call = |f: &str, args: &str, what: &str, lift: &dyn Fn(&str) -> String| -> String { match status { true => format!( - "final s_ = a_()..value = 0;\n\t\t\tfinal r_ = {f}(_n.home, {args}, s_);\n\t\t\t_status(s_.value, '{what}');\n\t\t\treturn {};", + "final s_ = _n.status;\n\t\t\tfinal r_ = {f}(_n.home, {args}, s_);\n\t\t\t_status(s_.value, '{what}');\n\t\t\treturn {};", lift("r_") ), false => format!("return {};", lift(&format!("{f}(_n.home, {args})"))), @@ -1610,7 +1610,7 @@ impl<'a> Gen<'a> { let what = format!("{c}_drop"); let f = self.served_fn(&what, vec![owner.clone(), Ty::Out(Box::new(Ty::Status))], Ty::Void); body.push(format!( - "\t@override\n\tvoid _drop() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\tfinal s_ = a_()..value = 0;\n\t\t\t{f}(_n.home, _raw, s_);\n\t\t\t_status(s_.value, '{what}');\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + "\t@override\n\tvoid _drop() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\tfinal s_ = _n.status;\n\t\t\t{f}(_n.home, _raw, s_);\n\t\t\t_status(s_.value, '{what}');\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" )); } self.traits(&ty, &c, object.traits, &mut body, &mut members); @@ -2049,7 +2049,7 @@ impl<'a> Gen<'a> { let status = export.call.status().is_some(); let check = format!("_status(s_.value, '{c_name}');"); if status { - pre.push("final s_ = a_()..value = 0;".to_string()); + pre.push("final s_ = _n.status;".to_string()); } // A call this target lacks throws before any argument is lowered or given to Rust. if !abi.api.everywhere(self.on) { From d9f1ee71a261142ac6022fd907fb971b5a6f3fe6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 05:15:57 -0800 Subject: [PATCH 16/30] docs: bench the per-handle rows, and leave checks-raise the handle-free decision Measured on desktop against main: the getter and handle call gain in Kotlin and Go, while Go's object return and Go's and Kotlin's stamped write lose to the GC freeing each closed handle. object-ownership now requires checks-raise. Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 40 ++++++++++++----- quest/m1/ffi-export/checks-raise.md | 59 ++++++++++++-------------- quest/m1/ffi-speed/object-ownership.md | 12 ++++-- 3 files changed, 65 insertions(+), 46 deletions(-) diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 957020807..ef4b188f7 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -157,23 +157,41 @@ frame path: 7. **The binding's own call.** Over C's getter: Swift adds 8 ns (its hold count's two atomics, since dropped from all but its classes, as below), Kotlin 14 wall (JNI and its hold count), Dart 19 (an `_Arena` per call and its hold), Go 31 (cgo, `defer`, and its hold), and Python 37 (argument parsing and the - GIL released and taken back, so Rust may call Python from another thread). BoltFFI's + GIL released and taken back, so Rust may call Python from another thread); each hold + count is since dropped for a checked type, as below. BoltFFI's Python getter, which holds the GIL, measured 63 ns. ### Per-handle baseline For [checks-raise](/quest/m1/ffi-export/checks-raise.md)'s one accounting: the u64 getter is one borrowed handle and nothing else, so its row less Rust's 0.4 ns is what a -handle costs a call. C, C++: 10 ns, the runtime's two atomics. Swift: 18, its hold count -adding two more. Kotlin: 24 wall, Go 41, Dart 29, Python 47, each with its call's own -transition besides. - -Swift has since made each object a noncopyable struct and dropped its hold count, but for -a class: a `Copy` object or one thrown as an error. Measured 2026-11-11 at that change, 5 -rounds interleaved with C at a load average of 7 to 12, its getter costs 10 ns, as C's -does, and `track.is_closed()`, on `TrackProducer`, still a class since moq throws it, 26 -against C's 18, as in the table. A change to the accounting moves this row and the handle call's by the -same amount; measure both, CPU first, at the load beside them. +handle costs a call. Measured 2026-11-13 on desktop at a load average of 2.4 to 4.1, +main at `7b0958b3` against [#730](https://github.com/kixel-ai/rust2/pull/730), run +alternately: 12 rounds of main and 6 of #730, median wall ns (CPU ns where it differs). +#730 drops the hold counts Kotlin, Go, Python, and Dart kept beside the runtime's, so a +checked call borrows through the runtime's two atomics alone, and close drops the value +through Rust, leaving the handle to the GC. + +| row | C | Kotlin | Python | Swift | Go | Dart | +|---|---|---|---|---|---|---| +| handle call, main | 18 | 31 (60) | 54 | 26 | 47 | 41 (45) | +| handle call, #730 | 18 | 24 (45) | 53 | 26 | 40 | 42 (45) | +| u64 getter, main | 10 | 23 (45) | 48 | 10 | 39 | 27 | +| u64 getter, #730 | 10 | 15 (30) | 47 | 10 | 33 | 28 | +| object return, main | 29 | 87 (190) | 99 | 37 | 183 (198) | 238 | +| object return, #730 | 30 | 94 (190) | 98 | 38 | 691 (2,080) | 214 | +| stamped write, main | 159 | 310 (340) | 337 | 205 | 429 (447) | 433 | +| stamped write, #730 | 168 | 613 (2,165) | 332 | 212 | 1,061 (2,533) | 450 | + +The getter and the handle call gain what the counts cost: 8 ns in Kotlin and 5 to 7 in +Go. The object return and the stamped write lose more in Go and Kotlin: a handle closed or +moved out of is freed only once the GC finds its object unreachable, so its cleanup, or +its phantom reference, stays registered, where a count let close free it at once. Go's +object return with `Close` also stopping the cleanup, unsafe without a count, measured +205 ns, so the cleanup is the whole loss. Dart's checked calls first lost 4 ns to a +status allocated per call, now one cell per isolate. Swift's getter costs C's since it +dropped its hold count for each noncopyable object (2026-11-11). A change to the +accounting moves these rows; measure them at the load beside them. ### Against the targets diff --git a/quest/m1/ffi-export/checks-raise.md b/quest/m1/ffi-export/checks-raise.md index 248ca2597..5d1237167 100644 --- a/quest/m1/ffi-export/checks-raise.md +++ b/quest/m1/ffi-export/checks-raise.md @@ -5,45 +5,42 @@ A run-time check a [rust2-ffi](/ffi/README.md) binding fails raises the language's own error and leaves the process running, everywhere the binding has a caller to raise to, and each handle's borrows are counted once per call, in Rust: the bindings keep no hold count -of their own beside the runtime's. +of their own beside the runtime's, and closing or moving out of an object costs no more +than the count it replaced. ## Plan Decided with the maintainer on 2026-11-09 and 2026-11-12, in the quest audits: checks raise and panics abort; the one accounting is the runtime's, in `ffi/runtime/rt.rs`, whose -failed check returns a status the binding raises from; and the exporter decides at export -time which types need a check. - -What the code does now: - -- A function whose handles can fail a check takes a trailing `_status *status`; - NULL aborts with the message, as C++ passes. Kotlin throws `IllegalStateException`, - Python `ValueError` or `RuntimeError`, Dart `StateError`, Swift `CheckError`, and Go - returns an error matching `rt.ErrClosed` or `rt.ErrBorrowed`. An item off the target - throws in Swift and returns `errors.ErrUnsupported` in Go. A type no export borrows - exclusively, moves, or lends, and that is `Sync`, is borrowed uncounted (`Object::peek`). -- Still aborting where a caller exists: Swift's `description`, `debugDescription`, `==`, - and `clone()` of a checked object, which its protocols cannot let throw; Go's `String()` - and `GoString()`; a record's or data enum's trait functions (`_eq`, `_to_string`, - `_debug`) whose value holds a checked handle; and Swift's `Arena.hold`, which traps on a - closed class handle. -- Kotlin, Go, Python, and Dart keep a hold count of their own beside the runtime's, so - close waits for running calls; a checked call costs four atomic operations per handle. - Dropping them needs the Rust allocation freed only once the binding's GC releases the - object, with close dropping the value instead: a moved-out state the runtime already - has. - -Measure the bench's handle call and u64 getter rows before and after, on an idle host. - -Public API: the bindings' close semantics, if close stops waiting for a running call. +failed check returns a status the binding raises from; the exporter decides at export +time which types need a check; and close in Kotlin, Go, Python, and Dart is Rust-like, +taking the value through the runtime's `TAKEN`, so a call after it raises `moved` and a +close racing a call raises in whichever loses. + +[#730](https://github.com/kixel-ai/rust2/pull/730) does all of that. Every place with a +caller raises, Swift's `description`, `==`, and `clone()` included; what a closure returns +still aborts on a failed check, since Rust, its caller, has no error to take. + +What is left is the handle itself. With no count of its own, a binding cannot tell when a +call on another thread has read the handle but not yet borrowed it in Rust, so the handle +a close or a move empties is freed only once the GC finds its object unreachable. Go +leaves its `runtime.AddCleanup` registered and Kotlin its phantom reference, and the +[bench](/bench/ffi/README.md#per-handle-baseline) measures it: Go's object return went from +183 to 691 ns (2,080 CPU) and its stamped write from 429 to 1,061; Kotlin's stamped write +from 310 to 613 (2,165 CPU). The getter gained 8 ns in Kotlin and 6 in Go. Python frees by +reference count and Dart's finalizer costs no more, so neither lost. + +Public API: none beyond #730's. ## Waiting -- Maintainer: whether close in Kotlin, Go, Python, and Dart drops the value at once and leaves the - handle's memory to the GC, failing a running call's later uses with `moved`, so the - bindings' hold counts go. Recommended: yes; it is the runtime's existing `TAKEN` state. +- Maintainer: how Go and Kotlin free a handle a close or a move emptied. Recommended: each + keeps a count for a checked type only to free the handle once no call holds it, with + close staying Rust-like; it gives back the getter's 6 to 8 ns to win back 300 to 500 ns + per object returned or stamped, which moq pays per frame. The other way is to keep #730 + as it is and look for a cheaper deferred free under + [object-ownership](/quest/m1/ffi-speed/object-ownership.md). ## Related -- [Copy types cross by value](/quest/m1/ffi-export/copy-values.md) - landing it first leaves only thrown errors as Swift classes -- [Cheaper returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) - rewrites the same handle word, so its baselines move with this +- [Copy types cross by value](/quest/m1/ffi-export/copy-values.md) - landing it first leaves only thrown errors as Swift classes, and the stamped write without a `Timestamp` object diff --git a/quest/m1/ffi-speed/object-ownership.md b/quest/m1/ffi-speed/object-ownership.md index aba533f9c..6e022a472 100644 --- a/quest/m1/ffi-speed/object-ownership.md +++ b/quest/m1/ffi-speed/object-ownership.md @@ -17,13 +17,17 @@ Ownership stays automatic: a dropped object is still freed without a call from t (`ffi/src/go.rs`'s `own_`). So take candidates from the profile. One left: allocate the wrapper and its handle together, so a return allocates once. - Explicit `Close` stays, and the cleanup stays as the backstop; dropping it is refused, - since a missed `Close` would leak. Since checks-raise (#730), a checked type's `Close` - drops the value through Rust's `_drop` and keeps no hold count of its own, so the - cleanup alone frees the handle; profile from there. + since a missed `Close` would leak. Since #730, a checked type's `Close` drops the value + through Rust's `_drop` and keeps no hold count, so the cleanup alone frees the handle, + which costs Go's object return 500 ns; how that changes is checks-raise's open + decision. - Re-measure the bench's object return and stamped write. +## Required + +- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - rewrites the same handle word and decides when a closed handle is freed; profile after it lands, or the baselines move + ## Related -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - rewrites the same handle word; profile after it lands, or the baselines move - [Copy values](/quest/m1/ffi-export/copy-values.md) - removes the per-frame `Timestamp` object, the other half of Go's and Dart's stamped write From 530fc0dabf0299500043929c68ab33844e85405a Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:44:24 -0800 Subject: [PATCH 17/30] fix(ffi): Go and Kotlin count a checked handle's calls, so close and a move free it at once Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.go | 49 ++++++++------- ffi/runtime/rt.kt | 65 +++++++++---------- ffi/src/collect.rs | 29 +++------ ffi/src/dart.rs | 6 +- ffi/src/go.rs | 88 +++++++++++++++++++++----- ffi/src/kotlin.rs | 142 +++++++++++++++++++++--------------------- ffi/tests/api.go | 46 +++++++------- ffi/tests/api.kt | 23 ++++--- ffi/tests/fixtures.rs | 12 +--- 9 files changed, 256 insertions(+), 204 deletions(-) diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index a09b579cf..f786203ae 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -58,13 +58,11 @@ func (e closedError_) Is(target error) bool { return target == ErrClosed } -// Handle_ is what an object owns, or is lent for a call. One of a value Rust checks, which -// drop drops, is Rust's alone to account: Close drops the value as Rust's own drop does, -// failing while a call borrows it, a call after it fails as Rust's check of a moved value -// does, and the handle is freed once the GC collects the object, which each call keeps -// reachable. Any other counts its calls: Close frees it then, failing while a call holds -// it, and a call after it returns ErrClosed. For the generated packages, which share it: -// use none of it. +// Handle_ is what an object owns, or is lent for a call: each call holds it, so it is +// freed only once no call does, and a call after Close returns ErrClosed. Close of one +// whose value Rust checks, which drop drops, drops the value as Rust's own drop does, +// failing while a call borrows it in Rust; of any other, it fails while a call holds it. +// For the generated packages, which share it: use none of it. type Handle_ struct { raw unsafe.Pointer state atomic.Int64 @@ -78,7 +76,7 @@ type Handle_ struct { } // NewHandle_ is a handle of raw, which free frees, or nil for one Rust lends, and drop -// drops the value of, or nil for one Go counts. +// drops the value of, or nil for one Rust does not check. func NewHandle_(raw unsafe.Pointer, free func(unsafe.Pointer), drop func(unsafe.Pointer) error, name string) *Handle_ { h := &Handle_{raw: raw, free: free, drop: drop, name: name} h.state.Store(held_) @@ -87,9 +85,6 @@ func NewHandle_(raw unsafe.Pointer, free func(unsafe.Pointer), drop func(unsafe. // Hold is the handle, held until Release, or nil once closed. func (h *Handle_) Hold() unsafe.Pointer { - if h.drop != nil { - return h.raw - } for { n := h.state.Load() if n&closed_ != 0 { @@ -108,7 +103,7 @@ func (h *Handle_) Closed() error { // Release gives up a call's hold. func (h *Handle_) Release() { - if h.drop == nil && h.state.Add(-held_) < held_ && h.free != nil { + if h.state.Add(-held_) < held_ && h.free != nil { h.free(h.raw) } } @@ -116,7 +111,17 @@ func (h *Handle_) Release() { // Close closes h as Handle_ says; again, it does nothing. func (h *Handle_) Close() error { if h.drop != nil { - return h.drop(h.raw) + raw := h.Hold() + if raw == nil { + return nil + } + err := h.drop(raw) + h.Release() + if err != nil { + return err + } + h.Moved() + return nil } for { n := h.state.Load() @@ -137,18 +142,20 @@ func (h *Handle_) Close() error { } } -// Collected frees an owned handle once the GC collects its object: at once if Rust -// accounts it, since no call runs on an object nothing reaches, or else once no call holds -// it. +// Moved closes an owned handle a call moved the value out of, or Close dropped, freeing it +// once no call holds it. +func (h *Handle_) Moved() { + // A cleanup left registered until the GC slows each one registered after it. + h.Cleanup.Stop() + h.abandon() +} + +// Collected frees an owned handle the GC collected the object of, once no call holds it. func (h *Handle_) Collected() { - if h.drop != nil { - h.free(h.raw) - return - } h.abandon() } -// abandon gives up the owner's hold of a counted one, freeing it once no call holds it. +// abandon gives up the owner's hold, freeing it once no call holds it. func (h *Handle_) abandon() { for { n := h.state.Load() diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index b02af0ff4..a35c76f6b 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -119,19 +119,14 @@ class Writer { private var held: java.util.ArrayList? = null - /** The owners of the handles encoded, which the call passes its JNI function to keep. */ - var kept: java.util.ArrayList? = null - private set - - /** The handle of the object [owner], held until [release], or 0 for none. */ - fun handle(v: Handle?, owner: Any?) { + /** An object's handle, held until [release], or 0 for none. */ + fun handle(v: Handle?) { if (v == null) { i64(0L) return } i64(v.hold()) (held ?: java.util.ArrayList().also { held = it }).add(v) - (kept ?: java.util.ArrayList().also { kept = it }).add(owner!!) } fun finish(): ByteArray = b.array().copyOf(b.position()) @@ -277,13 +272,11 @@ abstract class Cleanup(owner: Any?) : java.lang.ref.PhantomReference(owner, } /** - * A handle [owner] owns, or is lent for a call. One of a value Rust checks, which [drop] - * drops, is Rust's alone to account: [close] drops the value as Rust's own drop does, - * failing while a call borrows it, a call after it throws as Rust's check of a moved value - * fails, and the handle is freed once the GC collects [owner], which each call passes to - * its JNI function so the GC cannot collect it meanwhile. Any other counts its calls: [close] - * frees it then, throwing while a call holds it, and a call after it throws. An owned one - * is freed once the GC collects [owner], if nothing closed it. + * A handle [owner] owns, or is lent for a call: each call holds it, so it is freed only + * once no call does, and a call after [close] throws. [close] of one whose value Rust + * checks, which [drop] drops, drops the value as Rust's own drop does, throwing while a + * call borrows it in Rust; of any other, it throws while a call holds it. An owned one is + * freed once the GC collects [owner], if nothing closed it. */ class Handle( owner: Any, @@ -292,8 +285,8 @@ class Handle( private val type: String, private val drop: Close? = null, ) : Cleanup(if (free == null) null else owner) { - // The holds of a counted one, one for the owner until closed and one per call, shifted - // past a bit set once closed: one word, so a handle allocates nothing beside itself. + // The holds, one for the owner until closed and one per call, shifted past a bit set + // once closed: one word, so a handle allocates nothing beside itself. @Volatile @JvmField internal var state = HELD init { @@ -301,7 +294,6 @@ class Handle( } fun hold(): Long { - if (drop != null) return raw while (true) { val n = state kotlin.check(n and CLOSED == 0L) { "$type is closed" } @@ -309,13 +301,23 @@ class Handle( } } - fun release() { - if (drop == null) freed(STATE.addAndGet(this, -HELD)) - } + fun release() = freed(STATE.addAndGet(this, -HELD)) - /** Close it as [Handle] says, passing [owner] to keep it reachable; again, it does nothing. */ - fun close(owner: Any) { - if (drop != null) return drop.close(raw, owner) + /** Close it as [Handle] says; again, it does nothing. */ + fun close() { + if (drop != null) { + while (true) { + val n = state + if (n and CLOSED != 0L) return + if (STATE.compareAndSet(this, n, n + HELD)) break + } + try { + drop.close(raw) + } finally { + release() + } + return abandon() + } while (true) { val n = state if (n and CLOSED != 0L) return @@ -333,7 +335,10 @@ class Handle( kotlin.check(state == CLOSED) { "$type: a call still holds it when its lend ends" } } - /** Give up the owner's hold of a counted one, freeing it once no call holds one. */ + /** + * Give up the owner's hold, freeing it once no call holds one: once a call moved its + * value out, or the GC collected the owner. + */ fun abandon() { while (true) { val n = state @@ -347,12 +352,8 @@ class Handle( if (n < HELD && free != null && untrack()) free.drop(raw) } - // Nothing reaches the owner, so no call of a checked one runs; a counted one's call - // still holding it frees it once it returns. - override fun collected() = when (drop) { - null -> abandon() - else -> if (untrack()) free!!.drop(raw) else Unit - } + // A call still holding it frees it once it returns. + override fun collected() = abandon() /** Run [block] with the raw handle, held until it returns. */ inline fun hold(block: (Long) -> R): R { @@ -386,9 +387,9 @@ fun interface Drop { fun drop(raw: Long) } -/** Drops the value of a raw handle, passing its [owner] to keep it reachable meanwhile. */ +/** Drops the value of a raw handle, leaving the handle. */ fun interface Close { - fun close(raw: Long, owner: Any) + fun close(raw: Long) } /** diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 92373fe26..087f14088 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1920,8 +1920,7 @@ impl<'tcx> Collector<'tcx> { || (api.records.iter()).any(|r| args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) || (api.enums.iter()).any(|e| { args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) - }) - || (api.objects.iter()).any(|o| args(&o.args) || o.methods.iter().any(signature)) + }) || (api.objects.iter()).any(|o| args(&o.args) || o.methods.iter().any(signature)) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if !used { self.api.enums.pop(); @@ -1945,9 +1944,8 @@ impl<'tcx> Collector<'tcx> { || (api.records.iter()).any(|r| args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) || (api.enums.iter()).any(|e| { args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) - }) - || (api.objects.iter().enumerate()) - .any(|(i, o)| i != gone && (args(&o.args) || o.methods.iter().any(signature))) + }) || (api.objects.iter().enumerate()) + .any(|(i, o)| i != gone && (args(&o.args) || o.methods.iter().any(signature))) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if used { continue; @@ -2500,8 +2498,7 @@ impl<'tcx> Collector<'tcx> { // is, takes it from the future, as it would a parameter. ty::Ref(_, inner, _) if Some(*inner) == self_ty - && polled.is_some() - && let Some(kind) = self.task(ty, true) => + && polled.is_some() && let Some(kind) = self.task(ty, true) => { tasks.push(Task { at: 0, kind }); None @@ -3428,8 +3425,7 @@ impl<'tcx> Collector<'tcx> { .inputs() .iter() .filter(|&&input| input == param) - .count() - > 1 + .count() > 1 { return Some(Err(why("closure type taken twice", param.to_string()))); } @@ -3773,16 +3769,11 @@ impl<'tcx> Collector<'tcx> { let plain = matches!( ty.kind(), ty::Adt(..) - | ty::Bool - | ty::Char - | ty::Int(_) - | ty::Uint(_) - | ty::Float(_) - | ty::Str - | ty::Array(..) - | ty::Slice(_) - | ty::Ref(..) - | ty::Tuple(_) + | ty::Bool | ty::Char + | ty::Int(_) | ty::Uint(_) + | ty::Float(_) | ty::Str + | ty::Array(..) | ty::Slice(_) + | ty::Ref(..) | ty::Tuple(_) ); let (method, driven) = match (implements(iterator, ty), implements(into_iterator, ty)) { (true, _) => (next, item), diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index ba7fde6a2..81f24e1eb 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -601,12 +601,10 @@ impl<'a> Gen<'a> { !matches!( ty, Ty::Buf - | Ty::U128 - | Ty::Duration + | Ty::U128 | Ty::Duration | Ty::SocketAddr | Ty::Struct(_) - | Ty::Waker - | Ty::ForeignFuture + | Ty::Waker | Ty::ForeignFuture | Ty::ForeignReader ) } diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 6bbb94965..43f09cfea 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -38,7 +38,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::fmt::Write; use crate::abi::{self, Abi, Ty, pascal}; -use crate::ir::{Access, Closure, Family, Function, Home, Int, Literal, On, Param, Type}; +use crate::ir::{Access, Closure, Family, Function, Home, Int, Literal, On, Param, Receiver, Type}; const RUNTIME: &str = include_str!("../runtime/rt.go"); const CONVERSIONS: &str = include_str!("../runtime/ffi_cgo.go"); @@ -1850,23 +1850,25 @@ impl<'a> Gen<'a> { let name = self.named(&ty).1; let rust = abi.api.rust(&object.path, &object.args); let handle = self.qualify(Packages::RT, "Handle_"); - let (text, close) = match abi.checked[i] { - true => ( - "a handle the GC frees once it collects it", - "// Close drops the value now, as Rust's drop does: a call after it fails, and Close fails\n\ - // while a call borrows it, changing nothing.\n", - ), - false => ( - "a handle Close frees, or else the GC once it collects it", + let close = match abi.checked[i] { + true => { + "// Close drops the value now, as Rust's drop does, and frees the handle once no call holds\n\ + // it: a call after it fails, and Close fails while a call borrows the value, changing\n\ + // nothing.\n" + } + false => { "// Close frees the handle now, rather than once the GC collects it: a call after it fails,\n\ - // and Close fails while a call holds it, changing nothing.\n", - ), + // and Close fails while a call holds it, changing nothing.\n" + } }; - let text = format!("{name} is `{rust}`, {text}.\n\n{}", object.docs); + let text = format!( + "{name} is `{rust}`, a handle Close frees, or else the GC once it collects it.\n\n{}", + object.docs + ); let _ = write!( self.types, "{}type {name} struct {{\n\t// Handle_ is the handle it owns, for the generated packages: use none of it.\n\tHandle_ *{handle}\n}}\n\n\ - {close}func (self *{name}) Close() error {{\n\terr := self.Handle_.Close()\n\truntime.KeepAlive(self)\n\treturn err\n}}\n\n", + {close}func (self *{name}) Close() error {{\n\treturn self.Handle_.Close()\n}}\n\n", docs(text.trim(), "") ); let mut members = vec!["Close".to_string(), "Handle_".to_string()]; @@ -2161,6 +2163,34 @@ impl<'a> Gen<'a> { }); } + /// The Go statement closing each handle the value `e` of type `ty` holds, once a call + /// moved their values out. + fn close(&self, ty: &Type, e: &str, depth: usize) -> String { + match ty { + Type::Option(inner) => match inner.base() { + Type::Object(_) => format!("if {e} != nil {{ {e}.Handle_.Moved() }}"), + _ => { + let v = format!("v{depth}_"); + format!("if {e} != nil {{ {v} := *{e}; {} }}", self.close(inner, &v, depth + 1)) + } + }, + Type::Box(inner) => self.close(inner, e, depth), + Type::Tuple(elements) if elements.len() == 1 => self.close(&elements[0], e, depth), + Type::Tuple(elements) => { + let closes: Vec = (elements.iter().enumerate()) + .filter(|(_, ty)| moved(self.abi, ty)) + .map(|(k, ty)| self.close(ty, &format!("{e}.V{k}"), depth)) + .collect(); + closes.join("; ") + } + Type::List(_, inner) => { + let v = format!("v{depth}_"); + format!("for _, {v} := range {e} {{ {} }}", self.close(inner, &v, depth + 1)) + } + _ => format!("{e}.Handle_.Moved()"), + } + } + /// `function`'s Go form: a method of `owner` (whose IR type is `owner_ty`) if it has one /// and a receiver, or else a function of the package. fn function(&mut self, function: &Function, owner: Option<&Ty>, owner_ty: Option<&Type>) -> Method { @@ -2230,8 +2260,7 @@ impl<'a> Gen<'a> { let everywhere = abi.api.everywhere(function.on); let closable = receiver.is_some() && owner_ty.is_some_and(|ty| self.holds(ty)) || function.params.iter().any(|p| self.holds(&p.ty)) - || status - || !everywhere; + || status || !everywhere; if function.err.is_some() || function.future || closable { results.push("error".to_string()); } @@ -2245,12 +2274,14 @@ impl<'a> Gen<'a> { }; let mut prelude: Vec = Vec::new(); + // What closes each handle a call moved the value out of, once it passed its checks. + let mut moves: Vec = Vec::new(); let mut checked = false; let mut arena = false; let mut args: Vec = Vec::new(); let mut c_params = export.call.params.iter(); let mut go_receiver = None; - if receiver.is_some() { + if let Some(receiver) = receiver { let (_, c) = c_params.next().expect("a method has its receiver"); let ty = owner_ty.expect("a method has its type").clone(); let go = self.go(&ty); @@ -2262,6 +2293,10 @@ impl<'a> Gen<'a> { fail("self.Handle_.Closed()") )); args.push(format!("(*C.{name})(h_)")); + let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); + if receiver == Receiver::Owned && !copy { + moves.push("self.Handle_.Moved()".to_string()); + } } _ => { let c = match abi.ty(&ty) { @@ -2303,6 +2338,9 @@ impl<'a> Gen<'a> { continue; } params.push((name.clone(), self.go_param(¶m.ty))); + if moved(abi, ¶m.ty) { + moves.push(self.close(¶m.ty, &name, 0)); + } match (¶m.ty, c) { (Type::Closure(closure), _) => { let trampoline = self.trampoline(¶m.ty, closure); @@ -2387,8 +2425,13 @@ impl<'a> Gen<'a> { "{}(int32(c_status_), \"{c_name}\")", self.qualify(Packages::RT, "Status_") ); - format!("if c_status_ != 0 {{\n\t{}\n}}", fail(&error)) + let mut failed = format!("if c_status_ != 0 {{\n\t{}\n}}", fail(&error)); + for moved in &moves { + failed = format!("{failed}\n{moved}"); + } + failed }); + assert!(status || moves.is_empty(), "{c_name} moves a value Rust checks"); let mut body = Vec::new(); if status { body.push(format!("var c_status_ C.{}", abi.rt("status"))); @@ -2644,6 +2687,17 @@ fn poll_error(abi: &Abi<'_>) -> String { format!("C.{}_POLL_ERROR", abi.prefix.to_uppercase()) } +/// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, +/// a list, or a tuple, unless `Copy`, and never through a reference. +fn moved(abi: &Abi<'_>, ty: &Type) -> bool { + match ty { + Type::Object(i) => !abi.api.objects[*i].copy, + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), + Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), + _ => false, + } +} + /// `ty` returned: a borrow is returned as a clone, so owned. fn strip_refs(ty: &Type) -> Type { match ty { diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 45df102c1..f81d09a87 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -25,7 +25,7 @@ use std::hash::Hasher; use rustc_data_structures::stable_hash::StableHasher; use crate::abi::{self, Abi, Ty, pascal}; -use crate::ir::{Access, Family, Function, Int, Literal, Mutability, On, Param, Type}; +use crate::ir::{Access, Family, Function, Int, Literal, Mutability, On, Param, Receiver, Type}; const RUNTIME: &str = include_str!("../runtime/rt.kt"); pub const GLUE_RUNTIME: &str = include_str!("../runtime/jni.rs"); @@ -553,7 +553,7 @@ impl<'a> Gen<'a> { format!("{e}.let {{ {l} -> {w}.i32({l}.size); for ({v} in {l}) {{ {item} }} }}") } Type::Option(inner) => match inner.base() { - Type::Object(_) => format!("{w}.handle({e}?.handle, {e})"), + Type::Object(_) => format!("{w}.handle({e}?.handle)"), base if Abi::is_buf(base) => self.write_value(base, w, e), // None is a length of -1. Type::List(..) => { @@ -573,7 +573,7 @@ impl<'a> Gen<'a> { true => format!("{w}.i32({e}.value)"), false => format!("w_{}({w}, {e})", abi.enumeration(*i)), }, - Type::Object(_) => format!("{w}.handle({e}.handle, {e})"), + Type::Object(_) => format!("{w}.handle({e}.handle)"), Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => format!("w_{}({w}, {e})", abi.range(ty)), Type::Tuple(elements) => { let t = self.fresh("t"); @@ -696,39 +696,21 @@ impl<'a> Gen<'a> { /// The Kotlin expression passing `e` of type `ty` back to Rust as the C type `c`, as a /// callback or closure returns it: a scalar, a string's bytes, or a new handle to a /// copy of a `Copy` object, made without its `Clone`, or 0 for none, which Rust frees. - fn lower_ret(&mut self, c: &Ty, ty: &Type, e: &str) -> String { + fn lower_ret(&self, c: &Ty, ty: &Type, e: &str) -> String { match c { Ty::Int(int) => to_signed(*int, e), Ty::Buf => format!("{e}.encodeToByteArray()"), Ty::Handle { name, .. } => { let clone = format!("Native.{}_copy", self.bare(name)); - let r = self.fresh("r"); - let kept = match self.abi.value_status(ty) { - true => format!(", {r}"), - false => String::new(), - }; match ty { - Type::Option(_) => { - format!("{e}?.let {{ {r} -> {r}.handle.hold {{ h -> {clone}(h{kept}) }} }} ?: 0L") - } - _ => format!("{e}.let {{ {r} -> {r}.handle.hold {{ h -> {clone}(h{kept}) }} }}"), + Type::Option(_) => format!("{e}?.handle?.hold {{ h -> {clone}(h) }} ?: 0L"), + _ => format!("{e}.handle.hold {{ h -> {clone}(h) }}"), } } _ => e.to_string(), } } - /// Pass `e` to the native method as one more parameter the glue ignores, which JNI - /// keeps reachable until the call returns: the owner of a checked object's handle, or - /// a list of them, whose collection would free the handle while Rust reads it. A fence - /// on every JVM and Android level, where `Reference.reachabilityFence` needs API 28. - fn keep(&mut self, arg: &mut Arg, e: &str, n: &mut usize) { - *n += 1; - arg.jni.push("jobject"); - arg.native.push("Any?".to_string()); - arg.exprs.push(e.to_string()); - } - /// How Kotlin passes the value `e` of IR type `ty` as the C parameter `c`, named /// `name` in C, and how the glue takes it. `callback` names the parameter's closure /// trampoline, if one. @@ -768,13 +750,15 @@ impl<'a> Gen<'a> { true => format!("Native.hold({e}?.handle) {{ {h} ->"), false => format!("{e}.handle.hold {{ {h} ->"), }); + // Taken by value, the value moves out, leaving the handle only to free. + if moved(abi, ty) { + arg.after.push(match option { + true => format!("{e}?.handle?.abandon()"), + false => format!("{e}.handle.abandon()"), + }); + } arg.exprs.push(h); arg.rust_args.push(format!("{p} as usize as _")); - // Taken by value, the value moves out, which Rust's check accounts. One of a - // checked object passes its owner too, kept reachable through the call. - if abi.value_status(ty) { - self.keep(&mut arg, e, n); - } } Ty::Buf => { let option = matches!(ty.base(), Type::Option(_)); @@ -794,6 +778,11 @@ impl<'a> Gen<'a> { } Ty::U128 | Ty::Duration | Ty::SocketAddr | Ty::Struct(_) | Ty::In(_) => { let w = self.fresh("w"); + // Taken by value, each object in a list moves out, as one passed alone does. + if moved(abi, ty) { + let close = self.close(ty, e); + arg.after.push(close); + } arg.open = Some(format!("Native.Writer().use {{ {w} ->")); let value = self.write_value(ty, &w, e); arg.inside.push(value); @@ -805,10 +794,6 @@ impl<'a> Gen<'a> { Ty::In(_) => format!("&a{p}"), _ => format!("a{p}"), }); - // The owners of the checked objects encoded, kept reachable through the call. - if abi.value_status(ty) { - self.keep(&mut arg, &format!("{w}.kept"), n); - } } Ty::Fn(..) => { let trampoline = trampoline.expect("a closure has its trampoline"); @@ -1039,18 +1024,10 @@ impl<'a> Gen<'a> { ), false => format!("let ret = {call};\n\t\t{then}"), }; - // What keeps the owners of its checked handles reachable through the call. - let (keep, kept): (&[(&str, &str)], &str) = match (status, object) { - (false, _) => (&[], ""), - (true, true) => (&[("jobject", "Any?")], ", this"), - (true, false) => (&[("jobject", "Any?")], ", w.kept"), - }; - let mut unary_params = vec![(jni_self, native_self)]; - unary_params.extend_from_slice(keep); let unary = |g: &mut Self, suffix: &str, sig: &str| { g.native( &format!("{bare}_{suffix}"), - &unary_params, + &[(jni_self, native_self)], ("jbyteArray", "ByteArray?"), &checked( &format!("crate::{c}_{suffix}({rust_self})"), @@ -1058,7 +1035,7 @@ impl<'a> Gen<'a> { "rt::owned(env, ret)", ), ); - format!("\t{sig} = {open}\n\t\tNative.{bare}_{suffix}({arg}{kept})!!.decodeToString()\n\t}}\n") + format!("\t{sig} = {open}\n\t\tNative.{bare}_{suffix}({arg})!!.decodeToString()\n\t}}\n") }; if t.display { // Final, so a data class variant of a sealed class keeps it rather than making @@ -1077,7 +1054,7 @@ impl<'a> Gen<'a> { { self.native( &format!("{bare}_copy"), - &[&[("jlong", "Long")], keep].concat(), + &[("jlong", "Long")], ("jlong", "Long"), &checked( &format!("crate::{c}_copy(p0 as usize as _)"), @@ -1089,7 +1066,7 @@ impl<'a> Gen<'a> { if object && t.clone { self.native( &format!("{bare}_clone"), - &[&[("jlong", "Long")], keep].concat(), + &[("jlong", "Long")], ("jlong", "Long"), &checked( &format!("crate::{c}_clone(p0 as usize as _)"), @@ -1098,35 +1075,22 @@ impl<'a> Gen<'a> { ), ); body.push(format!( - "\t/** A new handle to a clone. */\n\tfun clone(): {class} = handle.hold {{ h -> {class}(Native.{bare}_clone(h{kept})) }}\n" + "\t/** A new handle to a clone. */\n\tfun clone(): {class} = handle.hold {{ h -> {class}(Native.{bare}_clone(h)) }}\n" )); } if object && t.eq { - let (params, b, other) = match status { - true => ( - vec![ - ("jlong", "Long"), - ("jobject", "Any?"), - ("jlong", "Long"), - ("jobject", "Any?"), - ], - "p2", - ", other", - ), - false => (vec![("jlong", "Long"), ("jlong", "Long")], "p1", ""), - }; self.native( &format!("{bare}_eq"), - ¶ms, + &[("jlong", "Long"), ("jlong", "Long")], ("jboolean", "Boolean"), &checked( - &format!("crate::{c}_eq(p0 as usize as _, {b} as usize as _)"), + &format!("crate::{c}_eq(p0 as usize as _, p1 as usize as _)"), &format!("{c}_eq"), "ret", ), ); body.push(format!( - "\toverride fun equals(other: Any?): Boolean =\n\t\tother is {class} && handle.hold {{ a -> other.handle.hold {{ b -> Native.{bare}_eq(a{kept}, b{other}) }} }}\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): Int = 0\n" + "\toverride fun equals(other: Any?): Boolean =\n\t\tother is {class} && handle.hold {{ a -> other.handle.hold {{ b -> Native.{bare}_eq(a, b) }} }}\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): Int = 0\n" )); } if t.default { @@ -1443,18 +1407,18 @@ impl<'a> Gen<'a> { true => { self.native( &format!("{bare}_drop"), - &[("jlong", "Long"), ("jobject", "Any?")], + &[("jlong", "Long")], ("()", "Unit"), &format!( "let mut status = 0;\n\t\tcrate::{c}_drop(p0 as usize as _, &mut status);\n\t\tif status != 0 {{\n\t\t\treturn rt::raise(env, status, \"{c}_drop\");\n\t\t}}" ), ); format!( - "\tinternal val handle = Native.Handle(this, raw, if (lent) null else Native.Drop {{ Native.{bare}_free(it) }}, \"{name}\", if (lent) null else Native.Close {{ h, o -> Native.{bare}_drop(h, o) }})\n\n\t/** Drop the value now, as Rust's drop does: a call after this throws, and one still running makes this throw. */\n\toverride fun close() = handle.close(this)\n" + "\tinternal val handle = Native.Handle(this, raw, if (lent) null else Native.Drop {{ Native.{bare}_free(it) }}, \"{name}\", if (lent) null else Native.Close {{ Native.{bare}_drop(it) }})\n\n\t/** Drop the value now, as Rust's drop does, and free the handle once no call holds it: a call after this throws, and one still borrowing the value makes this throw. */\n\toverride fun close() = handle.close()\n" ) } false => format!( - "\tinternal val handle = Native.Handle(this, raw, if (lent) null else Native.Drop {{ Native.{bare}_free(it) }}, \"{name}\")\n\n\t/** Free the handle now: a call after this throws, and one still running makes this throw. */\n\toverride fun close() = handle.close(this)\n" + "\tinternal val handle = Native.Handle(this, raw, if (lent) null else Native.Drop {{ Native.{bare}_free(it) }}, \"{name}\")\n\n\t/** Free the handle now: a call after this throws, and one still running makes this throw. */\n\toverride fun close() = handle.close()\n" ), }]; let mut statics = Vec::new(); @@ -1838,7 +1802,7 @@ impl<'a> Gen<'a> { let mut c_params = export.call.params.iter(); self.suspends = export.future.is_some(); let receiver = function.receiver.filter(|_| owner.is_some()); - if receiver.is_some() { + if let Some(receiver) = receiver { let (_, c) = c_params.next().expect("a method has its receiver"); let ty = owner_ty.expect("a method has its type").clone(); let mut arg = match c { @@ -1853,15 +1817,15 @@ impl<'a> Gen<'a> { arg.open = Some(format!("this.handle.hold {{ {h} ->")); arg.exprs.push(h); arg.rust_args.push(format!("{p} as usize as _")); - // A `self` method moves the value out, which Rust's check accounts. - if abi.value_status(&ty) { - self.keep(&mut arg, "this", &mut n); + let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); + if receiver == Receiver::Owned && !copy { + arg.after.push("this.handle.abandon()".to_string()); } arg } _ => self.arg(&ty, c, "this", &mut n, None), }; - arg.native.truncate(arg.jni.len()); + arg.native.truncate(1); args.push(arg); } let mut kt_params = Vec::new(); @@ -2180,6 +2144,33 @@ impl<'a> Gen<'a> { Some(indent_by(&code, indent_level)) } + /// The Kotlin statement closing each handle the value `e` of type `ty` holds, once a + /// call moved their values out. + fn close(&mut self, ty: &Type, e: &str) -> String { + match ty { + Type::Option(inner) => { + let v = self.fresh("v"); + let close = self.close(inner, &v); + format!("{e}?.let {{ {v} -> {close} }}") + } + Type::Box(inner) => self.close(inner, e), + Type::Tuple(elements) => { + let t = self.fresh("t"); + let closes: Vec = (elements.iter().enumerate()) + .filter(|(_, ty)| moved(self.abi, ty)) + .map(|(k, ty)| self.close(ty, &format!("{t}.{}", ordinal(k)))) + .collect(); + format!("{e}.let {{ {t} -> {} }}", closes.join("; ")) + } + Type::List(_, inner) => { + let v = self.fresh("v"); + let close = self.close(inner, &v); + format!("{e}.forEach {{ {v} -> {close} }}") + } + _ => format!("{e}.handle.abandon()"), + } + } + /// The glue's out values for `outs`, of a function returning `ok` or `err`, the /// pointers the C call takes after its parameters, and the statements writing them, /// `ok` saying whether it returned ok. @@ -2292,6 +2283,17 @@ fn bytes(ty: &Type) -> bool { } } +/// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, +/// a list, or a tuple, unless `Copy`, and never through a reference. +fn moved(abi: &Abi<'_>, ty: &Type) -> bool { + match ty { + Type::Object(i) => !abi.api.objects[*i].copy, + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), + Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), + _ => false, + } +} + /// Whether `ty` crosses as a buffer of UTF-8 text, or none. fn string(ty: &Type) -> bool { decode(ty).is_some() diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 128564775..cc37f4d08 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -17,6 +17,7 @@ import ( "net/netip" "os" "runtime" + "slices" "strings" "sync" "sync/atomic" @@ -1042,7 +1043,7 @@ func futures() { check(<-got == "held", "the waiting call ends as it would") check(other.Close() == nil, "Close passes once no call borrows it") _, err = other.Recv(ctx) - check(errors.Is(err, rt.ErrClosed) && err.Error() == "api_Channel_recv: a handle's value was moved out", "a call after Close fails") + check(errors.Is(err, rt.ErrClosed) && err.Error() == "Channel is closed", "a call after Close fails") sender.Close() } @@ -1213,36 +1214,31 @@ func uncollected(n uint32, what string) { } // closed checks that every call taking a closed object returns ErrClosed, however it was -// closed, and that a second Close does nothing: as Rust's check of a moved value for one -// Rust checks, and as Go's own for one whose calls only borrow it. +// closed, and that a second Close does nothing. func closed() { ctx := context.Background() isClosed := func(err error, want, what string) { check(errors.Is(err, rt.ErrClosed) && err.Error() == want, fmt.Sprintf("%s: %v", what, err)) } - moved := func(function string) string { - return function + ": a handle's value was moved out" - } - // A method, a trait's, and a field's accessors. c := api.NewCounter("c", 1) check(c.Close() == nil && c.Close() == nil, "Close, then again, passes") _, err := c.Get() - isClosed(err, moved("api_Counter_get"), "a method of a closed object") + isClosed(err, "Counter is closed", "a method of a closed object") _, err = c.Clone() - isClosed(err, moved("api_Counter_clone"), "Clone of a closed object") - check(c.GoString() == moved("api_Counter_debug"), "GoString of a closed object is the error") + isClosed(err, "Counter is closed", "Clone of a closed object") + check(c.GoString() == "Counter is closed", "GoString of a closed object is the error") tally := api.NewTally() tally.Close() _, err = api.NewTally().Equal(tally) - isClosed(err, moved("api_Tally_eq"), "Equal with a closed object") + isClosed(err, "Tally is closed", "Equal with a closed object") registry := must(api.NewRegistry(api.NewCounter("first", 1))) registry.Close() _, err = registry.FirstGet() - isClosed(err, moved("api_Registry_first_get"), "a field's getter of a closed object") + isClosed(err, "Registry is closed", "a field's getter of a closed object") // A call that fails moves nothing in. second := api.NewCounter("second", 2) - isClosed(registry.FirstSet(second), moved("api_Registry_first_set"), "a field's setter of a closed object") + isClosed(registry.FirstSet(second), "Registry is closed", "a field's setter of a closed object") check(must(second.Get()) == 2, "a failed call leaves what it would move") // A record's method, whose receiver holds an object. @@ -1255,27 +1251,27 @@ func closed() { // An argument, alone, or nested where the call holds it through its arena. _, err = api.BumpBoth(api.NewTally(), tally) - isClosed(err, moved("api_bump_both"), "a closed argument") + isClosed(err, "Tally is closed", "a closed argument") open := api.NewCounter("open", 3) _, err = api.Worth(&[]*api.Counter{open, c}, nil) - isClosed(err, moved("api_worth"), "a closed object in a list") + isClosed(err, "Counter is closed", "a closed object in a list") check(must(open.Get()) == 3, "a failed call leaves the rest of a list") // An async call. channel := api.NewChannel() channel.Close() _, err = channel.Recv(ctx) - isClosed(err, moved("api_Channel_recv"), "an async call of a closed object") + isClosed(err, "Channel is closed", "an async call of a closed object") // Moved by a call, plain or boxed, or closed once the callback it was lent to returned. taken := api.NewCounter("m", 0) must(taken.IntoLabel(nil)) _, err = taken.Get() - isClosed(err, moved("api_Counter_get"), "a method of a moved object") + isClosed(err, "Counter is closed", "a method of a moved object") tail := must(api.NewNode(1, nil)) must(api.NewNode(2, tail)) _, err = tail.Sum() - isClosed(err, moved("api_Node_sum"), "a method of a boxed moved object") + isClosed(err, "Node is closed", "a method of a boxed moved object") var kept *api.Counter must(must(api.NewRegistry(api.NewCounter("c", 0))).Edit(func(counter *api.Counter) bool { kept = counter @@ -1290,21 +1286,23 @@ func closed() { race(latch, func() (bool, error) { v, err := latch.Get() return err == nil && *v == 5, err - }, "Latch is closed", "Latch: a handle is already borrowed") + }, []string{"Latch is closed"}, "Latch: a handle is already borrowed") before := api.Dropped() token := api.NewToken(5) race(token, func() (bool, error) { id, err := token.IdGet() return id == 5, err - }, moved("api_Token_id_get"), "api_Token_drop: a handle is already borrowed") + }, []string{"Token is closed", "api_Token_id_get: a handle's value was moved out"}, "api_Token_drop: a handle is already borrowed") check(api.Dropped() == before+1, "Close racing calls drops Rust's value once") fmt.Println("closed ok") } // race closes c while 8 goroutines call call on it: each call runs on the live value or, -// once c is closed, fails with closed; a Close a running call beats fails with borrowed, as -// Rust refuses a drop under a borrow, and changes nothing, so the next tries again. -func race(c interface{ Close() error }, call func() (bool, error), closed, borrowed string) { +// once c is closed, fails with one of closed, which for a value Rust checks includes its +// moved check, met by a call that held the handle before Close; a Close a running call +// beats fails with borrowed, as Rust refuses a drop under a borrow, and changes nothing, so +// the next tries again. +func race(c interface{ Close() error }, call func() (bool, error), closed []string, borrowed string) { var wg sync.WaitGroup var ran, failed atomic.Int64 for range 8 { @@ -1314,7 +1312,7 @@ func race(c interface{ Close() error }, call func() (bool, error), closed, borro for { live, err := call() if err != nil { - check(errors.Is(err, rt.ErrClosed) && err.Error() == closed, fmt.Sprintf("a call racing Close: %v", err)) + check(errors.Is(err, rt.ErrClosed) && slices.Contains(closed, err.Error()), fmt.Sprintf("a call racing Close: %v", err)) failed.Add(1) return } diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 44772b998..715382db4 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -774,7 +774,7 @@ fun futures() { check(got == "held", "the awaiting call ends as it would") other.close() val moved = runCatching { other.clear() }.exceptionOrNull() - check(moved is IllegalStateException && moved.message == "api_Channel_clear: a handle's value was moved out", "a call after close throws: $moved") + check(moved is IllegalStateException && moved.message == "Channel is closed", "a call after close throws: $moved") sender.close() } @@ -992,7 +992,7 @@ fun owned() { check(dropped() == before + 1u, "closing frees at once") token.close() check(dropped() == before + 1u, "closing again frees nothing") - check(runCatching { token.idGet }.exceptionOrNull()?.message == "api_Token_id_get: a handle's value was moved out", "a call after close throws") + check(runCatching { token.idGet }.exceptionOrNull()?.message == "Token is closed", "a call after close throws") // The file serves wasm32 too: what only wasm32 has throws on the host's library. check(pointerBits(platform()) == 64u && pointerBits(Platform.Native(64u)) == 64u, "the host is native") @@ -1077,8 +1077,13 @@ fun raced() { var before = dropped() val latch = Latch.default() latch.set(5u) - race(latch, { it.get() == 5u }, "Latch is closed", "Latch: a handle is already borrowed") - race(Token.new(5u), { it.idGet == 5u }, "api_Token_id_get: a handle's value was moved out", "api_Token_drop: a handle is already borrowed") + race(latch, { it.get() == 5u }, setOf("Latch is closed"), "Latch: a handle is already borrowed") + race( + Token.new(5u), + { it.idGet == 5u }, + setOf("Token is closed", "api_Token_id_get: a handle's value was moved out"), + "api_Token_drop: a handle is already borrowed", + ) check(dropped() == before + 1u, "close racing calls drops Rust's value once") // Calls on values nothing else reaches, while another thread collects: each reads what @@ -1107,9 +1112,11 @@ fun raced() { } // Close [made] while 8 threads call [call] on it: each call runs on the live value or, once -// it is closed, throws [closed]; a close a running call beats throws [borrowed], as Rust -// refuses a drop under a borrow, and changes nothing, so the next tries again. -fun race(made: T, call: (T) -> Boolean, closed: String, borrowed: String) { +// it is closed, throws one of [closed], which for a value Rust checks includes its moved +// check, met by a call that held the handle before close; a close a running call beats +// throws [borrowed], as Rust refuses a drop under a borrow, and changes nothing, so the next +// tries again. +fun race(made: T, call: (T) -> Boolean, closed: Set, borrowed: String) { val ran = java.util.concurrent.atomic.AtomicInteger() val failed = java.util.concurrent.atomic.AtomicInteger() val callers = List(8) { @@ -1118,7 +1125,7 @@ fun race(made: T, call: (T) -> Boolean, closed: String, borr val live = try { call(made) } catch (e: IllegalStateException) { - check(e.message == closed, "a call racing close throws: ${e.message}") + check(e.message in closed, "a call racing close throws: ${e.message}") failed.incrementAndGet() return@thread } diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 787211d3d..a75a315fb 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -591,14 +591,8 @@ fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { run(&mut vm(&["futures"])); run(&mut vm(&["owned"])); for (case, message) in [ - ( - "moved", - "IllegalStateException: api_Counter_get: a handle's value was moved out", - ), - ( - "boxed moved", - "IllegalStateException: api_Node_sum: a handle's value was moved out", - ), + ("moved", "IllegalStateException: Counter is closed"), + ("boxed moved", "IllegalStateException: Node is closed"), ("lent kept", "Counter is closed"), ( "lent moved", @@ -1099,7 +1093,7 @@ fn api_go() { "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("nil reader", "a nil io.ReadSeeker"), - ("returned closed", "api_Instant_copy: a handle's value was moved out"), + ("returned closed", "Instant is closed"), ("family", "an address that is not IPv4 has no Rust SocketAddrV4"), ("outlived", OUTLIVED), ("text", "api_greet: a string is not UTF-8"), From d72ec44c2314deffaf6274031bc751a8fe2d4452 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:07:02 -0800 Subject: [PATCH 18/30] docs: Go and Kotlin keep their counts; checks-raise is finished; Go's status allocation is a follow-up Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 80 ++++++++++++++++---------- ffi/README.md | 42 +++++++------- quest/m1/ffi-export/README.md | 2 +- quest/m1/ffi-export/checks-raise.md | 46 --------------- quest/m1/ffi-export/copy-values.md | 1 - quest/m1/ffi-export/lossless.md | 4 -- quest/m1/ffi-export/not-send.md | 6 +- quest/m1/ffi-export/ship.md | 1 - quest/m1/ffi-speed/go-status.md | 32 +++++++++++ quest/m1/ffi-speed/object-ownership.md | 9 +-- quest/m2/wasm/backend.md | 1 - 11 files changed, 105 insertions(+), 119 deletions(-) delete mode 100644 quest/m1/ffi-export/checks-raise.md create mode 100644 quest/m1/ffi-speed/go-status.md diff --git a/bench/ffi/README.md b/bench/ffi/README.md index ef4b188f7..f3cf4072d 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -157,41 +157,59 @@ frame path: 7. **The binding's own call.** Over C's getter: Swift adds 8 ns (its hold count's two atomics, since dropped from all but its classes, as below), Kotlin 14 wall (JNI and its hold count), Dart 19 (an `_Arena` per call and its hold), Go 31 (cgo, `defer`, and its hold), and Python 37 (argument parsing and the - GIL released and taken back, so Rust may call Python from another thread); each hold - count is since dropped for a checked type, as below. BoltFFI's + GIL released and taken back, so Rust may call Python from another thread); Python's and + Dart's hold counts are since dropped for a checked type, as below. BoltFFI's Python getter, which holds the GIL, measured 63 ns. ### Per-handle baseline -For [checks-raise](/quest/m1/ffi-export/checks-raise.md)'s one accounting: the u64 -getter is one borrowed handle and nothing else, so its row less Rust's 0.4 ns is what a -handle costs a call. Measured 2026-11-13 on desktop at a load average of 2.4 to 4.1, -main at `7b0958b3` against [#730](https://github.com/kixel-ai/rust2/pull/730), run -alternately: 12 rounds of main and 6 of #730, median wall ns (CPU ns where it differs). -#730 drops the hold counts Kotlin, Go, Python, and Dart kept beside the runtime's, so a -checked call borrows through the runtime's two atomics alone, and close drops the value -through Rust, leaving the handle to the GC. - -| row | C | Kotlin | Python | Swift | Go | Dart | -|---|---|---|---|---|---|---| -| handle call, main | 18 | 31 (60) | 54 | 26 | 47 | 41 (45) | -| handle call, #730 | 18 | 24 (45) | 53 | 26 | 40 | 42 (45) | -| u64 getter, main | 10 | 23 (45) | 48 | 10 | 39 | 27 | -| u64 getter, #730 | 10 | 15 (30) | 47 | 10 | 33 | 28 | -| object return, main | 29 | 87 (190) | 99 | 37 | 183 (198) | 238 | -| object return, #730 | 30 | 94 (190) | 98 | 38 | 691 (2,080) | 214 | -| stamped write, main | 159 | 310 (340) | 337 | 205 | 429 (447) | 433 | -| stamped write, #730 | 168 | 613 (2,165) | 332 | 212 | 1,061 (2,533) | 450 | - -The getter and the handle call gain what the counts cost: 8 ns in Kotlin and 5 to 7 in -Go. The object return and the stamped write lose more in Go and Kotlin: a handle closed or -moved out of is freed only once the GC finds its object unreachable, so its cleanup, or -its phantom reference, stays registered, where a count let close free it at once. Go's -object return with `Close` also stopping the cleanup, unsafe without a count, measured -205 ns, so the cleanup is the whole loss. Dart's checked calls first lost 4 ns to a -status allocated per call, now one cell per isolate. Swift's getter costs C's since it -dropped its hold count for each noncopyable object (2026-11-11). A change to the -accounting moves these rows; measure them at the load beside them. +For [#730](https://github.com/kixel-ai/rust2/pull/730)'s one accounting: the u64 getter +is one borrowed handle and nothing else, so its row less Rust's 0.4 ns is what a handle +costs a call. Measured 2026-11-13 on desktop at a load average of 2.4 to 4.1, main at +`7b0958b3` against #730 before Go and Kotlin took their counts back, run alternately: 12 +rounds of main and 6 of #730, median wall ns (CPU ns where it differs). #730 drops the hold +counts Python and Dart kept beside the runtime's for a checked type, so its call borrows +through the runtime's two atomics alone, and close drops the value through Rust. + +| row | C | Python | Swift | Dart | +|---|---|---|---|---| +| handle call, main | 18 | 54 | 26 | 41 (45) | +| handle call, #730 | 18 | 53 | 26 | 42 (45) | +| u64 getter, main | 10 | 48 | 10 | 27 | +| u64 getter, #730 | 10 | 47 | 10 | 28 | +| object return, main | 29 | 99 | 37 | 238 | +| object return, #730 | 30 | 98 | 38 | 214 | +| stamped write, main | 159 | 337 | 205 | 433 | +| stamped write, #730 | 168 | 332 | 212 | 450 | + +Go and Kotlin dropped theirs too at first, and gained 5 to 8 ns on the getter, but a +handle closed or moved out of was then freed only once the GC found its object +unreachable, which kept Go's cleanup and Kotlin's phantom reference registered: Go's +object return went from 183 to 691 ns and its stamped write from 429 to 1,061, Kotlin's +stamped write from 310 to 613. So each still counts its calls, freeing the handle once a +close or a move leaves no call holding it. Measured 2026-11-13 on nixos (AMD, 32 threads) +at a load average of 28 to 47, main at `e9cca797` against #730, 10 runs of each program +alternating, median wall ns: + +| row | Kotlin | Go | +|---|---|---| +| handle call, main | 13 | 35 | +| handle call, #730 | 14 | 49 | +| u64 getter, main | 12 | 34 | +| u64 getter, #730 | 11 | 50 | +| object return, main | 156 | 261 | +| object return, #730 | 66 | 317 | +| stamped write, main | 457 | 745 | +| stamped write, #730 | 441 | 835 | + +Kotlin's rows match main's within the noise: its object return's fastest runs took 57 and +60 ns. Go's checked call allocates its status, +since cgo moves to the heap a local whose address it passes C, which costs 6 ns in a +cgo call alone; its close calls Rust's `_drop`, a second crossing an object return and a +stamped write pay. Dart's checked calls first lost 4 ns to a status allocated per call, +now one cell per isolate. Swift's getter costs C's since it dropped its hold count for +each noncopyable object (2026-11-11). A change to the accounting moves these rows; +measure them at the load beside them. ### Against the targets diff --git a/ffi/README.md b/ffi/README.md index 7f844c58d..00648c1d5 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -452,15 +452,15 @@ in as a `ByteArray`, bytes returned as their owner, and anything else encoded in such as a function's or an exception's, takes a trailing `_` where a type, the runtime, or an earlier name has it. A keyword is quoted in backticks, and a member named as one Kotlin gives its type takes a trailing `_`. A constructor such as `new` stays a companion function: `Counter.new(1)`. -- **Objects** are classes owning their handle, `AutoCloseable`. Of a checked type, - `close()` drops the value through Rust's own `_drop`, as Rust's `drop` would: a call - after it throws as for a value moved out, and one while a call borrows the value throws - instead, as Rust refuses that drop, leaving the value to a later `close()`. Of any other - type, `close()` frees the handle, and throws while a call holds it; a call after it - throws. Either way the handle itself is freed once the GC collects the object, on a - daemon thread a phantom reference queue feeds, so each call passes its object to keep - it reachable. A `self` method, or passing the object by value, moves its value out, - after which a call throws. `Clone` is `clone()`, and `PartialEq` is `equals`. +- **Objects** are classes owning their handle, `AutoCloseable`, which each call holds + until it returns, so the handle is freed only once no call holds it; a call after + `close()` throws. Of a checked type, `close()` drops the value through Rust's own + `_drop`, as Rust's `drop` would, and throws instead while a call borrows the value, as + Rust refuses that drop, leaving the value to a later `close()`. Of any other type, + `close()` frees the handle, and throws while a call holds it. One never closed is freed + once the GC collects it, on a daemon thread a phantom reference queue feeds. A `self` + method, or passing the object by value, moves its value out and closes it once the call + returns. `Clone` is `clone()`, and `PartialEq` is `equals`. A field is a `val`, `firstGet`, and its setter a function, `firstSet(value)`, named as the C ones, since setting an object moves it in and closes its handle, which an assignment would hide. @@ -752,18 +752,18 @@ reads a go.mod. named as a Go keyword, a predeclared name, or a package the code uses, take a trailing `_` too, as does a variant's or error's name or any other in a package that another there, or the runtime's, takes. -- **Objects** are pointers to structs owning their handle, each an `io.Closer`. Of a - checked type, `Close()` drops the value through Rust's own `_drop`, as Rust's `drop` - would: a call after it returns an error matching `rt.ErrClosed`, as for a value moved - out, and a `Close()` while a call on another goroutine borrows the value returns one - matching `rt.ErrBorrowed` instead, as Rust refuses that drop, leaving the value to a - later `Close()`. Of any other type, `Close()` frees the handle, and returns an error - matching `rt.ErrBorrowed` while a call holds it. A second `Close()` does nothing, and the - handle itself is freed once the GC collects the object, through `runtime.AddCleanup`. A - call that meets a closed object, as its receiver or anywhere in its arguments, returns an - error matching `rt.ErrClosed`, so every such call returns an `error`; `String()` and - `GoString()`, which return none, return the error's text. A `self` method, or passing - the object by value, moves its value out. `Clone` is `Clone()`, and a `pub` field a getter, `FirstGet()`, +- **Objects** are pointers to structs owning their handle, each an `io.Closer`, which + each call holds until it returns, so the handle is freed only once no call holds it. Of + a checked type, `Close()` drops the value through Rust's own `_drop`, as Rust's `drop` + would, and returns an error matching `rt.ErrBorrowed` instead while a call on another + goroutine borrows the value, as Rust refuses that drop, leaving the value to a later + `Close()`. Of any other type, `Close()` frees the handle, and returns an error matching + `rt.ErrBorrowed` while a call holds it. A second `Close()` does nothing, and one never + closed is freed once the GC collects it, through `runtime.AddCleanup`. A call that meets + a closed object, as its receiver or anywhere in its arguments, returns an error matching + `rt.ErrClosed`, so every such call returns an `error`; `String()` and `GoString()`, + which return none, return the error's text. A `self` method, or passing the object by + value, moves its value out and closes it once the call returns. `Clone` is `Clone()`, and a `pub` field a getter, `FirstGet()`, and a setter, `FirstSet(v)`. - **Checks**: a borrow or move Rust's rules refuse returns an error naming the function and the rule, before any Rust code runs, so the call changed nothing: one matching `rt.ErrClosed` for a value moved out, or a lent diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index f022ef841..a45a0512a 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -32,7 +32,7 @@ Decided with the maintainer on 2026-11-09, in the quest audit: ones ([lossless](/quest/m1/ffi-export/lossless.md)). Rejected: idiomatic types whose truncations and aborts become errors; today's mappings. - ✅ A failed run-time check raises the language's own error, and a Rust panic aborts - ([checks-raise](/quest/m1/ffi-export/checks-raise.md)). Rejected: catching panics at the + ([#730](https://github.com/kixel-ai/rust2/pull/730)). Rejected: catching panics at the boundary; aborting on every failed check. - ✅ A type or future that is not `Send` exports, checked by thread ([not-send](/quest/m1/ffi-export/not-send.md)). Rejected: a Rust-owned thread for every diff --git a/quest/m1/ffi-export/checks-raise.md b/quest/m1/ffi-export/checks-raise.md deleted file mode 100644 index 5d1237167..000000000 --- a/quest/m1/ffi-export/checks-raise.md +++ /dev/null @@ -1,46 +0,0 @@ -# [M] Failed checks raise - -## Goal - -A run-time check a [rust2-ffi](/ffi/README.md) binding fails raises the language's own -error and leaves the process running, everywhere the binding has a caller to raise to, and -each handle's borrows are counted once per call, in Rust: the bindings keep no hold count -of their own beside the runtime's, and closing or moving out of an object costs no more -than the count it replaced. - -## Plan - -Decided with the maintainer on 2026-11-09 and 2026-11-12, in the quest audits: checks -raise and panics abort; the one accounting is the runtime's, in `ffi/runtime/rt.rs`, whose -failed check returns a status the binding raises from; the exporter decides at export -time which types need a check; and close in Kotlin, Go, Python, and Dart is Rust-like, -taking the value through the runtime's `TAKEN`, so a call after it raises `moved` and a -close racing a call raises in whichever loses. - -[#730](https://github.com/kixel-ai/rust2/pull/730) does all of that. Every place with a -caller raises, Swift's `description`, `==`, and `clone()` included; what a closure returns -still aborts on a failed check, since Rust, its caller, has no error to take. - -What is left is the handle itself. With no count of its own, a binding cannot tell when a -call on another thread has read the handle but not yet borrowed it in Rust, so the handle -a close or a move empties is freed only once the GC finds its object unreachable. Go -leaves its `runtime.AddCleanup` registered and Kotlin its phantom reference, and the -[bench](/bench/ffi/README.md#per-handle-baseline) measures it: Go's object return went from -183 to 691 ns (2,080 CPU) and its stamped write from 429 to 1,061; Kotlin's stamped write -from 310 to 613 (2,165 CPU). The getter gained 8 ns in Kotlin and 6 in Go. Python frees by -reference count and Dart's finalizer costs no more, so neither lost. - -Public API: none beyond #730's. - -## Waiting - -- Maintainer: how Go and Kotlin free a handle a close or a move emptied. Recommended: each - keeps a count for a checked type only to free the handle once no call holds it, with - close staying Rust-like; it gives back the getter's 6 to 8 ns to win back 300 to 500 ns - per object returned or stamped, which moq pays per frame. The other way is to keep #730 - as it is and look for a cheaper deferred free under - [object-ownership](/quest/m1/ffi-speed/object-ownership.md). - -## Related - -- [Copy types cross by value](/quest/m1/ffi-export/copy-values.md) - landing it first leaves only thrown errors as Swift classes, and the stamped write without a `Timestamp` object diff --git a/quest/m1/ffi-export/copy-values.md b/quest/m1/ffi-export/copy-values.md index 04171b5f1..b88994eb5 100644 --- a/quest/m1/ffi-export/copy-values.md +++ b/quest/m1/ffi-export/copy-values.md @@ -45,6 +45,5 @@ layout fields. ## Related -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - a value has no handle to check, so fewer calls can fail one - [Kotlin records](/quest/m1/ffi-speed/kotlin-records.md) - a record holds `Copy` handles today, so its layout changes with this - [Cheaper returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) - the other half of Go's and Dart's stamped write diff --git a/quest/m1/ffi-export/lossless.md b/quest/m1/ffi-export/lossless.md index 259292d0d..490d9bf74 100644 --- a/quest/m1/ffi-export/lossless.md +++ b/quest/m1/ffi-export/lossless.md @@ -50,7 +50,3 @@ is the limit and no exact type exists at a call's speed, such as a Python `int` Public API: every generated API's types for durations, addresses, paths, and text (breaking), and the runtimes' conversion helpers. - -## Required - -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - how a binding raises, which the checked conversions and invalid text use diff --git a/quest/m1/ffi-export/not-send.md b/quest/m1/ffi-export/not-send.md index 4ccccf05e..181800024 100644 --- a/quest/m1/ffi-export/not-send.md +++ b/quest/m1/ffi-export/not-send.md @@ -20,7 +20,7 @@ an assert is fine "where a language has no concept of something being thread loc - The handle records its owner thread, and each call compares it: no atomics, so it costs less than a `Sync` type's borrow. The wrong thread - [raises](/quest/m1/ffi-export/checks-raise.md). + raises, as each failed check does. - A free from another thread never drops the value there. GC finalizers run elsewhere: Kotlin's phantom-reference daemon thread, Go's `runtime.AddCleanup` goroutine, Python's collector, Swift's `deinit` on the releasing thread, and Dart's finalizer with no @@ -49,7 +49,3 @@ an assert is fine "where a language has no concept of something being thread loc check. Public API: the items that now export, and Swift's `Sendable` conformances (breaking). - -## Required - -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - the wrong-thread check raises through the same accounting diff --git a/quest/m1/ffi-export/ship.md b/quest/m1/ffi-export/ship.md index fa4c84e41..ac7a83019 100644 --- a/quest/m1/ffi-export/ship.md +++ b/quest/m1/ffi-export/ship.md @@ -20,7 +20,6 @@ then see change. A sibling planned later adds itself to `Required` here. ## Required - [Refusals](/quest/m1/ffi-refusals/README.md) - the census lists no refusals at the moq pin, and `ffi.toml` is gone -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - which generated functions throw - [Lossless binding types](/quest/m1/ffi-export/lossless.md) - the types of durations, addresses, paths, and text - [Copy types cross by value](/quest/m1/ffi-export/copy-values.md) - how a `Copy` type is held in every language - [Not Send](/quest/m1/ffi-export/not-send.md) - the types and futures that export checked by thread diff --git a/quest/m1/ffi-speed/go-status.md b/quest/m1/ffi-speed/go-status.md new file mode 100644 index 000000000..16e2908ee --- /dev/null +++ b/quest/m1/ffi-speed/go-status.md @@ -0,0 +1,32 @@ +# [S] Go's checked calls allocate no status + +## Goal + +A [rust2-ffi](/ffi/README.md) Go call that can fail a check passes its +`_status` without a heap allocation. Today each declares `var c_status_` and +passes `&c_status_` to C, which cgo moves to the heap: `go build -gcflags=-m` reports it +for every checked function (756 in moq's `net` package), and a bare cgo call measured +6 ns more with the pointer than without, about what Go's checked getter lost against +main in [the bench](/bench/ffi/README.md#per-handle-baseline). + +## Plan + +- Recommended: pass the stack cell's address hidden from escape analysis, as a `uintptr` + turned back into a pointer at the call. A goroutine inside a cgo call is never shrunk + (the runtime's `isShrinkStackSafe` refuses one in a syscall), and the glue writes the + status only when a check fails, before any Rust code runs, so before any callback into + Go could grow and move the stack. Make that ordering a checked invariant of the glue, + not a convention. The fixtures run `go vet`, which flags a `uintptr` made back into a + pointer. +- Rejected: a thread-local last error, refused on 2026-11-12 as hidden state a caller can + forget to read; a `sync.Pool` of C cells, which costs more than the allocation; Dart's + one cell per isolate, since goroutines share threads. +- Re-measure the Go getter and handle call against main, CPU first. + +## Waiting + +- Maintainer: whether Go may pass a stack address that escape analysis cannot see, which + relies on the runtime never moving a stack in a cgo call before a callback, a behavior + cgo's documented rules do not promise. Recommended: yes, with the ordering checked in + the glue, as it wins back 6 ns on every checked Go call; the other way is to keep the + allocation. diff --git a/quest/m1/ffi-speed/object-ownership.md b/quest/m1/ffi-speed/object-ownership.md index 6e022a472..279733ba2 100644 --- a/quest/m1/ffi-speed/object-ownership.md +++ b/quest/m1/ffi-speed/object-ownership.md @@ -17,16 +17,9 @@ Ownership stays automatic: a dropped object is still freed without a call from t (`ffi/src/go.rs`'s `own_`). So take candidates from the profile. One left: allocate the wrapper and its handle together, so a return allocates once. - Explicit `Close` stays, and the cleanup stays as the backstop; dropping it is refused, - since a missed `Close` would leak. Since #730, a checked type's `Close` drops the value - through Rust's `_drop` and keeps no hold count, so the cleanup alone frees the handle, - which costs Go's object return 500 ns; how that changes is checks-raise's open - decision. + since a missed `Close` would leak. - Re-measure the bench's object return and stamped write. -## Required - -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - rewrites the same handle word and decides when a closed handle is freed; profile after it lands, or the baselines move - ## Related - [Copy values](/quest/m1/ffi-export/copy-values.md) - removes the per-frame `Timestamp` diff --git a/quest/m2/wasm/backend.md b/quest/m2/wasm/backend.md index 712b2fbae..af7fe5be0 100644 --- a/quest/m2/wasm/backend.md +++ b/quest/m2/wasm/backend.md @@ -104,4 +104,3 @@ Public API: the TypeScript binding's shape, and the name of the web target. ## Required - [Lossless binding types](/quest/m1/ffi-export/lossless.md) - the types a new binding copies are the exact ones -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - the one accounting a new binding copies raises From 38bdd3fb22dd8c36e88be8a62354564292693cfd Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:09:40 -0800 Subject: [PATCH 19/30] style(ffi): format with the workspace's rustfmt Co-Authored-By: Claude Opus 5.5 --- ffi/src/collect.rs | 29 +++++++++++++++++++---------- ffi/src/dart.rs | 6 ++++-- ffi/src/go.rs | 3 ++- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 087f14088..92373fe26 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1920,7 +1920,8 @@ impl<'tcx> Collector<'tcx> { || (api.records.iter()).any(|r| args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) || (api.enums.iter()).any(|e| { args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) - }) || (api.objects.iter()).any(|o| args(&o.args) || o.methods.iter().any(signature)) + }) + || (api.objects.iter()).any(|o| args(&o.args) || o.methods.iter().any(signature)) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if !used { self.api.enums.pop(); @@ -1944,8 +1945,9 @@ impl<'tcx> Collector<'tcx> { || (api.records.iter()).any(|r| args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) || (api.enums.iter()).any(|e| { args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) - }) || (api.objects.iter().enumerate()) - .any(|(i, o)| i != gone && (args(&o.args) || o.methods.iter().any(signature))) + }) + || (api.objects.iter().enumerate()) + .any(|(i, o)| i != gone && (args(&o.args) || o.methods.iter().any(signature))) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if used { continue; @@ -2498,7 +2500,8 @@ impl<'tcx> Collector<'tcx> { // is, takes it from the future, as it would a parameter. ty::Ref(_, inner, _) if Some(*inner) == self_ty - && polled.is_some() && let Some(kind) = self.task(ty, true) => + && polled.is_some() + && let Some(kind) = self.task(ty, true) => { tasks.push(Task { at: 0, kind }); None @@ -3425,7 +3428,8 @@ impl<'tcx> Collector<'tcx> { .inputs() .iter() .filter(|&&input| input == param) - .count() > 1 + .count() + > 1 { return Some(Err(why("closure type taken twice", param.to_string()))); } @@ -3769,11 +3773,16 @@ impl<'tcx> Collector<'tcx> { let plain = matches!( ty.kind(), ty::Adt(..) - | ty::Bool | ty::Char - | ty::Int(_) | ty::Uint(_) - | ty::Float(_) | ty::Str - | ty::Array(..) | ty::Slice(_) - | ty::Ref(..) | ty::Tuple(_) + | ty::Bool + | ty::Char + | ty::Int(_) + | ty::Uint(_) + | ty::Float(_) + | ty::Str + | ty::Array(..) + | ty::Slice(_) + | ty::Ref(..) + | ty::Tuple(_) ); let (method, driven) = match (implements(iterator, ty), implements(into_iterator, ty)) { (true, _) => (next, item), diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 81f24e1eb..ba7fde6a2 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -601,10 +601,12 @@ impl<'a> Gen<'a> { !matches!( ty, Ty::Buf - | Ty::U128 | Ty::Duration + | Ty::U128 + | Ty::Duration | Ty::SocketAddr | Ty::Struct(_) - | Ty::Waker | Ty::ForeignFuture + | Ty::Waker + | Ty::ForeignFuture | Ty::ForeignReader ) } diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 43f09cfea..24ce43291 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -2260,7 +2260,8 @@ impl<'a> Gen<'a> { let everywhere = abi.api.everywhere(function.on); let closable = receiver.is_some() && owner_ty.is_some_and(|ty| self.holds(ty)) || function.params.iter().any(|p| self.holds(&p.ty)) - || status || !everywhere; + || status + || !everywhere; if function.err.is_some() || function.future || closable { results.push("error".to_string()); } From a19f98623bfabf6706c163bb6c3579c62f31bb29 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:12:12 -0800 Subject: [PATCH 20/30] fix(ffi): a refused call changes no argument, records' receivers check, and the runtime claims its statuses Swift writes back and closes moved classes only once a call passed its checks. A moved argument holds a borrow of each checked Copy object it copies, alone or in a record or data enum, until the copies are made, so no check fails after its moves commit. A record's or enum's method takes a status for the handles its receiver holds, an empty variant lifts as Ok, and C claims each _STATUS_* name. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/src/lib.rs | 46 ++++++++++ ffi/runtime/rt.rs | 64 +++++++++++++- ffi/src/abi.rs | 24 +++-- ffi/src/glue.rs | 172 +++++++++++++++++++++++++++++++++--- ffi/src/swift.rs | 23 ++++- ffi/tests/api.c | 51 +++++++++++ ffi/tests/api.swift | 13 +++ 7 files changed, 369 insertions(+), 24 deletions(-) diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 56d032615..4fd1079da 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -738,6 +738,46 @@ pub fn seat(ticket: Ticket, row: u32) -> Seat { Seat { ticket, row } } +impl Seat { + /// Its ticket's number: a record's method whose receiver holds a checked object. + pub fn number(&self) -> u32 { + self.ticket.number + } +} + +/// The counter's value plus the number of each ticket beside it: a counter moved in +/// beside copied checked objects, alone and held in a record and an enum. +pub fn admit(entry: (Counter, Ticket, Seat, Choice)) -> u64 { + let (counter, ticket, seat, choice) = entry; + counter.value + u64::from(ticket.number + seat.ticket.number + choose(choice)) +} + +/// A ticket picked, or none: a data enum with an empty variant beside a checked object. +pub enum Choice { + Empty, + Picked(Ticket), +} + +/// The number of the ticket picked, or 0. +pub fn choose(choice: Choice) -> u32 { + match choice { + Choice::Empty => 0, + Choice::Picked(ticket) => ticket.number, + } +} + +/// An enum named as the runtime's status, whose constants C would name as its statuses. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Status { + Moved, + Kept, +} + +/// Whether `status` is `Kept`. +pub fn is_kept(status: Status) -> bool { + status == Status::Kept +} + /// What `check` returns, a closure with no value but its error. pub fn attempt(check: impl FnOnce() -> Result<(), String>) -> Result<(), String> { check() @@ -2167,6 +2207,12 @@ pub fn nudge(point: &mut Point, by: i32) { point.y += by; } +/// Nudge `point` by the length of `refusal`'s reason: a value Rust writes back beside an +/// object a check of can fail. +pub fn nudge_by(point: &mut Point, refusal: &Refusal) { + nudge(point, i32::try_from(refusal.reason.len()).unwrap_or(i32::MAX)); +} + /// A config of a crate no export is, behind an `Arc`, as moq-tokio's listener returns /// rustls's: an opaque handle, which Rust takes back. pub fn secure(alpn: &str) -> Arc { diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index 47445a549..77b78f834 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -987,6 +987,60 @@ impl Object { Ok(()) } + /// Borrow a `Copy` value shared until `takes` drops, so [`Self::held`] copies it out + /// once the call's moves commit, and no check after them can fail. + /// + /// # Safety + /// As [`Self::reserve`]. + pub unsafe fn hold<'a>(handle: *const c_void, takes: &mut Takes<'a>, what: &str) -> Result<(), Status> + where + T: Sync, + { + // SAFETY: the caller's contract. + let object = unsafe { Self::get(handle, what) }; + object.acquire()?; + takes.held.push((handle, Self::unhold)); + Ok(()) + } + + /// [`Self::hold`], for a value that is not `Sync`. + /// + /// # Safety + /// As [`Self::reserve`]. + pub unsafe fn hold_local<'a>(handle: *const c_void, takes: &mut Takes<'a>, what: &str) -> Result<(), Status> { + // SAFETY: the caller's contract. + let object = unsafe { Self::get(handle, what) }; + object.acquire_local()?; + takes.held.push((handle, Self::unhold_local)); + Ok(()) + } + + /// # Safety + /// [`Self::hold`] borrowed `handle`'s value, and this ends that borrow. + unsafe fn unhold(handle: *const c_void) { + // SAFETY: the caller's contract. + unsafe { &*handle.cast::() }.release(); + } + + /// # Safety + /// [`Self::hold_local`] borrowed `handle`'s value, and this ends that borrow. + unsafe fn unhold_local(handle: *const c_void) { + // SAFETY: the caller's contract. + unsafe { &*handle.cast::() }.release_local(); + } + + /// A copy of the value [`Self::hold`] or [`Self::hold_local`] borrowed. + /// + /// # Safety + /// One of them borrowed `handle`'s value into a `Takes` not yet dropped. + pub unsafe fn held(handle: *const c_void) -> T + where + T: Copy, + { + // SAFETY: the caller's contract: the borrow excludes every write. + unsafe { *(*handle.cast::()).value() } + } + /// Move out the value [`Self::take`] reserved, leaving the handle to be freed. /// /// # Safety @@ -1069,11 +1123,13 @@ impl Drop for Object { } /// The values a call reserved to move out, given back unless it commits: so a call whose -/// later check fails moved nothing. A call's first few allocate nothing. +/// later check fails moved nothing. A call's first few allocate nothing. It also holds the +/// `Copy` values the call copies out borrowed until it drops, after they are copied. #[derive(Default)] pub struct Takes<'a> { first: [Option<&'a AtomicUsize>; 2], rest: Vec<&'a AtomicUsize>, + held: Vec<(*const c_void, unsafe fn(*const c_void))>, } impl<'a> Takes<'a> { @@ -1085,7 +1141,7 @@ impl<'a> Takes<'a> { } /// Keep every value reserved, for the call to move out. - pub fn commit(mut self) { + pub fn commit(&mut self) { self.first = [None; 2]; self.rest.clear(); } @@ -1096,6 +1152,10 @@ impl Drop for Takes<'_> { for state in self.first.iter().flatten().chain(&self.rest) { state.store(0, Ordering::Release); } + for (handle, unhold) in self.held.drain(..) { + // SAFETY: `hold` or `hold_local` borrowed it, for this to end. + unsafe { unhold(handle) }; + } } } diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index 378005600..51cc0bda7 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -926,12 +926,22 @@ impl<'a> Abi<'a> { /// Whether a call of `function`, a method of `owner` if it has one, can fail a check of /// a handle it borrows or moves, so it takes a `status` to write why. pub fn status(&self, function: &Function, owner: Option<&Ty>) -> bool { - let receiver = match (function.receiver, owner) { - (Some(_), Some(Ty::Handle { name, .. })) => (0..self.api.objects.len()).find(|&i| &self.object(i) == name), - _ => None, - }; - let mut handles: Vec = receiver.into_iter().collect(); + let mut handles = Vec::new(); let mut seen = Vec::new(); + match (function.receiver, owner) { + (Some(_), Some(Ty::Handle { name, .. })) => { + handles.extend((0..self.api.objects.len()).find(|&i| &self.object(i) == name)) + } + // A record's or enum's receiver lifts each handle it holds. + (Some(_), Some(owner)) => { + let records = (0..self.api.records.len()).map(Type::Record); + let enums = (0..self.api.enums.len()).map(Type::Enum); + if let Some(ty) = records.chain(enums).find(|ty| &self.ty(ty) == owner) { + self.api.handles(&ty, &mut handles, &mut seen); + } + } + _ => {} + } for param in &function.params { self.api.handles(¶m.ty, &mut handles, &mut seen); } @@ -1063,6 +1073,10 @@ impl<'a> Abi<'a> { let name = format!("{}_{constant}", self.prefix.to_uppercase()); self.claim(name, "the runtime".to_string(), crate_span, None); } + for (status, _) in STATUSES { + let name = format!("{}_STATUS_{}", self.prefix.to_uppercase(), status.to_uppercase()); + self.claim(name, "the runtime".to_string(), crate_span, None); + } let mut types: Vec<&Type> = Vec::new(); // Each function, with its type's C name and segments if it is a method. let mut functions: Vec<(&Function, Option, Vec)> = Vec::new(); diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 68872b24d..16578db67 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -423,26 +423,62 @@ impl Gen<'_> { self.abi.value_status(ty) } - /// Whether `ty` moves an object's value out of its handle, as passed by value. + /// Whether `ty` moves an object's value out of its handle, as passed by value, held + /// alone or in any value. fn moves(&self, ty: &Type) -> bool { + self.holds(ty, &|i| !self.abi.api.objects[i].copy, &mut Vec::new()) + } + + /// Whether lifting `ty` within a moved argument reserves anything into `takes`: a value + /// it moves out, or a checked `Copy` one it copies. + fn reserves(&self, ty: &Type) -> bool { + let pick = |i: usize| !self.abi.api.objects[i].copy || self.abi.checked[i]; + self.holds(ty, &pick, &mut Vec::new()) + } + + /// Whether a value of `ty`, passed by value, holds an object whose index `pick` picks. + fn holds(&self, ty: &Type, pick: &dyn Fn(usize) -> bool, seen: &mut Vec<(bool, usize)>) -> bool { + let api = self.abi.api; match ty { - Type::Object(i) => !self.abi.api.objects[*i].copy, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => self.moves(inner), - Type::Tuple(elements) => elements.iter().any(|ty| self.moves(ty)), + Type::Object(i) => pick(*i), + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => self.holds(inner, pick, seen), + Type::Tuple(elements) => elements.iter().any(|ty| self.holds(ty, pick, seen)), + Type::Record(i) if !seen.contains(&(false, *i)) => { + seen.push((false, *i)); + (api.records[*i].fields.iter()).any(|f| self.holds(&f.ty, pick, seen)) + } + Type::Enum(i) if !seen.contains(&(true, *i)) => { + seen.push((true, *i)); + (api.enums[*i].variants.iter().flat_map(|v| &v.fields)).any(|f| self.holds(&f.ty, pick, seen)) + } _ => false, } } - /// The statement reserving each value an argument `e` of type `ty` moves out, into - /// `takes`, so the move happens only once every check passed. + /// The statement reserving into `takes` each value an argument `e` of type `ty` moves + /// out, and borrowing each checked `Copy` one it copies, so the move happens only once + /// every check passed, and no check after it can fail. fn reserve(&self, ty: &Type, e: &str) -> String { match ty { - Type::Object(i) => { + Type::Object(i) if !self.abi.api.objects[*i].copy => { self.checked(&format!( - "unsafe {{ rt::Object::<{}>::reserve({e}, &mut takes, w) }}", + "unsafe {{ rt::Object::<{}>::reserve({e}, takes, w) }}", self.object_path(*i) )) + ";" } + Type::Object(i) if self.abi.checked[*i] => { + let hold = match self.abi.api.objects[*i].sync { + true => "hold", + false => "hold_local", + }; + self.checked(&format!( + "unsafe {{ rt::Object::<{}>::{hold}({e}, takes, w) }}", + self.object_path(*i) + )) + ";" + } + Type::Object(_) => String::new(), + Type::Record(i) => self.checked(&format!("reserve_{}(&{e}, takes, w)", self.abi.record(*i))) + ";", + Type::Enum(i) => self.checked(&format!("reserve_{}(&{e}, takes, w)", self.abi.enumeration(*i))) + ";", Type::Box(inner) => self.reserve(inner, e), Type::Option(inner) => match inner.base() { Type::Object(_) => format!("if !{e}.is_null() {{ {} }}", self.reserve(inner, e)), @@ -454,7 +490,7 @@ impl Gen<'_> { self.reserve(inner, "(*c)") ), Type::Tuple(elements) => (elements.iter().enumerate()) - .filter(|(_, ty)| self.moves(ty)) + .filter(|(_, ty)| self.reserves(ty)) .map(|(n, ty)| self.reserve(ty, &format!("{e}._{n}"))) .collect::>() .join(" "), @@ -552,7 +588,7 @@ impl Gen<'_> { }; let lifted = self.lift(inner, "(*c)"); // A failed check inside returns from the closure each item lifts in. - let fails = self.checks(inner) || (self.moves(inner) && !self.reserved.get()); + let fails = !self.reserved.get() && (self.checks(inner) || self.moves(inner)); match self.checking.get() && fails { true => format!( "unsafe {{ {e}.items(w) }}.iter().map(|c| -> ::std::result::Result<_, rt::Status> {{ Ok({lifted}) }}).collect::<::std::result::Result<{}<{args}>, rt::Status>>()?", @@ -581,6 +617,13 @@ impl Gen<'_> { }, Type::Box(inner) => format!("::std::boxed::Box::new({})", self.lift(inner, e)), Type::Ref(_, inner) => self.lift(inner, e), + // Reserved, its checked copies passed their checks. + Type::Record(i) if self.reserved.get() && self.reserves(ty) => { + format!("lift_{}_taken(&{e}, w)", self.abi.record(*i)) + } + Type::Enum(i) if self.reserved.get() && self.reserves(ty) => { + format!("lift_{}_taken(&{e}, w)", self.abi.enumeration(*i)) + } Type::Record(i) => match self.checks(ty) { true => self.checked(&format!("lift_{}(&{e}, w)", self.abi.record(*i))), false => format!("lift_{}(&{e}, w)", self.abi.record(*i)), @@ -599,6 +642,8 @@ impl Gen<'_> { let value = match (object.copy, self.reserved.get()) { // One no export can misuse is copied out uncounted, as it is borrowed. (true, _) if !self.abi.checked[*i] => format!("*unsafe {{ rt::Object::<{path}>::peek({e}, w) }}"), + // Reserved, it is borrowed until the call's moves are made. + (true, true) => format!("unsafe {{ rt::Object::<{path}>::held({e}) }}"), (true, _) => format!( "*{}", self.checked(&format!( @@ -1206,6 +1251,29 @@ impl Gen<'_> { .collect(); self.item(format!("fn free_{name}(c: {name}) {{\n\t{}\n}}\n", frees.join("\n\t"))); } + if self.reserves(&ty) { + self.checking.set(true); + let reserves: Vec = (record.fields.iter().enumerate()) + .filter(|(_, f)| self.reserves(&f.ty)) + .map(|(n, f)| self.reserve(&f.ty, &format!("c.f{n}"))) + .collect(); + self.checking.set(false); + self.reserved.set(true); + let lifted: Vec = (record.fields.iter().enumerate()) + .map(|(n, f)| self.lift(&f.ty, &format!("c.f{n}"))) + .collect(); + self.reserved.set(false); + let build = match record.tuple { + true => format!("{path}({})", lifted.join(", ")), + false => { + let fields: Vec = (record.fields.iter().zip(&lifted)) + .map(|(f, l)| format!("{}: {l}", f.name)) + .collect(); + format!("{path} {{ {} }}", fields.join(", ")) + } + }; + self.reserved_helpers(&name, &path, &reserves.join("\n\t"), &build); + } self.value_traits(&name, &path, &ty, record.traits); let owner = Ty::Struct(name.clone()); for method in &record.methods { @@ -1304,7 +1372,11 @@ impl Gen<'_> { ); } if variant.fields.is_empty() { - let _ = write!(lift, "{arm}\n\t\t{n} => {pattern},"); + let lifted = match checks { + true => format!("Ok({pattern})"), + false => pattern.clone(), + }; + let _ = write!(lift, "{arm}\n\t\t{n} => {lifted},"); let _ = write!( lower, "{arm}\n\t\t{pattern} => {name} {{ tag: {n}, u: unsafe {{ ::std::mem::zeroed() }} }}," @@ -1381,6 +1453,60 @@ impl Gen<'_> { )); } } + if !e.plain() && self.reserves(&ty) { + let mut reserve = String::new(); + let mut lift = String::new(); + for (n, variant) in e.variants.iter().enumerate() { + let arm = self.arm(variant.on); + let fields = |lifted: &[String]| match (variant.fields.is_empty(), variant.tuple) { + (true, _) => format!("{path}::{}", variant.name), + (false, true) => format!("{path}::{}({})", variant.name, lifted.join(", ")), + (false, false) => { + let fields: Vec = (variant.fields.iter().zip(lifted)) + .map(|(f, l)| format!("{}: {l}", f.name)) + .collect(); + format!("{path}::{} {{ {} }}", variant.name, fields.join(", ")) + } + }; + if let Some(cfg) = self.cfg(variant.on) { + let missing = format!( + "\n\t\t#[cfg(not({cfg}))]\n\t\t{n} => rt::fail(w, \"the variant is not on this library's target\")," + ); + reserve.push_str(&missing); + lift.push_str(&missing); + } + if variant.fields.is_empty() { + let _ = write!(lift, "{arm}\n\t\t{n} => {},", fields(&[])); + continue; + } + self.checking.set(true); + let reserves: Vec = (variant.fields.iter().enumerate()) + .filter(|(_, f)| self.reserves(&f.ty)) + .map(|(k, f)| self.reserve(&f.ty, &format!("p.f{k}"))) + .collect(); + self.checking.set(false); + if !reserves.is_empty() { + let _ = write!( + reserve, + "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{}\n\t\t}}", + reserves.join("\n\t\t\t") + ); + } + self.reserved.set(true); + let lifted: Vec = (variant.fields.iter().enumerate()) + .map(|(k, f)| self.lift(&f.ty, &format!("p.f{k}"))) + .collect(); + self.reserved.set(false); + let _ = write!( + lift, + "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{}\n\t\t}}", + fields(&lifted) + ); + } + let reserve = format!("match c.tag {{{reserve}\n\t\t_ => {{}}\n\t}}"); + let build = format!("match c.tag {{{lift}\n\t\t_ => rt::fail(w, \"an enum tag is invalid\"),\n\t}}"); + self.reserved_helpers(&name, &path, &reserve, &build); + } self.value_traits(&name, &path, &ty, e.traits); let owner = self.abi.ty(&ty); for method in &e.methods { @@ -1388,6 +1514,18 @@ impl Gen<'_> { } } + /// A record's or data enum's helpers for a moved argument holding one: `reserve_`, + /// running `reserve`, which reserves into `takes` what it moves out and borrows what it + /// copies, and `lift__taken`, which makes `build` of what was reserved, unchecked. + fn reserved_helpers(&mut self, name: &str, path: &str, reserve: &str, build: &str) { + self.item(format!( + "fn reserve_{name}(c: &{name}, takes: &mut rt::Takes<'_>, w: &str) -> ::std::result::Result<(), rt::Status> {{\n\tlet _ = (c, &takes, w);\n\t{reserve}\n\tOk(())\n}}\n" + )); + self.item(format!( + "fn lift_{name}_taken(c: &{name}, w: &str) -> {path} {{\n\tlet _ = w;\n\t{build}\n}}\n" + )); + } + /// A callback: its table of foreign functions, and the Rust type that implements the /// trait by calling them. fn callback(&mut self, i: usize) { @@ -1780,12 +1918,20 @@ impl Gen<'_> { let mut stmts = { let mut all = stmts; all.extend(checks); - if !reserves.is_empty() { - all.insert(0, "let mut takes = rt::Takes::default();".to_string()); + let reserved = !reserves.is_empty(); + if reserved { + all.insert( + 0, + "let mut reserved = rt::Takes::default();\n\tlet takes = &mut reserved;".to_string(), + ); all.extend(reserves); all.push("takes.commit();".to_string()); } all.extend(moves); + // The copies are made, so their borrows end before the call runs. + if reserved { + all.push("drop(reserved);".to_string()); + } all }; // A future made of a `Poll` calls the function at each poll, passing the poll's diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 2f7e163be..826636ec8 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -1637,8 +1637,20 @@ impl<'a> Gen<'a> { args.push(arg); } let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); + // What runs once the call returns, gated on its checks passing when it can fail one: + // a failed check, or an argument whose lowering throws, leaves every argument as it + // was, as Rust ran nothing. + let ran = + status && (function.params.iter().any(|p| p.ty.written()) || args.iter().any(|a| !a.after.is_empty())); + if ran { + prelude.push("var c_ran_ = false".to_string()); + } + let gate = |stmts: &str| match ran { + true => format!("defer {{ if c_ran_ {{ {stmts} }} }}"), + false => format!("defer {{ {stmts} }}"), + }; // Each value passed for `&mut` takes what Rust wrote back, once the call's own - // access to it ends, on every path, a throw included. + // access to it ends, on every path the call ran, a throw included. let mut written = Vec::new(); for (param, name) in function.params.iter().zip(&names).filter(|(p, _)| p.ty.written()) { let out = Abi::written(function, &abi::escape(¶m.name)); @@ -1648,7 +1660,7 @@ impl<'a> Gen<'a> { .expect("a written parameter has its out"); prelude.push(self.out_decl(&out, ty)); let lifted = self.lift(param.ty.base(), &format!("c_{out}_")); - prelude.push(format!("defer {{ {name} = {lifted} }}")); + prelude.push(gate(&format!("{name} = {lifted}"))); written.push(out); } if arena { @@ -1659,7 +1671,7 @@ impl<'a> Gen<'a> { } let after: Vec = args.iter().flat_map(|a| a.after.clone()).collect(); if !after.is_empty() { - prelude.push(format!("defer {{ {} }}", after.join("; "))); + prelude.push(gate(&after.join("; "))); } let exprs: Vec = args.iter().flat_map(|a| a.exprs.clone()).collect(); @@ -1678,7 +1690,10 @@ impl<'a> Gen<'a> { inner.push("var c_status_: Swift.Int32 = 0".to_string()); ( vec!["&c_status_".to_string()], - Some(format!("try Native.status(c_status_, \"{c_name}\")")), + Some(match ran { + true => format!("try Native.status(c_status_, \"{c_name}\")\nc_ran_ = true"), + false => format!("try Native.status(c_status_, \"{c_name}\")"), + }), ) } false => (Vec::new(), None), diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 6fd2619e9..1c682b782 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -1688,6 +1688,57 @@ static int statuses(void) { api_Meter_free(first); api_Meters_free(meters); + /* A counter moved in beside a dropped ticket, alone or held in a record or an enum, is + * refused before the move, so the counter is still there; once every ticket is live, + * the call moves it. */ + api_Counter *admitted = api_Counter_new(str("a"), 5); + api_Ticket *ticket = api_Ticket_new(3); + api_Ticket *dropped = api_Ticket_new(4); + api_Ticket_drop(dropped, NULL); + api_Choice picked = {.tag = API_CHOICE_PICKED, .picked = {._0 = ticket}}; + api_Choice empty = {.tag = API_CHOICE_EMPTY}; + api_tuple_Counter_Ticket_Seat_Choice entries[] = { + {admitted, dropped, {ticket, 1}, empty}, + {admitted, ticket, {dropped, 1}, empty}, + {admitted, ticket, {ticket, 1}, {.tag = API_CHOICE_PICKED, .picked = {._0 = dropped}}}, + }; + for (size_t i = 0; i < sizeof(entries) / sizeof(entries[0]); i++) { + status = 0; + api_admit(entries[i], &status); + uint64_t still = api_Counter_get(admitted, NULL); + if (status != API_STATUS_MOVED || still != 5) { + fprintf(stderr, "admitting beside a dropped ticket %zu wrote %d, leaving %llu\n", i, status, (unsigned long long)still); + failed = 1; + } + } + status = 0; + uint64_t sum = api_admit((api_tuple_Counter_Ticket_Seat_Choice){admitted, ticket, {ticket, 1}, picked}, &status); + if (status != 0 || sum != 14) { + fprintf(stderr, "admitting wrote %d, summing %llu\n", status, (unsigned long long)sum); + failed = 1; + } + api_Counter_get(admitted, &status); + if (status != API_STATUS_MOVED) { + fprintf(stderr, "a counter admitted wrote %d\n", status); + failed = 1; + } + /* A record's method lifts the handle its receiver holds, failing as a call does. */ + status = 0; + api_Seat seat = {dropped, 2}; + api_Seat_number(&seat, &status); + if (status != API_STATUS_MOVED) { + fprintf(stderr, "a seat's number with a dropped ticket wrote %d\n", status); + failed = 1; + } + status = 0; + if (api_choose(empty, &status) != 0 || api_choose(picked, &status) != 3 || status != 0) { + fprintf(stderr, "choosing wrote %d\n", status); + failed = 1; + } + api_Counter_free(admitted); + api_Ticket_free(ticket); + api_Ticket_free(dropped); + status = 0; api_Registry *registry = api_Registry_new(api_Counter_new(str("c"), 0), NULL); if (!api_Registry_edit(registry, move_lent_status, &status, NULL)) { diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index fd768c2e4..92a28e70e 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -633,6 +633,19 @@ func script() { } catch { check(false, "api_reasons threw \(error)") } + // A call that throws before Rust runs changes none of its arguments: it closes no + // class it would move, and writes back no value Rust would. + let live = refusal("live") + var mixed = UniqueArray() + mixed.append(TupleCounterRefusal(Counter.new("e", 5), live)) + mixed.append(TupleCounterRefusal(Counter.new("f", 6), kept)) + check((try? reasons(mixed, nil)) == nil, "api_reasons of a closed Refusal beside a live one throws") + check(try! live.description == "refused: live", "a refused call leaves the class it would move open") + var at = Point(x: 1, y: 2) + check((try? nudgeBy(&at, kept)) == nil, "api_nudge_by of a closed Refusal throws") + check(at == Point(x: 1, y: 2), "a refused call writes nothing back") + try! nudgeBy(&at, live) + check(at == Point(x: 5, y: 6), "a call writes back what Rust left") // Each optional borrowed parameter is an overload with and without its label. let one = Counter.new("one", 1) check( From 1c1cf99cd5479ad3232b4d34385e09c3ce66d31c Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:27:16 -0800 Subject: [PATCH 21/30] fix(ffi): answer #730's review threads, and port moq's programs and Miri to the status ABI - rt: a move a call reserves holds RESERVED until the call commits, so a close or use meanwhile fails as borrowed instead of reading it as gone, and a rollback revives no value a close reported dropped. - Dart: the served glue gives the call a status of its own and fills the isolate's cell only once `serve` stopped serving, so a call it served meanwhile reads only its own. - Swift: a lowering that throws runs before any box the call gives, which a throw would leak; a trait body that throws, and `default()` of a type some targets lack, throws the target check instead of trapping. - Go: the std traits of a type some targets lack return the target error instead of panicking: `Default` and `Equal` gain an `error`, `String` and `GoString` read as it. - Docs: a passed call leaves `*status` alone; trait members follow the target rule, and a variant traps in Swift and panics in Go. Doc comments split by inserted code are rejoined. - Tests: the Swift target check is caught and named; new wasm32-only `Page` and `Booth` check every binding's traits off target; `relay` checks Swift frees a callback it gave before a throw; Miri's test drives the status ABI and checks a close under a reservation. - ffi/moq/moq.c and moq.swift take the status ABI, which `just ffi moq` needs nightly. - go-status records the maintainer's 2026-11-13 answers. Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 3 +- ffi/README.md | 13 +- ffi/fixtures/api/src/lib.rs | 22 ++- ffi/moq/moq.c | 308 ++++++++++++++++---------------- ffi/moq/moq.swift | 153 ++++++++-------- ffi/runtime/rt.dart | 6 +- ffi/runtime/rt.rs | 30 ++-- ffi/runtime/rt.swift | 4 +- ffi/src/cpp.rs | 2 +- ffi/src/dart.rs | 26 ++- ffi/src/go.rs | 36 +++- ffi/src/swift.rs | 68 +++++-- ffi/tests/api.dart | 6 + ffi/tests/api.go | 15 ++ ffi/tests/api.kt | 3 + ffi/tests/api.py | 3 + ffi/tests/api.swift | 52 +++++- ffi/tests/fixtures.rs | 1 - ffi/tests/miri/api.rs | 41 +++-- quest/m1/ffi-speed/go-status.md | 27 ++- 20 files changed, 505 insertions(+), 314 deletions(-) diff --git a/bench/ffi/README.md b/bench/ffi/README.md index cd110f607..6c1dcb3c9 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -212,7 +212,8 @@ Kotlin's rows match main's within the noise: its object return's fastest runs to since cgo moves to the heap a local whose address it passes C, which costs 6 ns in a cgo call alone; its close calls Rust's `_drop`, a second crossing an object return and a stamped write pay. Dart's checked calls first lost 4 ns to a status allocated per call, -now one cell per isolate. Swift's getter costs C's since it dropped its hold count for +now one cell per isolate, which the glue fills from the call's own once the call stopped +serving the isolate, for 1 ns more. Swift's getter costs C's since it dropped its hold count for each noncopyable object (2026-11-11). A change to the accounting moves these rows; measure them at the load beside them. diff --git a/ffi/README.md b/ffi/README.md index ec36e4da6..19c4d051f 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -165,10 +165,14 @@ item that target compiles works exactly, and no other does: target before calling it, raising the language's own error, as for a closed object: `UnsupportedOperationException` in Kotlin, `NotImplementedError` in Python, a `CheckError` thrown in Swift, an error matching `errors.ErrUnsupported` returned in Go, - and `UnsupportedError` in Dart. C and C++ abort with the message. + and `UnsupportedError` in Dart. C and C++ abort with the message. A type's std traits + are its methods here, and its `Default` a constructor, except where the language's own + protocol cannot fail: Swift traps in a `description`, `debugDescription`, or `==` that + does not throw, and Go's `String` and `GoString` return the error's text. - So does a variant only some targets have, when a value holding it crosses into a target - without it. A record's fields cross whole, so one whose fields differ between targets - is refused, as below. + without it, except that Swift traps and Go panics, as a value's conversion there returns + no error. A record's fields cross whole, so one whose fields differ between targets is + refused, as below. - A type only some targets have is declared everywhere, and only those targets' items make or take it. - A constant only some targets have keeps its value everywhere, since reading it calls @@ -235,7 +239,8 @@ where it was while its file is unchanged. or a moved-out handle, fails the call before any Rust code runs, so it changes nothing: a function whose handles can fail a check takes a trailing `_status *status`, through which it writes the `_STATUS_*` that failed, returning a zeroed value, - and `_status_message` names it. A NULL `status` aborts with the message, naming + and `_status_message` names it. A call that passes its checks leaves `*status` + as it was, so a caller zeroes it before each call it reads it after. A NULL `status` aborts with the message, naming the function, as C++ passes; each other binding raises its language's own error, as below. A null handle aborts. Only a type some export can misuse is checked: one that is not `Sync`, or that an export borrows as `&mut`, moves out, or lends a closure; a call diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index c0c9f9aee..177bcc8ac 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -2635,6 +2635,12 @@ pub fn reasons(pairs: Vec<(Counter, Refusal)>, last: Option<(Counter, Refusal)>) .join(", ") } +/// How many of `refusals` `log` keeps, each logged at level 0: a callback the binding gives +/// before a list whose classes a move may have closed. +pub fn relay(log: Box, refusals: Vec) -> u32 { + refusals.iter().filter(|r| log.log(0, &r.to_string())).count() as u32 +} + /// Always refuses, for `reason`. pub fn refuse(reason: &str) -> Result> { Err(Box::new(Refusal { @@ -2719,12 +2725,26 @@ impl Worker { } } -/// A record only wasm32 has. +/// A record only wasm32 has, whose std traits fail on another target as its functions do. #[cfg(target_arch = "wasm32")] +#[derive(Debug, Default, PartialEq)] pub struct Page { pub size: u32, } +/// A record only wasm32 has, holding a checked object, so its std traits take a status. +#[cfg(target_arch = "wasm32")] +#[derive(Debug, PartialEq)] +pub struct Booth { + pub ticket: Ticket, +} + +/// The number of `booth`'s ticket. +#[cfg(target_arch = "wasm32")] +pub fn booth_number(booth: Booth) -> u32 { + booth.ticket.number +} + /// wasm32's page. #[cfg(target_arch = "wasm32")] pub fn page() -> Page { diff --git a/ffi/moq/moq.c b/ffi/moq/moq.c index 05f700cf9..aab3809c2 100644 --- a/ffi/moq/moq.c +++ b/ffi/moq/moq.c @@ -45,14 +45,14 @@ static int is(moq_buf buf, const char *s) { /* Moves the stats track in data into the lent catalog, in place. */ static void sets_stats(void *data, moq_hang_Catalog_Extra *catalog) { - moq_hang_Catalog_Extra_stats_set(catalog, data); + moq_hang_Catalog_Extra_stats_set(catalog, data, NULL); } /* Whether the lent catalog lists its stats track as `stats`: counts it in data. */ static void lists_stats(void *data, moq_hang_Catalog_Extra *catalog) { - moq_hang_catalog_Stats *stats = moq_hang_Catalog_Extra_stats_get(catalog); + moq_hang_catalog_Stats *stats = moq_hang_Catalog_Extra_stats_get(catalog, NULL); moq_hang_catalog_Stats *want = moq_hang_catalog_Stats_new(str("stats")); - if (stats != NULL && moq_hang_catalog_Stats_eq(stats, want)) { + if (stats != NULL && moq_hang_catalog_Stats_eq(stats, want, NULL)) { (*(int *)data)++; } moq_hang_catalog_Stats_free(want); @@ -72,10 +72,10 @@ static int contains(moq_buf buf, const char *s) { /* Sets the lent catalog's `app` section to the JSON document in data, in place. */ static void sets_app(void *data, moq_hang_Catalog_Extra *catalog) { - moq_mux_catalog_hang_Extra *sections = moq_hang_Catalog_Extra_ext_get(catalog); + moq_mux_catalog_hang_Extra *sections = moq_hang_Catalog_Extra_ext_get(catalog, NULL); moq_mux_Error *err = NULL; - CHECK(moq_mux_catalog_hang_Extra_set(sections, str("app"), data, &err)); - moq_hang_Catalog_Extra_ext_set(catalog, sections); + CHECK(moq_mux_catalog_hang_Extra_set(sections, str("app"), data, &err, NULL)); + moq_hang_Catalog_Extra_ext_set(catalog, sections, NULL); moq_mux_catalog_hang_Extra_free(sections); } @@ -88,11 +88,11 @@ struct readback { static void reads_back(void *data, moq_hang_Catalog_Extra *catalog) { struct readback *back = data; moq_hang_Error *hang_err = NULL; - CHECK(moq_hang_Catalog_Extra_to_json(catalog, &back->json, &hang_err)); - moq_mux_catalog_hang_Extra *sections = moq_hang_Catalog_Extra_ext_get(catalog); - moq_Json *app = moq_mux_catalog_hang_Extra_get(sections, str("app")); + CHECK(moq_hang_Catalog_Extra_to_json(catalog, &back->json, &hang_err, NULL)); + moq_mux_catalog_hang_Extra *sections = moq_hang_Catalog_Extra_ext_get(catalog, NULL); + moq_Json *app = moq_mux_catalog_hang_Extra_get(sections, str("app"), NULL); if (app != NULL) { - back->app = moq_Json_to_text(app); + back->app = moq_Json_to_text(app, NULL); moq_Json_free(app); } moq_mux_catalog_hang_Extra_free(sections); @@ -167,50 +167,50 @@ int main(void) { CHECK(moq_contract() == MOQ_CONTRACT); moq_broadcast_Info *info = moq_broadcast_Info_new(); - moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(info); + moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(info, NULL); moq_broadcast_Info_free(info); moq_track_Producer *track = NULL; - CHECK(moq_broadcast_Producer_create_track(broadcast, str("video"), NULL, &track, &err)); - CHECK(is(moq_track_Producer_name(track), "video")); + CHECK(moq_broadcast_Producer_create_track(broadcast, str("video"), NULL, &track, &err, NULL)); + CHECK(is(moq_track_Producer_name(track, NULL), "video")); /* A second track of the same name is an error, printed as Rust prints it. */ moq_track_Producer *duplicate = NULL; - CHECK(!moq_broadcast_Producer_create_track(broadcast, str("video"), NULL, &duplicate, &err)); - CHECK(err != NULL && is(moq_Error_to_string(err), "duplicate")); + CHECK(!moq_broadcast_Producer_create_track(broadcast, str("video"), NULL, &duplicate, &err, NULL)); + CHECK(err != NULL && is(moq_Error_to_string(err, NULL), "duplicate")); moq_Error_free(err); - moq_track_Consumer *consumer = moq_track_Producer_consume(track); - moq_track_Consumer_subscribe_future *subscribing = moq_track_Consumer_subscribe(consumer, NULL); + moq_track_Consumer *consumer = moq_track_Producer_consume(track, NULL); + moq_track_Consumer_subscribe_future *subscribing = moq_track_Consumer_subscribe(consumer, NULL, NULL); moq_track_Subscriber *subscriber = NULL; CHECK(moq_track_Consumer_subscribe_poll(subscribing, waker, &subscriber, &err) == MOQ_POLL_READY); moq_track_Consumer_subscribe_future_free(subscribing); /* A group's arrival wakes the subscriber's pending receive. */ - moq_track_Subscriber_recv_group_future *receiving = moq_track_Subscriber_recv_group(subscriber); + moq_track_Subscriber_recv_group_future *receiving = moq_track_Subscriber_recv_group(subscriber, NULL); moq_group_Consumer *group = NULL; CHECK(moq_track_Subscriber_recv_group_poll(receiving, waker, &group, &err) == MOQ_POLL_PENDING); moq_group_Producer *writer = NULL; - CHECK(moq_track_Producer_append_group(track, &writer, &err)); + CHECK(moq_track_Producer_append_group(track, &writer, &err, NULL)); CHECK(wakes == 1); CHECK(moq_track_Subscriber_recv_group_poll(receiving, waker, &group, &err) == MOQ_POLL_READY); moq_track_Subscriber_recv_group_future_free(receiving); - CHECK(group != NULL && moq_group_Consumer_index(group) == 0); + CHECK(group != NULL && moq_group_Consumer_index(group, NULL) == 0); /* So does a frame's. */ - moq_group_Consumer_read_frame_future *reading = moq_group_Consumer_read_frame(group); + moq_group_Consumer_read_frame_future *reading = moq_group_Consumer_read_frame(group, NULL); moq_frame_Frame *frame = NULL; CHECK(moq_group_Consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_PENDING); - CHECK(moq_group_Producer_write_frame(writer, NULL, str("hello"), &err)); + CHECK(moq_group_Producer_write_frame(writer, NULL, str("hello"), &err, NULL)); CHECK(wakes == 2); CHECK(moq_group_Consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_READY); moq_group_Consumer_read_frame_future_free(reading); - CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame), "hello")); - CHECK(moq_frame_Frame_timestamp_get(frame) == NULL); + CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame, NULL), "hello")); + CHECK(moq_frame_Frame_timestamp_get(frame, NULL) == NULL); /* When it arrived: kio's clock's `Instant`, from kio's own export. */ - moq_clock_Instant *arrived = moq_frame_Frame_arrived(frame); + moq_clock_Instant *arrived = moq_frame_Frame_arrived(frame, NULL); moq_clock_Instant *now = moq_clock_now(); - CHECK(moq_clock_Instant_checked_duration_since(now, arrived).some); + CHECK(moq_clock_Instant_checked_duration_since(now, arrived, NULL).some); moq_clock_Instant_free(now); moq_clock_Instant_free(arrived); moq_frame_Frame_free(frame); @@ -220,21 +220,21 @@ int main(void) { moq_Timestamp *at = NULL; moq_TimeOverflow *overflow = NULL; CHECK(moq_Timestamp_from_millis(1500, &at, &overflow)); - CHECK(moq_Timestamp_as_secs(at) == 1); - CHECK(!moq_group_Producer_write_frame(writer, at, str("late"), &err)); - CHECK(err != NULL && is(moq_Error_to_string(err), "frame timestamp doesn't fit its track")); + CHECK(moq_Timestamp_as_secs(at, NULL) == 1); + CHECK(!moq_group_Producer_write_frame(writer, at, str("late"), &err, NULL)); + CHECK(err != NULL && is(moq_Error_to_string(err, NULL), "frame timestamp doesn't fit its track")); moq_Error_free(err); - CHECK(moq_Timestamp_as_secs(at) == 1); + CHECK(moq_Timestamp_as_secs(at, NULL) == 1); /* The last frame, bytes that are not UTF-8, then the group's end, which reads as None. */ - CHECK(moq_group_Producer_write_frame(writer, NULL, str("\xffworld"), &err)); - CHECK(moq_group_Producer_finish(writer, &err)); - reading = moq_group_Consumer_read_frame(group); + CHECK(moq_group_Producer_write_frame(writer, NULL, str("\xffworld"), &err, NULL)); + CHECK(moq_group_Producer_finish(writer, &err, NULL)); + reading = moq_group_Consumer_read_frame(group, NULL); CHECK(moq_group_Consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_READY); moq_group_Consumer_read_frame_future_free(reading); - CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame), "\xffworld")); + CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame, NULL), "\xffworld")); moq_frame_Frame_free(frame); - reading = moq_group_Consumer_read_frame(group); + reading = moq_group_Consumer_read_frame(group, NULL); frame = (moq_frame_Frame *)1; CHECK(moq_group_Consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_READY); CHECK(frame == NULL); @@ -253,11 +253,11 @@ int main(void) { moq_origin_Config *config = moq_origin_Config_default(); moq_origin_Producer *origin = NULL; moq_origin_Driver *driver = NULL; - moq_origin_Producer_new(config, &origin, &driver); + moq_origin_Producer_new(config, &origin, &driver, NULL); moq_origin_Config_free(config); CHECK(origin != NULL && driver != NULL); - moq_Path *root = moq_origin_Producer_root(origin); - CHECK(is(moq_Path_as_str(root), "")); + moq_Path *root = moq_origin_Producer_root(origin, NULL); + CHECK(is(moq_Path_as_str(root, NULL), "")); moq_Path_free(root); /* Scoped beneath a root to patterns, which moq-pattern's own export builds. */ @@ -265,56 +265,56 @@ int main(void) { moq_InvalidPattern invalid; CHECK(moq_Pattern_subtree(str("alice"), &alice, &invalid)); moq_Patterns *patterns = moq_Patterns_new(); - CHECK(moq_Patterns_insert(patterns, alice)); + CHECK(moq_Patterns_insert(patterns, alice, NULL)); moq_Pattern_free(alice); moq_origin_Producer *scoped = NULL; - CHECK(moq_origin_Producer_scope(origin, str("room"), patterns, &scoped, &err)); + CHECK(moq_origin_Producer_scope(origin, str("room"), patterns, &scoped, &err, NULL)); moq_Patterns_free(patterns); - root = moq_origin_Producer_root(scoped); - CHECK(is(moq_Path_as_str(root), "room")); + root = moq_origin_Producer_root(scoped, NULL); + CHECK(is(moq_Path_as_str(root, NULL), "room")); moq_Path_free(root); moq_Path *absolute = NULL; - CHECK(moq_origin_Producer_absolute(scoped, str("alice"), &absolute, &err)); - CHECK(is(moq_Path_as_str(absolute), "room/alice")); + CHECK(moq_origin_Producer_absolute(scoped, str("alice"), &absolute, &err, NULL)); + CHECK(is(moq_Path_as_str(absolute, NULL), "room/alice")); moq_Path_free(absolute); - moq_Patterns *allowed = moq_origin_Producer_allowed(scoped); - CHECK(moq_Patterns_matches(allowed, str("alice/cam"))); - CHECK(!moq_Patterns_matches(allowed, str("bob"))); + moq_Patterns *allowed = moq_origin_Producer_allowed(scoped, NULL); + CHECK(moq_Patterns_matches(allowed, str("alice/cam"), NULL)); + CHECK(!moq_Patterns_matches(allowed, str("bob"), NULL)); moq_Patterns_free(allowed); - moq_origin_Consumer *reader = moq_origin_Producer_consume(scoped); - CHECK(moq_origin_Consumer_absolute(reader, str("bob"), &absolute, &err)); - CHECK(is(moq_Path_as_str(absolute), "room/bob")); + moq_origin_Consumer *reader = moq_origin_Producer_consume(scoped, NULL); + CHECK(moq_origin_Consumer_absolute(reader, str("bob"), &absolute, &err, NULL)); + CHECK(is(moq_Path_as_str(absolute, NULL), "room/bob")); moq_Path_free(absolute); moq_origin_Consumer_free(reader); moq_origin_Producer_free(scoped); /* A broadcast the origin creates is stamped with its path, which its info lends. */ moq_origin_Publication *publication = NULL; - CHECK(moq_origin_Producer_create_broadcast(origin, str("room/alice"), &publication, &err)); + CHECK(moq_origin_Producer_create_broadcast(origin, str("room/alice"), &publication, &err, NULL)); moq_broadcast_Producer *created = moq_origin_Publication_broadcast(publication); moq_origin_Publication_free(publication); - moq_broadcast_Info *stamped = moq_broadcast_Producer_info(created); - moq_Path *path = moq_broadcast_Info_path_get(stamped); - CHECK(is(moq_Path_as_str(path), "room/alice")); + moq_broadcast_Info *stamped = moq_broadcast_Producer_info(created, NULL); + moq_Path *path = moq_broadcast_Info_path_get(stamped, NULL); + CHECK(is(moq_Path_as_str(path, NULL), "room/alice")); /* Paths own their segments, so C builds them, and relative ones, from a string. */ moq_Path *cam = NULL; moq_path_Error path_error; CHECK(!moq_Path_new(str("/cam/"), &cam, &path_error) && path_error == MOQ_PATH_ERROR_EMPTY_SEGMENT); CHECK(moq_Path_new(str("cam"), &cam, &path_error)); - CHECK(is(moq_Path_as_str(cam), "cam")); + CHECK(is(moq_Path_as_str(cam, NULL), "cam")); moq_Path *joined = NULL; - CHECK(moq_Path_join(path, cam, &joined, &path_error)); - CHECK(is(moq_Path_as_str(joined), "room/alice/cam")); - moq_Path *stripped = moq_Path_strip_prefix(joined, path); - CHECK(stripped != NULL && moq_Path_eq(stripped, cam)); + CHECK(moq_Path_join(path, cam, &joined, &path_error, NULL)); + CHECK(is(moq_Path_as_str(joined, NULL), "room/alice/cam")); + moq_Path *stripped = moq_Path_strip_prefix(joined, path, NULL); + CHECK(stripped != NULL && moq_Path_eq(stripped, cam, NULL)); moq_Path_free(stripped); - CHECK(moq_Path_strip_prefix(path, joined) == NULL); + CHECK(moq_Path_strip_prefix(path, joined, NULL) == NULL); moq_path_Relative *up = NULL; CHECK(moq_path_Relative_new(str("../bob"), &up, &path_error)); moq_Path *resolved = NULL; - CHECK(moq_Path_resolve(joined, up, &resolved, &path_error)); - CHECK(is(moq_Path_as_str(resolved), "room/bob")); + CHECK(moq_Path_resolve(joined, up, &resolved, &path_error, NULL)); + CHECK(is(moq_Path_as_str(resolved, NULL), "room/bob")); moq_Path_free(resolved); moq_path_Relative_free(up); moq_Path_free(joined); @@ -329,13 +329,13 @@ int main(void) { moq_Waiter *waiter = moq_Waiter_new(waker); now = moq_clock_now(); moq_clock_Instant *deadline = NULL; - CHECK(moq_origin_Driver_poll(driver, now, waiter, &deadline, &err)); + CHECK(moq_origin_Driver_poll(driver, now, waiter, &deadline, &err, NULL)); moq_clock_Instant_free(deadline); moq_clock_Instant_free(now); /* A client publishes the origin through moq-net's generic `with_publisher`, at each * type that implements its bound, by reference, so C keeps its handle. */ moq_Client *client = moq_Client_new(); - moq_Client *publishing = moq_Client_with_publisher_Producer(client, origin); + moq_Client *publishing = moq_Client_with_publisher_Producer(client, origin, NULL); CHECK(publishing != NULL); moq_Client_free(publishing); moq_Client_free(client); @@ -346,8 +346,8 @@ int main(void) { now = moq_clock_now(); deadline = NULL; err = NULL; - CHECK(!moq_origin_Driver_poll(driver, now, waiter, &deadline, &err)); - CHECK(deadline == NULL && err != NULL && is(moq_Error_to_string(err), "closed")); + CHECK(!moq_origin_Driver_poll(driver, now, waiter, &deadline, &err, NULL)); + CHECK(deadline == NULL && err != NULL && is(moq_Error_to_string(err, NULL), "closed")); moq_Error_free(err); err = NULL; moq_clock_Instant_free(now); @@ -360,12 +360,12 @@ int main(void) { moq_hang_catalog_AudioConfig *audio = NULL; moq_mux_Error *mux_err = NULL; CHECK(moq_mux_codec_opus_config((moq_buf){.ptr = head, .len = sizeof head}, &audio, &mux_err)); - CHECK(moq_hang_catalog_AudioConfig_sample_rate_get(audio) == 48000); - CHECK(moq_hang_catalog_AudioConfig_channel_count_get(audio) == 2); - moq_hang_catalog_AudioCodec codec = moq_hang_catalog_AudioConfig_codec_get(audio); + CHECK(moq_hang_catalog_AudioConfig_sample_rate_get(audio, NULL) == 48000); + CHECK(moq_hang_catalog_AudioConfig_channel_count_get(audio, NULL) == 2); + moq_hang_catalog_AudioCodec codec = moq_hang_catalog_AudioConfig_codec_get(audio, NULL); CHECK(codec.tag == MOQ_HANG_CATALOG_AUDIOCODEC_OPUS); moq_hang_catalog_AudioCodec_free(&codec); - moq_buf description = moq_hang_catalog_AudioConfig_description_get(audio); + moq_buf description = moq_hang_catalog_AudioConfig_description_get(audio, NULL); CHECK(description.len == sizeof head && memcmp(description.ptr, head, sizeof head) == 0); moq_buf_free(description); moq_hang_catalog_AudioConfig_free(audio); @@ -386,10 +386,10 @@ int main(void) { }; moq_track_Subscription *unranged = moq_track_Subscription_default(); moq_track_Subscription *ranged = - moq_track_Subscription_with_ranges(unranged, (moq_vec_track_Range){.ptr = ranges, .len = 2}); + moq_track_Subscription_with_ranges(unranged, (moq_vec_track_Range){.ptr = ranges, .len = 2}, NULL); moq_track_Subscription_free(unranged); - moq_vec_track_Range got = moq_track_Subscription_ranges_get(ranged); - CHECK(got.len == 2 && moq_track_Range_eq(&got.ptr[0], &ranges[0])); + moq_vec_track_Range got = moq_track_Subscription_ranges_get(ranged, NULL); + CHECK(got.len == 2 && moq_track_Range_eq(&got.ptr[0], &ranges[0], NULL)); CHECK(got.len == 2 && got.ptr[1].start.tag == MOQ_TRACK_ANCHOR_ABSOLUTE && got.ptr[1].start.absolute._0 == 2); CHECK(got.len == 2 && got.ptr[1].end.some && got.ptr[1].end.value.absolute._0 == 6); moq_vec_track_Range_free(got); @@ -401,72 +401,72 @@ int main(void) { * `is_claimed` is named by the last segment of its argument, and whose `mutate` lends * the catalog to C, which sets its fields in place. */ moq_broadcast_Info *listed = moq_broadcast_Info_new(); - moq_broadcast_Producer *publisher = moq_broadcast_Info_produce(listed); + moq_broadcast_Producer *publisher = moq_broadcast_Info_produce(listed, NULL); moq_broadcast_Info_free(listed); moq_mux_catalog_Config *catalog_config = moq_mux_catalog_Config_default(); moq_hang_Catalog_Extra *hang = moq_hang_Catalog_Extra_default(); - moq_mux_catalog_Config_Extra *extra = moq_mux_catalog_Config_with_catalog_Extra(catalog_config, hang); + moq_mux_catalog_Config_Extra *extra = moq_mux_catalog_Config_with_catalog_Extra(catalog_config, hang, NULL); moq_mux_catalog_Config_free(catalog_config); moq_hang_Catalog_Extra_free(hang); moq_mux_catalog_Producer_Extra *catalog = NULL; - CHECK(moq_mux_catalog_Producer_Extra_new(publisher, extra, &catalog, &mux_err)); + CHECK(moq_mux_catalog_Producer_Extra_new(publisher, extra, &catalog, &mux_err, NULL)); moq_mux_catalog_Config_Extra_free(extra); - CHECK(!moq_mux_catalog_Producer_Extra_is_claimed_BinaryConfig(catalog, str("state"))); + CHECK(!moq_mux_catalog_Producer_Extra_is_claimed_BinaryConfig(catalog, str("state"), NULL)); /* A field of the lent catalog, set in place: the next call reads it. */ moq_hang_catalog_Stats *stats = moq_hang_catalog_Stats_new(str("stats")); - CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, sets_stats, stats, &mux_err)); + CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, sets_stats, stats, &mux_err, NULL)); moq_hang_catalog_Stats_free(stats); int listed_stats = 0; - CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, lists_stats, &listed_stats, &mux_err)); + CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, lists_stats, &listed_stats, &mux_err, NULL)); CHECK(listed_stats == 1); /* A JSON track the catalog lists, published through moq-mux's generic `json_snapshot` * at a JSON document, and a section C parses from text, set in the lent catalog and * read back as text. Its track has a timescale, since every write is stamped. */ moq_Timescale *milli = moq_Timescale_MILLI(); - moq_track_Info *status_info = moq_track_Info_with_timescale(moq_track_Info_default(), milli); + moq_track_Info *status_info = moq_track_Info_with_timescale(moq_track_Info_default(), milli, NULL); moq_Timescale_free(milli); moq_track_Producer *status = NULL; - CHECK(moq_broadcast_Producer_create_track(publisher, str("status"), status_info, &status, &err)); + CHECK(moq_broadcast_Producer_create_track(publisher, str("status"), status_info, &status, &err, NULL)); moq_mux_json_Config *json_config = moq_mux_json_Config_default(); moq_mux_json_Options *json_options = moq_mux_json_Options_default(); moq_mux_json_Snapshot_Json *status_snapshot = NULL; CHECK(moq_mux_catalog_Producer_Extra_json_snapshot_Config(catalog, status, json_config, json_options, &status_snapshot, - &mux_err)); + &mux_err, NULL)); moq_mux_json_Config_free(json_config); moq_mux_json_Options_free(json_options); moq_track_Producer_free(status); - CHECK(is(moq_mux_json_Snapshot_Json_name(status_snapshot), "status")); - CHECK(moq_mux_catalog_Producer_Extra_is_claimed_JsonConfig(catalog, str("status"))); + CHECK(is(moq_mux_json_Snapshot_Json_name(status_snapshot, NULL), "status")); + CHECK(moq_mux_catalog_Producer_Extra_is_claimed_JsonConfig(catalog, str("status"), NULL)); /* A value C parses is published as the track's latest, which a subscriber reads back * as JSON text. */ moq_Json *latest = NULL; moq_buf json_err; CHECK(moq_Json_parse(str("{\"on\": true}"), &latest, &json_err)); moq_Timestamp *stamp = moq_Timestamp_now(); - CHECK(moq_mux_json_Snapshot_Json_update(status_snapshot, latest, stamp, &mux_err)); + CHECK(moq_mux_json_Snapshot_Json_update(status_snapshot, latest, stamp, &mux_err, NULL)); moq_Timestamp_free(stamp); moq_Json_free(latest); - moq_track_Subscriber *status_subscriber = moq_mux_json_Snapshot_Json_consume(status_snapshot); - moq_track_Subscriber_recv_group_future *status_receiving = moq_track_Subscriber_recv_group(status_subscriber); + moq_track_Subscriber *status_subscriber = moq_mux_json_Snapshot_Json_consume(status_snapshot, NULL); + moq_track_Subscriber_recv_group_future *status_receiving = moq_track_Subscriber_recv_group(status_subscriber, NULL); moq_group_Consumer *status_group = NULL; CHECK(moq_track_Subscriber_recv_group_poll(status_receiving, waker, &status_group, &err) == MOQ_POLL_READY); moq_track_Subscriber_recv_group_future_free(status_receiving); moq_frame_Frame *status_frame = NULL; if (status_group != NULL) { - moq_group_Consumer_read_frame_future *status_reading = moq_group_Consumer_read_frame(status_group); + moq_group_Consumer_read_frame_future *status_reading = moq_group_Consumer_read_frame(status_group, NULL); CHECK(moq_group_Consumer_read_frame_poll(status_reading, waker, &status_frame, &err) == MOQ_POLL_READY); moq_group_Consumer_read_frame_future_free(status_reading); moq_group_Consumer_free(status_group); } - CHECK(status_frame != NULL && is(moq_frame_Frame_payload_get(status_frame), "{\"on\":true}")); + CHECK(status_frame != NULL && is(moq_frame_Frame_payload_get(status_frame, NULL), "{\"on\":true}")); moq_frame_Frame_free(status_frame); moq_track_Subscriber_free(status_subscriber); moq_Json *app = NULL; CHECK(moq_Json_parse(str("{\"v\": [1, true]}"), &app, &json_err)); - CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, sets_app, app, &mux_err)); + CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, sets_app, app, &mux_err, NULL)); moq_Json_free(app); struct readback back = {0}; - CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, reads_back, &back, &mux_err)); + CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, reads_back, &back, &mux_err, NULL)); CHECK(contains(back.json, "\"status\"")); moq_buf_free(back.json); CHECK(is(back.app, "{\"v\":[1,true]}")); @@ -475,7 +475,7 @@ int main(void) { CHECK(is(json_err, "EOF while parsing a value at line 1 column 5")); moq_mux_catalog_hang_Extra *sections = moq_mux_catalog_hang_Extra_default(); CHECK(moq_Json_parse(str("1"), &app, &json_err)); - CHECK(!moq_mux_catalog_hang_Extra_set(sections, str("json"), app, &mux_err)); + CHECK(!moq_mux_catalog_hang_Extra_set(sections, str("json"), app, &mux_err, NULL)); CHECK(mux_err != NULL && is(moq_mux_Error_to_string(mux_err), "reserved catalog section: json")); moq_mux_Error_free(mux_err); mux_err = NULL; @@ -492,7 +492,7 @@ int main(void) { moq_tokio_quic_Config *quic = moq_tokio_quic_Config_default(); moq_tokio_Client *tokio_client = NULL; moq_tokio_Error *tokio_err = NULL; - CHECK(moq_tokio_connect_Config_init(client_config, quic, &tokio_client, &tokio_err)); + CHECK(moq_tokio_connect_Config_init(client_config, quic, &tokio_client, &tokio_err, NULL)); moq_tokio_connect_Config_free(client_config); moq_tokio_quic_Config_free(quic); moq_tokio_Client_free(tokio_client); @@ -505,44 +505,44 @@ int main(void) { /* A session's config, filled in as Rust assigns it, and a nested field by getting a copy, * editing it, and setting it back. */ moq_tokio_connect_Config *connect = moq_tokio_connect_Config_default(); - moq_tokio_tls_Connect *tls = moq_tokio_connect_Config_tls_get(connect); - moq_tokio_tls_Connect_insecure_set(tls, (moq_option_bool){.some = true, .value = true}); - moq_tokio_tls_Connect_host_name_set(tls, str("relay.example")); + moq_tokio_tls_Connect *tls = moq_tokio_connect_Config_tls_get(connect, NULL); + moq_tokio_tls_Connect_insecure_set(tls, (moq_option_bool){.some = true, .value = true}, NULL); + moq_tokio_tls_Connect_host_name_set(tls, str("relay.example"), NULL); /* A `Vec`, an array of buffers C keeps. */ moq_buf pins[] = {str("ab:cd")}; - moq_tokio_tls_Connect_fingerprint_set(tls, (moq_vec_string){.ptr = pins, .len = 1}); - moq_tokio_connect_Config_tls_set(connect, tls); + moq_tokio_tls_Connect_fingerprint_set(tls, (moq_vec_string){.ptr = pins, .len = 1}, NULL); + moq_tokio_connect_Config_tls_set(connect, tls, NULL); moq_tokio_tls_Connect_free(tls); - moq_tokio_connect_Config_implementation_set(connect, str("moq.c")); + moq_tokio_connect_Config_implementation_set(connect, str("moq.c"), NULL); /* A URL, parsed from text, failing with why, and moved in. */ moq_Url *url = NULL; moq_buf parse_err; CHECK(!moq_Url_parse(str("relay.example"), &url, &parse_err)); CHECK(is(parse_err, "relative URL without a base")); CHECK(moq_Url_parse(str("https://relay.example:4443/demo"), &url, &parse_err)); - moq_tokio_connect_Config_url_set(connect, url); + moq_tokio_connect_Config_url_set(connect, url, NULL); moq_Url_free(url); - tls = moq_tokio_connect_Config_tls_get(connect); - moq_option_bool insecure = moq_tokio_tls_Connect_insecure_get(tls); + tls = moq_tokio_connect_Config_tls_get(connect, NULL); + moq_option_bool insecure = moq_tokio_tls_Connect_insecure_get(tls, NULL); CHECK(insecure.some && insecure.value); - CHECK(is(moq_tokio_tls_Connect_host_name_get(tls), "relay.example")); + CHECK(is(moq_tokio_tls_Connect_host_name_get(tls, NULL), "relay.example")); /* TLS files, as OS paths: none by default. */ - moq_vec_path roots = moq_tokio_tls_Connect_root_get(tls); - moq_vec_string fingerprints = moq_tokio_tls_Connect_fingerprint_get(tls); + moq_vec_path roots = moq_tokio_tls_Connect_root_get(tls, NULL); + moq_vec_string fingerprints = moq_tokio_tls_Connect_fingerprint_get(tls, NULL); CHECK(roots.ptr != NULL && roots.len == 0); CHECK(fingerprints.len == 1 && fingerprints.ptr[0].len == 5 && memcmp(fingerprints.ptr[0].ptr, "ab:cd", 5) == 0); moq_vec_path_free(roots); moq_vec_string_free(fingerprints); moq_tokio_tls_Connect_free(tls); - CHECK(is(moq_tokio_connect_Config_implementation_get(connect), "moq.c")); - url = moq_tokio_connect_Config_url_get(connect); - CHECK(url != NULL && is(moq_Url_to_string(url), "https://relay.example:4443/demo")); + CHECK(is(moq_tokio_connect_Config_implementation_get(connect, NULL), "moq.c")); + url = moq_tokio_connect_Config_url_get(connect, NULL); + CHECK(url != NULL && is(moq_Url_to_string(url, NULL), "https://relay.example:4443/demo")); /* The config's URL, dialed with no tokens. */ - moq_auth_Tokens *tokens = moq_auth_Tokens_new((moq_vec_auth_Token){0}); + moq_auth_Tokens *tokens = moq_auth_Tokens_new((moq_vec_auth_Token){0}, NULL); moq_Versions *versions = moq_Versions_default(); - moq_auth_Dial *dial = moq_auth_Tokens_dial(tokens, url, versions); - moq_Url *dialed = moq_auth_Dial_url_get(dial); - CHECK(moq_Url_eq(dialed, url)); + moq_auth_Dial *dial = moq_auth_Tokens_dial(tokens, url, versions, NULL); + moq_Url *dialed = moq_auth_Dial_url_get(dial, NULL); + CHECK(moq_Url_eq(dialed, url, NULL)); moq_Url_free(dialed); moq_auth_Dial_free(dial); moq_auth_Tokens_free(tokens); @@ -551,13 +551,13 @@ int main(void) { /* A backoff's fields are settings left unset, and the methods of their names resolve them. */ moq_tokio_Backoff *backoff = moq_tokio_Backoff_default(); - CHECK(moq_tokio_Backoff_initial_get(backoff) == NULL); - moq_duration initial = moq_tokio_Backoff_initial(backoff); + CHECK(moq_tokio_Backoff_initial_get(backoff, NULL) == NULL); + moq_duration initial = moq_tokio_Backoff_initial(backoff, NULL); CHECK(initial.secs == 1 && initial.nanos == 0); moq_tokio_Backoff_free(backoff); /* A `Vec`, each element a buffer the array frees. */ - moq_vec_string alpns = moq_Versions_alpns(versions); + moq_vec_string alpns = moq_Versions_alpns(versions, NULL); CHECK(alpns.len > 0); for (size_t i = 0; i < alpns.len; i++) { CHECK(alpns.ptr[i].len > 0); @@ -571,16 +571,16 @@ int main(void) { CHECK(is(moq_tokio_listen_Bind_to_string(&v4), "127.0.0.1:0")); CHECK(!moq_tokio_listen_Bind_eq(&bind, &v4)); moq_tokio_listen_Config *listen = moq_tokio_listen_Config_default(); - moq_tokio_listen_Config_bind_set(listen, (moq_option_tokio_listen_Bind){.some = true, .value = bind}); - moq_option_tokio_listen_Bind bound = moq_tokio_listen_Config_bind_get(listen); + moq_tokio_listen_Config_bind_set(listen, (moq_option_tokio_listen_Bind){.some = true, .value = bind}, NULL); + moq_option_tokio_listen_Bind bound = moq_tokio_listen_Config_bind_get(listen, NULL); CHECK(bound.some && moq_tokio_listen_Bind_eq(&bound.value, &bind)); moq_tokio_listen_Bind_free(&bound.value); /* The preferred address of each family, which takes only that family. */ moq_socket_addr preferred = {.ip = {10, 0, 0, 1}, .port = 4443}; - moq_tokio_listen_Config_preferred_v4_set(listen, (moq_option_socket_addr){.some = true, .value = preferred}); - moq_option_socket_addr preferred_v4 = moq_tokio_listen_Config_preferred_v4_get(listen); + moq_tokio_listen_Config_preferred_v4_set(listen, (moq_option_socket_addr){.some = true, .value = preferred}, NULL); + moq_option_socket_addr preferred_v4 = moq_tokio_listen_Config_preferred_v4_get(listen, NULL); CHECK(preferred_v4.some && !preferred_v4.value.v6 && preferred_v4.value.ip[0] == 10 && preferred_v4.value.port == 4443); - CHECK(!moq_tokio_listen_Config_preferred_v6_get(listen).some); + CHECK(!moq_tokio_listen_Config_preferred_v6_get(listen, NULL).some); moq_tokio_listen_Config_free(listen); /* A constant slice, as an array of clones. */ moq_vec_string alpns_all = moq_ALPNS(); @@ -592,38 +592,38 @@ int main(void) { * back. Neither has a runtime of C's, so moq-tokio's fallback runs both. */ moq_origin_Producer *served = moq_tokio_origin_spawn(); moq_origin_Publication *live = NULL; - CHECK(moq_origin_Producer_create_broadcast(served, str("live"), &live, &err)); + CHECK(moq_origin_Producer_create_broadcast(served, str("live"), &live, &err, NULL)); moq_origin_Route *route = moq_origin_Route_default(); - CHECK(moq_origin_Publication_announce(live, route, &err)); + CHECK(moq_origin_Publication_announce(live, route, &err, NULL)); moq_origin_Route_free(route); moq_broadcast_Producer *live_broadcast = moq_origin_Publication_broadcast(live); moq_track_Producer *live_track = NULL; - CHECK(moq_broadcast_Producer_create_track(live_broadcast, str("video"), NULL, &live_track, &err)); + CHECK(moq_broadcast_Producer_create_track(live_broadcast, str("video"), NULL, &live_track, &err, NULL)); moq_group_Producer *live_group = NULL; - CHECK(moq_track_Producer_append_group(live_track, &live_group, &err)); - CHECK(moq_group_Producer_write_frame(live_group, NULL, str("hello"), &err)); - CHECK(moq_group_Producer_finish(live_group, &err)); + CHECK(moq_track_Producer_append_group(live_track, &live_group, &err, NULL)); + CHECK(moq_group_Producer_write_frame(live_group, NULL, str("hello"), &err, NULL)); + CHECK(moq_group_Producer_finish(live_group, &err, NULL)); moq_group_Producer_free(live_group); moq_tokio_server_Config *server_config = moq_tokio_server_Config_default(); - moq_tokio_listen_Config *listen_config = moq_tokio_server_Config_listen_get(server_config); + moq_tokio_listen_Config *listen_config = moq_tokio_server_Config_listen_get(server_config, NULL); moq_tokio_listen_Bind any = {.tag = MOQ_TOKIO_LISTEN_BIND_ADDR, .addr = {._0 = {.port = 0, .v6 = true}}}; - moq_tokio_listen_Config_bind_set(listen_config, (moq_option_tokio_listen_Bind){.some = true, .value = any}); + moq_tokio_listen_Config_bind_set(listen_config, (moq_option_tokio_listen_Bind){.some = true, .value = any}, NULL); moq_tokio_tls_Listen *tls_listen = moq_tokio_tls_Listen_default(); moq_buf names[] = {str("localhost")}; - moq_tokio_tls_Listen_generate_set(tls_listen, (moq_vec_string){.ptr = names, .len = 1}); - moq_tokio_listen_Config_tls_set(listen_config, tls_listen); + moq_tokio_tls_Listen_generate_set(tls_listen, (moq_vec_string){.ptr = names, .len = 1}, NULL); + moq_tokio_listen_Config_tls_set(listen_config, tls_listen, NULL); moq_tokio_tls_Listen_free(tls_listen); - moq_tokio_server_Config_listen_set(server_config, listen_config); + moq_tokio_server_Config_listen_set(server_config, listen_config, NULL); moq_tokio_listen_Config_free(listen_config); moq_tokio_Server *server = NULL; - CHECK(moq_tokio_server_Config_init(server_config, &server, &tokio_err)); + CHECK(moq_tokio_server_Config_init(server_config, &server, &tokio_err, NULL)); moq_tokio_server_Config_free(server_config); moq_socket_addr local; - CHECK(moq_tokio_Server_local_addr(server, &local, &tokio_err)); + CHECK(moq_tokio_Server_local_addr(server, &local, &tokio_err, NULL)); CHECK(local.v6 && local.port != 0); - moq_origin_Consumer *published = moq_origin_Producer_consume(served); - serving = moq_tokio_Server_serve_publish(server, published); + moq_origin_Consumer *published = moq_origin_Producer_consume(served, NULL); + serving = moq_tokio_Server_serve_publish(server, published, NULL); moq_tokio_Server_free(server); moq_origin_Consumer_free(published); @@ -631,67 +631,67 @@ int main(void) { snprintf(address, sizeof address, "moqt://localhost:%u", (unsigned)local.port); moq_tokio_connect_Config *dialing = moq_tokio_connect_Config_default(); CHECK(moq_Url_parse(str(address), &url, &parse_err)); - moq_tokio_connect_Config_url_set(dialing, url); + moq_tokio_connect_Config_url_set(dialing, url, NULL); moq_Url_free(url); - tls = moq_tokio_connect_Config_tls_get(dialing); - moq_tokio_tls_Connect_insecure_set(tls, (moq_option_bool){.some = true, .value = true}); - moq_tokio_connect_Config_tls_set(dialing, tls); + tls = moq_tokio_connect_Config_tls_get(dialing, NULL); + moq_tokio_tls_Connect_insecure_set(tls, (moq_option_bool){.some = true, .value = true}, NULL); + moq_tokio_connect_Config_tls_set(dialing, tls, NULL); moq_tokio_tls_Connect_free(tls); moq_tokio_client_Config *session_client = moq_tokio_client_Config_default(); - moq_tokio_client_Config_connect_set(session_client, dialing); + moq_tokio_client_Config_connect_set(session_client, dialing, NULL); moq_tokio_connect_Config_free(dialing); moq_tokio_Client *dialer = NULL; - CHECK(moq_tokio_client_Config_init(session_client, &dialer, &tokio_err)); + CHECK(moq_tokio_client_Config_init(session_client, &dialer, &tokio_err, NULL)); moq_tokio_client_Config_free(session_client); - moq_tokio_Client *dialer_copy = moq_tokio_Client_clone(dialer); - moq_tokio_Client *once = moq_tokio_Client_with_reconnect(dialer_copy, false); + moq_tokio_Client *dialer_copy = moq_tokio_Client_clone(dialer, NULL); + moq_tokio_Client *once = moq_tokio_Client_with_reconnect(dialer_copy, false, NULL); moq_tokio_Client_free(dialer_copy); moq_origin_Producer *received = moq_tokio_origin_spawn(); - moq_origin_Consumer *remote = moq_origin_Producer_consume(received); - moq_tokio_Connection *connection = moq_tokio_Client_consume(once, received); + moq_origin_Consumer *remote = moq_origin_Producer_consume(received, NULL); + moq_tokio_Connection *connection = moq_tokio_Client_consume(once, received, NULL); moq_tokio_Client_free(once); moq_origin_Producer_free(received); CHECK(connection != NULL); - moq_tokio_Connection_established_future *establishing = moq_tokio_Connection_established(connection); + moq_tokio_Connection_established_future *establishing = moq_tokio_Connection_established(connection, NULL); moq_tokio_Connection_free(connection); moq_tokio_Connection *established = NULL; AWAIT(moq_tokio_Connection_established_poll(establishing, session_waker, &established, &tokio_err)); moq_tokio_Connection_established_future_free(establishing); CHECK(established != NULL); - moq_origin_Consumer_request_future *requesting = moq_origin_Consumer_request(remote, str("live"), NULL); + moq_origin_Consumer_request_future *requesting = moq_origin_Consumer_request(remote, str("live"), NULL, NULL); moq_broadcast_Consumer *remote_broadcast = NULL; AWAIT(moq_origin_Consumer_request_poll(requesting, session_waker, &remote_broadcast, &err)); moq_origin_Consumer_request_future_free(requesting); CHECK(remote_broadcast != NULL); moq_track_Consumer *remote_track = NULL; - CHECK(moq_broadcast_Consumer_track(remote_broadcast, str("video"), &remote_track, &err)); - subscribing = moq_track_Consumer_subscribe(remote_track, NULL); + CHECK(moq_broadcast_Consumer_track(remote_broadcast, str("video"), &remote_track, &err, NULL)); + subscribing = moq_track_Consumer_subscribe(remote_track, NULL, NULL); AWAIT(moq_track_Consumer_subscribe_poll(subscribing, session_waker, &subscriber, &err)); moq_track_Consumer_subscribe_future_free(subscribing); CHECK(subscriber != NULL); - receiving = moq_track_Subscriber_recv_group(subscriber); + receiving = moq_track_Subscriber_recv_group(subscriber, NULL); group = NULL; AWAIT(moq_track_Subscriber_recv_group_poll(receiving, session_waker, &group, &err)); moq_track_Subscriber_recv_group_future_free(receiving); CHECK(group != NULL); - reading = moq_group_Consumer_read_frame(group); + reading = moq_group_Consumer_read_frame(group, NULL); frame = NULL; AWAIT(moq_group_Consumer_read_frame_poll(reading, session_waker, &frame, &err)); moq_group_Consumer_read_frame_future_free(reading); - CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame), "hello")); + CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame, NULL), "hello")); moq_frame_Frame_free(frame); moq_group_Consumer_free(group); moq_track_Subscriber_free(subscriber); moq_track_Consumer_free(remote_track); moq_broadcast_Consumer_free(remote_broadcast); - moq_tokio_Connection_close_future *closing = moq_tokio_Connection_close(established); + moq_tokio_Connection_close_future *closing = moq_tokio_Connection_close(established, NULL); moq_tokio_Connection_free(established); AWAIT(moq_tokio_Connection_close_poll(closing, session_waker, &tokio_err)); moq_tokio_Connection_close_future_free(closing); - moq_tokio_Client_close_future *client_closing = moq_tokio_Client_close(dialer); + moq_tokio_Client_close_future *client_closing = moq_tokio_Client_close(dialer, NULL); moq_tokio_Client_free(dialer); AWAIT(moq_tokio_Client_close_poll(client_closing, session_waker)); moq_tokio_Client_close_future_free(client_closing); diff --git a/ffi/moq/moq.swift b/ffi/moq/moq.swift index 07d632d5e..cbdf341a1 100644 --- a/ffi/moq/moq.swift +++ b/ffi/moq/moq.swift @@ -135,9 +135,9 @@ func block(_ body: @escaping @Sendable () async throws -> T) throw } /// Whether `path` is `other`: `==` takes a `Path`, which an optional one holds. -func same(_ path: borrowing Path?, _ other: borrowing Path) -> Bool { +func same(_ path: borrowing Path?, _ other: borrowing Path) throws -> Bool { switch path { - case .some(let path): return path == other + case .some(let path): return try path == other case .none: return false } } @@ -148,28 +148,28 @@ func same(_ path: borrowing Path?, _ other: borrowing Path) -> Bool { func origin(_ waiter: borrowing Waiter, _ wakes: Cell, _ before: inout Int) throws -> OriginDriver { // An origin and the driver that runs it, a pair. Its root, which it lends, is returned // as a new handle to a clone. - let pair = OriginProducer.new(OriginConfig.default()) + let pair = try OriginProducer.new(OriginConfig.default()) let origin = pair._0 var driver = pair._1 - expect(origin.root().asStr() == "", "an origin's root is empty") + expect(try origin.root().asStr() == "", "an origin's root is empty") // Scoped beneath a root to patterns, which moq-pattern's own export builds. var patterns = Patterns.new() expect(try patterns.insert(Pattern.subtree("alice")), "a pattern inserts") let scoped = try origin.scope("room", patterns) - expect(scoped.root().asStr() == "room", "a scope has its root") + expect(try scoped.root().asStr() == "room", "a scope has its root") expect(try scoped.absolute("alice").asStr() == "room/alice", "a scope's paths are absolute") - let allowed = scoped.allowed() - expect(allowed.matches("alice/cam"), "a scope allows its patterns") - expect(!allowed.matches("bob"), "a scope allows nothing else") - let consumer = scoped.consume() + let allowed = try scoped.allowed() + expect(try allowed.matches("alice/cam"), "a scope allows its patterns") + expect(try !allowed.matches("bob"), "a scope allows nothing else") + let consumer = try scoped.consume() expect(try consumer.absolute("bob").asStr() == "room/bob", "a scope's consumer shares its root") // A broadcast the origin creates is stamped with its path, which its info lends. do { let publication = try origin.createBroadcast("room/alice") - let path = publication.broadcast().info().pathGet - expect(path.asStr() == "room/alice", "a broadcast has its path") + let path = try publication.broadcast().info().pathGet() + expect(try path.asStr() == "room/alice", "a broadcast has its path") // Paths own their segments, so Swift builds them, and relative ones, from a string. do { @@ -179,18 +179,18 @@ func origin(_ waiter: borrowing Waiter, _ wakes: Cell, _ before: inout Int) expect(e == .emptySegment, "an empty segment is its own error") } let cam = try Path.new("cam") - expect(cam.asStr() == "cam", "a path builds from its segments") + expect(try cam.asStr() == "cam", "a path builds from its segments") let joined = try path.join(cam) - expect(joined.asStr() == "room/alice/cam", "paths join") - expect(same(joined.stripPrefix(path), cam), "a path strips its prefix") - expect(path.stripPrefix(joined) == nil, "a path strips no longer prefix") + expect(try joined.asStr() == "room/alice/cam", "paths join") + expect(try same(joined.stripPrefix(path), cam), "a path strips its prefix") + expect(try path.stripPrefix(joined) == nil, "a path strips no longer prefix") expect(try joined.resolve(PathRelative.new("../bob")).asStr() == "room/bob", "a relative path resolves") } _ = try driver.poll(clockNow(), waiter) // A client publishes the origin through moq-net's generic `with_publisher`, at each // type that implements its bound, by reference, so Swift keeps its handle. - _ = Client.new().withPublisherProducer(origin) + _ = try Client.new().withPublisherProducer(origin) before = wakes.value return driver } @@ -198,16 +198,16 @@ func origin(_ waiter: borrowing Waiter, _ wakes: Cell, _ before: inout Int) func script() throws { let q = Queue() - let broadcast = BroadcastInfo.new().produce() + let broadcast = try BroadcastInfo.new().produce() let track = try broadcast.createTrack("video", nil) - expect(track.name() == "video", "a track has its name") + expect(try track.name() == "video", "a track has its name") // A second track of the same name is an error, printed as Rust prints it. do { _ = try broadcast.createTrack("video", nil) expect(false, "a duplicate track is an error") } catch let e as Moq.Error { - expect("\(e)" == "duplicate", "a duplicate track's error is Rust's") + expect(try e.description == "duplicate", "a duplicate track's error is Rust's") } let subscriber = Held(try block { try await track.consume().subscribe(nil) }) @@ -225,7 +225,7 @@ func script() throws { expect(q.size == 1, "a group's arrival wakes the receive") q.drain() let first = Held(group.take()!) - expect(first.value.index() == 0, "the first group is 0") + expect(try first.value.index() == 0, "the first group is 0") // So does a frame's. let frame = Slot() @@ -240,22 +240,22 @@ func script() throws { expect(q.size == 1, "a frame's arrival wakes the read") q.drain() let hello = frame.take()! - expect(text(hello.payloadGet) == "hello", "a frame has its payload") - expect(hello.timestampGet == nil, "an untimed frame has no timestamp") + expect(text(try hello.payloadGet) == "hello", "a frame has its payload") + expect(try hello.timestampGet == nil, "an untimed frame has no timestamp") // When it arrived: kio's clock's `Instant`, from kio's own export. - expect(clockNow().checkedDurationSince(hello.arrived()) != nil, "a frame arrived before now") + expect(try clockNow().checkedDurationSince(hello.arrived()) != nil, "a frame arrived before now") // A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is // untimed, so a timestamped frame is refused. let at = try Timestamp.fromMillis(1500) - expect(at.asSecs() == 1, "1500 ms is 1 s") + expect(try at.asSecs() == 1, "1500 ms is 1 s") do { try writer.writeFrame(at, Data("late".utf8)) expect(false, "a timestamped frame on an untimed track is an error") } catch let e as Moq.Error { - expect("\(e)" == "frame timestamp doesn't fit its track", "the late frame's error is Rust's") + expect(try e.description == "frame timestamp doesn't fit its track", "the late frame's error is Rust's") } - expect(at.asSecs() == 1, "a copied timestamp stays usable") + expect(try at.asSecs() == 1, "a copied timestamp stays usable") // The last frame, bytes that are not UTF-8, then the group's end, which reads as nil. let world = Data([0xff]) + Data("world".utf8) @@ -263,7 +263,7 @@ func script() throws { try writer.finish() try block { let last = try await first.value.readFrame() - expect(last?.payloadGet == world, "the last frame reads") + expect(try last?.payloadGet == world, "the last frame reads") expect(try await first.value.readFrame() == nil, "a finished group reads nil") } @@ -280,17 +280,17 @@ func script() throws { _ = try driver.poll(clockNow(), waiter) expect(false, "a driver whose origin is gone finishes") } catch let e as Moq.Error { - expect("\(e)" == "closed", "a finished driver's error is Rust's") + expect(try e.description == "closed", "a finished driver's error is Rust's") } // moq-mux reads an OpusHead into hang's catalog config: 44.1 kHz in, stereo, which the // catalog lists at the decoder's 48 kHz, keeping the head as its description. let head = Data("OpusHead".utf8) + Data([1, 2, 0x38, 0x01, 0x44, 0xac, 0, 0, 0, 0, 0]) let audio = try muxCodecOpusConfig(head) - expect(audio.sampleRateGet == 48000, "Opus decodes at 48 kHz") - expect(audio.channelCountGet == 2, "the head is stereo") - expect(audio.codecGet == .opus, "the codec is Opus") - expect(audio.descriptionGet == head, "the head is the description") + expect(try audio.sampleRateGet == 48000, "Opus decodes at 48 kHz") + expect(try audio.channelCountGet == 2, "the head is stereo") + expect(try audio.codecGet == .opus, "the codec is Opus") + expect(try audio.descriptionGet == head, "the head is the description") // A truncated head is moq-mux's error. do { _ = try muxCodecOpusConfig(head.prefix(8)) @@ -301,18 +301,21 @@ func script() throws { // A subscription's ranges, from any iterable of them: an array. let groups = TrackRange.groups(BoundsU64(start: .included(2), end: .excluded(6))) - let ranged = TrackSubscription.default().withRanges([TrackRange.live(), groups]).rangesGet - expect(ranged == [TrackRange.live(), groups], "a subscription keeps its ranges") - expect(groups.start == .absolute(2), "groups start at their first") - expect(groups.end == .absolute(6), "groups end before their end") + let ranged = try TrackSubscription.default().withRanges([TrackRange.live(), groups]).rangesGet + expect( + try ranged.count == 2 && ranged[0] == TrackRange.live() && ranged[1] == groups, + "a subscription keeps its ranges" + ) + expect(try groups.start == .absolute(2), "groups start at their first") + expect(try groups.end.map { try $0 == .absolute(6) } == true, "groups end before their end") // A catalog at moq-mux's untyped extension, from the config Swift builds, whose generic // `is_claimed` is named by the last segment of its argument, and whose `mutate` lends // the catalog to Swift. - var publisher = BroadcastInfo.new().produce() - let extra = MuxCatalogConfig.default().withCatalogExtra(HangCatalogExtra.default()) + var publisher = try BroadcastInfo.new().produce() + let extra = try MuxCatalogConfig.default().withCatalogExtra(HangCatalogExtra.default()) var catalog = try MuxCatalogProducerExtra.new(&publisher, extra) - expect(!catalog.isClaimedBinaryConfig("state"), "an unclaimed name is not claimed") + expect(try !catalog.isClaimedBinaryConfig("state"), "an unclaimed name is not claimed") let read = Cell(0) try catalog.mutate { lent in if try! lent.toJson().hasPrefix("{") { @@ -326,27 +329,27 @@ func script() throws { // read back as text. var status = try catalog.jsonSnapshotConfig( try publisher.createTrack("status", TrackInfo.default().withTimescale(Timescale.MILLI)), MuxJsonConfig.default(), MuxJsonOptions.default()) - expect(status.name() == "status", "a JSON snapshot has its track's name") - expect(catalog.isClaimedJsonConfig("status"), "the JSON snapshot claims its name") + expect(try status.name() == "status", "a JSON snapshot has its track's name") + expect(try catalog.isClaimedJsonConfig("status"), "the JSON snapshot claims its name") // A value Swift parses is published as the track's latest, which a subscriber reads // back as JSON text. let on = try Json.parse(#"{"on": true}"#) try status.update(on, Timestamp.now()) - let latest = Held(status.consume()) + let latest = Held(try status.consume()) try block { var group = try await latest.value.recvGroup() let frame = try await group?.readFrame() - expect(text(frame?.payloadGet) == #"{"on":true}"#, "a subscriber reads the latest value") + expect(text(try frame?.payloadGet) == #"{"on":true}"#, "a subscriber reads the latest value") } try catalog.mutate { lent in - var sections = lent.extGet + var sections = try! lent.extGet() try! sections.set("app", Json.parse(#"{"v": [1, true]}"#)) - lent.extSet(sections) + try! lent.extSet(sections) } let app = Cell(nil) try catalog.mutate { lent in expect(try! lent.toJson().contains("\"status\""), "the catalog lists the JSON track") - app.value = lent.extGet.get("app")?.toText() + app.value = try! lent.extGet().get("app")?.toText() } expect(app.value == #"{"v":[1,true]}"#, "the catalog reads its section back as text") // Malformed text fails to parse, and a reserved section is moq-mux's error. @@ -377,19 +380,19 @@ func script() throws { } // A session's config, its URL and pinned fingerprints set as Rust assigns them. var connect = TokioConnectConfig.default() - var tls = connect.tlsGet - tls.fingerprintSet(["ab:cd"]) - connect.tlsSet(tls) - connect.urlSet(try Url.parse("https://relay.example:4443/demo")) - expect(connect.tlsGet.fingerprintGet == ["ab:cd"], "a TLS config keeps its fingerprints") - let url = connect.urlGet! + var tls = try connect.tlsGet() + try tls.fingerprintSet(["ab:cd"]) + try connect.tlsSet(tls) + try connect.urlSet(try Url.parse("https://relay.example:4443/demo")) + expect(try connect.tlsGet().fingerprintGet == ["ab:cd"], "a TLS config keeps its fingerprints") + let url = try connect.urlGet()! let versions = Versions.default() - let dialed = AuthTokens.new(UniqueArray()).dial(url, versions).urlGet - expect(dialed == url, "a dial keeps its URL") - expect(dialed.description == "https://relay.example:4443/demo", "a URL prints as Rust's") + let dialed = try AuthTokens.new(UniqueArray()).dial(url, versions).urlGet() + expect(try dialed == url, "a dial keeps its URL") + expect(try dialed.description == "https://relay.example:4443/demo", "a URL prints as Rust's") // A `Vec`, as an array. expect(!ALPNS.isEmpty, "a constant slice crosses as an array") - let alpns = versions.alpns() + let alpns = try versions.alpns() expect(!alpns.isEmpty && alpns.allSatisfy { !$0.isEmpty }, "each version has an ALPN") // A socket address, crossing in a listener's bind. let loopback: [UInt8] = Array(repeating: 0, count: 15) + [1] @@ -398,14 +401,14 @@ func script() throws { let v4 = SocketAddr(ip: [127, 0, 0, 1], port: 0) expect("\(TokioListenBind.addr(v4))" == "127.0.0.1:0", "an IPv4 bind prints as Rust's") var listen = TokioListenConfig.default() - listen.bindSet(bind) + try listen.bindSet(bind) // The preferred address of each family, which takes only that family. - listen.preferredV4Set(v4) - expect(listen.preferredV4Get == v4 && listen.preferredV6Get == nil, "a listener keeps its preferred IPv4 address") - expect(listen.bindGet == bind, "a listener's config keeps its bind") + try listen.preferredV4Set(v4) + expect(try listen.preferredV4Get == v4 && listen.preferredV6Get == nil, "a listener keeps its preferred IPv4 address") + expect(try listen.bindGet == bind, "a listener's config keeps its bind") // TLS files, as OS paths, and fingerprints, as strings: none by default. let defaults = TokioTlsConnect.default() - expect(defaults.rootGet.isEmpty && defaults.fingerprintGet.isEmpty, "a default TLS config lists none") + expect(try defaults.rootGet.isEmpty && defaults.fingerprintGet.isEmpty, "a default TLS config lists none") // A session, as moq.c runs it: a server bound to port 0 reads back the port it got and // serves with one call, in a task of its own, and a client dials it with one call and @@ -425,12 +428,12 @@ func script() throws { try liveGroup.finish() var serverConfig = TokioServerConfig.default() - var listen = serverConfig.listenGet - listen.bindSet(.addr(SocketAddr(ip: Array(repeating: 0, count: 16), port: 0))) - var tls = listen.tlsGet - tls.generateSet(["localhost"]) - listen.tlsSet(tls) - serverConfig.listenSet(listen) + var listen = try serverConfig.listenGet() + try listen.bindSet(.addr(SocketAddr(ip: Array(repeating: 0, count: 16), port: 0))) + var tls = try listen.tlsGet() + try tls.generateSet(["localhost"]) + try listen.tlsSet(tls) + try serverConfig.listenSet(listen) let server = try serverConfig.init_() let local = try server.localAddr() expect(local.port != 0, "a server bound to port 0 reads back the port it got") @@ -440,23 +443,23 @@ func script() throws { let serving = Task { try await owned.take()!.servePublish(served.consume()) } var connect = TokioConnectConfig.default() - connect.urlSet(try Url.parse("moqt://localhost:\(local.port)")) - var insecure = connect.tlsGet - insecure.insecureSet(true) - connect.tlsSet(insecure) + try connect.urlSet(try Url.parse("moqt://localhost:\(local.port)")) + var insecure = try connect.tlsGet() + try insecure.insecureSet(true) + try connect.tlsSet(insecure) var clientConfig = TokioClientConfig.default() - clientConfig.connectSet(connect) + try clientConfig.connectSet(connect) let client = try clientConfig.init_() let received = tokioOriginSpawn() - let remote = received.consume() - let connection = client.clone().withReconnect(false).consume(received) + let remote = try received.consume() + let connection = try client.clone().withReconnect(false).consume(received) expect(connection != nil, "a client with a URL dials it") let established = try await connection!.established() var subscriber = try await remote.request("live", nil).track("video").subscribe(nil) var group = try await subscriber.recvGroup() let frame = try await group?.readFrame() - expect(text(frame?.payloadGet) == "hello", "a session reads the frame the server serves") + expect(text(try frame?.payloadGet) == "hello", "a session reads the frame the server serves") try await established.close() try await client.close() diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index a23d8c0a4..aab44b65e 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -531,8 +531,10 @@ final _n = _Native(); final class _Native { late final _Stack stack = _Stack(); - /// Where a call that can fail a check writes the failure: one for the isolate, since a - /// call reads it as it returns, before any other can run, and a failure clears it. + /// Where a call that can fail a check writes the failure: one for the isolate, since the + /// glue writes it on the isolate's thread as the call returns, once it served every call + /// queued meanwhile, and the call reads it before any other can run, and a failure + /// clears it. late final ffi.Pointer status = _dartAlloc(4).cast()..value = 0; late final isolate.RawReceivePort _port; late final ffi.Pointer home; diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index 0c62484db..9a03a35f3 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -565,10 +565,12 @@ pub fn char(code: u32, what: &str) -> char { const EXCLUSIVE: usize = usize::MAX; const TAKEN: usize = usize::MAX - 1; +const RESERVED: usize = usize::MAX - 2; /// A value behind a handle: shared through an `Arc`, and borrowed as Rust borrows it, with /// the borrow checked at run time. `state` counts shared borrows, or is [`EXCLUSIVE`] -/// while one exclusive borrow lasts, or [`TAKEN`] once the value moved out. A value that is +/// while one exclusive borrow lasts, [`RESERVED`] while a call holds it to move out once its +/// checks pass, or [`TAKEN`] once the value moved out. A value that is /// not `Sync` is shared on one thread at a time instead: `state` is that thread's id, and /// `depth` counts its borrows, which only that thread touches. pub struct Object { @@ -732,7 +734,7 @@ impl Object { loop { match state { TAKEN => return Err(Status::Moved), - EXCLUSIVE => return Err(Status::Exclusive), + EXCLUSIVE | RESERVED => return Err(Status::Exclusive), _ => {} } match self @@ -762,7 +764,7 @@ impl Object { Ok(_) => {} Err(owner) if owner == me => {} Err(TAKEN) => return Err(Status::Moved), - Err(EXCLUSIVE) => return Err(Status::Exclusive), + Err(EXCLUSIVE | RESERVED) => return Err(Status::Exclusive), Err(_) => return Err(Status::Thread), } // SAFETY: only the thread in `state` touches `depth`. @@ -802,7 +804,7 @@ impl Object { /// Take `state` from 0 to `to`, as an exclusive borrow or a move does. fn acquire_from_zero(&self, to: usize) -> Result<(), Status> { - if self.lender.is_some() || (to == TAKEN && !self.lent.is_null()) { + if self.lender.is_some() || (matches!(to, TAKEN | RESERVED) && !self.lent.is_null()) { return Err(Status::Lent); } match self.state.compare_exchange(0, to, Ordering::Acquire, Ordering::Relaxed) { @@ -980,7 +982,7 @@ impl Object { pub unsafe fn reserve<'a>(handle: *const c_void, takes: &mut Takes<'a>, what: &str) -> Result<(), Status> { // SAFETY: the caller's contract. let object = unsafe { Self::get(handle, what) }; - object.acquire_from_zero(TAKEN)?; + object.acquire_from_zero(RESERVED)?; // SAFETY: the state outlives `takes`, by the caller's contract; a pointer to it, // so the value's type need not. takes.push(unsafe { &*::std::ptr::addr_of!(object.state) }); @@ -1041,11 +1043,11 @@ impl Object { unsafe { *(*handle.cast::()).value() } } - /// Move out the value [`Self::take`] reserved, leaving the handle to be freed. + /// Move out the value [`Self::take`] took, or [`Self::reserve`] reserved for a call whose + /// `Takes` committed, leaving the handle to be freed. /// /// # Safety - /// [`Self::take`] reserved `handle`'s value for a call that passed its checks, and no - /// other read moves it out. + /// One of them did so for `handle`'s value, and no other read moves it out. pub unsafe fn taken(handle: *const c_void) -> T { // SAFETY: the caller's contract. let object = unsafe { &*handle.cast::() }; @@ -1056,8 +1058,9 @@ impl Object { /// Drop the value now, as a move out would, leaving the handle to be freed: what a /// binding's close does, so a later use fails as moved, as Rust refuses a use after a - /// drop. It fails, changing nothing, while a call or future borrows the value, as Rust - /// refuses a drop under a borrow, and does nothing once the value is gone. A handle a + /// drop. It fails, changing nothing, while a call or future borrows the value or a call + /// reserved it to move out, as Rust refuses a drop under a borrow, and does nothing once + /// the value is gone. A handle a /// call returned lent from another's value only stops lending it; one lent to C for a /// call fails, as Rust lent it. /// @@ -1140,10 +1143,11 @@ impl<'a> Takes<'a> { } } - /// Keep every value reserved, for the call to move out. + /// Mark every value reserved moved out, for the call to move out. pub fn commit(&mut self) { - self.first = [None; 2]; - self.rest.clear(); + for state in self.first.iter_mut().filter_map(Option::take).chain(self.rest.drain(..)) { + state.store(TAKEN, Ordering::Release); + } } } diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index 2ac6bdaa3..23e031d25 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -53,8 +53,6 @@ public struct U128: Hashable, Comparable, Sendable, CustomStringConvertible { } } -/// Where a seek counts its offset from, as swift-system's `FileDescriptor.SeekOrigin` and -/// Rust's `SeekFrom` do. /// A call Rust's rules refuse, thrown before any Rust runs: a handle borrowed in a way /// the borrow checker forbids beside another call's, used once its value moved out or its /// lend ended, or an item called on a library whose target lacks it. @@ -67,6 +65,8 @@ public struct CheckError: Swift.Error, CustomStringConvertible, Sendable { public var description: Swift.String { "\(function): \(message)" } } +/// Where a seek counts its offset from, as swift-system's `FileDescriptor.SeekOrigin` and +/// Rust's `SeekFrom` do. public enum SeekOrigin: Sendable { case start case current diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index c85b1be34..908decdde 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -255,7 +255,6 @@ impl<'a> Header<'a> { } } - /// The arena a body lowering each of `tys` declares, if one needs it. /// The status a record's or enum's trait function takes, if a handle in it is checked: /// null, so a failed check aborts, as each other call of C++'s does. fn value_status(&self, ty: &Type) -> &'static str { @@ -265,6 +264,7 @@ impl<'a> Header<'a> { } } + /// The arena a body lowering each of `tys` declares, if one needs it. fn scope<'t>(&self, tys: impl IntoIterator) -> String { match tys.into_iter().any(|ty| self.arena(ty)) { true => format!( diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 016f78b15..344f7d5e0 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -776,14 +776,32 @@ impl<'a> Gen<'a> { let decls: Vec = (call.params.iter().enumerate()) .map(|(n, (_, ty))| format!(", p{n}: {}", rs(ty))) .collect(); - let args: Vec = (0..call.params.len()).map(|n| format!("p{n}")).collect(); + let mut args: Vec = (0..call.params.len()).map(|n| format!("p{n}")).collect(); + // A status the call writes reaches the isolate's one cell only once `serve` stopped + // serving: a call it served meanwhile, on the isolate's thread, read its own first. A + // null one, which aborts on a failed check, passes through. + let status = call.params.last().is_some_and(|(_, ty)| abi::is_status(ty)); + let body = match status { + true => { + let cell = args.pop().expect("a status is a parameter"); + args.push("s_".to_string()); + format!( + "let mut status = 0;\n\tlet s_ = match {cell}.is_null() {{\n\t\ttrue => {cell},\n\t\tfalse => &raw mut status,\n\t}};\n\tlet r = unsafe {{ home.serve(move || crate::{}({})) }};\n\tif !{cell}.is_null() {{\n\t\tunsafe {{ *{cell} = status }};\n\t}}\n\tr", + call.name, + args.join(", ") + ) + } + false => format!( + "unsafe {{ home.serve(move || crate::{}({})) }}", + call.name, + args.join(", ") + ), + }; let _ = write!( self.glue, - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}(home: *const c_void{}) -> {} {{\n\tlet home = unsafe {{ &*home.cast::() }};\n\tunsafe {{ home.serve(move || crate::{}({})) }}\n}}\n\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}(home: *const c_void{}) -> {} {{\n\tlet home = unsafe {{ &*home.cast::() }};\n\t{body}\n}}\n\n", decls.concat(), rs(&call.ret), - call.name, - args.join(", ") ); let native = self.native_fn(&name, params, call.ret.clone()); let api = self.abi.api; diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 9c259c8d8..eecc33873 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -1369,6 +1369,9 @@ impl<'a> Gen<'a> { } // A value holding an object fails once that object is closed, as an object does. let closable = object || self.holds(ty); + // On a target the type is not on, only the binding makes a value of it, and each + // trait fails as a function the target lacks does. + let limited = !self.abi.api.everywhere(self.abi.on(ty)); // The receiver, a held handle or the value lowered, and the return of `failed`, an // expression of the error, once a closed object fails the call. let open = |failed: &dyn Fn(&str) -> String| match (object, arena) { @@ -1469,7 +1472,7 @@ impl<'a> Gen<'a> { true => open(&failed), false => open(&|_: &str| unreachable!("a value holding no object is never closed")), }; - let (results, ok) = match closable { + let (results, ok) = match closable || limited { true => (vec!["bool".to_string(), "error".to_string()], ", nil"), false => (vec!["bool".to_string()], ""), }; @@ -1485,10 +1488,10 @@ impl<'a> Gen<'a> { checked(&format!("C.{c}_eq({this}, &c_other_)"), &format!("{c}_eq"), &failed) ), (false, true) => { - format!("{open}c_other_ := lower_{c}(a_, other)\nreturn bool(C.{c}_eq({this}, &c_other_))") + format!("{open}c_other_ := lower_{c}(a_, other)\nreturn bool(C.{c}_eq({this}, &c_other_)){ok}") } (false, false) => { - format!("{open}c_other_ := lower_{c}(other)\nreturn bool(C.{c}_eq({this}, &c_other_))") + format!("{open}c_other_ := lower_{c}(other)\nreturn bool(C.{c}_eq({this}, &c_other_)){ok}") } }; methods.push(method( @@ -1505,21 +1508,36 @@ impl<'a> Gen<'a> { true => format!("own_{c}(C.{c}_default())"), false => format!("lift_{c}(C.{c}_default())"), }; + let (results, ok) = match limited { + true => (vec![name.clone(), "error".to_string()], ", nil"), + false => (vec![name.clone()], ""), + }; Method { docs: format!("{func} is Rust's Default."), receiver: None, name: func, params: Vec::new(), - results: vec![name.clone()], - body: format!("return {lift}"), + results, + body: format!("return {lift}{ok}"), } }); let (mut methods, mut default) = (methods, default); - // On a target a value's type is not on, only the binding makes one, which calls in - // for none of these. - if let Some(check) = self.on(self.abi.on(ty), c) { + if limited { + let supported = self.qualify(Packages::RT, "Supported_"); + let on = self.abi.on(ty).0 & self.abi.api.all().0; + let zero = self.zero(ty); for method in methods.iter_mut().chain(default.as_mut()) { - method.body = format!("{check}\n{}", method.body); + let (what, failed) = match method.name.as_str() { + "String" => ("to_string", "err_.Error()".to_string()), + "GoString" => ("debug", "err_.Error()".to_string()), + "Clone" => ("clone", "nil, err_".to_string()), + "Equal" => ("eq", "false, err_".to_string()), + _ => ("default", format!("{zero}, err_")), + }; + method.body = format!( + "if err_ := {supported}({on}, \"{c}_{what}\"); err_ != nil {{\n\treturn {failed}\n}}\n{}", + method.body + ); } } (methods, default) diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index d2a84ad6e..0c7476d66 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -193,6 +193,10 @@ struct Arg { open: Option<(String, String)>, exprs: Vec, after: Vec, + /// Whether an expression gives Rust a retained box, which only the call takes. + gives: bool, + /// Whether its lowering, its one expression, throws. + throws: bool, } struct Gen<'a> { @@ -218,15 +222,18 @@ struct Gen<'a> { } impl<'a> Gen<'a> { - /// The statement that traps unless the library's target is among those of `on`, as the - /// item `what` is, then `then`; or `then` alone if it is on every target. - /// The check that the library's target is among those of `on`, as the item `what` is, - /// which traps, followed by `then`. - fn on(&self, on: On, what: &str, then: &str) -> String { + /// The statement that throws, if the body around it can, or else traps, unless the + /// library's target is among those of `on`, as the item `what` is, then `then`; or `then` + /// alone if it is on every target. + fn on(&self, on: On, what: &str, then: &str, throws: bool) -> String { let api = self.abi.api; + let check = match throws { + true => "try Native.on", + false => "Native.onOrTrap", + }; match api.everywhere(on) { true => then.to_string(), - false => format!("Native.onOrTrap({}, \"{what}\"){then}", on.0 & api.all().0), + false => format!("{check}({}, \"{what}\"){then}", on.0 & api.all().0), } } @@ -795,14 +802,17 @@ impl<'a> Gen<'a> { // One whose checks can fail throws, so conforms to no protocol of these, whose // requirements cannot throw; any other's checks never fail. let status = self.abi.traits_status_of(ty); - let on = self.on(self.on, c, "\n\t\t"); - // The statements lowering a value receiver for the function `what`. + // The statements lowering a value receiver for the function `what`, which on a target + // the type is not on throws where it can. let value = |what: &str| { let arena = match self.arena(ty) { true => format!("let a_ = Native.Arena(\"{what}\")\n\t\tdefer {{ a_.finish() }}\n\t\t"), false => String::new(), }; - format!("{on}Native.check()\n\t\t{arena}") + format!( + "{}Native.check()\n\t\t{arena}", + self.on(self.on, what, "\n\t\t", status) + ) }; let lower = match self.throwing(ty) { true => format!("try self._lower({})", self.arena_arg(ty)), @@ -919,9 +929,15 @@ impl<'a> Gen<'a> { Type::Object(_) => format!("{name}(raw: {c}_default())"), _ => format!("{name}._lift({c}_default())"), }; + // A constructor the target lacks throws, as a function's call does. + let limited = !self.abi.api.everywhere(self.on); + let throws = match limited { + true => " throws", + false => "", + }; body.push(format!( - "\t/// Rust's `Default`.\n\tpublic static func `default`() -> {name} {{\n\t\t{}Native.check()\n\t\treturn {lift}\n\t}}\n", - self.on(self.on, c, "\n\t\t") + "\t/// Rust's `Default`.\n\tpublic static func `default`(){throws} -> {name} {{\n\t\t{}Native.check()\n\t\treturn {lift}\n\t}}\n", + self.on(self.on, &format!("{c}_default"), "\n\t\t", limited) )); } } @@ -1048,7 +1064,7 @@ impl<'a> Gen<'a> { false => cases.push(format!("\tcase {case}({})\n", fields.join(", "))), } // A value of a variant the library's target lacks is refused as it crosses. - let refused = self.on(variant.on, &format!("{c}.{}", variant.name), "\n\t\t\t"); + let refused = self.on(variant.on, &format!("{c}.{}", variant.name), "\n\t\t\t", false); if e.plain() { lifts.push(format!("\t\tcase {tag}: return .{case}")); lowers.push(format!("\t\tcase .{case}:\n\t\t\t{refused}return {tag}")); @@ -1293,6 +1309,7 @@ impl<'a> Gen<'a> { let sw = self.sw(ty); arg.exprs.push(self.trampoline(ty, closure_)); arg.exprs.push(format!("Native.Box<{sw}>.give({e})")); + arg.gives = true; arg.exprs .push(format!("{{ data_ in Native.Box<{sw}>.release(data_) }}")); } @@ -1312,9 +1329,18 @@ impl<'a> Gen<'a> { arg.open = Some((format!("{tr}Native.reader(&{e}) {{ {r} in"), "}".to_string())); arg.exprs.push(r); } - (Type::Callback(i, _), _) => arg.exprs.push(format!("Native.lower_{}({e})", abi.callback(*i))), - (Type::Waker, _) => arg.exprs.push(format!("Native.waker({e})")), - (Type::Future, _) => arg.exprs.push(format!("Native.future({e})")), + (Type::Callback(i, _), _) => { + arg.exprs.push(format!("Native.lower_{}({e})", abi.callback(*i))); + arg.gives = true; + } + (Type::Waker, _) => { + arg.exprs.push(format!("Native.waker({e})")); + arg.gives = true; + } + (Type::Future, _) => { + arg.exprs.push(format!("Native.future({e})")); + arg.gives = true; + } (ty, Ty::Buf) if Abi::is_buf(ty) || matches!(ty.base(), Type::Option(inner) if Abi::is_buf(inner)) => { let b = format!("b{k}_"); arg.open = Some((format!("{tr}Native.view({e}) {{ {b} in"), "}".to_string())); @@ -1336,7 +1362,9 @@ impl<'a> Gen<'a> { } (ty, _) => { arena = self.arena(ty); - arg.exprs.push(self.lower(ty, e)); + let lowered = self.lower(ty, e); + arg.throws = lowered.contains("try "); + arg.exprs.push(lowered); } } (arg, arena) @@ -1672,6 +1700,14 @@ impl<'a> Gen<'a> { if !after.is_empty() { prelude.push(gate(&after.join("; "))); } + // A lowering that throws runs before any box is given, which the call alone would + // take: a throw between them would leak it. + if args.iter().any(|a| a.gives) { + for (k, arg) in args.iter_mut().enumerate().filter(|(_, a)| a.throws) { + prelude.push(format!("let c_arg{k}_ = {}", arg.exprs[0])); + arg.exprs[0] = format!("c_arg{k}_"); + } + } let exprs: Vec = args.iter().flat_map(|a| a.exprs.clone()).collect(); // A tuple of one returned whole is its element, written through its one out pointer. diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 8066df419..60b16e521 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -1033,6 +1033,12 @@ void targets() { lacks(() => api.wasmOnly(), 'api_wasm_only'); lacks(() => api.pointerBits(const api.PlatformWasm(bits: 32)), 'api_Platform.Wasm'); lacks(() => api.isFamily(api.Family.wasm), 'api_Family.Wasm'); + lacks(() => api.Page.default_(), 'api_Page_default'); + lacks(() => const api.Page(size: 1).debug(), 'api_Page_debug'); + lacks(() => const api.Page(size: 1) == const api.Page(size: 2), 'api_Page_eq'); + final booth = api.Booth(ticket: api.Ticket(1)); + lacks(() => booth.debug(), 'api_Booth_debug'); + lacks(() => booth == api.Booth(ticket: booth.ticket), 'api_Booth_eq'); } @pragma('vm:never-inline') diff --git a/ffi/tests/api.go b/ffi/tests/api.go index bc925c1ce..39a79ad19 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -770,6 +770,21 @@ func script() { // A record's traits borrow the checked handle it holds. front, back := must(api.NewSeat(nine, 1)), must(api.NewSeat(nine, 2)) fmt.Printf("seat %s %t %t\n", front.GoString(), must(front.Equal(front)), must(front.Equal(back))) + // The packages serve wasm32 too: the traits of a type only wasm32 has fail on the host's + // library as its functions do. + { + _, err := api.PageDefault() + check(errors.Is(err, errors.ErrUnsupported), "a Default off the target is unsupported") + _, err = api.Page{Size: 1}.Equal(api.Page{Size: 1}) + check(errors.Is(err, errors.ErrUnsupported), "an Equal off the target is unsupported") + booth := api.Booth{Ticket: nine} + _, err = booth.Equal(booth) + check(errors.Is(err, errors.ErrUnsupported), "a checked Equal off the target is unsupported") + check( + booth.GoString() == "api_Booth_debug: it is not on this library's target", + "a GoString off the target reads as its error", + ) + } for _, ticket := range []*api.Ticket{copied, renewed, seven, nine} { ticket.Close() } diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index ee2c9ba7d..e0669ce8e 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -1017,6 +1017,9 @@ fun owned() { "api_wasm_only" to { wasmOnly() }, "api_Platform.Wasm" to { pointerBits(Platform.Wasm(32u)) }, "api_Family.Wasm" to { isFamily(Family.WASM) }, + "api_Page_default" to { Page.default() }, + "api_Page_debug" to { Page(1u).debug() }, + "api_Booth_debug" to { Booth(Ticket.new(1u)).debug() }, )) { val thrown = runCatching(call).exceptionOrNull() check(thrown is UnsupportedOperationException && thrown.message == "$what: it is not on this library's target", "$what throws: $thrown") diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 9c8d87903..1d9f1d762 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -1190,6 +1190,9 @@ def raises(error, f, what): (api.wasm_only, "api_wasm_only"), (lambda: api.pointer_bits(api.Platform.Wasm(32)), "api_Platform.Wasm"), (lambda: api.is_family(api.Family.WASM), "api_Family.Wasm"), + (api.Page.default, "api_Page_default"), + (lambda: repr(api.Page(size=1)), "api_Page_debug"), + (lambda: repr(api.Booth(ticket=api.Ticket.new(1))), "api_Booth_debug"), ]: try: f() diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index d0dbba201..f3f207038 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -21,6 +21,23 @@ final class PrintLog: Log { } } +/// A Log that counts its frees. +final class DropLog: Log { + let drops: Cell + + init(_ drops: Cell) { + self.drops = drops + } + + func log(_: UInt8, _: String) -> Bool { + true + } + + deinit { + drops.value += 1 + } +} + /// A Log that cancels a task at each line, and counts them. final class CancelLog: Log, @unchecked Sendable { let task: Cell?> @@ -65,6 +82,31 @@ func closed(_ function: String, _ body: () throws -> Void) { } } +/// Check that `body` throws the CheckError of `function`, which the library's target lacks. +func lacks(_ function: String, _ body: () throws -> Void) { + do { + try body() + check(false, "\(function) throws off its target") + } catch let e as CheckError { + check( + e.function == function && e.message == "it is not on this library's target", + "\(function) threw \(e)" + ) + } catch { + check(false, "\(function) threw \(error)") + } +} + +/// The file serves wasm32 too: what only wasm32 has throws on the host's library, wherever +/// Swift lets it. +func targets() { + lacks("api_wasm_only") { _ = try wasmOnly() } + lacks("api_Page_default") { _ = try Page.default() } + let booth = Booth(ticket: Ticket.new(1)) + lacks("api_Booth_debug") { _ = try booth.debugDescription } + lacks("api_Booth_eq") { _ = try booth == booth } +} + /// The refusal `refuse` throws for `reason`: a class, since Swift throws it. func refusal(_ reason: String) -> Refusal { do { @@ -657,6 +699,10 @@ func script() { mixed.append(TupleCounterRefusal(Counter.new("f", 6), kept)) check((try? reasons(mixed, nil)) == nil, "api_reasons of a closed Refusal beside a live one throws") check(try! live.description == "refused: live", "a refused call leaves the class it would move open") + // Nor does it keep a callback it gave before the throw. + let drops = Cell(0) + check((try? relay(DropLog(drops), [kept])) == nil, "api_relay of a closed Refusal throws") + check(drops.value == 1, "a call refused before Rust runs frees the callback it lowered") var at = Point(x: 1, y: 2) check((try? nudgeBy(&at, kept)) == nil, "api_nudge_by of a closed Refusal throws") check(at == Point(x: 1, y: 2), "a refused call writes nothing back") @@ -826,6 +872,7 @@ func script() { print("hop \(nextHop(hop).map { "Some(\($0))" } ?? "None")") } print("farther hop \(fartherHop(PairNonZeroU16(left: 3, right: .max)))") + targets() } func printTail(_ read: () throws -> (Data, UInt64)) { @@ -1262,9 +1309,8 @@ func broken(_ which: String) { _ = try! stray() case "lent stray": try! visitStray { _ in } - // The file serves wasm32 too, whose items the host's library lacks. - case "target": - _ = try! wasmOnly() + // The file serves wasm32 too, whose items the host's library lacks, where Swift cannot + // throw. case "variant": _ = pointerBits(.wasm(bits: 32)) case "plain variant": diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index b3c6a7dbf..0d9594b0e 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -948,7 +948,6 @@ fn api_swift() { ("outlived", OUTLIVED), ("stray", "a path from Rust is not UTF-8"), ("lent stray", "a path from Rust is not UTF-8"), - ("target", "api_wasm_only: it is not on this library's target"), ("variant", "api_Platform.Wasm: it is not on this library's target"), ("plain variant", "api_Family.Wasm: it is not on this library's target"), ] { diff --git a/ffi/tests/miri/api.rs b/ffi/tests/miri/api.rs index a9a8a7ed1..9449d5de6 100644 --- a/ffi/tests/miri/api.rs +++ b/ffi/tests/miri/api.rs @@ -102,16 +102,35 @@ unsafe extern "C" fn foreign_free(data: *mut c_void) { fn handles() { unsafe { let counter = api_Counter_new(view("c"), 5); - assert_eq!(api_Counter_get(counter), 5); + assert_eq!(api_Counter_get(counter, std::ptr::null_mut()), 5); let mut out = 0; - assert!(api_Counter_add(counter, 2, &mut out, std::ptr::null_mut())); - assert_eq!(api_Counter_get(counter), 7); - assert_eq!(take(api_Counter_name(counter)), "c"); + assert!(api_Counter_add(counter, 2, &mut out, std::ptr::null_mut(), std::ptr::null_mut())); + assert_eq!(api_Counter_get(counter, std::ptr::null_mut()), 7); + assert_eq!(take(api_Counter_name(counter, std::ptr::null_mut())), "c"); let other = api_Counter_new(view("d"), 3); - assert_eq!(api_Counter_plus(counter, other), 10); + assert_eq!(api_Counter_plus(counter, other, std::ptr::null_mut()), 10); api_Counter_free(other); // Moving the value out leaves the handle for the caller to free. - assert_eq!(api_Counter_into_sum(counter, 1), 8); + assert_eq!(api_Counter_into_sum(counter, 1, std::ptr::null_mut()), 8); + api_Counter_free(counter); + } +} + +/// A close while a call reserved the value to move out fails as borrowed, so the call's +/// rollback, once a later check of it fails, revives no value the close reported dropped. +#[test] +fn close_while_reserved() { + type Counter = rt::Object<::api_export::Counter>; + unsafe { + let counter = api_Counter_new(view("c"), 5); + let mut takes = rt::Takes::default(); + Counter::reserve(counter, &mut takes, "reserve").unwrap(); + assert_eq!(Counter::close(counter, "close"), Err(rt::Status::Borrowed)); + assert!(Counter::borrow(counter, "borrow").is_err()); + drop(takes); + let mut status = 0; + assert_eq!(api_Counter_get(counter, &mut status), 5); + assert_eq!(status, 0); api_Counter_free(counter); } } @@ -135,26 +154,26 @@ fn channel() { let w = waker(&counts); unsafe { let channel = api_Channel_new(); - let recv = api_Channel_recv(channel); + let recv = api_Channel_recv(channel, std::ptr::null_mut()); let mut payload = rt::Buf::NONE; assert_eq!(api_Channel_recv_poll(recv, w, &mut payload), rt::PENDING); - api_Channel_send(channel, view("hi")); + api_Channel_send(channel, view("hi"), std::ptr::null_mut()); assert_eq!(counts.wakes.load(Ordering::SeqCst), 1); assert_eq!(api_Channel_recv_poll(recv, w, &mut payload), rt::READY); assert_eq!(take(payload), "hi"); api_Channel_recv_future_free(recv); // A send from another thread wakes the poll's waker there. - let recv = api_Channel_recv(channel); + let recv = api_Channel_recv(channel, std::ptr::null_mut()); assert_eq!(api_Channel_recv_poll(recv, w, &mut payload), rt::PENDING); - api_Channel_send_from_thread(channel, view("far")); + api_Channel_send_from_thread(channel, view("far"), std::ptr::null_mut()); assert_eq!(counts.wakes.load(Ordering::SeqCst), 2); assert_eq!(api_Channel_recv_poll(recv, w, &mut payload), rt::READY); assert_eq!(take(payload), "far"); api_Channel_recv_future_free(recv); // Cancelled while pending, it frees what it held, its waker included. - let recv = api_Channel_recv(channel); + let recv = api_Channel_recv(channel, std::ptr::null_mut()); assert_eq!(api_Channel_recv_poll(recv, w, &mut payload), rt::PENDING); api_Channel_recv_future_free(recv); api_Channel_free(channel); diff --git a/quest/m1/ffi-speed/go-status.md b/quest/m1/ffi-speed/go-status.md index 16e2908ee..8698e1288 100644 --- a/quest/m1/ffi-speed/go-status.md +++ b/quest/m1/ffi-speed/go-status.md @@ -11,22 +11,15 @@ main in [the bench](/bench/ffi/README.md#per-handle-baseline). ## Plan -- Recommended: pass the stack cell's address hidden from escape analysis, as a `uintptr` - turned back into a pointer at the call. A goroutine inside a cgo call is never shrunk - (the runtime's `isShrinkStackSafe` refuses one in a syscall), and the glue writes the - status only when a check fails, before any Rust code runs, so before any callback into - Go could grow and move the stack. Make that ordering a checked invariant of the glue, - not a convention. The fixtures run `go vet`, which flags a `uintptr` made back into a - pointer. +- ✅ Pass the stack cell's address hidden from escape analysis, as a `uintptr` turned back + into a pointer at the call (rejected: keep the heap allocation; decide later). Settled by + the maintainer 2026-11-13. A goroutine inside a cgo call is never shrunk (the runtime's + `isShrinkStackSafe` refuses one in a syscall), and the glue writes the status only when a + check fails, before any Rust code runs, so before any callback into Go could grow and + move the stack. The generated glue checks that ordering at run time: a status written + after a callback into Go began fails loudly, never silently. The fixtures run `go vet`, + which flags a `uintptr` made back into a pointer. - Rejected: a thread-local last error, refused on 2026-11-12 as hidden state a caller can - forget to read; a `sync.Pool` of C cells, which costs more than the allocation; Dart's - one cell per isolate, since goroutines share threads. + forget to read; a `sync.Pool` of C cells, which costs more than the allocation; one + shared cell, since goroutines share threads. - Re-measure the Go getter and handle call against main, CPU first. - -## Waiting - -- Maintainer: whether Go may pass a stack address that escape analysis cannot see, which - relies on the runtime never moving a stack in a cgo call before a callback, a behavior - cgo's documented rules do not promise. Recommended: yes, with the ordering checked in - the glue, as it wins back 6 ns on every checked Go call; the other way is to keep the - allocation. From 729415ada6e2b01d5a2d39f6d8ef618080a994e1 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:59:41 -0800 Subject: [PATCH 22/30] test(ffi): name the Kotlin seat beside main's numbers apart Co-Authored-By: Claude Opus 5.5 --- ffi/tests/api.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 32d149c65..ea0c7603d 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -600,8 +600,8 @@ fun script() { println("reissued $given $held, cloned ${Ticket.clones()}") // A record's traits borrow the checked handle it holds. val front = seat(nine, 1u) - val back = seat(nine, 2u) - println("seat ${front.debug()} ${front == front.copy()} ${front == back}") + val rear = seat(nine, 2u) + println("seat ${front.debug()} ${front == front.copy()} ${front == rear}") // A kept closure, called on another thread after the call that took it returned. println("spawned ${Spawned.new { 7u }.join()}") // A kept closure: called again at the instant it asks for, then once its waiter wakes. From 78a500c20e9075733f50300a4ec5b1ccd9626307 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:15:29 -0800 Subject: [PATCH 23/30] fix(ffi): report a type's handle becoming checked, or no longer, as a lock break A newly checked handle gives each call taking it a status and makes it throw in Swift, which the API lock passed as an addition. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 3 +++ ffi/fixtures/lock/expected.txt | 1 + ffi/fixtures/lock/src/lib.rs | 27 +++++++++++++++++++++++++++ ffi/src/lock.rs | 22 +++++++++++++++++++++- 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/ffi/README.md b/ffi/README.md index 32b68ddf0..6ce9b664f 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -209,6 +209,9 @@ API breaks the one in `file`, with an error at each item that names how: tag, one added to an enum another crate matches exhaustively, and an enum gaining its first variant with fields or losing its last, which changes it between a C enum and a struct; +- whether a type's handle is checked at run time, which gives or takes a status from each + call taking it, and makes it throw in Swift: borrowing it exclusively or moving it out in + an added method makes it checked; - the library's name, which every C name starts with; - a C name that now names another item: since a name depends on every item exported ([The C ABI](#the-c-abi)), adding `audio::encoder_finish` renames `audio_encoder::finish` diff --git a/ffi/fixtures/lock/expected.txt b/ffi/fixtures/lock/expected.txt index 97260a82d..0ea8170fd 100644 --- a/ffi/fixtures/lock/expected.txt +++ b/ffi/fixtures/lock/expected.txt @@ -8,6 +8,7 @@ fixtures/lock/src/lib.rs:191:1: rust2-ffi: the API breaks the baseline at tuple fixtures/lock/src/lib.rs:21:2: rust2-ffi: the API breaks the baseline at const `lock::limits_max::RATE`: its C name `LOCK_LIMITS_MAX_RATE` is now `LOCK_LIMITS_MAX_RATE_`, and `LOCK_LIMITS_MAX_RATE` names const `lock::limits::MAX_RATE` fixtures/lock/src/lib.rs:308:1: rust2-ffi: the API breaks the baseline at enum `lock::Speed`: variant `Fast` is removed; variant `Quick` is added to an exhaustive enum fixtures/lock/src/lib.rs:30:1: rust2-ffi: the API breaks the baseline at fn `lock::removed`: it is removed +fixtures/lock/src/lib.rs:331:1: rust2-ffi: the API breaks the baseline at type `lock::Gauge`: its handle is now checked at run time, so each call taking it gains a status, and throws in Swift fixtures/lock/src/lib.rs:36:1: rust2-ffi: the API breaks the baseline at fn `lock::portable`: it is no longer on wasm32-unknown-unknown fixtures/lock/src/lib.rs:46:1: rust2-ffi: the API breaks the baseline at fn `lock::scale`: its parameter `x`'s type; its return type fixtures/lock/src/lib.rs:66:2: rust2-ffi: the API breaks the baseline at fn `lock::Meter::add`: its parameter `n`'s type diff --git a/ffi/fixtures/lock/src/lib.rs b/ffi/fixtures/lock/src/lib.rs index 76c5be6d3..d39c31afc 100644 --- a/ffi/fixtures/lock/src/lib.rs +++ b/ffi/fixtures/lock/src/lib.rs @@ -325,3 +325,30 @@ pub fn speed(speed: Speed) -> u32 { pub fn failing_meter() -> Result<(), Meter> { Err(Meter::new()) } + +/// A handle `next` borrows exclusively, which checks each call taking it at run time: each +/// gains a status, and throws in Swift. +pub struct Gauge { + level: u32, +} + +impl Gauge { + pub fn new() -> Self { + Self { level: 0 } + } + + pub fn level(&self) -> u32 { + self.level + } + + #[cfg(feature = "next")] + pub fn raise(&mut self) { + self.level += 1; + } +} + +impl Default for Gauge { + fn default() -> Self { + Self::new() + } +} diff --git a/ffi/src/lock.rs b/ffi/src/lock.rs index 36e17dddc..6ec868185 100644 --- a/ffi/src/lock.rs +++ b/ffi/src/lock.rs @@ -265,6 +265,18 @@ pub fn breaks(base: &Api, now: &Api, prefix: &str) -> Vec { changes.push(format!("it is no longer on {}", lost.join(", "))); } changes.extend(changed(&base, &now, &maps, &ours.item, &theirs.item)); + if let (Item::Object(i), Item::Object(j)) = (ours.item, theirs.item) { + match (base.checked[i], now.checked[j]) { + (false, true) => changes.push( + "its handle is now checked at run time, so each call taking it gains a status, and throws in Swift" + .to_string(), + ), + (true, false) => changes.push( + "its handle is no longer checked at run time, so each call taking it loses its status".to_string(), + ), + _ => {} + } + } if ours.c != theirs.c { let mut renamed = format!("its C name `{}` is now `{}`", ours.c, theirs.c); if let Some(other) = now.named(&ours.c) { @@ -325,6 +337,9 @@ struct Index<'a> { /// Each tuple's struct by its Rust spelling: its C name, and a function that takes or /// returns it. tuples: BTreeMap, + /// Whether a call can fail a check of each object's handle, which gives each call + /// taking it a status. + checked: Vec, } impl<'a> Index<'a> { @@ -460,7 +475,12 @@ impl<'a> Index<'a> { }); } } - Self { api, entries, tuples } + Self { + api, + entries, + tuples, + checked: abi.checked.clone(), + } } /// The item whose C name is `c`. From 909e5aad75eedb1debd3cb6eaeea175e05916630 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:25:15 -0800 Subject: [PATCH 24/30] docs(ffi): place a pending future's out pointer before a checked call's status Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ffi/README.md b/ffi/README.md index 6ce9b664f..e661a44e7 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -290,7 +290,8 @@ where it was while its file is unchanged. one again, and owns what it borrowed: the handle it was called on stays borrowed until it finishes or is freed. `_start` is the call and a first poll with no waker in one: a future ready at once costs one call and no handle, and one that pends writes its - handle through the last parameter, which must not be null. A null waker wakes nothing + handle through its `_future` out pointer, which must not be null: the last parameter, or + the one before a checked call's `status`. A null waker wakes nothing and costs no allocation, and a future is one allocation. - **Wakers** move into Rust, which calls `drop` once it lets go of the last clone, so a state machine polled with a waker, such as moq's `Driver`s through kio's `Waiter`, runs From a2b0bcc7496627e271cf48e3a4897613f704061a Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:41:31 -0800 Subject: [PATCH 25/30] quest: drop ffi-abort-cores' link to the checks-raise quest this branch finishes Co-Authored-By: Claude Opus 5.5 --- quest/m0/ffi-abort-cores.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/quest/m0/ffi-abort-cores.md b/quest/m0/ffi-abort-cores.md index 3503f243b..9c8ec9e86 100644 --- a/quest/m0/ffi-abort-cores.md +++ b/quest/m0/ffi-abort-cores.md @@ -65,7 +65,3 @@ Kotlin abort on `java`. ## Hosts - linux - the core handler and `prctl` are Linux's; macOS's default core limit is already 0 - -## Related - -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - turns many of these aborts into raised errors, leaving fewer to cover From b527ab2c552c2fef36cbf06bbbbe3241a35b0eb9 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:49:08 -0800 Subject: [PATCH 26/30] test(ffi): pass main's new checked calls a status, and match the reserved move Co-Authored-By: Claude Opus 5.5 --- ffi/tests/api.c | 4 ++-- ffi/tests/fixtures.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 0b229f741..06d9058a3 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -709,8 +709,8 @@ static int script(void) { printf("\n"); api_vec_tuple_string_Counter_free(ranked); api_Counter *best = api_Board_best(board); - print("best ", api_Counter_name(best), " "); - printf("%llu\n", (unsigned long long)api_Counter_get(best)); + print("best ", api_Counter_name(best, NULL), " "); + printf("%llu\n", (unsigned long long)api_Counter_get(best, NULL)); api_Counter_free(best); api_Board_free(board); /* A map crosses as its pairs and a set as its items, which Rust collects in order, each diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 715b21150..9df26f140 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1500,7 +1500,7 @@ fn unreached_types() { assert!(glue.contains("fn pkg_copied_again("), "{glue}"); // A conversion target only an `impl Into` names is reached, and taken. assert!( - glue.contains("rt::Object::<::conv_reached::Value>::take(p0, w)"), + glue.contains("rt::Object::<::conv_reached::Value>::reserve(p0, takes, w)"), "{glue}" ); assert!(glue.contains("::kio_reached::Pending"), "{glue}"); From 85261792525c9f91d5d62d450471a616036b3f10 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:38:46 -0800 Subject: [PATCH 27/30] fix(ffi): quests outlive checks-raise's deletion, and rustfmt Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/src/lib.rs | 6 +++++- ffi/src/glue.rs | 12 +++++++----- quest/m1/ffi-speed/object-ownership.md | 3 --- quest/m1/ffi-speed/shared-borrow.md | 5 +++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index fb5871958..cd1b30110 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -968,7 +968,11 @@ impl Seat { /// copied checked objects, alone and held in a record and an enum. pub fn admit(entry: (Counter, Score, Seat, Choice)) -> u64 { let (counter, score, seat, choice) = entry; - counter.value.wrapping_add(score.count).wrapping_add(seat.score.count).wrapping_add(choose(choice)) + counter + .value + .wrapping_add(score.count) + .wrapping_add(seat.score.count) + .wrapping_add(choose(choice)) } /// A score picked, or none: a data enum with an empty variant beside a checked object. diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 328b3ff16..2043421e0 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -1521,7 +1521,10 @@ impl Gen<'_> { let releases: Vec = (record.fields.iter().enumerate()) .filter_map(|(n, f)| self.release(&f.ty, &format!("c.f{n}"))) .collect(); - self.item(format!("fn release_{name}(c: &{name}) {{\n\t{}\n}}\n", releases.join("\n\t"))); + self.item(format!( + "fn release_{name}(c: &{name}) {{\n\t{}\n}}\n", + releases.join("\n\t") + )); } if self.reserves(&ty) { self.checking.set(true); @@ -2186,16 +2189,15 @@ impl Gen<'_> { let mut moves = Vec::new(); let mut stmts = Vec::new(); let mut releases = Vec::new(); - let mut sort = |ty: &Type, (reserve, stmt, _): &(String, String, String)| { - match (reserve.is_empty(), self.checks(ty)) { + let mut sort = + |ty: &Type, (reserve, stmt, _): &(String, String, String)| match (reserve.is_empty(), self.checks(ty)) { (false, _) => { reserves.push(reserve.clone()); moves.push(stmt.clone()); } (true, true) => checks.push(stmt.clone()), (true, false) => stmts.push(stmt.clone()), - } - }; + }; let mut args = Vec::new(); let mut params = Vec::new(); let mut c_params = export.call.params.iter(); diff --git a/quest/m1/ffi-speed/object-ownership.md b/quest/m1/ffi-speed/object-ownership.md index ed9343d3b..ebe67c0e5 100644 --- a/quest/m1/ffi-speed/object-ownership.md +++ b/quest/m1/ffi-speed/object-ownership.md @@ -19,6 +19,3 @@ Ownership stays automatic: a dropped object is still freed without a call from t - Explicit `Close` stays, and the cleanup stays as the backstop; dropping it is refused, since a missed `Close` would leak. - Re-measure the bench's object return and stamped write. - -## Related - diff --git a/quest/m1/ffi-speed/shared-borrow.md b/quest/m1/ffi-speed/shared-borrow.md index 516967a6d..dc0e1e5e3 100644 --- a/quest/m1/ffi-speed/shared-borrow.md +++ b/quest/m1/ffi-speed/shared-borrow.md @@ -21,8 +21,9 @@ flag; an exclusive borrow, a move, or a free clears the bias and waits for the t slots naming the handle to empty, then falls back to the count. A slot collision falls back to the count too. The uncontended cost stays one atomic store and one load, so the single-thread rows must not rise. -The language hold counts then need the same treatment, or folding into the C ABI's check, -which [checks-raise](/quest/m1/ffi-export/checks-raise.md) may already do. +The language hold counts then need the same treatment: Go's and Kotlin's still count each +call beside the C ABI's check, while Python's and Dart's borrow through it alone for a +checked type since [kixel-ai/rust2#730](https://github.com/kixel-ai/rust2/pull/730). Rejected for now: per-handle sharded counters, which cost each handle a cache line per shard. From b5b99ca2dd1b58c5712d1acb6e488e93d7800790 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 12:11:08 -0800 Subject: [PATCH 28/30] fix(ffi): Kotlin's move cleanup binds no name a parameter takes The catch bound `e` and `.also` bound `it`, shadowing a moved parameter of either name, so its cleanup failed to compile. The api fixture's `spend(e, it)` covers it. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/src/lib.rs | 6 +++++ ffi/src/kotlin.rs | 47 +++++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index cd1b30110..cc5604fdf 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -1005,6 +1005,12 @@ pub fn heft(scored: (Score, Bytes), lent: &Parcel, moved: (Counter, Bytes)) -> u score.count + lent.score.count + counter.value + lengths as u64 } +/// Both counters' values, or an error once they overflow: moved parameters named as what +/// a binding's own cleanup might bind, an exception `e` and a lambda's `it`. +pub fn spend(e: Counter, it: Counter) -> Result { + e.value.checked_add(it.value).ok_or_else(|| "overflow".to_string()) +} + /// An enum named as the runtime's status, whose constants C would name as its statuses. #[derive(Clone, Copy, Debug, PartialEq)] pub enum Status { diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 643aed885..760cf97d0 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -523,6 +523,18 @@ struct Gen<'a> { } impl<'a> Gen<'a> { + /// The Kotlin call `raw`, then `after`, which closes what it moved out, however it ended + /// unless a failed check refused it, which moved nothing. It binds no name a parameter + /// `after` closes may take: its exception's is fresh, and its result is `_`. + fn moving(&mut self, raw: &str, after: &[String]) -> String { + let after = after.join("; "); + let thrown = self.fresh("e"); + format!( + "try {{\n{}\n}} catch ({thrown}: Throwable) {{\n\tif ({thrown} !is _Native.Refused) {{ {after} }}\n\tthrow {thrown}\n}}.also {{ _ -> {after} }}", + indent_by(raw, 1) + ) + } + fn fresh(&mut self, base: &str) -> String { loop { self.fresh += 1; @@ -2413,7 +2425,10 @@ impl<'a> Gen<'a> { let call = if after.is_empty() { raw } else { - self.refused_first(&bare, moving(&raw, &after)) + { + let moving = self.moving(&raw, &after); + self.refused_first(&bare, moving) + } }; let awaited = format!( "_Native.Signal().let {{ {s} -> _Native.await({call}, {s}, {{ f, n -> _Native.{poll_bare}(f, n) }}, {{ _Native.{free_bare}(it) }}) }}", @@ -2504,7 +2519,10 @@ impl<'a> Gen<'a> { let raw = if after.is_empty() { raw } else { - self.refused_first(&bare, moving(&raw, &after)) + { + let moving = self.moving(&raw, &after); + self.refused_first(&bare, moving) + } }; value = format!("({raw}) as {ok_kt}"); } else if let Some(c_ok) = direct(&outs, &written, &function.ok, function.err.as_ref()) { @@ -2549,7 +2567,10 @@ impl<'a> Gen<'a> { ); let caught = match after.is_empty() { true => caught, - false => self.refused_first(&bare, moving(&caught, &after)), + false => { + let moving = self.moving(&caught, &after); + self.refused_first(&bare, moving) + } }; value = match ok { Type::Unit => caught, @@ -2616,7 +2637,10 @@ impl<'a> Gen<'a> { let raw = if after.is_empty() { raw } else { - self.refused_first(&bare, moving(&raw, &after)) + { + let moving = self.moving(&raw, &after); + self.refused_first(&bare, moving) + } }; value = match ok { Type::Unit => format!("{raw}.let {{ }}"), @@ -2694,7 +2718,10 @@ impl<'a> Gen<'a> { let raw = if after.is_empty() { raw } else { - self.refused_first(&bare, moving(&raw, &after)) + { + let moving = self.moving(&raw, &after); + self.refused_first(&bare, moving) + } }; value = format!("({raw}) as {ok_kt}"); } else { @@ -3158,16 +3185,6 @@ fn bytes(ty: &Type) -> bool { } } -/// The Kotlin call `raw`, then `after`, which closes what it moved out, however it ended -/// unless a failed check refused it, which moved nothing. -fn moving(raw: &str, after: &[String]) -> String { - let after = after.join("; "); - format!( - "try {{\n{}\n}} catch (e: Throwable) {{\n\tif (e !is _Native.Refused) {{ {after} }}\n\tthrow e\n}}.also {{ {after} }}", - indent_by(raw, 1) - ) -} - /// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, /// a list, or a tuple, unless `Copy`, and never through a reference. fn moved(abi: &Abi<'_>, ty: &Type) -> bool { From 9fed597091e30afc35913ffe51bb616bd60749b0 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:37:37 -0800 Subject: [PATCH 29/30] test(ffi): a refused Score::note leaves the score as it was Co-Authored-By: Claude Opus 5.5 --- ffi/tests/api.c | 8 ++++++++ ffi/tests/api.go | 3 +++ 2 files changed, 11 insertions(+) diff --git a/ffi/tests/api.c b/ffi/tests/api.c index cda91885b..0f56da740 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -2123,6 +2123,14 @@ static int statuses(void) { fprintf(stderr, "a seat's count with a dropped mark wrote %d\n", status); failed = 1; } + /* A value's `&mut self` method refused over a dropped mark leaves the value as it was. */ + status = 0; + api_Score refused = api_Score_of(2); + api_Score_note(&refused, dropped, &status); + if (status != API_STATUS_MOVED || api_Score_count(&refused) != 2) { + fprintf(stderr, "a score noting a dropped mark wrote %d, leaving %llu\n", status, (unsigned long long)api_Score_count(&refused)); + failed = 1; + } status = 0; if (api_choose(empty, &status) != 0 || api_choose(picked, &status) != 3 || status != 0) { fprintf(stderr, "choosing wrote %d\n", status); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 0ad3b0328..0f0214749 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -509,6 +509,9 @@ func script() { notedBefore := must(noted.Note(notedBy)) fmt.Printf("noted %d %d\n", notedBefore, noted.Count()) notedBy.Close() + // Refused over a closed mark, the call writes nothing back. + _, refused := noted.Note(notedBy) + check(refused != nil && noted.Count() == 7, "a refused note leaves the score as it was") mark.Close() // Rust frees the implementation once it drops it, releasing Go's. From 41d9a719a918c9234eb440d487713c20425ad41e Mon Sep 17 00:00:00 2001 From: quest fixer Date: Sat, 14 Nov 2026 17:40:40 -0800 Subject: [PATCH 30/30] fix(ffi): reconcile Dart waker with object runtime checks --- ffi/runtime/rt.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index 66cf9db23..0b81d3ea3 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -318,14 +318,14 @@ bool _readerSeek( /// on a thread calling into Dart. final class Waker extends _Object { final ffi.Pointer _wake; - final ffi.Pointer _drop; + final ffi.Pointer _dropFn; final ffi.NativeFinalizer _finalizer; Waker._(_Waker raw) : _wake = raw.wake, - _drop = raw.drop, + _dropFn = raw.drop, _finalizer = _n.finalizer(raw.drop), - super(raw.data, true) { + super(raw.data, true, false) { _finalizer.attach(this, _raw, detach: this); } @@ -335,7 +335,7 @@ final class Waker extends _Object { @override void _free() { _finalizer.detach(this); - _dartWakerCall(_n.home, _drop, _raw); + _dartWakerCall(_n.home, _dropFn, _raw); } /// Wake the task through Rust: a call after [close] throws [StateError].