From 72e164e1d814fd5140d2e4904be920c8ca5eca82 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 17:56:20 -0800 Subject: [PATCH 01/24] chore(quest): claim quest/m1/ffi-export/copy-values.md Quest-Claim: 5d47f8349b240d7a From 3182bb3909052e05ad5fe1c34382ebe253899e79 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 19:42:48 -0800 Subject: [PATCH 02/24] wip(ffi): Copy types cross by value Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 75 ++++--- ffi/fixtures/api/examples/transcript.rs | 9 +- ffi/fixtures/api/src/lib.rs | 33 +++ ffi/runtime/jni.rs | 87 +++++++ ffi/runtime/python.rs | 32 +++ ffi/runtime/rt.dart | 14 -- ffi/runtime/rt.hpp | 10 + ffi/src/abi.rs | 64 +----- ffi/src/c.rs | 38 ++-- ffi/src/collect.rs | 286 ++++++++++++++++++------ ffi/src/cpp.rs | 65 +++--- ffi/src/dart.rs | 95 +++++--- ffi/src/glue.rs | 83 ++++--- ffi/src/go.rs | 85 ++++--- ffi/src/ir.rs | 56 ++++- ffi/src/kotlin.rs | 184 ++++++++++++--- ffi/src/merge.rs | 11 + ffi/src/python.rs | 68 +++--- ffi/src/swift.rs | 78 ++++--- ffi/tests/api.c | 103 +++++---- ffi/tests/api.cpp | 25 ++- ffi/tests/api.dart | 8 +- ffi/tests/api.go | 89 ++++---- ffi/tests/api.kt | 26 ++- ffi/tests/api.py | 7 + ffi/tests/api.swift | 7 + 26 files changed, 1108 insertions(+), 530 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 60386f2f6..b8f60f671 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -71,12 +71,13 @@ re-exports its items instead, which export. A type no path reaches is refused. | --- | --- | | a struct whose fields are all `pub`, not `#[non_exhaustive]`, which map, with no `Drop` of its own | a record: a struct, copied across | | an enum with variants whose fields map, with no `Drop` of its own | a C enum without fields, or a struct of a tag and a union | +| any other type that is `Copy` and `Send`, such as moq's `Timestamp` or std's `Instant` | an opaque value: a struct of its bytes, copied across, which only Rust makes or reads; its `pub` fields are getters, and a `&mut self` method or a setter is refused, as for a record; one no export returns is left out, since C could never make one | | any other type, if `Send` | an object: an opaque handle to an `Arc`, freed with `_free` | | a type whose only generic parameters are lifetimes, such as `Path<'a>` | an object holding its `'static` form | | a generic type, at each instance it infers | that instance as any other type, with each method whose impl and own bounds it meets; any other instance is refused | | a `pub` field of an object | a getter returning a copy, and a setter assigning in place, as `x.field = value` does | | a `pub` trait | a callback interface: a table of C functions and their data; a trait C cannot implement, such as one with a blanket impl, an associated type, or a supertrait but a marker, is left out, since C never passes one, and a signature taking one from C is refused | -| a type parameter or `impl` bounded by `Fn`, `FnMut`, or `FnOnce` alone, taking what a callback takes, objects by reference, or `Copy` objects, and returning a scalar, a `String`, a `Copy` object, an `Option` of one, or a `Result` of one and a `String` | a C function and its `void *` data; one a lifetime bound or the return lets outlive the call, as kio's `clock::drive` and a spawned task's are, adds a `free` Rust calls once it drops it; a closure of any other signature is refused | +| a type parameter or `impl` bounded by `Fn`, `FnMut`, or `FnOnce` alone, taking what a callback takes, objects by reference, or opaque values, and returning a scalar, a `String`, an opaque value, an `Option` of one, or a `Result` of one and a `String` | a C function and its `void *` data; one a lifetime bound or the return lets outlive the call, as kio's `clock::drive` and a spawned task's are, adds a `free` Rust calls once it drops it; a closure of any other signature is refused | | a type parameter or `impl` taken by value and bounded by `Future` or `IntoFuture`, whose every bound `Pin + Send>>` meets, as kio's `clock::timeout` takes, or taken as `Pin<&mut F>` by a poll, as kio's `Waiter::poll_future` takes | `_foreign_future`: a future of `()` C implements, which Rust takes as that type, or which the future the poll makes owns and pins; a future of another output is refused | | a type parameter or `impl` bounded by `Read` and `Seek` alone, behind `&mut`, which no lifetime bound or return lets outlive the call, as moq-mux's `container::mp4::repair` takes | `_foreign_reader`: a `read` and a `seek` C implements and their data, lent for the call; any other reader is refused | | `fn` and inherent `pub` methods | functions named through their type | @@ -129,7 +130,7 @@ maps, which may name more. Inference finishes before any type is mapped, so mapp never adds an instance. An instance that holds an instance of a type inside it, such as `Cell>`, is refused, as it could name ever deeper ones. -A record or enum holds only values and `Copy` handles: one with a field that does not map, +A record or enum holds only values: one with a field that does not map, or that holds itself, is an object instead, and the census lists it. A borrowed return is returned as a clone, so it must be `Clone`, and a handle in it is a new handle to the clone, but for an object a `&self` method of an object returns borrowed from its receiver @@ -188,8 +189,7 @@ dropped for the other target's span where the file differs. - **Handles** are `Arc`s, and a call borrows the value as Rust's receiver or parameter 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 + `T`, after which the handle is only freed. 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 @@ -197,6 +197,12 @@ dropped for the other target's span where the file differs. 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. +- **Opaque values** are `typedef struct { uintN_t opaque[n]; } ;`, the bytes of a + `Copy` type with private fields, in units of its alignment up to 8 bytes: C copies one + as any struct, and passes `&self` as a `const *`, but never makes or changes one, + and frees none. Its size and alignment are the largest of the export's targets, so one + header serves them all, a few bytes past a 32-bit target's own. A change of size is an + ABI change, as a record's field change is. - **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 @@ -239,13 +245,12 @@ dropped for the other target's span where the file differs. - **Closures** are a function and its data, which Rust calls from any thread, one call at a time. One lent for a call Rust calls only during the call; one Rust keeps comes with a `free`, which Rust calls once it drops the closure, done or not, maybe before - any call. One takes what a callback takes, and objects by reference or `Copy` objects, - each lent as a handle to the value where it is for that call: C borrows it no further - than Rust lent it, and moving out of it, freeing it, or keeping a borrow past the call - aborts. What it returns is Rust's to own: a handle C made or was given, one - `_copy` made of a `Copy` object C keeps, which copies it as Rust does without - running its `Clone`, or a buffer `_buf_copy` made; a `Result` returns false and - writes its error's buffer. + any call. One takes what a callback takes, objects by reference, each lent as a handle + to the value where it is for that call, and opaque values, copied: C borrows a lent + handle no further than Rust lent it, and moving out of it, freeing it, or keeping a + borrow past the call aborts. What it returns is Rust's to own: an opaque value, copied + as Rust copies it without running its `Clone`, or a buffer `_buf_copy` made; a + `Result` returns false and writes its error's buffer. - **Readers** are a `read`, a `seek`, and their data, lent for the call, which Rust calls from any thread, one call at a time. `read` writes at most the length it was given, `seek` takes `SEEK_SET`, whose offset's bits are a `uint64_t` that may pass @@ -338,7 +343,8 @@ exceptions and RTTI on or off: it throws nothing and reads no type information. - **Records** are structs, and **enums** an `enum class`, or for one with data a `std::variant` of a struct per variant, `_`. An enum's methods and traits are free functions taking it first. `PartialEq` is `==`, `Display` `to_string()`, - `Debug` `debug()`, and `Default` `default_()`. + `Debug` `debug()`, and `Default` `default_()`. An **opaque value** is a trivially + copyable class holding its C struct, with no public constructor, so only Rust makes one. - **Bytes** are passed as a `view`, which any contiguous bytes convert to, and returned as a `buf`, which owns Rust's buffer without a copy, so it outlives the object it came from. From C++20, either converts to `std::span`. A `view` or span @@ -352,8 +358,7 @@ exceptions and RTTI on or off: it throws nothing and reads no type information. - **Closures** are any callable, taken by a template parameter, which Rust calls from any thread, one call at a time. One lent for a call Rust calls only during the call; one Rust keeps it moves or copies into a copy of its own, which it destroys once done with it, - maybe before any call. An object it lends as `&T`, or a `Copy` one, is a `const` - reference to its class, and as `&mut T` an `api::borrow`, which reaches its methods + maybe before any call. An object it lends as `&T` is a `const` reference to its class, and as `&mut T` an `api::borrow`, which reaches its methods through `->` but neither moves nor copies. Moving out of either, or keeping a copy past the call, aborts. It returns bytes as anything a `view` takes, copied for Rust, and a `Result` as a `result`, or anything with its `operator bool`, `operator*`, and @@ -411,7 +416,11 @@ in as a `ByteArray`, bytes returned as their owner, and anything else encoded in assignment would hide. - **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()`. + `toString()`, `Debug` `debug()`, and `Default` a companion's `default()`. An **opaque + value** is a class with an internal constructor, so only Rust makes one: of no more than + 8 bytes on every target, and no padding, a value class over a `Long`, which allocates + nothing; otherwise one over a `LongArray`, whose `==` is Rust's, and whose `hashCode` + is 0, since none Kotlin computes agrees with it. - **Bytes** passed in are a `ByteArray`, copied, as are those Rust lends a closure or trait method. Bytes Rust returns, or a record holds, are a `Bytes`, read only through `size`, `get`, `slice`, `copyInto`, and `toByteArray()`, so no view of Rust's memory escapes and @@ -439,8 +448,7 @@ in as a `ByteArray`, bytes returned as their owner, and anything else encoded in - **Closures** are function types, which Rust calls from any thread: one lent for a call only during the call, and one Rust keeps, as `Spawned.new` does, until it drops it, when it deletes its global reference and the GC may collect it. An object it lends is - closed once the call returns. A `Copy` object it returns crosses as a new handle to a - clone, so Kotlin keeps its own. One returning a `Result` returns its value and fails by + closed once the call returns. One returning a `Result` returns its value and fails by throwing the `Exception` a function's `Err` throws, as `StringException("…")`; any other exception aborts, as a callback's does. - A **reader** is a `java.nio.channels.SeekableByteChannel`, which a `FileChannel` or @@ -502,7 +510,9 @@ caller imports it with no ignores. 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 `default()`. Constants are module or class attributes, made anew on each read unless a - literal. + literal. An **opaque value** is a slotted class holding its bytes as `bytes`, which only + Rust makes or reads: a value of another length raises `TypeError` where Rust would read + it. - **Bytes** passed in are any buffer, `bytes | bytearray | memoryview`: `bytes` is read in place, and others, which Python may change while Rust reads, are copied. Bytes Rust returns, or a record holds, are a read-only `memoryview` lending Rust's buffer until it @@ -524,8 +534,8 @@ caller imports it with no ignores. `close()` is called if it has one. - **Closures** are any callable, which Rust calls from any thread, one call at a time, taking the GIL: one lent for a call only during the call, and one Rust keeps until it - drops it, when it lets go of its reference. An object it lends, `Copy` ones included, - is closed once that call of it returns. What it returns Rust owns: a `str` is copied, + drops it, when it lets go of its reference. An object it lends is closed once that call + of it returns. What it returns Rust owns: a `str` is copied, and an object's handle cloned, so the caller's stays its own. One returning a `Result` fails by raising the exception of its error, such as `StringException(message)`, whose `error` Rust takes. Any other exception a closure or trait method raises prints its @@ -585,15 +595,16 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod `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, - which `Swift.Error` requires be copyable, is a final class instead, whose handle a call + conforms no noncopyable type to yet. An object 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 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. - **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`, and `Default` a static `default()`. An **opaque value** is + a struct holding its C struct, with no public initializer, so only Rust makes one. - **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. @@ -615,7 +626,7 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod at a time. One lent for a call Rust calls only during the call; one Rust keeps is `@escaping`, boxed until Rust drops it. An object it lends is `borrowing`, or `inout` for `&mut T`, so it cannot outlive the call; assigning another to the `inout` one traps, - since Rust's stays its own. A `Copy` object it takes is a copy of its own. One returning + since Rust's stays its own. One returning `Result` `throws`: Rust reads its error's `description`, so a thrown `StringError` returns its string as it was. - A **reader** is `inout some SeekableReader`, the runtime's protocol of @@ -695,7 +706,10 @@ reads a go.mod. `Debug` `GoString()`, `PartialEq` `Equal(other)`, all through Rust's, and `Default` a function of the package, `PointDefault()`. An object's exported `Handle_` field, and a trait's `Vtable_` function, are how one generated package reaches another's, - since cgo gives each its own C types: callers use neither. + since cgo gives each its own C types: callers use neither. An **opaque value** is a + struct of its bytes, `Bytes_`, and `Made_`, true once Rust made it: a zero value panics + where Rust would read it, and `==` does not compile, since bytes alike need not be + equal, so `Equal`, if it has one, is Rust's. - **Bytes** a call takes are a `[]byte`, and text a `string`, each read in place: Go's memory, pinned where C reads it through other memory. Bytes Rust passes a callback are copied into a `[]byte`. Bytes anywhere else, such as those Rust returns or a record @@ -731,7 +745,7 @@ reads a go.mod. - **Closures** are funcs, which Rust calls from any thread: one lent for a call only during the call, and one Rust keeps, as moq's `clock::drive` does, until Rust drops it, which releases it to the GC. An object it lends by reference is closed once the func - returns; a `Copy` one is its own clone, which it may keep. A `Result` returns an `error` + returns. A `Result` returns an `error` last, whose `Error()` crosses as Rust's `String`. What it returns crosses as Rust's: a `string` copied, and an object as a clone of its handle, so the func keeps its own; a closed one panics. @@ -794,7 +808,9 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass with data a sealed class with a final class per variant (`EventMoved`), so a `switch` over it is exhaustive. `Display` is `toString()`, `Debug` `debug()`, `PartialEq` `==`, all through Rust's, with a `hashCode` that throws an `UnsupportedError`, since no hash Dart - computes agrees with Rust's `==`, and `Default` a constructor, `default_()`. A plain enum's `==` is Dart's, by its value. + computes agrees with Rust's `==`, and `Default` a constructor, `default_()`. An + **opaque value** is a final class of its bytes as `int`s, with a private constructor, + so only Rust makes one. A plain enum's `==` is Dart's, by its value. - **Integers** are `int`. A narrower one passed in throws a `RangeError` when out of its range, and a `u64` passes its 64 bits, so one past `i64`'s range is negative in Dart. A `char` is a `String` of one Unicode scalar. @@ -822,9 +838,8 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass A call that throws before Rust sees it gives Rust none of its implementations. - **Closures** are Dart functions, which Rust calls on the isolate's thread: one lent for a call only during the call, and one Rust keeps until it drops it, which then lets go of - it. An object it lends, by reference or a `Copy` one by value, lives only for that call - of the closure. What it returns Rust takes: a `String` copied, and a `Copy` object as a - new handle to a clone. One whose Rust returns `Result` fails by throwing + it. An object it lends by reference lives only for that call of the closure. What it + returns Rust takes: a `String` copied, and an opaque value copied. One whose Rust returns `Result` fails by throwing `StringException`, the binding's own form of that error; anything else it throws aborts. - A **reader** Rust reads and seeks, a `Read + Seek` behind `&mut`, is a `SeekableReader`: `readInto` and `seek`, which Rust calls only during the call, on the isolate's thread. diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index f273a3cde..6f6bec1e8 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -301,7 +301,7 @@ fn main() { println!("payload {}", String::from_utf8_lossy(&message.payload)); println!("stamp {:?}", message.stamp.unwrap()); println!("sent {}", message.sent()); - // Setters assign in place: the bytes are copied in, and a `Copy` handle copied. + // Setters assign in place: the bytes are copied in, and a `Copy` value copied. let reset = Stamp::from_millis(4000); message.stamp = Some(reset); message.payload = Bytes::from_static(b"edited"); @@ -311,6 +311,13 @@ fn main() { message.stamp = None; println!("stamp {}", message.stamp.is_some()); + // A value past a word, copied across: a closure takes and returns one by value. + let moment = api::Moment::new(1500, 1000).unwrap(); + println!("moment {} {}", moment.millis(), api::Moment::new(1, 0).is_none()); + let shifted = api::shifted(moment, |m| m.after(500)); + println!("shifted {} {}", shifted.millis(), shifted == moment.after(500)); + println!("wrapped {} {}", moment.after(u64::MAX).millis(), moment == shifted); + println!("kept {}", api::emit(Print, vec![1, 2, 3, 4])); let logger = Logger::new(Box::new(Print)); println!("write {}", logger.write("hi")); diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index bc0211779..65181a167 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -948,6 +948,39 @@ impl Stamp { } } +/// A time on a clock of its own rate, as moq's `Timestamp`: `Copy` with private fields, past +/// a word on every target, and never all zero bytes, so only a value Rust made may cross. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Moment { + ticks: u64, + rate: std::num::NonZero, +} + +impl Moment { + /// `ticks` of `rate` a second, or none at a rate of zero. + pub fn new(ticks: u64, rate: u64) -> Option { + std::num::NonZero::new(rate).map(|rate| Self { ticks, rate }) + } + + /// Whole milliseconds since zero. + pub fn millis(&self) -> u64 { + (u128::from(self.ticks) * 1000 / u128::from(self.rate.get())) as u64 + } + + /// The moment `ticks` later, at the same rate, wrapping as `u64` does. + pub fn after(self, ticks: u64) -> Self { + Self { + ticks: self.ticks.wrapping_add(ticks), + ..self + } + } +} + +/// The moment `shift` returns for `moment`, which C copies in and out. +pub fn shifted(moment: Moment, shift: impl Fn(Moment) -> Moment) -> Moment { + shift(moment) +} + /// A stamp and a count: a record holding an object, whose method reads it. pub struct Stamped { pub at: Stamp, diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index bebc2d8ac..33cc7eb28 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -694,6 +694,93 @@ impl Writer { } } +/// An opaque value's C struct from the `long` Kotlin holds it in: its bytes, at the word's +/// lowest addresses, as [`to_word`] put them. +pub fn from_word(word: jlong) -> T { + const { assert!(size_of::() <= size_of::()) }; + let mut value = std::mem::MaybeUninit::::zeroed(); + // SAFETY: `T` is a struct of `MaybeUninit` units, any bytes of which are one. + unsafe { + std::ptr::copy_nonoverlapping((&raw const word).cast::(), value.as_mut_ptr().cast::(), size_of::()); + value.assume_init() + } +} + +/// The `long` Kotlin holds an opaque value's C struct in, which has no padding: Kotlin +/// compares two by it. +pub fn to_word(value: T) -> jlong { + const { assert!(size_of::() <= size_of::()) }; + let mut word: jlong = 0; + // SAFETY: every byte of a value with no padding is initialized, and the rest are zero. + unsafe { std::ptr::copy_nonoverlapping((&raw const value).cast::(), (&raw mut word).cast::(), size_of::()) }; + word +} + +/// An opaque value's C struct from the `W` words of the `long[]` Kotlin holds it in. +/// +/// # Safety +/// `array` is a live `long[]`. +pub unsafe fn from_words(env: *mut JNIEnv, array: jlongArray) -> T { + const { assert!(size_of::() <= W * size_of::()) }; + let mut words = [std::mem::MaybeUninit::::zeroed(); W]; + // SAFETY: `array` is live, and `T` is a struct of `MaybeUninit` units. + unsafe { + if array.is_null() || ((**env).v1_2.GetArrayLength)(env, array) != W as jsize { + fail(env, "jni", "an opaque value's words are not its own"); + } + ((**env).v1_2.GetLongArrayRegion)(env, array, 0, W as jsize, words.as_mut_ptr().cast()); + let mut value = std::mem::MaybeUninit::::zeroed(); + std::ptr::copy_nonoverlapping(words.as_ptr().cast::(), value.as_mut_ptr().cast::(), size_of::()); + value.assume_init() + } +} + +/// A new `long[]` of the `W` words an opaque value's C struct is held in: its bytes, then +/// zeros. Its padding crosses as the bytes it holds, which Kotlin never reads. +pub fn to_words(env: *mut JNIEnv, value: T) -> jlongArray { + const { assert!(size_of::() <= W * size_of::()) }; + let mut words = [std::mem::MaybeUninit::::zeroed(); W]; + // SAFETY: `env` is this thread's; JNI copies the words as bytes. + unsafe { + std::ptr::copy_nonoverlapping((&raw const value).cast::(), words.as_mut_ptr().cast::(), size_of::()); + let array = ((**env).v1_2.NewLongArray)(env, W as jsize); + if array.is_null() { + fail(env, "jni", "a long[] is out of memory"); + } + ((**env).v1_2.SetLongArrayRegion)(env, array, 0, W as jsize, words.as_ptr().cast()); + array + } +} + +/// An opaque value's C struct as it crosses encoded: the `W` words Kotlin holds it in, +/// whose little-endian bytes are its own on a little-endian target. +pub fn read_words(r: &mut Reader<'_>) -> T { + const { assert!(size_of::() <= W * size_of::() && cfg!(target_endian = "little")) }; + let bytes = r.take(W * size_of::()); + let mut value = std::mem::MaybeUninit::::zeroed(); + // SAFETY: `T` is a struct of `MaybeUninit` units, any bytes of which are one. + unsafe { + std::ptr::copy_nonoverlapping(bytes.as_ptr(), value.as_mut_ptr().cast::(), size_of::()); + value.assume_init() + } +} + +/// Encode an opaque value's C struct as the `W` words Kotlin holds it in: its bytes, then +/// zeros. Its padding crosses as the bytes it holds, which Kotlin never reads. +pub fn write_words(w: &mut Writer, value: T) { + const { assert!(size_of::() <= W * size_of::() && cfg!(target_endian = "little")) }; + let n = W * size_of::(); + w.0.reserve(n); + let start = w.0.len(); + // SAFETY: the vector has room for `n` more bytes, each written before its length grows. + unsafe { + let at = w.0.as_mut_ptr().add(start); + std::ptr::write_bytes(at, 0, n); + std::ptr::copy_nonoverlapping((&raw const value).cast::(), at, size_of::()); + w.0.set_len(start + n); + } +} + /// A C value as it crosses encoded: Kotlin writes one Rust reads, and Rust writes one it /// owns, freeing what it holds once copied. pub trait Wire: Sized { diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 7f39cf902..80ede112d 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -818,6 +818,38 @@ pub fn array_out(items: &[u8], index: usize) -> R { call_py(ty(index), vec![bytes]) } +/// An opaque value's C struct, from the `bytes` of exactly its size its class holds. +pub fn opaque_in(obj: *mut PyObject) -> R { + // SAFETY: the GIL is held, and `obj` is live; `T` is a struct of `MaybeUninit` units, + // any bytes of which are one. + unsafe { + if PyBytes_Check(obj) == 0 { + return expected_named(obj, "bytes"); + } + let mut ptr: *mut c_char = null_mut(); + let mut len = 0; + if PyBytes_AsStringAndSize(obj, &mut ptr, &mut len) != 0 { + return Err(Raised); + } + if len as usize != size_of::() { + return value_error(&format!("{len} bytes, not {}", size_of::())); + } + Ok(ptr.cast::().read_unaligned()) + } +} + +/// The `bytes` an opaque value's class holds of its C struct. Its padding crosses as the +/// bytes it holds, which Python never reads but to hand back. +pub fn opaque_out(value: T) -> R { + // SAFETY: the GIL is held, and `value` is `size_of::()` bytes. + unsafe { + Obj::new(PyBytes_FromStringAndSize( + (&raw const value).cast(), + size_of::() as Py_ssize_t, + )) + } +} + /// Bytes Rust lends a callback for its call, copied into `bytes`. pub fn lent_bytes(buf: rt::Buf) -> R { // SAFETY: the GIL is held, and a buffer Rust lent for the call. diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index af74e1d08..caa78270c 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -725,20 +725,6 @@ final class _Native { dst.ref.owner = src.owner; } - /// A handle Rust takes, of a clone of `object`'s value: what a closure returns of a - /// `Copy` object, which Rust copies out of it and frees. - ffi.Pointer handOver( - _Object object, - ffi.Pointer Function(ffi.Pointer, ffi.Pointer) clone, - ) { - final raw = object._hold(); - try { - return clone(home, raw); - } finally { - object._release(); - } - } - /// Abort with what a call into Dart threw, which Rust has nowhere to unwind to. Never abort(String what, Object error, StackTrace stack) => fail('$what: a call into Dart threw $error\n$stack'); diff --git a/ffi/runtime/rt.hpp b/ffi/runtime/rt.hpp index a07e362da..8b7521651 100644 --- a/ffi/runtime/rt.hpp +++ b/ffi/runtime/rt.hpp @@ -686,6 +686,16 @@ struct access { static auto own(T &object) { return object.raw_.own(); } + // A `Copy` value's C struct, which only Rust reads. + template + static auto bits(const T &value) noexcept { + return value.c_; + } + // The `Copy` value of the C struct `c` Rust made. + template + static T make(const C &c) noexcept { + return T(c); + } // `raw` owned by a `T`. template static T wrap(R *raw) { diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index f522ffd59..ac8ce2fc6 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -12,7 +12,7 @@ //! `moq_audio_encoder_finish_`. A `_` doubled at the split would read better, but C++ //! reserves every name containing `__`. Only the names that clash change. -use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::collections::{BTreeMap, HashMap}; use rustc_span::Span; @@ -150,9 +150,6 @@ pub struct Abi<'a> { pub tuples: BTreeMap>, /// Each `[u8; N]` struct by C name, with `N`. pub arrays: BTreeMap, - /// 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, /// Every C name claimed. claims: Vec, /// The C name of each item whose own clashed. @@ -172,7 +169,6 @@ impl<'a> Abi<'a> { lists: BTreeMap::new(), tuples: BTreeMap::new(), arrays: BTreeMap::new(), - copies: BTreeSet::new(), claims: Vec::new(), renames: HashMap::new(), clashes: Vec::new(), @@ -664,19 +660,11 @@ impl<'a> Abi<'a> { } /// The C function a closure is: after its data, each value it takes, lent for its - /// call, a `Copy` object as a handle; then for a `Result`, an out pointer for its value - /// and one for its error, returning whether it succeeded, or else its value. A - /// value it returns is Rust's: a buffer one `_buf_copy` made, or a new handle. + /// call; then for a `Result`, an out pointer for its value and one for its error, + /// returning whether it succeeded, or else its value. A buffer it returns is Rust's, + /// one `_buf_copy` made. pub fn closure(&self, closure: &Closure) -> Ty { - let mut params: Vec = (closure.params.iter()) - .map(|ty| match ty { - Type::Object(i) => Ty::Handle { - name: self.object(*i), - constant: true, - }, - ty => self.ty(ty), - }) - .collect(); + let mut params: Vec = closure.params.iter().map(|ty| self.ty(ty)).collect(); let ok = self.ty(&closure.ok); match &closure.err { None => Ty::Fn(params, Box::new(ok)), @@ -705,10 +693,7 @@ impl<'a> Abi<'a> { let ty = match owner { Ty::Handle { name, .. } => Ty::Handle { name: name.clone(), - constant: receiver == Receiver::Ref - || (receiver == Receiver::Owned - && (0..self.api.objects.len()) - .any(|i| self.api.objects[i].copy && &self.object(i) == name)), + constant: receiver == Receiver::Ref, }, // A value's receiver is read through a pointer, and never changed. value => Ty::In(Box::new(value.clone())), @@ -718,22 +703,7 @@ impl<'a> Abi<'a> { // What Rust writes back for each value C passes for `&mut`, after every parameter. let mut written = Vec::new(); for param in &function.params { - let copied = match ¶m.ty { - Type::Object(i) => Some(*i), - Type::Option(inner) => match **inner { - Type::Object(i) => Some(i), - _ => None, - }, - _ => None, - }; - let ty = match copied { - // Taken by value, a `Copy` value is copied out, leaving its handle as it was. - Some(i) if self.api.objects[i].copy => Ty::Handle { - name: self.object(i), - constant: true, - }, - _ => self.ty(¶m.ty), - }; + let ty = self.ty(¶m.ty); let name = escape(¶m.name); if let Type::Closure(closure) = ¶m.ty { // Named apart from every parameter of the function, which C requires. @@ -1090,7 +1060,6 @@ impl<'a> Abi<'a> { let mut lists = BTreeMap::new(); let mut tuples: BTreeMap> = BTreeMap::new(); let mut arrays = BTreeMap::new(); - let mut copies = BTreeSet::new(); let mut mixed = Vec::new(); let mut stack = types; while let Some(ty) = stack.pop() { @@ -1123,17 +1092,6 @@ impl<'a> Abi<'a> { stack.extend(elements); } Type::Closure(closure) => { - for ty in closure.params.iter().chain([&closure.ok]) { - let ty = match ty { - Type::Option(inner) => inner, - ty => ty, - }; - if let Type::Object(i) = ty - && self.api.objects[*i].copy - { - copies.insert(*i); - } - } stack.extend(&closure.params); stack.push(&closure.ok); stack.extend(&closure.err); @@ -1161,13 +1119,6 @@ impl<'a> Abi<'a> { for name in arrays.keys() { self.claim(name.clone(), "an array".to_string(), crate_span, None); } - for &i in &copies { - let object = &self.api.objects[i]; - let item = Item::Object(i); - let name = self.object(i); - let what = format!("type `{}`", self.api.rust(&object.path, &object.args)); - self.claim(format!("{name}_copy"), what, object.span, Self::made(&item, &name)); - } for (name, elements) in tuples .iter() .chain(mixed.iter().map(|(name, elements)| (name, elements))) @@ -1198,7 +1149,6 @@ impl<'a> Abi<'a> { self.lists = lists; self.tuples = tuples; self.arrays = arrays; - self.copies = copies; } /// Claim the functions a type's std traits export. diff --git a/ffi/src/c.rs b/ffi/src/c.rs index acdee884d..c625dd2b3 100644 --- a/ffi/src/c.rs +++ b/ffi/src/c.rs @@ -205,7 +205,7 @@ 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, record.opaque.is_none()); let owner = Ty::Struct(name.clone()); for method in &record.methods { self.function(method, Some(&owner)); @@ -237,16 +237,6 @@ impl Header<'_> { if t.clone { self.line(format!("{name} *{name}_clone(const {name} *self);")); } - if abi.copies.contains(&i) { - self.line( - comment( - "A new handle to a copy of the value, made as Rust copies it, never running its `Clone`: a closure hands Rust one it takes or returns.", - "", - ) - .trim_end(), - ); - self.line(format!("{name} *{name}_copy(const {name} *self);")); - } if t.eq { self.line(format!("bool {name}_eq(const {name} *a, const {name} *b);")); } @@ -333,6 +323,15 @@ impl Header<'_> { } if let Some(i) = (0..abi.api.records.len()).find(|&i| &abi.record(i) == name) { let record = &abi.api.records[i]; + if let Some(opaque) = &record.opaque { + self.line(format!( + "{}typedef struct {name} {{\n\t/* Its bytes, which only Rust reads: copy it, but never make or change one. */\n\t{} opaque[{}];\n}} {name};\n", + comment(&record.docs, ""), + c_unit(opaque.unit()), + opaque.units() + )); + return; + } let fields: Vec = record.fields.iter().map(|f| abi.ty(&f.ty)).collect(); for field in &fields { self.value(field); @@ -473,12 +472,9 @@ impl Header<'_> { "\nA handle it lends lives only for that call of `{name}`: free none, nor move out of one." )); } - let ok = matches!(closure.ok, Type::String) - || matches!(closure.ok.base(), Type::Object(_)) - || matches!(&closure.ok, Type::Option(inner) if matches!(**inner, Type::Object(_))); - if ok || closure.err.is_some() { + if closure.ok == Type::String || closure.err.is_some() { docs.push_str(&format!( - "\nWhat it returns is Rust's: a handle, or a buffer {}_buf_copy made.", + "\nA buffer it returns is Rust's, one {}_buf_copy made.", self.abi.prefix )); } @@ -577,6 +573,16 @@ fn has(traits: Traits) -> bool { traits.eq || traits.display || traits.debug || traits.default } +/// The C type of each unit an opaque value's struct holds, of `bytes` bytes. +pub fn c_unit(bytes: u64) -> &'static str { + match bytes { + 1 => "uint8_t", + 2 => "uint16_t", + 4 => "uint32_t", + _ => "uint64_t", + } +} + fn c_int(int: Int) -> &'static str { match int { Int::U8 => "uint8_t", diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index e2a8b1b08..be88ebc4d 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -45,8 +45,8 @@ pub struct Why { /// Types of crates that are no export, by crate and name, each exported as an object where /// some crate builds with it, and named at the facade's root by its role: `url::Url` as /// `Url` and `serde_json::Value`, which a `Serialize` or `Deserialize` parameter takes, as -/// `Json`, each made by parsing its text; and `std::time::Instant` as `Instant`, which -/// only Rust makes, and which C copies and compares. +/// `Json`, each made by parsing its text; and `std::time::Instant` as `Instant`, a value +/// only Rust makes, which C holds as its bytes and compares. const FOREIGN: [(&str, &str, &str); 3] = [ ("url", "Url", "Url"), ("serde_json", "Value", "Json"), @@ -58,8 +58,9 @@ pub const NOT_INFERRED: &str = "instance not inferred"; /// What C can never hold: left out and listed in the census rather than refused, since /// exporting it would need constructors or returns the crate has no use for. -const UNHELD: [&str; 3] = [ +const UNHELD: [&str; 4] = [ "object no export returns", + "value no export returns", "generic type no export instantiates", "type C never holds", ]; @@ -123,6 +124,8 @@ pub struct Report { #[derive(Clone)] enum Decl { Record(usize), + /// A `Copy` type C cannot see inside: a record of its bytes, numbered among records. + Value(usize), Enum(usize), Object(usize), Callback(usize), @@ -788,6 +791,12 @@ impl<'tcx> Collector<'tcx> { .any(|&(k, n, _)| k == krate.as_str() && n == name.as_str()) } + /// Whether the foreign type `did` is a value C holds as its bytes, as a `Copy` one is. + fn foreign_value(&self, did: DefId) -> bool { + let tcx = self.tcx; + !self.opaque(did) && implements(tcx, tcx.type_of(did).instantiate_identity().skip_norm_wip(), did, sym::Copy) + } + /// Each type a signature the export maps names, bounds included, at every choice /// [`Self::instantiate`] takes while inferring: each function's, constant's, and trait /// method's, and each type's `pub` fields and the inherent methods and constants that @@ -1221,6 +1230,7 @@ impl<'tcx> Collector<'tcx> { tuple: false, methods: Vec::new(), traits: Traits::default(), + opaque: None, }); } Decl::Enum(i) => { @@ -1240,11 +1250,30 @@ impl<'tcx> Collector<'tcx> { foreign: false, }); } + Decl::Value(i) => { + *i = api.records.len(); + let ty = tcx.type_of(def_id).instantiate(tcx, site.args).skip_norm_wip(); + let lifetimes = tcx.generics_of(def_id).own_counts().lifetimes; + let opaque = opaque(tcx, ty, lifetimes, false); + api.records.push(Record { + path, + home: home.clone(), + shown, + args: Vec::new(), + span, + on: On::ALL, + docs, + fields: Vec::new(), + tuple: false, + methods: Vec::new(), + traits: Traits::default(), + opaque: Some(opaque), + }); + } Decl::Object(i) => { *i = api.objects.len(); let ty = tcx.type_of(def_id).instantiate(tcx, site.args).skip_norm_wip(); let sync = implements(tcx, ty, def_id, sym::Sync); - let copy = implements(tcx, ty, def_id, sym::Copy); let lifetimes = tcx.generics_of(def_id).own_counts().lifetimes; api.objects.push(Object { path, @@ -1255,7 +1284,6 @@ impl<'tcx> Collector<'tcx> { on: On::ALL, docs, sync, - copy, shared: false, lifetimes, methods: Vec::new(), @@ -1281,11 +1309,35 @@ impl<'tcx> Collector<'tcx> { } // After every declared object, so `prune` can drop each that no export names. Until // here, each maps only to decide what does, never to an index kept. + // A foreign value goes after every declared record, for the same reason. for n in 0..self.foreign.len() { - let index = self.api.objects.len(); - self.foreign[n].1 = index; - let object = self.foreign_object(self.foreign[n].0, index); - self.api.objects.push(object); + let def_id = self.foreign[n].0; + let object = self.foreign_object(def_id, self.api.objects.len()); + match self.foreign_value(def_id) { + true => { + self.foreign[n].1 = self.api.records.len(); + let ty = tcx.type_of(def_id).instantiate_identity().skip_norm_wip(); + let opaque = opaque(tcx, ty, 0, true); + self.api.records.push(Record { + path: object.path, + home: object.home, + shown: object.shown, + args: object.args, + span: object.span, + on: object.on, + docs: object.docs, + fields: Vec::new(), + tuple: false, + methods: object.methods, + traits: object.traits, + opaque: Some(opaque), + }); + } + false => { + self.foreign[n].1 = self.api.objects.len(); + self.api.objects.push(object); + } + } } // After every declared enum, so `prune` can drop it if no export names it. if let Some(level) = &mut self.level { @@ -1353,7 +1405,6 @@ impl<'tcx> Collector<'tcx> { tcx.def_path_str(def_id) ), sync: true, - copy: false, shared: true, lifetimes: 0, methods: Vec::new(), @@ -1474,7 +1525,6 @@ impl<'tcx> Collector<'tcx> { on: On::ALL, docs: docs.to_string(), sync: true, - copy: implements(tcx, self_ty, def_id, sym::Copy), shared: traits.clone && self.clone_shares(self_ty, def_id), lifetimes: 0, methods, @@ -1560,15 +1610,22 @@ impl<'tcx> Collector<'tcx> { } } - /// An object, if the type can move across threads. Shared borrows of one that is not - /// `Sync` are exclusive. + /// An object, if the type can move across threads, or if it is also `Copy`, a value C + /// holds as its bytes. Shared borrows of an object that is not `Sync` are exclusive. fn object(&self, site: &Site<'tcx>) -> Decl { + let (tcx, ty, def_id) = (self.tcx, self.site_ty(site), site.key.def_id); + let copy = implements(tcx, ty, def_id, sym::Copy); if self.orphans.contains(&site.key) { - return Decl::Refused(why("object no export returns", "")); + let construct = match copy { + true => "value no export returns", + false => "object no export returns", + }; + return Decl::Refused(why(construct, "")); } - match implements(self.tcx, self.site_ty(site), site.key.def_id, sym::Send) { - true => Decl::Object(0), - false => Decl::Refused(why("object not Send", "")), + match (implements(tcx, ty, def_id, sym::Send), copy) { + (true, true) => Decl::Value(0), + (true, false) => Decl::Object(0), + (false, _) => Decl::Refused(why("object not Send", "")), } } @@ -1587,7 +1644,8 @@ impl<'tcx> Collector<'tcx> { defaults.push(ty); } } - let mut held = vec![false; api.objects.len()]; + // Each object and opaque value C can hold, which only Rust makes. + let mut held: std::collections::HashSet = std::collections::HashSet::new(); loop { let mut reached = Vec::new(); for ty in &defaults { @@ -1611,18 +1669,17 @@ impl<'tcx> Collector<'tcx> { } } } - reached.retain(|&i| !held[i]); + reached.retain(|ty| !held.contains(ty)); if reached.is_empty() { break; } - for i in reached { - held[i] = true; - } + held.extend(reached); } self.decls .iter() .filter_map(|(&key, decl)| match decl { - Decl::Object(i) if !held[*i] => Some(key), + Decl::Object(i) if !held.contains(&Type::Object(*i)) => Some(key), + Decl::Value(i) if !held.contains(&Type::Record(*i)) => Some(key), _ => None, }) .collect() @@ -1819,10 +1876,21 @@ impl<'tcx> Collector<'tcx> { e.methods = methods; e.traits = traits; } + Some(Decl::Value(index)) => { + let (mut methods, traits) = self.methods(&site); + if kind == DefKind::Struct { + methods.splice(0..0, self.accessors(&site, false)); + } + let methods = self.unmutated(methods); + let record = &mut self.api.records[index]; + record.args = args; + record.methods = methods; + record.traits = traits; + } Some(Decl::Object(index)) => { let (mut methods, traits) = self.methods(&site); if kind == DefKind::Struct { - methods.splice(0..0, self.accessors(&site)); + methods.splice(0..0, self.accessors(&site, true)); } let shared = traits.clone && self.clone_shares(self.site_ty(&site), def_id); let object = &mut self.api.objects[index]; @@ -1868,8 +1936,12 @@ impl<'tcx> Collector<'tcx> { } for n in (0..self.foreign.len()).rev() { let gone = self.foreign[n].1; + let value = self.foreign_value(self.foreign[n].0); let api = &self.api; - let object = Type::Object(gone); + let object = match value { + true => Type::Record(gone), + false => Type::Object(gone), + }; let signature = |f: &Function| { (f.params.iter().map(|p| &p.ty)) .chain([&f.ok]) @@ -1880,20 +1952,27 @@ impl<'tcx> Collector<'tcx> { let fields = |fields: &[Field]| fields.iter().any(|f| names(&f.ty, &object)); let args = |args: &[Type]| args.iter().any(|ty| names(ty, &object)); let used = api.functions.iter().any(signature) - || (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.records.iter().enumerate()).any(|(i, r)| { + (!value || i != gone) && (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)| (value || i != gone) && (args(&o.args) || o.methods.iter().any(signature))) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if used { continue; } - self.api.objects.remove(gone); + // A foreign value is among the last records, as an object among the last objects. + match value { + true => drop(self.api.records.remove(gone)), + false => drop(self.api.objects.remove(gone)), + } self.foreign.remove(n); - for later in &mut self.foreign[n..] { - later.1 -= 1; + let values: Vec = (self.foreign[n..].iter()).map(|&(did, _)| self.foreign_value(did)).collect(); + for (later, later_value) in self.foreign[n..].iter_mut().zip(values) { + if later_value == value { + later.1 -= 1; + } } let api = &mut self.api; let functions = (api.functions.iter_mut()) @@ -1907,12 +1986,12 @@ impl<'tcx> Collector<'tcx> { .chain([&mut f.ok]) .chain(&mut f.err) .chain(instance) - .for_each(|ty| renumber(ty, gone)); + .for_each(|ty| renumber(ty, gone, value)); } for r in &mut api.records { (r.args.iter_mut()) .chain(r.fields.iter_mut().map(|f| &mut f.ty)) - .for_each(|ty| renumber(ty, gone)); + .for_each(|ty| renumber(ty, gone, value)); } for e in &mut api.enums { (e.args.iter_mut()) @@ -1921,17 +2000,18 @@ impl<'tcx> Collector<'tcx> { .iter_mut() .flat_map(|v| v.fields.iter_mut().map(|f| &mut f.ty)), ) - .for_each(|ty| renumber(ty, gone)); + .for_each(|ty| renumber(ty, gone, value)); } for o in &mut api.objects { - o.args.iter_mut().for_each(|ty| renumber(ty, gone)); + o.args.iter_mut().for_each(|ty| renumber(ty, gone, value)); } } } - /// A getter and a setter for each `pub` field of a struct exported as an object: the - /// getter returns a copy, and the setter assigns in place, as `x.field = value` does. - fn accessors(&mut self, site: &Site<'tcx>) -> Vec { + /// A getter for each `pub` field of a struct exported as an object or opaque value, and + /// for an object, with `setters`, a setter: the getter returns a copy, and the setter + /// assigns in place, as `x.field = value` does, which C's copy of a value cannot. + fn accessors(&mut self, site: &Site<'tcx>, setters: bool) -> Vec { let tcx = self.tcx; let mut accessors = Vec::new(); let self_ty = self.site_ty(site); @@ -1972,6 +2052,7 @@ impl<'tcx> Collector<'tcx> { // A handle outlives every call, so a reference it holds is `'static`, which // C's borrow for the call is not. let set = match self.ty(ty, Pos::Param) { + _ if !setters => Ok(Type::Unit), Ok(set) if matches!(set, Type::Ref(..)) || lends_inside(&set) => { Err(why("'static borrow", ty.to_string())) } @@ -2015,6 +2096,9 @@ impl<'tcx> Collector<'tcx> { home: Home::default(), }; accessors.push(accessor(Receiver::Ref, Vec::new(), get, false)); + if !setters { + continue; + } let value = Param { name: "value".to_string(), ty: set, @@ -3217,7 +3301,7 @@ impl<'tcx> Collector<'tcx> { true => self.instances.iter().any(|site| site.key == key), false => matches!( self.decls.get(&key), - Some(Decl::Record(_) | Decl::Enum(_) | Decl::Object(_)) + Some(Decl::Record(_) | Decl::Value(_) | Decl::Enum(_) | Decl::Object(_)) ), } }); @@ -3267,7 +3351,7 @@ impl<'tcx> Collector<'tcx> { true => self.instances.iter().any(|site| site.key == key), false => matches!( self.decls.get(&key), - Some(Decl::Record(_) | Decl::Enum(_) | Decl::Object(_)) + Some(Decl::Record(_) | Decl::Value(_) | Decl::Enum(_) | Decl::Object(_)) ), } }); @@ -3342,10 +3426,16 @@ impl<'tcx> Collector<'tcx> { let sig = tcx.fn_sig(def_id).instantiate(tcx, tcx.mk_args(base)).skip_norm_wip(); // An `async fn`'s future, or a returned `impl Trait`, captures it. kept |= sig.skip_binder().output().walk().any(|arg| arg == param.into()); - // A `Copy` object, which C lends as its handle and hands back as a new one. - let env = TypingEnv::post_analysis(tcx, def_id); - let copied = - |ty: Ty<'tcx>, mapped: &Type| matches!(mapped, Type::Object(_)) && tcx.type_is_copy_modulo_regions(env, ty); + // An opaque value, or a reference to one, which C copies. + let copied = |ty: Ty<'tcx>, mapped: &Type| { + let base = match mapped { + Type::Ref(Mutability::Not, inner) => &**inner, + mapped => mapped, + }; + matches!(base, Type::Record(_)) + && matches!(ty.peel_refs().kind(), ty::Adt(adt, args) + if matches!(self.decl(adt.did(), args), Some(Decl::Value(_))) || self.foreign_value(adt.did())) + }; // Each parameter makes its own closure, which one type parameter cannot name twice. if sig .skip_binder() @@ -3373,8 +3463,7 @@ impl<'tcx> Collector<'tcx> { } match self.ty(input, Pos::Param) { // An object is lent as a handle for the call, but an opaque one, whose - // handle holds no `Arc` to lend; so is a `Copy` one taken by value, which C - // copies to keep. + // handle holds no `Arc` to lend; an opaque value is copied, which C keeps. Ok(ty) if foreign_arg(&ty) || matches!(&ty, Type::Ref(_, inner) if matches!(**inner, Type::Object(_))) @@ -3397,7 +3486,7 @@ impl<'tcx> Collector<'tcx> { } _ => (ret, None), }; - // What C hands Rust: a value, a `String`, or a `Copy` object or an `Option` of one. + // What C hands Rust: a scalar, a `String`, or an opaque value or an `Option` of one. let ok = match self.ty(ok, Pos::Return) { Ok(mapped) if foreign_return(&mapped) @@ -4358,8 +4447,9 @@ impl<'tcx> Collector<'tcx> { { Type::SocketAddr(family) } else if let Some(&(_, i)) = (self.foreign.iter()).find(|&&(ty, _)| ty == did && !self.opaque(did)) { - // A value holds only values, and handles it can copy. + // A value holds only values. match pos { + _ if self.foreign_value(did) => Type::Record(i), Pos::Field => return Err(why("object in a value", tcx.def_path_str(did))), _ => Type::Object(i), } @@ -4442,10 +4532,10 @@ impl<'tcx> Collector<'tcx> { None => tcx.def_path_str(did), }; match self.decls.get(&key) { - Some(Decl::Record(i)) => Type::Record(*i), + Some(Decl::Record(i) | Decl::Value(i)) => Type::Record(*i), Some(Decl::Enum(i)) => Type::Enum(*i), - // A value holds only values, and handles it can copy. - Some(Decl::Object(_)) if pos == Pos::Field && !implements(tcx, ty, did, sym::Copy) => { + // A value holds only values. + Some(Decl::Object(_)) if pos == Pos::Field => { return Err(why("object in a value", name)); } Some(Decl::Object(i)) => Type::Object(*i), @@ -4570,9 +4660,10 @@ fn foreign_arg(ty: &Type) -> bool { } /// Whether C can make a `ty` to pass, holding handles only to the objects in `held`. -fn buildable(api: &Api, ty: &Type, held: &[bool]) -> bool { +fn buildable(api: &Api, ty: &Type, held: &std::collections::HashSet) -> bool { match ty { - Type::Object(i) => held[*i], + Type::Object(_) => held.contains(ty), + Type::Record(i) if api.records[*i].opaque.is_some() => held.contains(ty), Type::Ref(_, inner) | Type::Box(inner) => buildable(api, inner, held), Type::Record(i) => api.records[*i].fields.iter().all(|f| buildable(api, &f.ty, held)), Type::Tuple(elements) => elements.iter().all(|ty| buildable(api, ty, held)), @@ -4584,10 +4675,11 @@ fn buildable(api: &Api, ty: &Type, held: &[bool]) -> bool { } } -/// The objects a returned `ty` may hand C a handle to. -fn returns(api: &Api, ty: &Type, objects: &mut Vec) { +/// The objects a returned `ty` may hand C a handle to, and the opaque values it may hand C. +fn returns(api: &Api, ty: &Type, objects: &mut Vec) { match ty { - Type::Object(i) => objects.push(*i), + Type::Object(_) => objects.push(ty.clone()), + Type::Record(i) if api.records[*i].opaque.is_some() => objects.push(ty.clone()), Type::Ref(_, inner) | Type::Box(inner) | Type::Option(inner) | Type::List(_, inner) => { returns(api, inner, objects) } @@ -4612,18 +4704,21 @@ fn serde(tcx: TyCtxt<'_>, tr: DefId) -> bool { ) } -/// Renumber each object after `gone`, which is dropped, in `ty`. -fn renumber(ty: &mut Type, gone: usize) { +/// Renumber each object after `gone`, which is dropped, in `ty`, or with `value`, each record. +fn renumber(ty: &mut Type, gone: usize, value: bool) { match ty { - Type::Object(o) if *o > gone => *o -= 1, - Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(_, inner) => renumber(inner, gone), - Type::Tuple(elements) => elements.iter_mut().for_each(|ty| renumber(ty, gone)), + Type::Object(o) if !value && *o > gone => *o -= 1, + Type::Record(r) if value && *r > gone => *r -= 1, + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(_, inner) => { + renumber(inner, gone, value) + } + Type::Tuple(elements) => elements.iter_mut().for_each(|ty| renumber(ty, gone, value)), Type::Closure(closure) => (closure .params .iter_mut() .chain([&mut closure.ok]) .chain(&mut closure.err)) - .for_each(|ty| renumber(ty, gone)), + .for_each(|ty| renumber(ty, gone, value)), _ => {} } } @@ -4832,6 +4927,67 @@ fn marker(tcx: TyCtxt<'_>, tr: DefId) -> bool { .contains(&Some(tr)) } +/// How C holds the `Copy` type `ty`, with `lifetimes` lifetime parameters, as its bytes. +fn opaque<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, lifetimes: usize, foreign: bool) -> crate::ir::Opaque { + let env = TypingEnv::fully_monomorphized(); + let ty = tcx.erase_and_anonymize_regions(ty); + // A type that has a layout on its target, as every exported `Copy` type does. + let layout = |ty: Ty<'tcx>| tcx.layout_of(env.as_query_input(ty)).ok().map(|l| l.layout); + let own = layout(ty).expect("an exported type has a layout"); + let eq = (tcx.get_diagnostic_item(sym::PartialEq)) + .and_then(|tr| (tcx.associated_item_def_ids(tr).iter().copied()).find(|&f| tcx.item_name(f) == sym::eq)); + // Whether every byte of `ty` belongs to an integer, `bool`, or `char` in it, and its + // `PartialEq`, if any, is derived or std's, so equal values have equal bytes. + fn dense<'tcx>(tcx: TyCtxt<'tcx>, env: TypingEnv<'tcx>, eq: Option, ty: Ty<'tcx>) -> bool { + match ty.kind() { + ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char => true, + ty::Pat(base, _) => dense(tcx, env, eq, *base), + ty::Array(element, _) => dense(tcx, env, eq, *element), + ty::Adt(adt, args) if adt.is_struct() => { + let std = matches!(tcx.crate_name(adt.did().krate).as_str(), "core" | "alloc" | "std"); + let derived = || { + let Some(eq) = eq else { return true }; + match ty::Instance::try_resolve(tcx, env, eq, tcx.mk_args(&[ty.into(), ty.into()])) { + Ok(Some(instance)) => { + (tcx.impl_of_assoc(instance.def_id())).is_none_or(|imp| tcx.is_automatically_derived(imp)) + } + _ => true, + } + }; + if !std && !derived() { + return false; + } + let Ok(own) = tcx.layout_of(env.as_query_input(ty)) else { + return false; + }; + let mut covered = 0; + for field in adt.all_fields() { + let Ok(field) = tcx.try_normalize_erasing_regions(env, ty::Unnormalized::new_wip(field.ty(tcx, args).skip_norm_wip())) + else { + return false; + }; + let Ok(layout) = tcx.layout_of(env.as_query_input(field)) else { + return false; + }; + if !dense(tcx, env, eq, field) { + return false; + } + covered += layout.size.bytes(); + } + covered == own.size.bytes() + } + _ => false, + } + } + crate::ir::Opaque { + size: own.size().bytes(), + align: own.align().abi.bytes(), + dense: dense(tcx, env, eq, ty), + lifetimes, + foreign, + } +} + /// Whether `ty`, the type `def_id` at some arguments, implements the std trait `item`. fn implements<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, def_id: DefId, item: rustc_span::Symbol) -> bool { let Some(tr) = tcx.get_diagnostic_item(item) else { diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 2fb058209..3c2eabf01 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -8,7 +8,8 @@ //! member or as anything in the namespace, which it would hide. An object is a class owning //! its handle through a `std::unique_ptr`, so it moves, or where `Clone` makes a value no //! call tells apart but by an `Arc`'s or `Rc`'s count, a `std::shared_ptr` its copies -//! share; a record a struct; an enum an +//! share; a record a struct, and a `Copy` type with private fields a class over its C +//! struct's bytes, which only Rust makes or reads; an enum an //! `enum class`, or for one with data a `std::variant` of a struct per variant, with its //! methods free functions; a closure, a future, or a reader C++ implements a template //! parameter. The runtime they share, [`runtime/rt.hpp`](../runtime/rt.hpp), holds the @@ -164,7 +165,6 @@ impl<'a> Header<'a> { }; format!("{constant}{} *", self.owned(object)) } - Type::Object(i) if abi.api.objects[*i].copy => format!("const {} *", self.owned(inner)), // Moved out of, as a handle taken by value is. _ if moves(abi, inner) => format!("::std::optional<{}>", self.held(inner)), _ => format!("const ::std::optional<{}> &", self.held(inner)), @@ -175,7 +175,6 @@ impl<'a> Header<'a> { crate::ir::Mutability::Mut => format!("{} &", self.owned(inner)), }, Type::Ref(_, inner) | Type::Box(inner) => self.param(inner), - Type::Object(i) if abi.api.objects[*i].copy => format!("const {} &", self.owned(ty)), Type::Record(_) | Type::Array(_) | Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => { format!("const {} &", self.owned(ty)) } @@ -290,8 +289,7 @@ impl<'a> Header<'a> { true => format!("static_cast<::{}>({e})", abi.enumeration(*i)), false => format!("{d}::lower({e})"), }, - // A `Copy` value is copied out, and any other moved out. - Type::Object(i) if abi.api.objects[*i].copy => format!("{d}::access::raw({e})"), + // Moved out. Type::Object(_) => format!("{d}::access::own({e})"), Type::Callback(..) => format!("{d}::lower(::std::move({e}))"), Type::Closure(..) | Type::Future | Type::Reader => { @@ -876,11 +874,27 @@ impl<'a> Header<'a> { while body.last().is_some_and(String::is_empty) { body.pop(); } - self.line(format!( - "{}struct {name} {{\n{}\n}};\n", - docs(&record.docs, ""), - body.join("\n") - )); + match record.opaque { + // Its bytes, which only Rust makes or reads: C++ copies them, but makes + // none, as it has no public constructor. + Some(_) => { + let text = format!( + "{}\n\nIts bytes, which only Rust makes or reads: copy it, but never make one.", + record.docs + ); + self.line(format!( + "{}class {name} {{\npublic:\n{}\n\nprivate:\n\tfriend struct {d}::access;\n\n\texplicit {name}(const ::{c} &c) noexcept : c_(c) {{}}\n\n\t::{c} c_;\n}};\n", + docs(text.trim(), ""), + body.join("\n"), + d = self.d() + )); + } + None => self.line(format!( + "{}struct {name} {{\n{}\n}};\n", + docs(&record.docs, ""), + body.join("\n") + )), + } } Type::Enum(i) => { let e = &abi.api.enums[*i]; @@ -974,6 +988,16 @@ impl<'a> Header<'a> { for (i, record) in api.records.iter().enumerate() { let c = abi.record(i); let name = self.name(&c); + if record.opaque.is_some() { + let d = self.d(); + self.line(format!( + "inline ::{c} lower(const ::{ns}::{name} &v) {{\n\treturn {d}::access::bits(v);\n}}\n" + )); + self.line(format!( + "inline ::{ns}::{name} lift(const ::{c} &c) {{\n\treturn {d}::access::make<::{ns}::{name}>(c);\n}}\n" + )); + continue; + } let lowered: Vec = record .fields .iter() @@ -1125,7 +1149,7 @@ impl<'a> Header<'a> { let (_, ty) = c_params.next().expect("a method has its receiver"); match (ty, free) { // Moved out of, as a handle taken by value is. - (Ty::Handle { name, .. }, _) if receiver == Some(Receiver::Owned) && !copy(abi, name) => { + (Ty::Handle { .. }, _) if receiver == Some(Receiver::Owned) => { args.push("raw_.own()".to_string()) } (Ty::Handle { .. }, _) => args.push("raw_.get()".to_string()), @@ -1257,7 +1281,7 @@ impl<'a> Header<'a> { (Some(Receiver::Ref), _) => " const".to_string(), (Some(Receiver::Mut), _) => String::new(), (Some(Receiver::Owned), _) => match owner { - Some(Ty::Handle { name, .. }) if !copy(abi, name) => " &&".to_string(), + Some(Ty::Handle { .. }) => " &&".to_string(), _ => " const".to_string(), }, }; @@ -1368,7 +1392,6 @@ impl<'a> Header<'a> { Type::Ref(crate::ir::Mutability::Mut, _) => { format!("::{}::borrow<{}>(l{k}_.get())", self.ns, self.owned(object)) } - // A `Copy` object Rust copied for the call, which its class reads in place. _ => format!("::std::as_const(l{k}_.get())"), }); } @@ -1403,18 +1426,13 @@ impl<'a> Header<'a> { } /// The C value a closure returns Rust of the C++ value `e` of type `ty`, which Rust then - /// owns: bytes as a buffer `_buf_copy` made, and a `Copy` object as a new handle to - /// a copy, made without its `Clone`. `e` is named, so it may be read more than once. + /// owns: bytes as a buffer `_buf_copy` made. `e` is named, so it may be read more + /// than once. fn returned(&self, ty: &Type, e: &str) -> String { - let abi = self.abi; let d = self.d(); match ty { ty if Abi::is_buf(ty) => format!("{d}::copy_bytes({e})"), Type::Option(inner) if Abi::is_buf(inner) => format!("({e} ? {d}::copy_bytes(*{e}) : {d}::none())"), - 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)), ty => self.lower(ty, e), } } @@ -1536,18 +1554,13 @@ fn record(abi: &Abi<'_>, c: &str) -> Type { /// taken by value is. fn moves(abi: &Abi<'_>, ty: &Type) -> bool { match ty { - Type::Object(i) => !abi.api.objects[*i].copy, + Type::Object(_) => true, Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moves(abi, inner), Type::Tuple(elements) => elements.iter().any(|e| moves(abi, e)), _ => false, } } -/// Whether the object named `c` in C is `Copy`. -fn copy(abi: &Abi<'_>, c: &str) -> bool { - (0..abi.api.objects.len()).any(|i| abi.api.objects[i].copy && abi.object(i) == c) -} - /// `ty` returned: a borrow is returned as a clone, so owned. fn strip_refs(ty: &Type) -> Type { match ty { diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 78150bdef..0535e13a5 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -660,6 +660,15 @@ impl<'a> Gen<'a> { fields.push((format!("_{n}"), abi.ty(element))); } } else if let Some(i) = (0..abi.api.records.len()).find(|&i| abi.record(i) == name) { + if let Some(opaque) = &abi.api.records[i].opaque { + let code = format!( + "final class {class} extends ffi.Struct {{\n\t@ffi.Array({})\n\texternal ffi.Array $opaque;\n}}\n\n", + opaque.units(), + opaque.unit() * 8 + ); + self.structs.insert(name, code); + return class; + } for field in &abi.api.records[i].fields { fields.push((abi::escape(&field.name), abi.ty(&field.ty))); } @@ -938,6 +947,11 @@ impl<'a> Gen<'a> { let dt = self.dt(ty); let abi = self.abi; let body = match ty { + Type::Record(i) if abi.api.records[*i].opaque.is_some() => { + let units = abi.api.records[*i].opaque.as_ref().map_or(0, |o| o.units()); + let lines: Vec = (0..units).map(|k| format!("c.$opaque[{k}] = v._{k};")).collect(); + lines.join("\n\t") + } Type::Record(i) => { let record = &abi.api.records[*i]; let mut lines = Vec::new(); @@ -1040,6 +1054,12 @@ impl<'a> Gen<'a> { let abi = self.abi; let dt = self.dt(ty); let (param, body) = match ty { + Type::Record(i) if abi.api.records[*i].opaque.is_some() => { + let class = self.struct_class(&Ty::Struct(c.to_string())); + let units = abi.api.records[*i].opaque.as_ref().map_or(0, |o| o.units()); + let words: Vec = (0..units).map(|k| format!("c.$opaque[{k}]")).collect(); + (class, format!("{dt}._({})", words.join(", "))) + } Type::Record(i) => { let class = self.struct_class(&Ty::Struct(c.to_string())); let record = &abi.api.records[*i]; @@ -1420,6 +1440,30 @@ impl<'a> Gen<'a> { let name = self.class(&c); let rust = abi.api.rust(&record.path, &record.args); let ty = Type::Record(i); + if let Some(opaque) = &record.opaque { + // Its bytes, in the units of its C struct, each an `int` of the same bits. + let units: Vec = (0..opaque.units()).map(|k| format!("_{k}")).collect(); + let mut members = Vec::new(); + let mut body = vec![format!( + "\tconst {name}._({});\n\n{}", + units.iter().map(|u| format!("this.{u}")).collect::>().join(", "), + units.iter().map(|u| format!("\tfinal int {u};\n")).collect::() + )]; + self.traits(&ty, &c, record.traits, &mut body, &mut members); + self.methods(&record.methods, &Ty::Struct(c.clone()), &ty, &mut body, &mut members); + self.check_members(&rust, &mut members); + let text = format!( + "{rust}\n\nIts bytes, which only Rust makes or reads: copy it, but never make one.\n\n{}", + record.docs + ); + let _ = write!( + self.types, + "{}final class {name} {{\n{}}}\n\n", + docs(text.trim(), ""), + body.join("\n") + ); + return; + } let fields: Vec<(String, String)> = (record.fields.iter()) .map(|f| (field_name(&f.name), self.dt(&f.ty))) .collect(); @@ -1770,11 +1814,7 @@ impl<'a> Gen<'a> { let mut lends = Vec::new(); for (n, ty) in closure.params.iter().enumerate() { match ty { - // An object, by reference or a `Copy` one by value, lent for the call. - Type::Object(_) => { - lends.push(format!("{}._lent(p{n})", self.dt(ty))); - args.push(format!("l{}", lends.len() - 1)); - } + // An object, by reference, lent for the call. Type::Ref(_, object) if matches!(**object, Type::Object(_)) => { lends.push(format!("{}._lent(p{n})", self.dt(object))); args.push(format!("l{}", lends.len() - 1)); @@ -1799,9 +1839,10 @@ impl<'a> Gen<'a> { Type::Unit => format!("{call};"), ok => { let value = self.returned(ok, "r_"); - let write = match Self::scalar(&abi.ty(ok)) { - true => format!("p{at}.value = {value};"), - false => format!("_n.setBuf(p{at}, {value});"), + let write = match abi.ty(ok) { + c if Self::scalar(&c) => format!("p{at}.value = {value};"), + Ty::Buf => format!("_n.setBuf(p{at}, {value});"), + _ => format!("p{at}.ref = {value};"), }; format!("final r_ = {call};\n\t{write}") } @@ -1822,24 +1863,25 @@ impl<'a> Gen<'a> { } /// The C value of the Dart value `e` of type `ty` a closure returns, which Rust then - /// owns: text as a buffer `buf_copy` made, and a `Copy` object as a new handle to a - /// copy, made without its `Clone`, which Rust copies out and frees. + /// owns: text as a buffer `buf_copy` made, and a value as a struct it writes. fn returned(&mut self, ty: &Type, e: &str) -> String { - let abi = self.abi; match ty { Type::String => format!("_n.copied({e})"), - Type::Object(i) => { - let c = abi.object(*i); - let handle = Ty::Handle { - name: c.clone(), - constant: true, - }; - let clone = self.served_fn(&format!("{c}_copy"), vec![handle.clone()], handle); - format!("_n.handOver({e}, {clone})") - } - Type::Option(inner) if matches!(**inner, Type::Object(_)) => { - let given = self.returned(inner, "v"); - format!("switch ({e}) {{ final v? => {given}, null => ffi.nullptr }}") + Type::Record(_) | Type::Option(_) => { + let c = self.c_name(ty); + let name = format!("_ret_{c}"); + if !self.helpers.contains_key(&name) { + let class = self.struct_class(&Ty::Struct(c.clone())); + let w = self.writer(&c, ty); + let dt = self.dt(ty); + self.helpers.insert( + name.clone(), + format!( + "{class} {name}({dt} v) {{\n\tfinal a_ = _Arena();\n\ttry {{\n\t\tfinal c = ffi.Struct.create<{class}>();\n\t\t{w}(v, c, a_);\n\t\treturn c;\n\t}} finally {{\n\t\ta_.end();\n\t}}\n}}\n\n" + ), + ); + } + format!("{name}({e})") } ty => self.lower(ty, e), } @@ -1927,8 +1969,7 @@ impl<'a> Gen<'a> { 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 { + if receiver == Receiver::Owned { // Qualified, apart from a parameter named `close`. after.push("this.close();".to_string()); } @@ -2307,10 +2348,10 @@ 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. +/// a list, or a tuple, and never through a reference. fn moved(abi: &Abi<'_>, ty: &Type) -> bool { match ty { - Type::Object(i) => !abi.api.objects[*i].copy, + Type::Object(_) => true, Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), _ => false, diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 6be1d20cf..51a6b0d97 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -11,7 +11,8 @@ use std::path::Path; use crate::abi::{self, Abi, CFG, Ty}; use crate::config::Package; use crate::ir::{ - Access, Closure, Collection, Family, Function, Literal, Mutability, On, Receiver, Task, TaskKind, Traits, Type, + Access, Closure, Collection, Family, Function, Literal, Mutability, On, Opaque, Receiver, Task, TaskKind, Traits, + Type, }; const RUNTIME: &str = include_str!("../runtime/rt.rs"); @@ -448,7 +449,11 @@ impl Gen<'_> { Type::Option(inner) => format!("Option<{}>", self.rust(inner)), Type::Ref(Mutability::Not, inner) => format!("&{}", self.rust(inner)), Type::Ref(Mutability::Mut, inner) => format!("&mut {}", self.rust(inner)), - Type::Record(i) => generic(&api.records[*i].path, 0, &api.records[*i].args), + Type::Record(i) => match &api.records[*i].opaque { + Some(opaque) if opaque.foreign => foreign_path(&api.records[*i].path), + Some(opaque) => generic(&api.records[*i].path, opaque.lifetimes, &api.records[*i].args), + None => generic(&api.records[*i].path, 0, &api.records[*i].args), + }, Type::Enum(i) if api.enums[*i].foreign => foreign_path(&api.enums[*i].path), Type::Enum(i) => generic(&api.enums[*i].path, 0, &api.enums[*i].args), Type::Object(i) if api.objects[*i].opaque => "rt::Opaque".to_string(), @@ -513,14 +518,7 @@ impl Gen<'_> { 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::Object(i) => format!("unsafe {{ rt::Object::<{}>::take({e}, w) }}", self.object_path(*i)), 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()"), @@ -542,16 +540,14 @@ impl Gen<'_> { let typed: Vec = (closure.params.iter().enumerate()) .map(|(n, ty)| format!("c{n}: {}", self.rust(ty))) .collect(); - // Each object the closure takes, and how it is lent: a `Copy` one by value too. + // Each object the closure borrows, and how it is lent. let lent = |ty: &Type| match ty { Type::Ref(m, inner) => match (&**inner, m) { - (&Type::Object(i), Mutability::Mut) => Some((i, "lend_mut", "")), - (&Type::Object(i), Mutability::Not) if self.abi.api.objects[i].sync => Some((i, "lend", "")), - (&Type::Object(i), Mutability::Not) => Some((i, "lend_local", "")), + (&Type::Object(i), Mutability::Mut) => Some((i, "lend_mut")), + (&Type::Object(i), Mutability::Not) if self.abi.api.objects[i].sync => Some((i, "lend")), + (&Type::Object(i), Mutability::Not) => Some((i, "lend_local")), _ => None, }, - &Type::Object(i) if self.abi.api.objects[i].sync => Some((i, "lend", "&")), - &Type::Object(i) => Some((i, "lend_local", "&")), _ => None, }; let mut args: Vec = std::iter::once("data.get()".to_string()) @@ -578,9 +574,9 @@ impl Gen<'_> { } let mut call = format!("unsafe {{ f({}) }}", args.join(", ")); for (n, ty) in closure.params.iter().enumerate().rev() { - if let Some((i, lend, by)) = lent(ty) { + if let Some((i, lend)) = lent(ty) { call = format!( - "rt::Object::<{}>::{lend}({by}c{n}, w, |h{n}| {call})", + "rt::Object::<{}>::{lend}(c{n}, w, |h{n}| {call})", self.object_path(i) ); } @@ -613,16 +609,10 @@ impl Gen<'_> { } /// The Rust value a closure C implements returned as the C value `e`, which Rust then - /// owns: a buffer `_buf_copy` made, or a handle, out of which it copies a - /// `Copy` object before freeing it. + /// owns, as a buffer `_buf_copy` made. fn returned(&self, ty: &Type, e: &str) -> String { match ty { Type::String => format!("unsafe {{ {e}.take_string(w) }}"), - Type::Object(i) => format!("unsafe {{ rt::Object::<{}>::returned({e}, w) }}", self.object_path(*i)), - Type::Option(inner) => format!( - "{{ let r = {e}; if r.is_null() {{ None }} else {{ Some({}) }} }}", - self.returned(inner, "r") - ), ty => self.lift(ty, e), } } @@ -871,11 +861,6 @@ impl Gen<'_> { self.traits(&name, &path, object.traits, |e| { format!("unsafe {{ rt::Object::<{path}>::{borrow}({e}, w) }}") }); - if self.abi.copies.contains(&i) { - 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" - )); - } let owner = Ty::Handle { name: name.clone(), constant: false, @@ -952,7 +937,9 @@ impl Gen<'_> { self.lower(ty, &format!("<{path} as ::std::default::Default>::default()")) )); } - if !matches!(self.abi.ty(ty), Ty::Enum(_)) { + // An opaque value is `Copy`, with nothing to free. + let opaque = matches!(ty, Type::Record(i) if self.abi.api.records[*i].opaque.is_some()); + if !matches!(self.abi.ty(ty), Ty::Enum(_)) && !opaque { let free = self.free(ty, "unsafe { this.read() }").unwrap_or_default(); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_free(this: *mut {c}) {{\n\tif this.is_null() {{\n\t\treturn;\n\t}}\n\t{free}\n}}\n" @@ -966,6 +953,10 @@ impl Gen<'_> { let name = self.abi.record(i); let path = self.rust(&Type::Record(i)); let ty = Type::Record(i); + if let Some(opaque) = record.opaque { + self.opaque(i, &opaque); + return; + } let mut fields = String::new(); for (n, field) in record.fields.iter().enumerate() { let _ = writeln!(fields, "\tpub f{n}: {},", rs(&self.abi.ty(&field.ty))); @@ -1033,6 +1024,34 @@ impl Gen<'_> { } } + /// An opaque value: its bytes in a struct of `MaybeUninit` units, as its padding may + /// be, read and written unaligned, as a unit's alignment may be less than its own. + fn opaque(&mut self, i: usize, opaque: &Opaque) { + let record = &self.abi.api.records[i]; + let name = self.abi.record(i); + let path = self.rust(&Type::Record(i)); + let ty = Type::Record(i); + let (unit, units) = (format!("u{}", opaque.unit() * 8), opaque.units()); + self.line(format!( + "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n\tpub opaque: [::std::mem::MaybeUninit<{unit}>; {units}],\n}}\n" + )); + self.item(format!( + "const _: () = assert!(::std::mem::size_of::<{path}>() <= ::std::mem::size_of::<{name}>(), \"{name} is smaller than its Rust type\");\n" + )); + self.item(format!( + "fn lift_{name}(c: &{name}, w: &str) -> {path} {{\n\tlet _ = w;\n\tunsafe {{ c.opaque.as_ptr().cast::<{path}>().read_unaligned() }}\n}}\n" + )); + // Zeroed past its own bytes, so a value with no padding has no uninitialized byte. + self.item(format!( + "fn lower_{name}(v: {path}) -> {name} {{\n\tlet mut c = {name} {{ opaque: [::std::mem::MaybeUninit::zeroed(); {units}] }};\n\tunsafe {{ c.opaque.as_mut_ptr().cast::<{path}>().write_unaligned(v) }};\n\tc\n}}\n" + )); + self.value_traits(&name, &path, &ty, record.traits); + let owner = Ty::Struct(name.clone()); + for method in &record.methods { + self.function(method, Some((&owner, ty.clone()))); + } + } + fn enumeration(&mut self, i: usize) { let e = &self.abi.api.enums[i]; self.on = e.on; @@ -1268,6 +1287,8 @@ impl Gen<'_> { fn view(&self, ty: &Type, e: &str) -> String { match ty { Type::Ref(_, inner) => match &**inner { + // An opaque value is `Copy`, lent as a copy. + Type::Record(_) => self.lower(inner, &format!("*{e}")), Type::Str => format!("rt::Buf::view({e}.as_bytes())"), Type::Path => format!("rt::Buf::view({e}.as_os_str().as_encoded_bytes())"), _ => format!("rt::Buf::view({e})"), diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 75bcc9f94..e0a135761 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -19,7 +19,9 @@ //! at any depth, returns an `error`; a record a struct; a plain enum an integer type and its //! constants; an enum with data an interface each variant's struct implements; a trait an //! interface; a closure a function; a reader an `io.ReadSeeker`; a future a call taking a -//! `context.Context` that blocks until the future is ready; a `Result` an `error`. The +//! `context.Context` that blocks until the future is ready; a `Result` an `error`. A `Copy` +//! type with private fields is a struct over its bytes, which only Rust makes or reads, +//! and a zero one panics where Rust would read it. The //! runtime they share, //! [`runtime/rt.go`](../runtime/rt.go), holds the handles and the bytes Rust returns, and //! each package's copy of [`runtime/ffi_cgo.go`](../runtime/ffi_cgo.go) the arena a call lowers @@ -1484,6 +1486,32 @@ impl<'a> Gen<'a> { let ty = Type::Record(i); let name = self.named(&ty).1; let rust = abi.api.rust(&record.path, &record.args); + if let Some(opaque) = &record.opaque { + let text = format!( + "{name} is `{rust}`, its bytes, which only Rust makes or reads: copy it, but never\nmake or change one. A zero {name} panics where Rust would read it, and == is a compile\nerror, since bytes alike need not be equal: Equal, if it has one, is Rust's.\n\n{}", + record.docs + ); + let _ = write!( + self.types, + "{}type {name} struct {{\n\t_ [0]func()\n\t// Bytes_ is its bytes, for the generated packages: use none of it.\n\tBytes_ [{}]uint{}\n\t// Made_ is whether Rust made it, rather than it being Go's zero value.\n\tMade_ bool\n}}\n\n", + docs(text.trim(), ""), + opaque.units(), + opaque.unit() * 8 + ); + let mut members = vec!["Bytes_".to_string(), "Made_".to_string()]; + let fields = members.clone(); + members.extend(self.methods_of( + &ty, + &c, + &rust, + record.traits, + &record.methods, + &Ty::Struct(c.clone()), + &fields, + )); + self.distinct(&rust, &members); + return; + } let fields: Vec<(String, String)> = (self.record_fields(i).into_iter().zip(&record.fields)) .map(|(name, f)| (name, self.go(&f.ty))) .collect(); @@ -1544,6 +1572,13 @@ impl<'a> Gen<'a> { let c = abi.record(i); let ty = Type::Record(i); let name = self.qualified(&ty); + if let Some(opaque) = &record.opaque { + let (local, bytes) = (self.named(&ty).1, format!("[{}]uint{}", opaque.units(), opaque.unit() * 8)); + return format!( + "func lift_{c}(c C.{c}) {name} {{\n\treturn {name}{{Bytes_: *(*{bytes})(unsafe.Pointer(&c)), Made_: true}}\n}}\n\n\ + func lower_{c}(v {name}) C.{c} {{\n\tif !v.Made_ {{\n\t\tpanic(\"a zero {local}, which only Rust makes\")\n\t}}\n\treturn *(*C.{c})(unsafe.Pointer(&v.Bytes_))\n}}\n\n" + ); + } let names = self.record_fields(i); let lifted: Vec = (record.fields.iter().zip(&names)) .map(|(f, field)| { @@ -1970,8 +2005,7 @@ impl<'a> Gen<'a> { /// The trampoline C calls for a closure of type `ty`, which Rust lends for a call or /// keeps: it lends each object it takes by reference as its type, ended once it returns, - /// owns a clone of each `Copy` object, and hands Rust what the closure returns, writing - /// a `Result` through its outs. + /// and hands Rust what the closure returns, writing a `Result` through its outs. fn trampoline(&mut self, ty: &Type, closure: &Closure) -> String { let go = self.go(ty); // Keyed by the Rust closure, since Go spells a borrowed and a copied object alike. @@ -1999,13 +2033,6 @@ impl<'a> Gen<'a> { let _ = writeln!(body, "\tdefer {l}.Handle_.End()"); args.push(l); } - // A copy is the callee's, which Go keeps as long as it likes: its own copy of - // the value Rust lends for the call, made as Rust copies it. - Type::Object(i) => { - self.convert(ty); - let c = self.abi.object(*i); - args.push(format!("own_{c}(C.{c}_copy({p}))")); - } ty => args.push(self.lift_lent(ty, p)), } } @@ -2057,42 +2084,15 @@ impl<'a> Gen<'a> { } /// The C value Rust takes from the Go value `e` of type `ty` a closure returned, which - /// Rust then owns: a string's copy, or a clone of a `Copy` object's handle. + /// Rust then owns: a string's copy, and a value as it lowers. fn returned(&mut self, ty: &Type, e: &str) -> String { match ty { Type::String => format!("cBufCopy({e})"), - Type::Object(i) => { - self.returns(*i); - format!("returned_{}({e})", self.abi.object(*i)) - } - Type::Option(inner) => { - let Type::Object(i) = **inner else { - unreachable!("a closure returns an Option only of a Copy object, not {inner:?}") - }; - self.returns(i); - format!("returnedOption_{}({e})", self.abi.object(i)) - } + Type::Record(_) | Type::Option(_) => self.lower(ty, e), ty => self.lower_scalar(ty, e), } } - /// The helpers handing Rust a clone of the handle of object `i` a closure returned, or - /// nil for none. - fn returns(&mut self, i: usize) { - let ty = Type::Object(i); - self.convert(&ty); - let c = self.abi.object(i); - let name = self.qualified(&ty); - let local = self.named(&ty).1; - 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 returnedOption_{c}(v *{name}) *C.{c} {{\n\tif v == nil {{\n\t\treturn nil\n\t}}\n\treturn returned_{c}(v)\n}}\n\n" - ) - }); - } - /// 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 { @@ -2219,8 +2219,7 @@ 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 { + if receiver == Receiver::Owned { after.push("self.Close()".to_string()); } } @@ -2574,10 +2573,10 @@ fn poll_error(abi: &Abi<'_>) -> 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. +/// a list, or a tuple, and never through a reference. fn moved(abi: &Abi<'_>, ty: &Type) -> bool { match ty { - Type::Object(i) => !abi.api.objects[*i].copy, + Type::Object(_) => true, Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), _ => false, diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index 05db62e09..d16f69f49 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -101,7 +101,8 @@ pub struct Home { pub outside: usize, } -/// A struct whose fields are all `pub`: a value, copied across. +/// A struct whose fields are all `pub`, or a `Copy` type C cannot see inside: a value, +/// copied across. #[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)] pub struct Record { pub path: Path, @@ -121,6 +122,49 @@ pub struct Record { pub tuple: bool, pub methods: Vec, pub traits: Traits, + /// For a `Copy` type with private fields, which has no `fields`: its bytes, which only + /// Rust reads. + pub opaque: Option, +} + +/// A `Copy` type held as its bytes, which every language copies and none reads. +#[derive(Clone, Copy, PartialEq, Eq, Debug, serde::Serialize, serde::Deserialize)] +pub struct Opaque { + /// Its size and alignment: on several targets, the largest of each, so one binding + /// holds it on all of them. + pub size: u64, + pub align: u64, + /// Whether its bytes are its fields' with no padding between them, each an integer, a + /// `bool`, or a `char`, and each struct's `PartialEq` in it, if any, derived or std's, + /// so two values Rust calls equal have equal bytes: on several targets, on all of them. + pub dense: bool, + /// How many lifetime parameters it has, each `'static` in the export, as an object's. + pub lifetimes: usize, + /// Whether it is a foreign type the facade maps, such as `std::time::Instant`: its path + /// is the type's in its own crate. + pub foreign: bool, +} + +impl Opaque { + /// Whether it fits a 64-bit word, which a language may hold it in with no allocation. + pub fn word(&self) -> bool { + self.size <= 8 + } + + /// The bytes of each unit its C struct holds it in, by its alignment, at most 8. + pub fn unit(&self) -> u64 { + self.align.clamp(1, 8) + } + + /// How many units its C struct holds: at least one, as C has no empty struct. + pub fn units(&self) -> u64 { + self.size.div_ceil(self.unit()).max(1) + } + + /// How many 64-bit words hold its C struct. + pub fn words(&self) -> u64 { + (self.units() * self.unit()).div_ceil(8) + } } #[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)] @@ -188,9 +232,6 @@ pub struct Object { pub docs: String, /// Whether it is `Sync`: if not, `&self` borrows it on one thread at a time. pub sync: bool, - /// Whether it is `Copy`: taken by value, it is copied out of its handle, as Rust - /// copies it, rather than moved out. - pub copy: bool, /// Whether copies of its handle may share one value, since no call tells that apart /// from `Clone` but by an `Arc`'s or `Rc`'s count: it is `Clone` and `Sync`, its clone owns no state that changes /// behind `&` or counts clones, and no export borrows it mutably. @@ -451,11 +492,10 @@ pub enum Type { /// A [`Type::Closure`]: what it takes and returns. #[derive(Clone, PartialEq, Eq, Hash, Debug, serde::Serialize, serde::Deserialize)] pub struct Closure { - /// Each value it takes, which Rust lends for the call: a value, a borrowed object, or - /// a `Copy` object, lent as its handle. + /// Each value it takes, which Rust lends for the call: a value or a borrowed object. pub params: Vec, - /// What it returns, or for a `Result`, its value: a value, a `String`, or a `Copy` - /// object or an `Option` of one, which the foreign side hands Rust. + /// What it returns, or for a `Result`, its value: a value or a `String`, which the + /// foreign side hands Rust. pub ok: Type, /// The error of a `Result` it returns: a `String`. pub err: Option, diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index b2cb436f3..9447d6489 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, Opaque, Param, Receiver, Type}; const RUNTIME: &str = include_str!("../runtime/rt.kt"); pub const GLUE_RUNTIME: &str = include_str!("../runtime/jni.rs"); @@ -677,6 +677,7 @@ impl<'a> Gen<'a> { _ => wrap(e), } } + Ty::Struct(name) if opaque_of(self.abi, c).is_some() => format!("{}({e})", self.class(name)), Ty::Buf => match (decode(ty), matches!(ty.base(), Type::Option(_)), lent) { (Some(decode), true, _) => format!("{e}?.{decode}"), (Some(decode), false, _) => format!("{e}!!.{decode}"), @@ -694,18 +695,17 @@ 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 { + /// callback or closure returns it: a scalar, a string's bytes, an opaque value's word + /// or words, or any other value encoded. + 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)); - match ty { - Type::Option(_) => format!("{e}?.handle?.hold {{ h -> {clone}(h) }} ?: 0L"), - _ => format!("{e}.handle.hold {{ h -> {clone}(h) }}"), - } + Ty::Struct(_) if let Some(opaque) = opaque_of(self.abi, c) => format!("{e}.{}", holder(&opaque)), + Ty::Struct(_) => { + let w = self.fresh("w"); + let write = self.write_value(ty, &w, e); + format!("Native.Writer().use {{ {w} -> {write}; {w}.finish() }}") } _ => e.to_string(), } @@ -719,7 +719,7 @@ impl<'a> Gen<'a> { let mut arg = Arg::default(); let p = format!("p{n}"); *n += 1; - let (jni, native) = jni_type(c, ty); + let (jni, native) = jni_type(self.abi, c, ty); arg.jni.push(jni); arg.native.push(native.to_string()); match c { @@ -776,6 +776,15 @@ impl<'a> Gen<'a> { "crate::{name} {{ vtable: &VTABLE_{name}, data: rt::global(env, {p}) }}" )); } + // An opaque value crosses as the word or words Kotlin holds it in. + Ty::Struct(_) | Ty::In(_) if let Some(opaque) = opaque_of(abi, c) => { + arg.exprs.push(format!("{e}.{}", holder(&opaque))); + arg.rust_pre.push(format!("let a{p} = {};", from_kotlin(&opaque, &p))); + arg.rust_args.push(match c { + Ty::In(_) => format!("&a{p}"), + _ => format!("a{p}"), + }); + } 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. @@ -995,16 +1004,25 @@ impl<'a> Gen<'a> { let class = self.class(c); let bare = self.bare(c).to_string(); let object = matches!(ty, Type::Object(_)); - // The receiver as the glue takes it: a handle, held for the call, or encoded. - let (jni_self, native_self, open, arg, rust_self) = match object { - true => ( + let opaque = match ty { + Type::Record(i) => self.abi.api.records[*i].opaque, + _ => None, + }; + // The receiver as the glue takes it: a handle, held for the call, an opaque value's + // word or words, or encoded. + let (jni_self, native_self, open, arg, rust_self) = match (object, opaque) { + (true, _) => ( "jlong", "Long", "handle.hold { h ->".to_string(), "h".to_string(), "p0 as usize as _".to_string(), ), - false => { + (false, Some(opaque)) => { + let (jni, native) = jni_type(self.abi, &Ty::Struct(c.to_string()), ty); + (jni, native, "run {".to_string(), holder(&opaque).to_string(), format!("&{}", from_kotlin(&opaque, "p0"))) + } + (false, None) => { let write = self.write_value(ty, "w", "this"); ( "jbyteArray", @@ -1036,16 +1054,6 @@ impl<'a> Gen<'a> { "/** Rust's `Debug` form. */\n\tfun debug(): String", )); } - if let Type::Object(i) = ty - && self.abi.copies.contains(i) - { - self.native( - &format!("{bare}_copy"), - &[("jlong", "Long")], - ("jlong", "Long"), - &format!("crate::{c}_copy(p0 as usize as _) as usize as jlong"), - ); - } if object && t.clone { self.native( &format!("{bare}_clone"), @@ -1057,6 +1065,22 @@ impl<'a> Gen<'a> { "\t/** A new handle to a clone. */\n\tfun clone(): {class} = handle.hold {{ h -> {class}(Native.{bare}_clone(h)) }}\n" )); } + // An opaque value Kotlin holds in a word compares by it, as a value class does. + if let Some(opaque) = opaque.filter(|opaque| t.eq && !word(opaque)) { + self.native( + &format!("{bare}_eq"), + &[("jlongArray", "LongArray"), ("jlongArray", "LongArray")], + ("jboolean", "Boolean"), + &format!( + "crate::{c}_eq(&{}, &{})", + from_kotlin(&opaque, "p0"), + from_kotlin(&opaque, "p1") + ), + ); + body.push(format!( + "\toverride fun equals(other: Any?): Boolean = other is {class} && Native.{bare}_eq(words, other.words)\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): Int = 0\n" + )); + } if object && t.eq { self.native( &format!("{bare}_eq"), @@ -1113,6 +1137,9 @@ impl<'a> Gen<'a> { let mut statics = Vec::new(); self.traits(&ty, &c, record.traits, &mut body, &mut statics); let mut members: Vec = fields.iter().map(|(f, _)| format!("val {f}")).collect(); + if let Some(opaque) = record.opaque { + members.push(format!("val {}", holder(&opaque))); + } let owner = Ty::Struct(c.clone()); for method in &record.methods { let place = match method.receiver { @@ -1143,6 +1170,10 @@ impl<'a> Gen<'a> { true => String::new(), false => format!(" {{\n{}}}", body.join("\n")), }; + if let Some(opaque) = record.opaque { + self.opaque(&c, &name, &opaque, &docs, &body); + return; + } match fields.is_empty() { true => { let _ = write!( @@ -1205,6 +1236,34 @@ impl<'a> Gen<'a> { ); } + /// An opaque value's class, `name` in Kotlin and `c` in C, and its codec: a value class + /// over the `Long` it fits in with no padding, or else a class over its words, which + /// compares through Rust's `PartialEq`. Only Rust makes one. + fn opaque(&mut self, c: &str, name: &str, opaque: &Opaque, docs: &str, body: &str) { + let words = opaque.words(); + let (head, write, read) = match word(opaque) { + true => ( + format!("@JvmInline\nvalue class {name} internal constructor(internal val bits: Long)"), + "\tw.i64(v.bits)\n".to_string(), + format!("{name}(r.i64())"), + ), + false => ( + format!("class {name} internal constructor(internal val words: LongArray)"), + "\tfor (word in v.words) w.i64(word)\n".to_string(), + format!("{name}(LongArray({words}) {{ r.i64() }})"), + ), + }; + let _ = write!(self.types, "{docs}{head}{body}\n\n"); + let _ = write!( + self.top, + "internal fun w_{c}(w: Native.Writer, v: {name}) {{\n{write}}}\n\ninternal fun r_{c}(r: Native.Reader): {name} = {read}\n\n" + ); + let _ = write!( + self.glue, + "impl Wire for crate::{c} {{\n\tfn read(r: &mut Reader<'_>) -> Self {{\n\t\trt::read_words::<_, {words}>(r)\n\t}}\n\n\tfn write(self, w: &mut Writer) {{\n\t\trt::write_words::<_, {words}>(w, self);\n\t}}\n}}\n\n" + ); + } + fn enumeration(&mut self, i: usize) { let abi = self.abi; let e = &abi.api.enums[i]; @@ -1505,7 +1564,7 @@ impl<'a> Gen<'a> { for (k, (ty, c)) in lent.iter().zip(c_params).enumerate() { let rust_ty = rust_c(self.abi, c); rust_params.push(format!("p{k}: {rust_ty}")); - let (jni, native) = jni_type(c, ty); + let (jni, native) = jni_type(self.abi, c, ty); sig.push_str(descriptor(jni)); kt_params.push(format!("p{k}: {native}")); let (field, value) = match c { @@ -1517,6 +1576,10 @@ impl<'a> Gen<'a> { Ty::Enum(_) => ("i", format!("p{k}")), Ty::Handle { .. } => ("j", format!("p{k} as usize as jlong")), Ty::Buf => ("l", format!("unsafe {{ rt::lent(env, p{k}) }}")), + Ty::Struct(_) if let Some(opaque) = opaque_of(self.abi, c) => match word(&opaque) { + true => ("j", to_kotlin(&opaque, &format!("p{k}"))), + false => ("l", to_kotlin(&opaque, &format!("p{k}"))), + }, _ => ( "l", format!("{{ let mut w = rt::Writer::default(); w.put(p{k}); w.array(env) }}"), @@ -1546,7 +1609,7 @@ impl<'a> Gen<'a> { (Some(_), [Ty::Out(ok), _]) => (**ok).clone(), (Some(_), _) => Ty::Void, }; - let (ret_jni, ret_native) = jni_type(&c_ok, ret); + let (ret_jni, ret_native) = jni_type(self.abi, &c_ok, ret); sig.push_str(descriptor(ret_jni)); let call_kind = call_method(ret_jni); let rust_ret = rust_c(self.abi, c_ret); @@ -1558,8 +1621,9 @@ impl<'a> Gen<'a> { Ty::Char => "ret as u32".to_string(), // A buffer Rust frees, as `buf_copy` makes one. Ty::Buf => "crate::rt::Buf::vec(rt::bytes(env, ret).unwrap_or_default())".to_string(), - // A handle Kotlin cloned for Rust to free. - Ty::Handle { .. } => "ret as usize as *const c_void".to_string(), + Ty::Struct(_) if let Some(opaque) = opaque_of(self.abi, &c_ok) => from_kotlin(&opaque, "ret"), + // Encoded, as an option of an opaque value. + Ty::Struct(_) => "rt::Reader::new(&rt::bytes(env, ret).unwrap_or_default()).get()".to_string(), _ => unreachable!("a callback or closure returns no {c_ok:?}"), }; let back = match thrown { @@ -1777,8 +1841,7 @@ 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 { + if receiver == Receiver::Owned { arg.after.push("this.close()".to_string()); } arg @@ -1973,7 +2036,7 @@ impl<'a> Gen<'a> { // Bytes cross copied or as their owner. Ty::Buf if !string(&ok) => ("jobject", "Any?"), Ty::Waker => ("jlong", "Long"), - _ => jni_type(&c_ret, &ok), + _ => jni_type(abi, &c_ret, &ok), }; let params: Vec<(&str, &str)> = native_params.iter().map(|(j, k)| (*j, k.as_str())).collect(); let ret_expr = match &c_ret { @@ -1987,6 +2050,7 @@ impl<'a> Gen<'a> { Ty::Waker => "rt::returned(ret)".to_string(), Ty::Buf if string(&ok) => "rt::owned(env, ret)".to_string(), Ty::Buf => "rt::hand(env, ret)".to_string(), + Ty::Struct(_) if let Some(opaque) = opaque_of(abi, &c_ret) => to_kotlin(&opaque, "ret"), _ => format!( "{{ let mut w = rt::Writer::default(); {}; w.array(env) }}", put(&ok, "ret") @@ -2234,10 +2298,10 @@ 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. +/// and never through a reference. fn moved(abi: &Abi<'_>, ty: &Type) -> bool { match ty { - Type::Object(i) => !abi.api.objects[*i].copy, + Type::Object(_) => true, Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), _ => false, @@ -2266,8 +2330,12 @@ fn abi_bound(abi: &Abi<'_>, int: Int) -> String { } /// The JNI type and the Kotlin native type of the C type `c`, of IR type `ty`. -fn jni_type(c: &Ty, ty: &Type) -> (&'static str, &'static str) { +fn jni_type(abi: &Abi<'_>, c: &Ty, ty: &Type) -> (&'static str, &'static str) { match c { + Ty::Struct(_) | Ty::In(_) if let Some(opaque) = opaque_of(abi, c) => match word(&opaque) { + true => ("jlong", "Long"), + false => ("jlongArray", "LongArray"), + }, Ty::Void => ("()", "Unit"), Ty::Bool => ("jboolean", "Boolean"), Ty::Int(int) => match jni_int(*int) { @@ -2291,6 +2359,51 @@ fn jni_type(c: &Ty, ty: &Type) -> (&'static str, &'static str) { } } +/// The opaque value the C type `c` is, by value or as a receiver. +fn opaque_of(abi: &Abi<'_>, c: &Ty) -> Option { + let name = match c { + Ty::Struct(name) => name, + Ty::In(inner) => match &**inner { + Ty::Struct(name) => name, + _ => return None, + }, + _ => return None, + }; + (0..abi.api.records.len()) + .find(|&i| &abi.record(i) == name) + .and_then(|i| abi.api.records[i].opaque) +} + +/// The property of an opaque value's class holding it. +fn holder(opaque: &Opaque) -> &'static str { + match word(opaque) { + true => "bits", + false => "words", + } +} + +/// Whether Kotlin holds `opaque` in a `Long`, with no allocation: it fits one and is +/// dense, so values Rust calls equal have equal bits, as the value class compares them. +fn word(opaque: &Opaque) -> bool { + opaque.word() && opaque.dense +} + +/// The Rust expression taking the opaque value Kotlin passed as `p`, as its C struct. +fn from_kotlin(opaque: &Opaque, p: &str) -> String { + match word(opaque) { + true => format!("rt::from_word({p})"), + false => format!("unsafe {{ rt::from_words::<_, {}>(env, {p}) }}", opaque.words()), + } +} + +/// The JNI value Kotlin takes the opaque value's C struct `e` as. +fn to_kotlin(opaque: &Opaque, e: &str) -> String { + match word(opaque) { + true => format!("rt::to_word({e})"), + false => format!("rt::to_words::<_, {}>(env, {e})", opaque.words()), + } +} + /// The JVM descriptor of a JNI type. fn descriptor(jni: &str) -> &'static str { match jni { @@ -2303,6 +2416,7 @@ fn descriptor(jni: &str) -> &'static str { "jfloat" => "F", "jdouble" => "D", "jbyteArray" => "[B", + "jlongArray" => "[J", _ => "Ljava/lang/Object;", } } diff --git a/ffi/src/merge.rs b/ffi/src/merge.rs index dbda04f10..605a4c90c 100644 --- a/ffi/src/merge.rs +++ b/ffi/src/merge.rs @@ -199,6 +199,12 @@ fn bare_record(r: &Record) -> Record { r.on = On::ALL; r.methods.clear(); r.fields.iter_mut().for_each(|f| f.docs.clear()); + // Merged apart: one binding holds the largest. + if let Some(opaque) = &mut r.opaque { + opaque.size = 0; + opaque.align = 0; + opaque.dense = true; + } }) } @@ -355,6 +361,11 @@ impl Merged { self.differ(what, (ours.on, ours.span), (record.on, record.span)); } self.api.records[at].on.0 |= record.on.0; + if let (Some(ours), Some(theirs)) = (&mut self.api.records[at].opaque, record.opaque) { + ours.size = ours.size.max(theirs.size); + ours.align = ours.align.max(theirs.align); + ours.dense &= theirs.dense; + } let methods = std::mem::take(&mut self.api.records[at].methods); self.api.records[at].methods = self.functions(methods, record.methods); } diff --git a/ffi/src/python.rs b/ffi/src/python.rs index 94419d55e..076d0b7b9 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -501,7 +501,7 @@ impl<'a> Gen<'a> { Type::List(..) => format!("r_{}({o}, s, {m})", 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(..)) { + let m = match matches!(**inner, Type::Ref(..)) { true => "false", false => m, }; @@ -532,13 +532,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, {m})", self.t(&abi.object(*i))), Type::Callback(i, _) => { let c = abi.callback(*i); format!( @@ -805,6 +799,7 @@ impl<'a> Gen<'a> { let bare = self.bare(c).to_string(); let class = self.class(c); let object = matches!(ty, Type::Object(_)); + let opaque = matches!(ty, Type::Record(i) if self.abi.api.records[*i].opaque.is_some()); // The receiver as the glue reads it: a handle, held for the call, or a value. let this = match object { true => self.lift( @@ -851,17 +846,26 @@ impl<'a> Gen<'a> { "\tdef clone(self) -> {class}:\n\t\t\"\"\"{docs}\"\"\"\n\t\t...\n" )); } - if object && t.eq { - let other = self.lift( - &Type::Ref(crate::ir::Mutability::Not, Box::new(ty.clone())), - "a[1]", - "false", - ); + // An opaque value compares through Rust, as an object does; a record's fields + // compare as a dataclass's. + if (object || opaque) && t.eq { + let other = match object { + true => self.lift( + &Type::Ref(crate::ir::Mutability::Not, Box::new(ty.clone())), + "a[1]", + "false", + ), + false => self.lift(ty, "a[1]", "false"), + }; + let (a, b) = match object { + true => ("this", "other"), + false => ("&this", "&other"), + }; self.native( &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\tlet ret = rt::call(|| crate::{c}_eq({a}, {b}));\n\t\ts.done();\n\t\trt::bool_out(ret)" ), ); // Python makes a class that defines `__eq__` alone unhashable, as `Eq` without `Hash` is. @@ -950,10 +954,31 @@ impl<'a> Gen<'a> { let t = self.register(&c, name.clone()); let ty = Type::Record(i); let mut body = Body::default(); + if record.opaque.is_some() { + body.python.push("\t__slots__ = (\"_bytes\",)\n".to_string()); + } self.traits(&ty, &c, record.traits, &mut body); let owner = Ty::Struct(c.clone()); let data: Vec = record.fields.iter().map(|f| field_name(&f.name)).collect(); self.methods(&record.methods, (&ty, &owner, &rust), &data, &[], &mut body); + if record.opaque.is_some() { + let text = format!( + "{rust}\n\n{}\n\nIts bytes, which only Rust makes or reads: copy it, but never make one.", + record.docs + ); + let _ = write!( + self.py, + "class {name}:\n{}{}\n\n", + docstring(text.trim(), "\t"), + body.members().join("\n") + ); + let key = self.key("_bytes"); + let _ = write!( + self.glue, + "fn r_{c}(o: *mut PyObject, s: &mut rt::Scope) -> R {{\n\trt::opaque_in(rt::field(o, {t}, {key}, s)?)\n}}\n\nfn w_{c}(v: crate::{c}) -> R {{\n\trt::record({t}, vec![({key}, rt::opaque_out(v))])\n}}\n\n" + ); + return; + } let fields: String = record .fields .iter() @@ -1306,21 +1331,10 @@ impl<'a> Gen<'a> { /// The glue's expression, an `R`, of the Python object `o` a closure returns as the /// C value of `ty` that Rust then owns: a string copied, as `_buf_copy` makes - /// one, and a new handle to a copy of an object, made without its `Clone`, so the Python - /// object keeps its own. + /// one. fn given(&mut self, ty: &Type, o: &str) -> String { - let abi = self.abi; match ty { Type::String => format!("rt::str_given({o})"), - Type::Object(i) => format!( - "rt::hold({o}, {}, s, false).map(|h| crate::{}_copy(h))", - self.t(&abi.object(*i)), - abi.object(*i) - ), - Type::Option(inner) if matches!(**inner, Type::Object(_)) => { - let some = self.given(inner, o); - format!("(if rt::is_none({o}) {{ Ok(std::ptr::null()) }} else {{ {some} }})") - } _ => self.lift(ty, o, "false"), } } diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index aee38ffb1..50893e4be 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -8,9 +8,10 @@ //! handle, freed in `deinit`, so Swift's compiler checks each move and borrow Rust makes: //! `self` is `consuming`, `&mut self` `mutating`, a parameter `consuming`, `borrowing`, or //! `inout`, a tuple holding one a struct of its own, and an array of them swift-collections' -//! `UniqueArray`. An object that is `Copy` or thrown as an error is a final class instead, -//! since `Swift.Error` requires a copyable type, whose handle a call holds at run time. A -//! record is a struct; an enum a Swift enum, with +//! `UniqueArray`. An object thrown as an error is a final class instead, since `Swift.Error` +//! requires a copyable type, whose handle a call holds at run time. A record is a struct, +//! and a `Copy` type with private fields a struct over the bytes of its C struct, which +//! only Rust makes or reads; an enum a Swift enum, with //! its data as associated values; a trait a protocol; a closure a function type; a future //! an `async` function; an error a thrown `Swift.Error`. The runtime they share, //! [`runtime/rt.swift`](../runtime/rt.swift), holds the classes' handles, the arena a call @@ -206,7 +207,7 @@ struct Gen<'a> { thrown: BTreeMap, /// The exported types thrown as errors, which conform to `Swift.Error`. conforms: BTreeSet, - /// The objects that are noncopyable structs: each but those `Copy` or thrown. + /// The objects that are noncopyable structs: each but those thrown. structs: BTreeSet, fresh: usize, /// The targets of the item being written, whose calls check theirs. @@ -292,11 +293,11 @@ impl<'a> Gen<'a> { } /// Whether passing `ty` moves the value of a class's handle out, so the call closes it: - /// by value, boxed, or in an `Option`, a list, or a tuple, unless `Copy`, and never - /// through a reference. A struct needs no close: Swift's compiler ends it. + /// by value, boxed, or in an `Option`, a list, or a tuple, and never through a + /// reference. A struct needs no close: Swift's compiler ends it. fn moved(&self, ty: &Type) -> bool { match ty { - Type::Object(i) => !self.abi.api.objects[*i].copy && !self.structs.contains(i), + Type::Object(i) => !self.structs.contains(i), Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => self.moved(inner), Type::Tuple(elements) => elements.iter().any(|ty| self.moved(ty)), _ => false, @@ -850,6 +851,9 @@ impl<'a> Gen<'a> { let name = self.class(&c); let rust = abi.api.rust(&record.path, &record.args); let ty = Type::Record(i); + if record.opaque.is_some() { + return self.opaque(i); + } let fields: Vec<(String, String)> = (record.fields.iter()) .map(|f| (field_name(&f.name), self.sw(&f.ty))) .collect(); @@ -909,6 +913,35 @@ impl<'a> Gen<'a> { ); } + /// A `Copy` type with private fields: a struct over the bytes of its C struct, which + /// only Rust makes or reads. Swift copies it as it copies the bytes. + fn opaque(&mut self, i: usize) { + let abi = self.abi; + let record = &abi.api.records[i]; + let c = abi.record(i); + let name = self.class(&c); + let rust = abi.api.rust(&record.path, &record.args); + let ty = Type::Record(i); + let mut members = Vec::new(); + let mut body = vec![format!( + "\t/// Its bytes, which only Rust reads.\n\tlet c_: {c}\n\n\tprivate init(c_: {c}) {{\n\t\tself.c_ = c_\n\t}}\n" + )]; + let mut protocols = vec!["Sendable"]; + self.traits(&ty, &c, record.traits, &mut body, &mut members, &mut protocols); + self.methods(&record.methods, &Ty::Struct(c.clone()), &ty, &mut body, &mut members); + self.distinct(&rust, &members); + body.push(format!("\tstatic func _lift(_ c: {c}) -> {name} {{\n\t\t{name}(c_: c)\n\t}}\n")); + body.push(format!("\tfunc _lower() -> {c} {{\n\t\tc_\n\t}}\n")); + let text = format!("{rust}\n\n{}", record.docs); + let _ = write!( + self.types, + "{}public struct {name}: {} {{\n{}}}\n\n", + docs(text.trim(), ""), + protocols.join(", "), + body.join("\n") + ); + } + fn enumeration(&mut self, i: usize) { let abi = self.abi; let e = &abi.api.enums[i]; @@ -1275,8 +1308,6 @@ impl<'a> Gen<'a> { body.push(format!("defer {{ {l}.handle.end() }}")); 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))), ty => args.push(self.lift_lent(ty, p)), } } @@ -1322,26 +1353,14 @@ impl<'a> Gen<'a> { format!("{{ {} in\n\t{}\n}}", params.join(", "), body.join("\n\t")) } - /// The C value of `e`, of type `ty`, which a closure returns for Rust to own: a scalar - /// as it is, text in a buffer `buf_copy` made, and a copy of an object's handle. + /// The C value of `e`, of type `ty`, which a closure returns for Rust to own: a value + /// as it lowers, and text in a buffer `buf_copy` made. fn returned(&mut self, ty: &Type, e: &str) -> String { - let abi = self.abi; match ty { Type::Str | Type::String => format!("Native.owned({e})"), - // 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) - ), - 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) - ), - _ => self.lower(ty, e), - }, + Type::Option(inner) if matches!(**inner, Type::Str | Type::String) => { + format!("({e}.map {{ Native.owned($0) }} ?? Native.none)") + } ty => self.lower(ty, e), } } @@ -1447,8 +1466,7 @@ impl<'a> Gen<'a> { Ty::Handle { .. } => { arg.open = Some((format!("{tr}self.handle.with {{ 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 { + if receiver == Receiver::Owned { arg.after.push("self.handle.close()".to_string()); } } @@ -1831,7 +1849,7 @@ fn lent_option(structs: &BTreeSet, ty: &Type) -> bool { matches!(ty, Type::Option(inner) if matches!(&**inner, Type::Ref(Mutability::Not, object) if unique(structs, object))) } -/// The objects Swift holds as noncopyable structs: each but those `Copy`, and those thrown +/// The objects Swift holds as noncopyable structs: each but those thrown /// as errors, which `Swift.Error` requires be copyable. fn structs(abi: &Abi<'_>) -> BTreeSet { let api = abi.api; @@ -1845,7 +1863,7 @@ fn structs(abi: &Abi<'_>) -> BTreeSet { }) .collect(); (0..api.objects.len()) - .filter(|i| !api.objects[*i].copy && !thrown.contains(i)) + .filter(|i| !thrown.contains(i)) .collect() } diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 3354b6ff0..dd3c30a4b 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -134,15 +134,21 @@ static api_buf made_times(void *data, uint32_t n) { return api_buf_copy(str(text)); } -/* 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) { +/* reissue's closures: a new ticket, and a copy of one C keeps. */ +static api_Ticket reissue_next(void *data, api_Ticket ticket) { (void)data; - return api_Ticket_new(api_Ticket_number(ticket) + 1); + return api_Ticket_new(api_Ticket_number(&ticket) + 1); } -static api_Ticket *reissue_kept(void *data, const api_Ticket *ticket) { +static api_Ticket reissue_kept(void *data, api_Ticket ticket) { (void)ticket; - return api_Ticket_copy(data); + return *(const api_Ticket *)data; +} + +/* shifted's closure: the moment 500 ticks later. */ +static api_Moment moment_after(void *data, api_Moment moment) { + (void)data; + return api_Moment_after(moment, 500); } static uint32_t spawned_task(void *data) { @@ -165,15 +171,15 @@ static api_buf made_view(void *data, uint32_t n) { * waiter, which it wakes, then stops. Rust frees it once it drops it. */ typedef struct { int calls; - const api_Instant *start; + api_Instant start; bool freed; } driver; -static bool drive_poll(void *data, const api_Instant *now, const api_Waiter *waiter, api_Instant **out, api_buf *err) { +static bool drive_poll(void *data, api_Instant now, const api_Waiter *waiter, api_option_Instant *out, api_buf *err) { driver *d = data; d->calls++; if (d->calls == 1) { - *out = api_later(now, (api_duration){.secs = 0, .nanos = 5000000}); + *out = (api_option_Instant){.some = true, .value = api_later(now, (api_duration){.secs = 0, .nanos = 5000000})}; return true; } if (d->calls == 2) { @@ -182,7 +188,7 @@ static bool drive_poll(void *data, const api_Instant *now, const api_Waiter *wai api_waker returned = api_Waiter_waker(waiter); returned.wake(returned.data); returned.drop(returned.data); - *out = NULL; + *out = (api_option_Instant){0}; return true; } char text[32]; @@ -192,12 +198,12 @@ static bool drive_poll(void *data, const api_Instant *now, const api_Waiter *wai } /* A drive that only ever waits. */ -static bool drive_idle(void *data, const api_Instant *now, const api_Waiter *waiter, api_Instant **out, api_buf *err) { +static bool drive_idle(void *data, api_Instant now, const api_Waiter *waiter, api_option_Instant *out, api_buf *err) { (void)data; (void)now; (void)waiter; (void)err; - *out = NULL; + *out = (api_option_Instant){0}; return true; } @@ -279,17 +285,13 @@ static void tail(api_foreign_reader reader) { static bool closures(void) { print("made ", api_made(3, made_times, NULL), "\n"); - /* 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), + /* A closure taking and returning `Copy` values 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), (unsigned)api_Ticket_clones()); - api_Ticket_free(held); - api_Ticket_free(given); - api_Ticket_free(seven); - api_Ticket_free(kept); /* A kept closure, called on another thread after the call that took it returned. */ bool spawned_freed = false; @@ -301,7 +303,7 @@ static bool closures(void) { } api_waker waker = {.wake = wake, .drop = drop, .data = NULL}; - api_Instant *start = api_now(); + api_Instant start = api_now(); driver d = {.start = start}; api_drive_future *drive = api_drive(start, drive_poll, &d, drive_free); api_buf stopped; @@ -318,7 +320,6 @@ static bool closures(void) { ok = ok && api_drive_poll(drive, waker, &stopped) == API_POLL_PENDING && !idle.freed; api_drive_future_free(drive); ok = ok && idle.freed; - api_Instant_free(start); memory hello = {.bytes = "hello world", .len = 11}; tail((api_foreign_reader){.read = memory_read, .seek = memory_seek, .data = &hello}); @@ -855,34 +856,41 @@ static int script(void) { print("", api_describe(events[i]), "\n"); } - /* A `Copy` handle passed by value is copied, so it stays usable. */ - api_Stamp *stamp = api_Stamp_from_millis(2500); + /* A `Copy` value passed by value is copied, so it stays usable, and frees nothing. */ + api_Stamp stamp = api_Stamp_from_millis(2500); printf("secs %llu\n", (unsigned long long)api_Stamp_secs(stamp)); - api_Stamp *later = api_Stamp_from_millis(3000); - printf("later %s\n", api_Stamp_later(later, stamp) ? "true" : "false"); - api_Stamp_free(later); + printf("later %s\n", api_Stamp_later(api_Stamp_from_millis(3000), stamp) ? "true" : "false"); printf("secs %llu\n", (unsigned long long)api_Stamp_secs(stamp)); - api_Message *message = api_Message_new(stamp, str("body")); - api_Stamp_free(stamp); + api_Message *message = api_Message_new((api_option_Stamp){.some = true, .value = stamp}, str("body")); print("payload ", api_Message_payload_get(message), "\n"); - api_Stamp *read = api_Message_stamp_get(message); - print("stamp ", api_Stamp_debug(read), "\n"); - api_Stamp_free(read); + api_option_Stamp read = api_Message_stamp_get(message); + print("stamp ", api_Stamp_debug(&read.value), "\n"); printf("sent %s\n", api_Message_sent(message) ? "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); + /* Setters assign in place: the bytes are copied in, and a `Copy` value copied. */ + api_Stamp reset = api_Stamp_from_millis(4000); + api_Message_stamp_set(message, (api_option_Stamp){.some = true, .value = reset}); api_Message_payload_set(message, str("edited")); print("payload ", api_Message_payload_get(message), "\n"); read = api_Message_stamp_get(message); - print("stamp ", api_Stamp_debug(read), "\n"); - api_Stamp_free(read); + print("stamp ", api_Stamp_debug(&read.value), "\n"); 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, (api_option_Stamp){0}); + printf("stamp %s\n", api_Message_stamp_get(message).some ? "true" : "false"); api_Message_free(message); + /* A value past a word, copied across: a closure takes and returns one by value. */ + api_option_Moment made = api_Moment_new(1500, 1000); + api_Moment moment = made.value; + printf("moment %llu %s\n", (unsigned long long)api_Moment_millis(&moment), + api_Moment_new(1, 0).some ? "false" : "true"); + api_Moment shifted = api_shifted(moment, moment_after, NULL); + api_Moment expected = api_Moment_after(moment, 500); + printf("shifted %llu %s\n", (unsigned long long)api_Moment_millis(&shifted), + api_Moment_eq(&shifted, &expected) ? "true" : "false"); + api_Moment wrapped = api_Moment_after(moment, UINT64_MAX); + printf("wrapped %llu %s\n", (unsigned long long)api_Moment_millis(&wrapped), + api_Moment_eq(&moment, &shifted) ? "true" : "false"); + static const api_Log_vtable print_log = {.free = log_free, .log = log_line}; uint8_t levels[] = {1, 2, 3, 4}; api_buf lines = {.ptr = levels, .len = sizeof levels}; @@ -1547,17 +1555,14 @@ static int script(void) { 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_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); 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"); - api_Instant_free(same); - api_Instant_free(ended); - api_Instant_free(began); + (unsigned long long)(backward.secs * 1000 + backward.nanos / 1000000), api_Instant_eq(&began, &same) ? "true" : "false", + api_Instant_eq(&began, &ended) ? "true" : "false"); /* tracing's level, and its filter as an optional level whose none is `OFF`. */ api_Verbosity *verbosity = api_Verbosity_new(API_LEVEL_WARN); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index a89ce001a..e7b5411dc 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -502,27 +502,36 @@ int script() { print("", api::describe(event), "\n"); } - // A `Copy` handle passed by value is copied, so it stays usable. + // A `Copy` value passed by value is copied, so it stays usable. { api::Stamp stamp = api::Stamp::from_millis(2500); std::printf("secs %llu\n", ull(stamp.secs())); std::printf("later %s\n", yes(api::Stamp::from_millis(3000).later(stamp))); std::printf("secs %llu\n", ull(stamp.secs())); - api::Message message = api::Message::new_(&stamp, "body"); + api::Message message = api::Message::new_(stamp, "body"); print("payload ", message.payload_get(), "\n"); print("stamp ", message.stamp_get()->debug(), "\n"); std::printf("sent %s\n", yes(message.sent())); - // Setters assign in place: the bytes are copied in, and a `Copy` handle copied. + // Setters assign in place: the bytes are copied in, and a `Copy` value copied. api::Stamp reset = api::Stamp::from_millis(4000); - message.stamp_set(&reset); + message.stamp_set(reset); message.payload_set("edited"); print("payload ", message.payload_get(), "\n"); print("stamp ", message.stamp_get()->debug(), "\n"); std::printf("secs %llu\n", ull(reset.secs())); - message.stamp_set(nullptr); + message.stamp_set(std::nullopt); std::printf("stamp %s\n", yes(message.stamp_get().has_value())); } + // A value past a word, copied across: a closure takes and returns one by value. + { + api::Moment moment = *api::Moment::new_(1500, 1000); + std::printf("moment %llu %s\n", ull(moment.millis()), yes(!api::Moment::new_(1, 0).has_value())); + api::Moment shifted = api::shifted(moment, [](const api::Moment &m) { return m.after(500); }); + std::printf("shifted %llu %s\n", ull(shifted.millis()), yes(shifted == moment.after(500))); + std::printf("wrapped %llu %s\n", ull(moment.after(UINT64_MAX).millis()), yes(moment == shifted)); + } + std::uint8_t levels[] = {1, 2, 3, 4}; std::printf("kept %u\n", api::emit(std::make_unique(), api::view(levels, sizeof levels))); { @@ -1044,15 +1053,13 @@ int script() { // Closures C++ implements that return what Rust then owns, or that Rust keeps. print("made ", api::made(3, [](std::uint32_t n) { return std::to_string(n) + " times"; }), "\n"); - // A closure taking and returning `Copy` objects copies them, never running their `Clone`. + // A closure taking and returning `Copy` values copies them, never running their `Clone`. { api::Ticket kept = api::Ticket::new_(9); std::uint32_t given = api::reissue(api::Ticket::new_(7), [](const api::Ticket &t) { return api::Ticket::new_(t.number() + 1); }) .number(); - std::uint32_t held = api::reissue(api::Ticket::new_(7), [&kept](const api::Ticket &) { - return std::move(kept); - }).number(); + std::uint32_t held = api::reissue(api::Ticket::new_(7), [&kept](const api::Ticket &) { return kept; }).number(); std::printf("reissued %u %u, cloned %u\n", static_cast(given), static_cast(held), static_cast(api::Ticket::clones())); } diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 2d7d0d4ef..67b5a68a2 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -628,14 +628,11 @@ Future script() async { for (final section in sections) { section.$2.close(); } - // An instant only Rust makes, which each call copies out of its handle, and compares. + // An instant only Rust makes, which each call copies, and compares. final began = api.now(); final ended = api.later(began, const Duration(milliseconds: 1500)); final same = api.later(began, Duration.zero); print('instants ${api.since(ended, began).inMilliseconds} ${api.since(began, ended).inMilliseconds} ${began == same} ${began == ended}'); - began.close(); - ended.close(); - same.close(); // tracing's level, and its filter as an optional level whose null is `OFF`. final verbosity = api.Verbosity(api.Level.warn); print('verbosity ${api.spell(verbosity.filter())} ${api.spell(api.louder(verbosity.filter()))}'); @@ -645,7 +642,7 @@ Future script() async { // A closure's string, which Rust takes. print('made ${api.made(3, (n) => '$n times')}'); - // A closure taking and returning `Copy` objects copies them, never running their `Clone`. + // A closure taking and returning `Copy` values copies them, never running their `Clone`. final nine = api.Ticket(9); final renewed = api.reissue(api.Ticket(7), (t) => api.Ticket(t.number() + 1)).number(); final copied = api.reissue(api.Ticket(7), (_) => nine).number(); @@ -674,7 +671,6 @@ Future script() async { 'a kept closure wakes its future', ); print('drive $stopped'); - start.close(); // A reader the caller implements: dart:io's file, then one whose reads fail. final dir = Directory.systemTemp.createTempSync('api'); final opened = (File('${dir.path}/tail')..writeAsStringSync('hello world')).openSync(); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index ecd169d46..097e75af5 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -392,25 +392,32 @@ func script() { fmt.Println(api.Describe(event)) } - // A `Copy` handle passed by value is copied, so it stays usable. + // A `Copy` value passed by value is copied, so it stays usable. stamp := api.StampFromMillis(2500) - fmt.Printf("secs %d\n", must(stamp.Secs())) - fmt.Printf("later %t\n", must(api.StampFromMillis(3000).Later(stamp))) - fmt.Printf("secs %d\n", must(stamp.Secs())) - message := must(api.NewMessage(stamp, []byte("body"))) + fmt.Printf("secs %d\n", stamp.Secs()) + fmt.Printf("later %t\n", api.StampFromMillis(3000).Later(stamp)) + fmt.Printf("secs %d\n", stamp.Secs()) + message := api.NewMessage(&stamp, []byte("body")) fmt.Printf("payload %s\n", must(message.PayloadGet()).Copy()) fmt.Printf("stamp %s\n", must(message.StampGet()).GoString()) fmt.Printf("sent %t\n", must(message.Sent())) - // Setters assign in place: the bytes are copied in, and a `Copy` handle copied. + // Setters assign in place: the bytes are copied in, and a `Copy` value copied. reset := api.StampFromMillis(4000) - ok(message.StampSet(reset)) + ok(message.StampSet(&reset)) ok(message.PayloadSet([]byte("edited"))) fmt.Printf("payload %s\n", must(message.PayloadGet()).Copy()) fmt.Printf("stamp %s\n", must(message.StampGet()).GoString()) - fmt.Printf("secs %d\n", must(reset.Secs())) + fmt.Printf("secs %d\n", reset.Secs()) ok(message.StampSet(nil)) fmt.Printf("stamp %t\n", must(message.StampGet()) != nil) + // A value past a word, copied across: a closure takes and returns one by value. + moment := *api.NewMoment(1500, 1000) + fmt.Printf("moment %d %t\n", moment.Millis(), api.NewMoment(1, 0) == nil) + shifted := api.Shifted(moment, func(m api.Moment) api.Moment { return m.After(500) }) + fmt.Printf("shifted %d %t\n", shifted.Millis(), shifted.Equal(moment.After(500))) + fmt.Printf("wrapped %d %t\n", moment.After(math.MaxUint64).Millis(), moment.Equal(shifted)) + // Rust frees the implementation once it drops it, releasing Go's. fmt.Printf("kept %d\n", api.Emit(printLog{}, []byte{1, 2, 3, 4})) logger := api.NewLogger(printLog{}) @@ -734,15 +741,12 @@ func script() { section.V1.Close() } fmt.Printf("sections %s\n", strings.Join(sectionTexts, ", ")) - // An instant only Rust makes, which each call copies out of its handle, and compares. - var began *std.Instant = api.Now() - ended := must(api.Later(began, 1500*time.Millisecond)) - same := must(api.Later(began, 0)) - fmt.Printf("instants %d %d %t %t\n", must(api.Since(ended, began)).Milliseconds(), must(api.Since(began, ended)).Milliseconds(), - must(began.Equal(same)), must(began.Equal(ended))) - began.Close() - ended.Close() - same.Close() + // An instant only Rust makes, which each call copies, and compares. + var began std.Instant = api.Now() + ended := api.Later(began, 1500*time.Millisecond) + same := api.Later(began, 0) + fmt.Printf("instants %d %d %t %t\n", api.Since(ended, began).Milliseconds(), api.Since(began, ended).Milliseconds(), + began.Equal(same), began.Equal(ended)) // tracing's level, and its filter as an optional level whose nil is `OFF`. verbosity := api.NewVerbosity(tracingcore.LevelWarn) fmt.Printf("verbosity %s %s\n", api.Spell(must(verbosity.Filter())), api.Spell(api.Louder(must(verbosity.Filter())))) @@ -752,34 +756,31 @@ func script() { verbosity.Close() fmt.Printf("made %s\n", api.Made(3, func(n uint32) string { return fmt.Sprintf("%d times", n) })) - // A closure taking and returning `Copy` objects copies them, never running their `Clone`. + // A closure taking and returning `Copy` values copies them, never running their `Clone`. nine, seven := api.NewTicket(9), api.NewTicket(7) - 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()) - for _, ticket := range []*api.Ticket{copied, renewed, seven, nine} { - ticket.Close() - } + renewed := api.Reissue(seven, func(t api.Ticket) api.Ticket { return api.NewTicket(t.Number() + 1) }) + copied := api.Reissue(seven, func(api.Ticket) api.Ticket { return nine }) + fmt.Printf("reissued %d %d, cloned %d\n", renewed.Number(), copied.Number(), api.TicketClones()) // A kept closure, called on another thread after the call that took it returned. spawned := api.NewSpawned(func() uint32 { return 7 }) fmt.Printf("spawned %d\n", must(spawned.Join())) // A kept closure: called again at the instant it asks for, then once its waiter wakes. driven := api.Now() calls := 0 - stopped := must(api.Drive(context.Background(), driven, func(now *std.Instant, waiter *api.Waiter) (*std.Instant, error) { + stopped := must(api.Drive(context.Background(), driven, func(now std.Instant, waiter *api.Waiter) (*std.Instant, error) { calls++ switch calls { case 1: - return must(api.Later(now, 5*time.Millisecond)), nil + later := api.Later(now, 5*time.Millisecond) + return &later, nil case 2: - fmt.Printf("drive %d\n", must(api.Since(now, driven)).Milliseconds()) + fmt.Printf("drive %d\n", api.Since(now, driven).Milliseconds()) must(waiter.Waker())() return nil, nil } return nil, fmt.Errorf("stopped after %d", calls) })) fmt.Printf("drive %s\n", stopped) - driven.Close() tail(strings.NewReader("hello world")) tail(brokenReader{}) // A seek from the start reaches the reader whole, past `math.MaxInt64`, as `Cursor` takes one. @@ -893,7 +894,7 @@ func closures() { cancelled, cancel := context.WithCancel(ctx) done := make(chan error) go func() { - _, err := api.Drive(cancelled, start, func(*std.Instant, *api.Waiter) (*std.Instant, error) { + _, err := api.Drive(cancelled, start, func(std.Instant, *api.Waiter) (*std.Instant, error) { _ = marker[0] select { case called <- struct{}{}: @@ -912,26 +913,21 @@ func closures() { cancel() check(errors.Is(<-done, context.Canceled), "a cancelled drive returns its context's error") released(freed, "a dropped future releases its closure") - start.Close() } // What a closure returns crosses: the instant it asks for, then its error. { start := api.Now() - later := must(api.Later(start, time.Second)) + later := api.Later(start, time.Second) var seen []int64 - stopped := must(api.Drive(ctx, start, func(now *std.Instant, _ *api.Waiter) (*std.Instant, error) { - seen = append(seen, must(api.Since(now, start)).Milliseconds()) + stopped := must(api.Drive(ctx, start, func(now std.Instant, _ *api.Waiter) (*std.Instant, error) { + seen = append(seen, api.Since(now, start).Milliseconds()) if len(seen) == 1 { - return later, nil + return &later, nil } return nil, errors.New("") })) check(stopped == "" && len(seen) == 2 && seen[1] == 1000, "a returned instant and an empty error cross") - // Rust took a clone: Go's is still its own. - check(must(later.Equal(later)), "a returned object stays Go's") - later.Close() - start.Close() } // A read's bytes and error both reach Rust, the error on the next read. @@ -1198,6 +1194,11 @@ func uncollected(n uint32, what string) { // closed, and that a second Close does nothing. func closed() { ctx := context.Background() + panics := func(f func()) (panicked any) { + defer func() { panicked = recover() }() + f() + return nil + } isClosed := func(err error, name, what string) { check(errors.Is(err, rt.ErrClosed) && err.Error() == name+" is closed", what) } @@ -1224,13 +1225,11 @@ func closed() { isClosed(registry.FirstSet(second), "Registry", "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. - stamp := api.StampFromMillis(2500) - stamped := api.Stamped{At: stamp, Count: 3} - 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") + // A record's method, whose receiver holds a value only Rust makes: Go's zero value of + // one panics before Rust reads it. + stamped := api.Stamped{At: api.StampFromMillis(2500), Count: 3} + check(stamped.Total() == 5, "a record's method reads the value it holds") + check(panics(func() { api.Stamped{Count: 3}.Total() }) == "a zero Stamp, which only Rust makes", "a zero value panics") // An argument, alone, or nested where the call holds it through its arena. _, err = api.BumpBoth(api.NewTally(), tally) diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index df02a065f..75472324e 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -253,6 +253,13 @@ fun script() { message.stampSet(null) println("stamp ${message.stampGet != null}") + // A value past a word, copied across: a closure takes and returns one by value. + val moment = Moment.new(1500u, 1000u)!! + println("moment ${moment.millis()} ${Moment.new(1u, 0u) == null}") + val shifted = shifted(moment) { m -> m.after(500u) } + println("shifted ${shifted.millis()} ${shifted == moment.after(500u)}") + println("wrapped ${moment.after(ULong.MAX_VALUE).millis()} ${moment == shifted}") + println("kept ${emit(PrintLog(), byteArrayOf(1, 2, 3, 4))}") Logger.new(PrintLog()).use { logger -> println("write ${logger.write("hi")}") } println("dropped") @@ -534,14 +541,11 @@ fun script() { val jsonSections = sections(listOf(Pair("b", Json.parse("[1]")), Pair("a", Json.parse("2")))) println("sections ${jsonSections.joinToString(", ") { (key, value) -> "$key ${value.toText()}" }}") jsonSections.forEach { it.second.close() } - // An instant only Rust makes, which each call copies out of its handle, and compares. - now().use { began -> - later(began, 1500.milliseconds).use { ended -> - later(began, kotlin.time.Duration.ZERO).use { same -> - println("instants ${since(ended, began).inWholeMilliseconds} ${since(began, ended).inWholeMilliseconds} ${began == same} ${began == ended}") - } - } - } + // An instant only Rust makes, which each call copies, and compares. + val began = now() + val ended = later(began, 1500.milliseconds) + val same = later(began, kotlin.time.Duration.ZERO) + println("instants ${since(ended, began).inWholeMilliseconds} ${since(began, ended).inWholeMilliseconds} ${began == same} ${began == ended}") // tracing's level, and its filter as an optional level whose null is `OFF`. Verbosity.new(Level.WARN).use { verbosity -> println("verbosity ${spell(verbosity.filter())} ${spell(louder(verbosity.filter()))}") @@ -550,7 +554,7 @@ fun script() { } println("made ${made(3u) { n -> "$n times" }}") - // A closure taking and returning `Copy` objects copies them, never running their `Clone`. + // A closure taking and returning `Copy` values copies them, never running their `Clone`. val nine = Ticket.new(9u) val given = reissue(Ticket.new(7u)) { t -> Ticket.new(t.number() + 1u) }.number() val held = reissue(Ticket.new(7u)) { nine }.number() @@ -558,7 +562,7 @@ fun script() { // 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. - now().use { start -> + now().let { start -> var calls = 0 val stopped = runBlocking { drive(start) { now, waiter -> @@ -938,7 +942,7 @@ fun broken(which: String) { "stray" -> stray() // Any exception but a closure's own error, and a reader's IOException, aborts. "closure threw" -> made(1u) { throw IllegalStateException("a make failed") } - "closure failed" -> now().use { start -> + "closure failed" -> now().let { start -> runBlocking { drive(start) { _, _ -> throw IllegalStateException("a poll failed") } } } "reader threw" -> tail( diff --git a/ffi/tests/api.py b/ffi/tests/api.py index caf7fe791..ef1befeeb 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -312,6 +312,13 @@ def edit(counter): message.stamp_set(None) print(f"stamp {flag(message.stamp_get is not None)}") + # A value past a word, copied across: a closure takes and returns one by value. + moment = api.Moment.new(1500, 1000) + print(f"moment {moment.millis()} {flag(api.Moment.new(1, 0) is None)}") + shifted = api.shifted(moment, lambda m: m.after(500)) + print(f"shifted {shifted.millis()} {flag(shifted == moment.after(500))}") + print(f"wrapped {moment.after(2**64 - 1).millis()} {flag(moment == shifted)}") + print(f"kept {api.emit(PrintLog(), bytes([1, 2, 3, 4]))}") logger = api.Logger.new(PrintLog()) print(f"write {flag(logger.write('hi'))}") diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index 93b3fcca3..a730ffe97 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -399,6 +399,13 @@ func script() { message.stampSet(nil) print("stamp \(message.stampGet != nil)") + // A value past a word, copied across: a closure takes and returns one by value. + let moment = Moment.new(1500, 1000)! + print("moment \(moment.millis()) \(Moment.new(1, 0) == nil)") + let shifted = shifted(moment) { $0.after(500) } + print("shifted \(shifted.millis()) \(shifted == moment.after(500))") + print("wrapped \(moment.after(UInt64.max).millis()) \(moment == shifted)") + // Bound first, so Swift's reference is gone before the print. let kept = emit(PrintLog(), Data([1, 2, 3, 4])) print("kept \(kept)") From 8ae8e8f6751ccab77fc1f4b0e26d1faa0adfaa95 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 21:48:34 -0800 Subject: [PATCH 03/24] wip(ffi): moq's tests and benches hold Copy types by value Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 6 ++--- ffi/fixtures/api/src/lib.rs | 10 ++++---- ffi/moq/bench/export.c | 21 +++++++-------- ffi/moq/bench/export.cpp | 16 ++++++------ ffi/moq/bench/export.dart | 10 +++----- ffi/moq/bench/export.go | 17 +++++-------- ffi/moq/bench/export.kt | 8 +++--- ffi/moq/bench/export.py | 2 +- ffi/moq/bench/export.swift | 2 +- ffi/moq/moq.c | 51 +++++++++++++++---------------------- ffi/moq/moq.cpp | 12 ++++----- ffi/moq/moq.dart | 2 +- ffi/moq/moq.go | 13 +++++----- ffi/moq/moq.kt | 2 +- ffi/moq/moq.py | 2 +- ffi/moq/moq.swift | 2 +- ffi/src/collect.rs | 44 ++++++++++++++++++++------------ ffi/src/dart.rs | 11 +++++--- ffi/src/python.rs | 4 +++ ffi/tests/api.c | 17 +++++++------ ffi/tests/api.cpp | 2 +- ffi/tests/api.dart | 7 +++++ ffi/tests/api.go | 11 ++++---- ffi/tests/api.py | 5 ++++ ffi/tests/fixtures.rs | 2 +- 25 files changed, 146 insertions(+), 133 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index b8f60f671..75bdce486 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -71,7 +71,7 @@ re-exports its items instead, which export. A type no path reaches is refused. | --- | --- | | a struct whose fields are all `pub`, not `#[non_exhaustive]`, which map, with no `Drop` of its own | a record: a struct, copied across | | an enum with variants whose fields map, with no `Drop` of its own | a C enum without fields, or a struct of a tag and a union | -| any other type that is `Copy` and `Send`, such as moq's `Timestamp` or std's `Instant` | an opaque value: a struct of its bytes, copied across, which only Rust makes or reads; its `pub` fields are getters, and a `&mut self` method or a setter is refused, as for a record; one no export returns is left out, since C could never make one | +| any other type that is `Copy` and `Send`, with no `pub` field and no `pub` method taking `&mut self`, such as moq's `Timestamp` or std's `Instant` | an opaque value: a struct of its bytes, copied across, which only Rust makes or reads; one no export returns is left out, since C could never make one | | any other type, if `Send` | an object: an opaque handle to an `Arc`, freed with `_free` | | a type whose only generic parameters are lifetimes, such as `Path<'a>` | an object holding its `'static` form | | a generic type, at each instance it infers | that instance as any other type, with each method whose impl and own bounds it meets; any other instance is refused | @@ -511,8 +511,8 @@ caller imports it with no ignores. `Event.Moved(p)`. `Display` is `str()`, `Debug` `repr()`, and `Default` a static `default()`. Constants are module or class attributes, made anew on each read unless a literal. An **opaque value** is a slotted class holding its bytes as `bytes`, which only - Rust makes or reads: a value of another length raises `TypeError` where Rust would read - it. + Rust makes or reads: making one raises `TypeError`, and bytes of another length raise + `ValueError` where Rust would read them. - **Bytes** passed in are any buffer, `bytes | bytearray | memoryview`: `bytes` is read in place, and others, which Python may change while Rust reads, are copied. Bytes Rust returns, or a record holds, are a read-only `memoryview` lending Rust's buffer until it diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 65181a167..b6b278d05 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -684,7 +684,7 @@ pub fn made(n: u32, make: impl FnOnce(u32) -> R) -> R { /// How many times a [`Ticket`]'s `Clone` ran. static TICKET_CLONES: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); -/// A `Copy` object whose `Clone` counts its calls, which a closure taking or returning one +/// A `Copy` value whose `Clone` counts its calls, which a closure taking or returning one /// never runs, as Rust copies it instead. #[derive(Copy, Debug, PartialEq)] pub struct Ticket { @@ -927,8 +927,7 @@ impl Future for Within { } } -/// A time: an object, since its field is private, but `Copy`, so passing it by value -/// copies it rather than moving it out of its handle. +/// A time: `Copy` with a private field, so a value only Rust makes, copied across. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Stamp { millis: u64, @@ -981,7 +980,7 @@ pub fn shifted(moment: Moment, shift: impl Fn(Moment) -> Moment) -> Moment { shift(moment) } -/// A stamp and a count: a record holding an object, whose method reads it. +/// A stamp and a count: a record holding a value only Rust makes, whose method reads it. pub struct Stamped { pub at: Stamp, pub count: u32, @@ -994,7 +993,8 @@ impl Stamped { } } -/// A message: an object, since one field is private, whose `pub` fields read as getters. +/// A message: an object, since one field is private and it is not `Copy`, whose `pub` +/// fields read as getters. #[derive(Clone, Debug)] pub struct Message { pub stamp: Option, diff --git a/ffi/moq/bench/export.c b/ffi/moq/bench/export.c index 01e679f46..e3c9efb5d 100644 --- a/ffi/moq/bench/export.c +++ b/ffi/moq/bench/export.c @@ -85,9 +85,9 @@ static moq_group_Producer *append_group(moq_track_Producer *track) { return group; } -static void write_frame(moq_group_Producer *group, const moq_Timestamp *at, moq_buf frame) { +static void write_frame(moq_group_Producer *group, 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, (moq_option_Timestamp){.some = true, .value = at}, frame, &err)) { fail("write_frame"); } } @@ -102,7 +102,7 @@ static void finish(moq_group_Producer *group) { /* Groups of 100 frames of `size` bytes written and each read back, its payload's first byte * touched, in about 64 MiB and at least 300 frames: megabytes a wall and a CPU second. */ -static span throughput(moq_track_Producer *track, moq_track_Subscriber *subscriber, const moq_Timestamp *at, size_t size, unsigned *sum) { +static span throughput(moq_track_Producer *track, moq_track_Subscriber *subscriber, moq_Timestamp at, size_t size, unsigned *sum) { size_t groups = (64u << 20) / (size * FRAMES); groups = groups < 3 ? 3 : groups > 2000 ? 2000 : groups; uint8_t *payload = calloc(size, 1); @@ -131,7 +131,7 @@ static void round_(int print) { moq_broadcast_Info *binfo = moq_broadcast_Info_new(); moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(binfo); moq_broadcast_Info_free(binfo); - moq_Timescale *micro = moq_Timescale_MICRO(); + moq_option_Timescale micro = {.some = true, .value = 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); @@ -176,26 +176,25 @@ static void round_(int print) { } span object = per(start, N); - /* Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. */ + /* Each frame stamped through a fresh timestamp, as a caller holding a u64 must. */ uint64_t micros = 0; start = now(); for (int g = 0; g < GROUPS; g++) { moq_group_Producer *group = append_group(track); for (int f = 0; f < FRAMES; f++) { - moq_Timestamp *at = NULL; - moq_TimeOverflow *overflow = NULL; + moq_Timestamp at; + moq_TimeOverflow overflow; if (!moq_Timestamp_from_micros(micros++, &at, &overflow)) { fail("from_micros"); } write_frame(group, at, frame); - moq_Timestamp_free(at); } finish(group); } span stamped = per(start, N); - moq_Timestamp *at = NULL; - moq_TimeOverflow *overflow = NULL; + moq_Timestamp at; + moq_TimeOverflow overflow; if (!moq_Timestamp_from_micros(micros, &at, &overflow)) { fail("from_micros"); } @@ -275,10 +274,8 @@ static void round_(int print) { } sink = sum; - moq_Timestamp_free(at); moq_track_Subscriber_free(subscriber); moq_track_Producer_free(track); - moq_Timescale_free(micro); moq_broadcast_Producer_free(broadcast); if (print) { diff --git a/ffi/moq/bench/export.cpp b/ffi/moq/bench/export.cpp index bba2680ee..ba37a0727 100644 --- a/ffi/moq/bench/export.cpp +++ b/ffi/moq/bench/export.cpp @@ -82,7 +82,7 @@ auto ready(F future, const char *what) { // Groups of 100 frames of `size` bytes written and each read back, its payload's first byte // touched, in about 64 MiB and at least 300 frames: megabytes a wall and a CPU second. -span throughput(moq::track_Producer &track, moq::track_Subscriber &subscriber, const moq::Timestamp &at, std::size_t size, unsigned &sum) { +span throughput(moq::track_Producer &track, moq::track_Subscriber &subscriber, moq::Timestamp at, std::size_t size, unsigned &sum) { std::size_t count = std::clamp((64u << 20) / (size * frames), 3, 2000); std::vector payload(size); auto start = now(); @@ -90,7 +90,7 @@ span throughput(moq::track_Producer &track, moq::track_Subscriber &subscriber, c auto group = must(track.append_group(), "append_group"); auto reader = ready(subscriber.recv_group(), "recv_group"); for (int f = 0; f < frames; f++) { - must(group.write_frame(&at, payload), "write_frame"); + must(group.write_frame(at, payload), "write_frame"); auto read = ready(reader.read_frame(), "read_frame"); sum += read.payload_get().data()[0]; } @@ -103,7 +103,7 @@ span throughput(moq::track_Producer &track, moq::track_Subscriber &subscriber, c void round(bool print) { moq::broadcast_Producer broadcast = moq::broadcast_Info::new_().produce(); moq::Timescale micro = moq::Timescale::MICRO(); - auto track = must(broadcast.create_track("bench", moq::track_Info::default_().with_timescale(µ)), "create_track"); + auto track = must(broadcast.create_track("bench", moq::track_Info::default_().with_timescale(micro)), "create_track"); std::uint8_t bytes[100] = {0}; moq::view frame(bytes, sizeof bytes); @@ -139,14 +139,14 @@ void round(bool print) { } span object = per(start); - // Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. + // Each frame stamped through a fresh timestamp, as a caller holding a u64 must. std::uint64_t micros = 0; start = now(); for (int g = 0; g < groups; g++) { auto group = must(track.append_group(), "append_group"); for (int f = 0; f < frames; f++) { auto at = must(moq::Timestamp::from_micros(micros++), "from_micros"); - must(group.write_frame(&at, frame), "write_frame"); + must(group.write_frame(at, frame), "write_frame"); } must(std::move(group).finish(), "finish"); } @@ -157,7 +157,7 @@ void round(bool print) { for (int g = 0; g < groups; g++) { auto group = must(track.append_group(), "append_group"); for (int f = 0; f < frames; f++) { - must(group.write_frame(&at, frame), "write_frame"); + must(group.write_frame(at, frame), "write_frame"); } must(std::move(group).finish(), "finish"); } @@ -172,7 +172,7 @@ void round(bool print) { auto group = must(track.append_group(), "append_group"); auto reader = ready(subscriber.recv_group(), "recv_group"); for (int f = 0; f < frames; f++) { - must(group.write_frame(&at, frame), "write_frame"); + must(group.write_frame(at, frame), "write_frame"); last = ready(reader.read_frame(), "read_frame"); } must(std::move(group).finish(), "finish"); @@ -190,7 +190,7 @@ void round(bool print) { std::fprintf(stderr, "bench: woken read_frame ready\n"); std::exit(1); } - must(group.write_frame(&at, frame), "write_frame"); + must(group.write_frame(at, frame), "write_frame"); (void)must(must(must(reading.poll(noop()), "woken read_frame"), "woken read_frame"), "woken read_frame"); } must(std::move(group).finish(), "finish"); diff --git a/ffi/moq/bench/export.dart b/ffi/moq/bench/export.dart index 5dd1bb90d..608691785 100644 --- a/ffi/moq/bench/export.dart +++ b/ffi/moq/bench/export.dart @@ -73,9 +73,7 @@ Future throughput(moq.TrackProducer track, moq.TrackSubscriber subscriber, Future> round() async { final out = {}; final broadcast = moq.BroadcastInfo().produce(); - final micro = moq.Timescale.MICRO; - final track = broadcast.createTrack('bench', moq.TrackInfo.default_().withTimescale(micro)); - micro.close(); + final track = broadcast.createTrack('bench', moq.TrackInfo.default_().withTimescale(moq.Timescale.MICRO)); final frame = Uint8List(100); var start = now(); @@ -107,15 +105,13 @@ Future> round() async { } out['object'] = per(start); - // Each frame stamped through a fresh timestamp object, as a caller holding an int must. + // Each frame stamped through a fresh timestamp, as a caller holding an int must. var micros = 0; start = now(); for (var g = 0; g < groups; g++) { final group = track.appendGroup(); for (var f = 0; f < frames; f++) { - final at = moq.Timestamp.fromMicros(micros++); - group.writeFrame(at, frame); - at.close(); + group.writeFrame(moq.Timestamp.fromMicros(micros++), frame); } group.finish(); group.close(); diff --git a/ffi/moq/bench/export.go b/ffi/moq/bench/export.go index 02de24577..41be193e8 100644 --- a/ffi/moq/bench/export.go +++ b/ffi/moq/bench/export.go @@ -95,8 +95,7 @@ func round() []result { var out []result broadcast := must(net.NewBroadcastInfo().Produce()) micro := net.TimescaleMICRO() - track := must(broadcast.CreateTrack("bench", must(net.TrackInfoDefault().WithTimescale(micro)))) - micro.Close() + track := must(broadcast.CreateTrack("bench", must(net.TrackInfoDefault().WithTimescale(µ)))) frame := make([]byte, 100) start := now() @@ -132,7 +131,7 @@ func round() []result { } out = append(out, result{"object", per(start, n)}) - // Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. + // Each frame stamped through a fresh timestamp, as a caller holding a u64 must. var micros uint64 start = now() for range groups { @@ -140,8 +139,7 @@ func round() []result { for range frames { at := must(net.TimestampFromMicros(micros)) micros++ - check(group.WriteFrame(at, frame)) - at.Close() + check(group.WriteFrame(&at, frame)) } check(group.Finish()) group.Close() @@ -153,7 +151,7 @@ func round() []result { for range groups { group := must(track.AppendGroup()) for range frames { - check(group.WriteFrame(at, frame)) + check(group.WriteFrame(&at, frame)) } check(group.Finish()) group.Close() @@ -173,7 +171,7 @@ func round() []result { group := must(track.AppendGroup()) reader := must(subscriber.RecvGroup(ctx)) for range frames { - check(group.WriteFrame(at, frame)) + check(group.WriteFrame(&at, frame)) if last != nil { last.Close() } @@ -195,7 +193,7 @@ func round() []result { for range frames { go func() { read <- must(reader.ReadFrame(ctx)) }() runtime.Gosched() - check(group.WriteFrame(at, frame)) + check(group.WriteFrame(&at, frame)) (<-read).Close() } reader.Close() @@ -205,7 +203,7 @@ func round() []result { out = append(out, result{"woken", per(start, n)}) for _, size := range sizes { - out = append(out, result{fmt.Sprintf("mbps:%d", size), throughput(ctx, track, subscriber, at, size)}) + out = append(out, result{fmt.Sprintf("mbps:%d", size), throughput(ctx, track, subscriber, &at, size)}) } subscriber.Close() @@ -216,7 +214,6 @@ func round() []result { } out = append(out, result{"payload", per(start, n)}) last.Close() - at.Close() track.Close() broadcast.Close() return out diff --git a/ffi/moq/bench/export.kt b/ffi/moq/bench/export.kt index 03b2a7d5c..10fc55edc 100644 --- a/ffi/moq/bench/export.kt +++ b/ffi/moq/bench/export.kt @@ -55,9 +55,7 @@ suspend fun throughput(track: TrackProducer, subscriber: TrackSubscriber, at: Ti fun round(): Map { val out = linkedMapOf() val broadcast = BroadcastInfo.new().produce() - val track = Timescale.MICRO.use { micro -> - broadcast.createTrack("bench", TrackInfo.default().withTimescale(micro)) - } + val track = broadcast.createTrack("bench", TrackInfo.default().withTimescale(Timescale.MICRO)) val frame = ByteArray(100) var sum = 0 @@ -82,13 +80,13 @@ fun round(): Map { repeat(N) { track.info().close() } out["object"] = per(start) - // Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. + // Each frame stamped through a fresh timestamp, as a caller holding a u64 must. var micros = 0uL start = now() repeat(GROUPS) { track.appendGroup().use { group -> repeat(FRAMES) { - Timestamp.fromMicros(micros++).use { at -> group.writeFrame(at, frame) } + group.writeFrame(Timestamp.fromMicros(micros++), frame) } group.finish() } diff --git a/ffi/moq/bench/export.py b/ffi/moq/bench/export.py index c76bde11b..eab05db2a 100644 --- a/ffi/moq/bench/export.py +++ b/ffi/moq/bench/export.py @@ -77,7 +77,7 @@ def round(loop): track.info() out["object"] = per(start) - # Each frame stamped through a fresh timestamp object, as a caller holding an int must. + # Each frame stamped through a fresh timestamp, as a caller holding an int must. micros = 0 start = now() for _ in range(GROUPS): diff --git a/ffi/moq/bench/export.swift b/ffi/moq/bench/export.swift index e95a8935e..8e9ebb6ce 100644 --- a/ffi/moq/bench/export.swift +++ b/ffi/moq/bench/export.swift @@ -112,7 +112,7 @@ func round() throws -> ([(String, Span)], Int) { } out.append(("object", per(start))) - // Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. + // Each frame stamped through a fresh timestamp, as a caller holding a u64 must. var micros: UInt64 = 0 start = now() for _ in 0.. before); now = moq_clock_now(); - deadline = NULL; + deadline = (moq_option_clock_Instant){0}; 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(!deadline.some && err != NULL && is(moq_Error_to_string(err), "closed")); moq_Error_free(err); err = NULL; - moq_clock_Instant_free(now); moq_Waiter_free(waiter); moq_origin_Driver_free(driver); @@ -422,9 +416,8 @@ int main(void) { /* 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_Timescale_free(milli); + moq_track_Info *status_info = + moq_track_Info_with_timescale(moq_track_Info_default(), (moq_option_Timescale){.some = true, .value = moq_Timescale_MILLI()}); moq_track_Producer *status = NULL; CHECK(moq_broadcast_Producer_create_track(publisher, str("status"), status_info, &status, &err)); moq_mux_json_Config *json_config = moq_mux_json_Config_default(); @@ -442,9 +435,7 @@ int main(void) { 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)); - moq_Timestamp_free(stamp); + CHECK(moq_mux_json_Snapshot_Json_update(status_snapshot, latest, moq_Timestamp_now(), &mux_err)); 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); @@ -551,7 +542,7 @@ 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); + CHECK(!moq_tokio_Backoff_initial_get(backoff).some); moq_duration initial = moq_tokio_Backoff_initial(backoff); CHECK(initial.secs == 1 && initial.nanos == 0); moq_tokio_Backoff_free(backoff); @@ -601,7 +592,7 @@ int main(void) { CHECK(moq_broadcast_Producer_create_track(live_broadcast, str("video"), NULL, &live_track, &err)); 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_write_frame(live_group, (moq_option_Timestamp){0}, str("hello"), &err)); CHECK(moq_group_Producer_finish(live_group, &err)); moq_group_Producer_free(live_group); diff --git a/ffi/moq/moq.cpp b/ffi/moq/moq.cpp index 2f948eaf8..7d699d24a 100644 --- a/ffi/moq/moq.cpp +++ b/ffi/moq/moq.cpp @@ -149,7 +149,7 @@ int main() { { auto reading = group.read_frame(); CHECK(!reading.poll(waker)); - CHECK(writer->write_frame(nullptr, "hello")); + CHECK(writer->write_frame(std::nullopt, "hello")); CHECK(wakes == 2); auto frame = reading.poll(waker); CHECK(frame && *frame && frame->value()); @@ -160,17 +160,17 @@ int main() { CHECK(moq::clock_now().checked_duration_since(hello.arrived()).has_value()); } - // A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is + // A timestamp is a `Copy` value copied across, which allocates nothing. The track is // untimed, so a timestamped frame is refused. auto at = moq::Timestamp::from_millis(1500); CHECK(at && at->as_secs() == 1); - auto late = writer->write_frame(&*at, "late"); + auto late = writer->write_frame(*at, "late"); CHECK(!late && is(late.error().to_string(), "frame timestamp doesn't fit its track")); CHECK(at->as_secs() == 1); // The last frame, bytes that are not UTF-8, then the group's end, which reads as None, // both blocking. - CHECK(writer->write_frame(nullptr, "\xffworld")); + CHECK(writer->write_frame(std::nullopt, "\xffworld")); CHECK(std::move(*writer).finish()); auto world = group.read_frame().get(); CHECK(world && world.value() && is(world.value()->payload_get(), "\xffworld")); @@ -300,7 +300,7 @@ int main() { // lent catalog and read back as text. // Its track has a timescale, since every write is stamped. auto milli = moq::Timescale::MILLI(); - auto status = publisher.create_track("status", moq::track_Info::default_().with_timescale(&milli)); + auto status = publisher.create_track("status", moq::track_Info::default_().with_timescale(milli)); CHECK(status); auto status_snapshot = catalog->json_snapshot_Config(std::move(*status), moq::mux_json_Config::default_(), moq::mux_json_Options::default_()); @@ -389,7 +389,7 @@ int main() { auto live_track = live_broadcast.create_track("video", std::nullopt); CHECK(live_track); auto live_group = live_track->append_group(); - CHECK(live_group && live_group->write_frame(nullptr, "hello")); + CHECK(live_group && live_group->write_frame(std::nullopt, "hello")); CHECK(std::move(*live_group).finish()); moq::tokio_server_Config server_config = moq::tokio_server_Config::default_(); diff --git a/ffi/moq/moq.dart b/ffi/moq/moq.dart index cd00edf57..d50a002e7 100644 --- a/ffi/moq/moq.dart +++ b/ffi/moq/moq.dart @@ -116,7 +116,7 @@ Future local() async { // When it arrived: kio's clock's `Instant`, from kio's own export. expect(moq.clockNow().checkedDurationSince(hello.arrived()) != null, 'a frame arrived before now'); - // A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is + // A timestamp is a `Copy` value copied across, which allocates nothing. The track is // untimed, so a timestamped frame is refused. final at = moq.Timestamp.fromMillis(1500); expect(at.asSecs() == 1, '1500 ms is 1 s'); diff --git a/ffi/moq/moq.go b/ffi/moq/moq.go index fa82edc4d..9f03d6efc 100644 --- a/ffi/moq/moq.go +++ b/ffi/moq/moq.go @@ -157,15 +157,15 @@ func script() { expect(string(must(hello.PayloadGet()).Copy()) == "hello", "a frame has its payload") expect(must(hello.TimestampGet()) == nil, "an untimed frame has no timestamp") // When it arrived: kio's clock's `Instant`, from kio's own export. - expect(must(kio.ClockNow().CheckedDurationSince(must(hello.Arrived()))) != nil, "a frame arrived before now") + expect(kio.ClockNow().CheckedDurationSince(must(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 + // A timestamp is a `Copy` value Go copies, which allocates nothing. The track is // untimed, so a timestamped frame is refused. at := must(net.TimestampFromMillis(1500)) - expect(must(at.AsSecs()) == 1, "1500 ms is 1 s") - err = writer.WriteFrame(at, []byte("late")) + expect(at.AsSecs() == 1, "1500 ms is 1 s") + err = writer.WriteFrame(&at, []byte("late")) expect(errors.As(err, &merr) && err.Error() == "frame timestamp doesn't fit its track", "the late frame's error is Rust's") - expect(must(at.AsSecs()) == 1, "a copied timestamp stays usable") + expect(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. world := append([]byte{0xff}, "world"...) @@ -274,7 +274,8 @@ func script() { // A JSON track the catalog lists, published through moq-mux's generic `json_snapshot` at // a JSON document, and a section Go parses from text, set in the lent catalog and read // back as text. - status := must(catalog.JsonSnapshotConfig(must(publisher.CreateTrack("status", must(net.TrackInfoDefault().WithTimescale(net.TimescaleMILLI())))), mux.JsonConfigDefault(), mux.JsonOptionsDefault())) + milli := net.TimescaleMILLI() + status := must(catalog.JsonSnapshotConfig(must(publisher.CreateTrack("status", must(net.TrackInfoDefault().WithTimescale(&milli)))), mux.JsonConfigDefault(), mux.JsonOptionsDefault())) expect(must(status.Name()) == "status", "a JSON snapshot has its track's name") expect(must(catalog.IsClaimedJsonConfig("status")), "the JSON snapshot claims its name") // A value Go parses is published as the track's latest, which a subscriber reads back diff --git a/ffi/moq/moq.kt b/ffi/moq/moq.kt index 8cac2b723..80537c148 100644 --- a/ffi/moq/moq.kt +++ b/ffi/moq/moq.kt @@ -89,7 +89,7 @@ fun main() { // When it arrived: kio's clock's `Instant`, from kio's own export. expect(clockNow().checkedDurationSince(hello.arrived()) != null, "a frame arrived before now") - // A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is + // A timestamp is a `Copy` value copied across, which allocates nothing. The track is // untimed, so a timestamped frame is refused. val at = Timestamp.fromMillis(1500u) expect(at.asSecs() == 1uL, "1500 ms is 1 s") diff --git a/ffi/moq/moq.py b/ffi/moq/moq.py index d23540460..5162ca2c4 100644 --- a/ffi/moq/moq.py +++ b/ffi/moq/moq.py @@ -83,7 +83,7 @@ def local(loop): # When it arrived: kio's clock's `Instant`, from kio's own export. expect(moq.clock_now().checked_duration_since(hello.arrived()) is not None, "a frame arrived before now") - # A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is + # A timestamp is a `Copy` value copied across, which allocates nothing. The track is # untimed, so a timestamped frame is refused. at = moq.Timestamp.from_millis(1500) expect(at.as_secs() == 1, "1500 ms is 1 s") diff --git a/ffi/moq/moq.swift b/ffi/moq/moq.swift index 532010f9b..782674da3 100644 --- a/ffi/moq/moq.swift +++ b/ffi/moq/moq.swift @@ -245,7 +245,7 @@ func script() throws { // When it arrived: kio's clock's `Instant`, from kio's own export. expect(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 + // A timestamp is a `Copy` value copied across, which allocates nothing. 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") diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index be88ebc4d..9ae207aa6 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1611,10 +1611,11 @@ impl<'tcx> Collector<'tcx> { } /// An object, if the type can move across threads, or if it is also `Copy`, a value C - /// holds as its bytes. Shared borrows of an object that is not `Sync` are exclusive. + /// holds as its bytes, unless C changes it in place, which a copy could not write back. + /// Shared borrows of an object that is not `Sync` are exclusive. fn object(&self, site: &Site<'tcx>) -> Decl { let (tcx, ty, def_id) = (self.tcx, self.site_ty(site), site.key.def_id); - let copy = implements(tcx, ty, def_id, sym::Copy); + let copy = implements(tcx, ty, def_id, sym::Copy) && !self.changed(def_id); if self.orphans.contains(&site.key) { let construct = match copy { true => "value no export returns", @@ -1629,6 +1630,25 @@ impl<'tcx> Collector<'tcx> { } } + /// Whether C may change the type `def_id` in place: through a `pub` field's setter, or + /// an inherent `pub` method taking `&mut self`. + fn changed(&self, def_id: DefId) -> bool { + let tcx = self.tcx; + let fields = matches!(tcx.def_kind(def_id), DefKind::Struct) + && tcx.adt_def(def_id).all_fields().any(|field| field.vis.is_public()); + fields + || (tcx.inherent_impls(def_id).iter()).any(|&imp| { + (tcx.associated_items(imp).in_definition_order()).any(|item| { + item.is_method() + && tcx.visibility(item.def_id).is_public() + && matches!( + tcx.fn_sig(item.def_id).skip_binder().inputs().skip_binder().first().map(|input| input.kind()), + Some(ty::Ref(_, _, mutability)) if mutability.is_mut() + ) + }) + }) + } + /// The objects C can never hold a handle to: no export it can call returns one. fn orphans(&self) -> Vec> { let api = &self.api; @@ -1876,12 +1896,9 @@ impl<'tcx> Collector<'tcx> { e.methods = methods; e.traits = traits; } + // One with no `pub` field, nor a `&mut self` method, as `object` decides. Some(Decl::Value(index)) => { - let (mut methods, traits) = self.methods(&site); - if kind == DefKind::Struct { - methods.splice(0..0, self.accessors(&site, false)); - } - let methods = self.unmutated(methods); + let (methods, traits) = self.methods(&site); let record = &mut self.api.records[index]; record.args = args; record.methods = methods; @@ -1890,7 +1907,7 @@ impl<'tcx> Collector<'tcx> { Some(Decl::Object(index)) => { let (mut methods, traits) = self.methods(&site); if kind == DefKind::Struct { - methods.splice(0..0, self.accessors(&site, true)); + methods.splice(0..0, self.accessors(&site)); } let shared = traits.clone && self.clone_shares(self.site_ty(&site), def_id); let object = &mut self.api.objects[index]; @@ -2008,10 +2025,9 @@ impl<'tcx> Collector<'tcx> { } } - /// A getter for each `pub` field of a struct exported as an object or opaque value, and - /// for an object, with `setters`, a setter: the getter returns a copy, and the setter - /// assigns in place, as `x.field = value` does, which C's copy of a value cannot. - fn accessors(&mut self, site: &Site<'tcx>, setters: bool) -> Vec { + /// A getter and a setter for each `pub` field of a struct exported as an object: the + /// getter returns a copy, and the setter assigns in place, as `x.field = value` does. + fn accessors(&mut self, site: &Site<'tcx>) -> Vec { let tcx = self.tcx; let mut accessors = Vec::new(); let self_ty = self.site_ty(site); @@ -2052,7 +2068,6 @@ impl<'tcx> Collector<'tcx> { // A handle outlives every call, so a reference it holds is `'static`, which // C's borrow for the call is not. let set = match self.ty(ty, Pos::Param) { - _ if !setters => Ok(Type::Unit), Ok(set) if matches!(set, Type::Ref(..)) || lends_inside(&set) => { Err(why("'static borrow", ty.to_string())) } @@ -2096,9 +2111,6 @@ impl<'tcx> Collector<'tcx> { home: Home::default(), }; accessors.push(accessor(Receiver::Ref, Vec::new(), get, false)); - if !setters { - continue; - } let value = Param { name: "value".to_string(), ty: set, diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 0535e13a5..b5a850cc4 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -2230,10 +2230,15 @@ impl<'a> Gen<'a> { } // A function of a type returning it is a constructor, which, unlike a static // member, may share its name with an instance member, as a Rust function may - // with a field. An object's `new` is its unnamed one, which a record's fields - // take. + // with a field. An object's or opaque value's `new` is its unnamed one, which a + // record's fields take. _ if is_static && !function.future && Some(&ok) == owner_ty => { - if snake == "new" && matches!(owner_ty, Some(Type::Object(_))) { + let unnamed = match owner_ty { + Some(Type::Object(_)) => true, + Some(Type::Record(i)) => self.abi.api.records[*i].opaque.is_some(), + _ => false, + }; + if snake == "new" && unnamed { (format!("factory {ret_type}({})", params.join(", ")), "new ".to_string()) } else { ( diff --git a/ffi/src/python.rs b/ffi/src/python.rs index 076d0b7b9..1d2634f95 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -956,6 +956,10 @@ impl<'a> Gen<'a> { let mut body = Body::default(); if record.opaque.is_some() { body.python.push("\t__slots__ = (\"_bytes\",)\n".to_string()); + // Rust makes one without calling it. + body.python.push(format!( + "\tdef __init__(self) -> None:\n\t\traise TypeError(\"only Rust makes a {name}\")\n" + )); } self.traits(&ty, &c, record.traits, &mut body); let owner = Ty::Struct(c.clone()); diff --git a/ffi/tests/api.c b/ffi/tests/api.c index dd3c30a4b..85eaf9ea8 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -148,7 +148,7 @@ static api_Ticket reissue_kept(void *data, api_Ticket ticket) { /* shifted's closure: the moment 500 ticks later. */ static api_Moment moment_after(void *data, api_Moment moment) { (void)data; - return api_Moment_after(moment, 500); + return api_Moment_after(&moment, 500); } static uint32_t spawned_task(void *data) { @@ -858,9 +858,10 @@ static int script(void) { /* A `Copy` value passed by value is copied, so it stays usable, and frees nothing. */ api_Stamp stamp = api_Stamp_from_millis(2500); - printf("secs %llu\n", (unsigned long long)api_Stamp_secs(stamp)); - printf("later %s\n", api_Stamp_later(api_Stamp_from_millis(3000), stamp) ? "true" : "false"); - printf("secs %llu\n", (unsigned long long)api_Stamp_secs(stamp)); + printf("secs %llu\n", (unsigned long long)api_Stamp_secs(&stamp)); + api_Stamp later = api_Stamp_from_millis(3000); + printf("later %s\n", api_Stamp_later(&later, stamp) ? "true" : "false"); + printf("secs %llu\n", (unsigned long long)api_Stamp_secs(&stamp)); api_Message *message = api_Message_new((api_option_Stamp){.some = true, .value = stamp}, str("body")); print("payload ", api_Message_payload_get(message), "\n"); api_option_Stamp read = api_Message_stamp_get(message); @@ -873,7 +874,7 @@ static int script(void) { print("payload ", api_Message_payload_get(message), "\n"); read = api_Message_stamp_get(message); print("stamp ", api_Stamp_debug(&read.value), "\n"); - printf("secs %llu\n", (unsigned long long)api_Stamp_secs(reset)); + printf("secs %llu\n", (unsigned long long)api_Stamp_secs(&reset)); api_Message_stamp_set(message, (api_option_Stamp){0}); printf("stamp %s\n", api_Message_stamp_get(message).some ? "true" : "false"); api_Message_free(message); @@ -884,10 +885,10 @@ static int script(void) { printf("moment %llu %s\n", (unsigned long long)api_Moment_millis(&moment), api_Moment_new(1, 0).some ? "false" : "true"); api_Moment shifted = api_shifted(moment, moment_after, NULL); - api_Moment expected = api_Moment_after(moment, 500); + api_Moment later_moment = api_Moment_after(&moment, 500); printf("shifted %llu %s\n", (unsigned long long)api_Moment_millis(&shifted), - api_Moment_eq(&shifted, &expected) ? "true" : "false"); - api_Moment wrapped = api_Moment_after(moment, UINT64_MAX); + api_Moment_eq(&shifted, &later_moment) ? "true" : "false"); + api_Moment wrapped = api_Moment_after(&moment, UINT64_MAX); printf("wrapped %llu %s\n", (unsigned long long)api_Moment_millis(&wrapped), api_Moment_eq(&moment, &shifted) ? "true" : "false"); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index e7b5411dc..a906431ad 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -1519,7 +1519,7 @@ int lifetimes() { { api::buf payload; { - api::Message message = api::Message::new_(nullptr, "kept"); + api::Message message = api::Message::new_(std::nullopt, "kept"); payload = message.payload_get(); } check(payload.str() == "kept", "bytes outlive the object they came from"); diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 67b5a68a2..0eea25298 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -322,6 +322,13 @@ Future script() async { message.stampSet(null); print('stamp ${message.stampGet != null}'); + // A value past a word, copied across: a closure takes and returns one by value. + final moment = api.Moment.new_(1500, 1000)!; + print('moment ${moment.millis()} ${api.Moment.new_(1, 0) == null}'); + final shifted = api.shifted(moment, (m) => m.after(500)); + print('shifted ${shifted.millis()} ${shifted == moment.after(500)}'); + print('wrapped ${moment.after(-1).millis()} ${moment == shifted}'); + print('kept ${api.emit(PrintLog(), bytes([1, 2, 3, 4]))}'); final logger = api.Logger(PrintLog()); print('write ${logger.write('hi')}'); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 097e75af5..3c7301b6a 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -1353,7 +1353,8 @@ func bytes() { // Bytes an object returns outlive its Close: no Bytes depends on an object. body := strings.Repeat("b", 2048) - message := must(api.NewMessage(api.StampFromMillis(1), []byte(body))) + stamp := api.StampFromMillis(1) + message := api.NewMessage(&stamp, []byte(body)) payload := must(message.PayloadGet()) message.Close() runtime.GC() @@ -1451,11 +1452,9 @@ func broken(which string) { case "nil reader": api.Tail(nil, 1) // An instant Go closed, which Rust cannot copy out. - case "returned closed": - start := api.Now() - api.Drive(context.Background(), start, func(now *std.Instant, _ *api.Waiter) (*std.Instant, error) { - now.Close() - return now, nil + case "returned zero": + api.Drive(context.Background(), api.Now(), func(std.Instant, *api.Waiter) (*std.Instant, error) { + return &std.Instant{}, nil }) default: check(false, "no case "+which) diff --git a/ffi/tests/api.py b/ffi/tests/api.py index ef1befeeb..dbd36d7be 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -318,6 +318,11 @@ def edit(counter): shifted = api.shifted(moment, lambda m: m.after(500)) print(f"shifted {shifted.millis()} {flag(shifted == moment.after(500))}") print(f"wrapped {moment.after(2**64 - 1).millis()} {flag(moment == shifted)}") + try: + api.Moment() + check(False, "Python makes no opaque value") + except TypeError: + pass print(f"kept {api.emit(PrintLog(), bytes([1, 2, 3, 4]))}") logger = api.Logger.new(PrintLog()) diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index a348ea731..3359b8233 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1071,7 +1071,7 @@ fn api_go() { "api_Counter_into_label: a lent handle's value cannot move out", ), ("nil reader", "a nil io.ReadSeeker"), - ("returned closed", "Instant is closed"), + ("returned zero", "a zero Instant, which only Rust makes"), ("family", "an address that is not IPv4 has no Rust SocketAddrV4"), ("outlived", OUTLIVED), ("text", "api_greet: a string is not UTF-8"), From 170ff76c01aa06d69c5769d532fbadcc9fddd1d0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 02:56:00 -0800 Subject: [PATCH 04/24] wip(ffi): Python holds a Copy value's bytes in place, in the runtime's Value Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 5 ++- ffi/moq/bench/export.dart | 1 - ffi/moq/bench/export.kt | 1 - ffi/moq/moq.dart | 1 - ffi/moq/moq.go | 5 ++- ffi/runtime/native.py | 3 ++ ffi/runtime/python.rs | 70 ++++++++++++++++++++++++--------------- ffi/src/python.rs | 11 ++---- 8 files changed, 54 insertions(+), 43 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 75bdce486..293480df3 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -510,9 +510,8 @@ caller imports it with no ignores. 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 `default()`. Constants are module or class attributes, made anew on each read unless a - literal. An **opaque value** is a slotted class holding its bytes as `bytes`, which only - Rust makes or reads: making one raises `TypeError`, and bytes of another length raise - `ValueError` where Rust would read them. + literal. An **opaque value** is a class of the runtime's `Value`, which holds its bytes + in place, so only Rust makes or reads one: making one raises `TypeError`. - **Bytes** passed in are any buffer, `bytes | bytearray | memoryview`: `bytes` is read in place, and others, which Python may change while Rust reads, are copied. Bytes Rust returns, or a record holds, are a read-only `memoryview` lending Rust's buffer until it diff --git a/ffi/moq/bench/export.dart b/ffi/moq/bench/export.dart index 608691785..109c274b9 100644 --- a/ffi/moq/bench/export.dart +++ b/ffi/moq/bench/export.dart @@ -181,7 +181,6 @@ Future> round() async { } out['payload'] = per(start); last!.close(); - at.close(); track.close(); broadcast.close(); return out; diff --git a/ffi/moq/bench/export.kt b/ffi/moq/bench/export.kt index 10fc55edc..81fd6801f 100644 --- a/ffi/moq/bench/export.kt +++ b/ffi/moq/bench/export.kt @@ -151,7 +151,6 @@ fun round(): Map { repeat(N) { sum += read.payloadGet[0] } } out["payload"] = per(start) - at.close() track.close() broadcast.close() sink += sum diff --git a/ffi/moq/moq.dart b/ffi/moq/moq.dart index d50a002e7..621588ed7 100644 --- a/ffi/moq/moq.dart +++ b/ffi/moq/moq.dart @@ -57,7 +57,6 @@ Future drive(moq.OriginDriver driver) { final next = driver.poll(moq.clockNow(), waiter); if (next != null) { deadline = Timer(next.saturatingDurationSince(moq.clockNow()), poll); - next.close(); } } on moq.Error catch (e) { waiter.close(); diff --git a/ffi/moq/moq.go b/ffi/moq/moq.go index 9f03d6efc..45c7b0f5e 100644 --- a/ffi/moq/moq.go +++ b/ffi/moq/moq.go @@ -114,8 +114,7 @@ func drive(driver *net.OriginDriver) <-chan error { } var deadline <-chan time.Time if next != nil { - deadline = time.After(must(next.SaturatingDurationSince(kio.ClockNow()))) - next.Close() + deadline = time.After(next.SaturatingDurationSince(kio.ClockNow())) } select { case <-woken: @@ -252,7 +251,7 @@ func script() { }) ranges := []net.TrackRange{net.TrackRangeLive(), groups} ranged := must(must(net.TrackSubscriptionDefault().WithRanges(ranges)).RangesGet()) - expect(slices.EqualFunc(ranged, ranges, func(a, b net.TrackRange) bool { return must(a.Equal(b)) }), "a subscription keeps its ranges") + expect(slices.EqualFunc(ranged, ranges, func(a, b net.TrackRange) bool { return a.Equal(b) }), "a subscription keeps its ranges") expect(groups.Start == net.TrackAnchorAbsolute{V0: 2}, "groups start at their first") expect(groups.End != nil && *groups.End == net.TrackAnchorAbsolute{V0: 6}, "groups end before their end") diff --git a/ffi/runtime/native.py b/ffi/runtime/native.py index 019e708c3..4194d37fe 100644 --- a/ffi/runtime/native.py +++ b/ffi/runtime/native.py @@ -9,6 +9,9 @@ class _n: class Object: __slots__ = () + class Value: + __slots__ = () + class Future: __slots__ = () diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 80ede112d..85698689b 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -818,35 +818,27 @@ pub fn array_out(items: &[u8], index: usize) -> R { call_py(ty(index), vec![bytes]) } -/// An opaque value's C struct, from the `bytes` of exactly its size its class holds. -pub fn opaque_in(obj: *mut PyObject) -> R { - // SAFETY: the GIL is held, and `obj` is live; `T` is a struct of `MaybeUninit` units, - // any bytes of which are one. - unsafe { - if PyBytes_Check(obj) == 0 { - return expected_named(obj, "bytes"); - } - let mut ptr: *mut c_char = null_mut(); - let mut len = 0; - if PyBytes_AsStringAndSize(obj, &mut ptr, &mut len) != 0 { - return Err(Raised); - } - if len as usize != size_of::() { - return value_error(&format!("{len} bytes, not {}", size_of::())); - } - Ok(ptr.cast::().read_unaligned()) +/// The `Copy` value a `Value` of the class registered at `index` holds, which only Rust +/// made: Python makes none, as `Value` disallows it. +pub fn value_in(obj: *mut PyObject, index: usize) -> R { + if !is(obj, index) { + return expected(obj, index); } + // SAFETY: the GIL is held, and `obj` a `Value` `value_out` made of a `T`, which its + // class always holds; `T` is a struct of `MaybeUninit` units. + unsafe { Ok(obj.cast::().add(size_of::()).cast::().read_unaligned()) } } -/// The `bytes` an opaque value's class holds of its C struct. Its padding crosses as the -/// bytes it holds, which Python never reads but to hand back. -pub fn opaque_out(value: T) -> R { - // SAFETY: the GIL is held, and `value` is `size_of::()` bytes. +/// A new `Value` of the class registered at `index`, holding `value` in place. +pub fn value_out(index: usize, value: T) -> R { + let class = ty(index); + // SAFETY: the GIL is held, `class` subclasses `Value`, whose items are its bytes, and + // the object is `size_of::()` of them past its header. unsafe { - Obj::new(PyBytes_FromStringAndSize( - (&raw const value).cast(), - size_of::() as Py_ssize_t, - )) + let alloc: allocfunc = std::mem::transmute(PyType_GetSlot(class.cast(), Py_tp_alloc)); + let obj = Obj::new(alloc(class.cast(), size_of::() as Py_ssize_t))?; + obj.ptr().cast::().add(size_of::()).cast::().write_unaligned(value); + Ok(obj) } } @@ -2382,6 +2374,7 @@ fn is_type(obj: *mut PyObject, slot: &std::sync::OnceLock) -> bool { } static OBJECT: std::sync::OnceLock = std::sync::OnceLock::new(); +static VALUE_BASE: std::sync::OnceLock = std::sync::OnceLock::new(); static FUTURE: std::sync::OnceLock = std::sync::OnceLock::new(); static QUEUE: std::sync::OnceLock = std::sync::OnceLock::new(); static NODE: std::sync::OnceLock = std::sync::OnceLock::new(); @@ -2505,6 +2498,23 @@ unsafe fn add_type( size: usize, flags: u64, slots: &mut [PyType_Slot], +) -> R<()> { + // SAFETY: the caller's contract. + unsafe { add_sized(module, cell, name, size, 0, flags, slots) } +} + +/// [`add_type`], of objects each `itemsize` bytes longer per item. +/// +/// # Safety +/// As [`add_type`]'s. +unsafe fn add_sized( + module: *mut PyObject, + cell: &std::sync::OnceLock, + name: &CStr, + size: usize, + itemsize: usize, + flags: u64, + slots: &mut [PyType_Slot], ) -> R<()> { let qualified = format!("{}.{}\0", module_name(module), name.to_str().expect("ASCII")); // Python keeps the name for the type's life. @@ -2512,7 +2522,7 @@ unsafe fn add_type( let mut spec = PyType_Spec { name: qualified.as_ptr().cast(), basicsize: size as c_int, - itemsize: 0, + itemsize: itemsize as c_int, flags: (Py_TPFLAGS_DEFAULT as u64 | flags) as _, slots: slots.as_mut_ptr(), }; @@ -2569,6 +2579,14 @@ unsafe fn runtime(module: *mut PyObject) -> R<()> { ), slot(0, null_mut()), ])?; + // Its items are a value's bytes, past the header. + add_sized(module, &VALUE_BASE, c"Value", size_of::(), 1, base, &mut [ + slot( + Py_tp_doc, + c"A Rust `Copy` value's bytes, which only Rust makes or reads.".as_ptr().cast_mut().cast(), + ), + slot(0, null_mut()), + ])?; add_type(module, &FUTURE, c"Future", size_of::(), base, &mut [ slot(Py_tp_dealloc, future_dealloc as *mut c_void), slot(Py_tp_methods, FUTURE_METHODS.ptr().cast()), diff --git a/ffi/src/python.rs b/ffi/src/python.rs index 1d2634f95..1e6e3462e 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -955,11 +955,7 @@ impl<'a> Gen<'a> { let ty = Type::Record(i); let mut body = Body::default(); if record.opaque.is_some() { - body.python.push("\t__slots__ = (\"_bytes\",)\n".to_string()); - // Rust makes one without calling it. - body.python.push(format!( - "\tdef __init__(self) -> None:\n\t\traise TypeError(\"only Rust makes a {name}\")\n" - )); + body.python.push("\t__slots__ = ()\n".to_string()); } self.traits(&ty, &c, record.traits, &mut body); let owner = Ty::Struct(c.clone()); @@ -972,14 +968,13 @@ impl<'a> Gen<'a> { ); let _ = write!( self.py, - "class {name}:\n{}{}\n\n", + "class {name}(_n.Value):\n{}{}\n\n", docstring(text.trim(), "\t"), body.members().join("\n") ); - let key = self.key("_bytes"); let _ = write!( self.glue, - "fn r_{c}(o: *mut PyObject, s: &mut rt::Scope) -> R {{\n\trt::opaque_in(rt::field(o, {t}, {key}, s)?)\n}}\n\nfn w_{c}(v: crate::{c}) -> R {{\n\trt::record({t}, vec![({key}, rt::opaque_out(v))])\n}}\n\n" + "fn r_{c}(o: *mut PyObject, _s: &mut rt::Scope) -> R {{\n\trt::value_in(o, {t})\n}}\n\nfn w_{c}(v: crate::{c}) -> R {{\n\trt::value_out({t}, v)\n}}\n\n" ); return; } From 7766768fb30ab4b570d25d6d9eaf403702507781 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 03:07:04 -0800 Subject: [PATCH 05/24] feat(ffi): the API lock catches a Copy value's layout change An opaque value's size and alignment are part of the ABI, so the lock now compares them and reports the new bytes. Drops the abi parameter clippy found only recursed. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/lock/expected.txt | 1 + ffi/fixtures/lock/src/lib.rs | 27 +++++++++++++++++++++++++++ ffi/src/collect.rs | 28 ++++++++++++++++++++-------- ffi/src/cpp.rs | 18 ++++++++---------- ffi/src/dart.rs | 10 +++++----- ffi/src/glue.rs | 5 +---- ffi/src/go.rs | 15 +++++++++------ ffi/src/kotlin.rs | 20 +++++++++++++------- ffi/src/lock.rs | 9 +++++++++ ffi/src/swift.rs | 8 ++++---- 10 files changed, 97 insertions(+), 44 deletions(-) diff --git a/ffi/fixtures/lock/expected.txt b/ffi/fixtures/lock/expected.txt index 433c9a80e..cf7d9d9f6 100644 --- a/ffi/fixtures/lock/expected.txt +++ b/ffi/fixtures/lock/expected.txt @@ -5,6 +5,7 @@ fixtures/lock/src/lib.rs:135:1: rust2-ffi: the API breaks the baseline at enum ` fixtures/lock/src/lib.rs:176:1: rust2-ffi: the API breaks the baseline at const `lock::DEPTH`: its type: it was `u32`, and is now `u64` fixtures/lock/src/lib.rs:182:1: rust2-ffi: the API breaks the baseline at const `lock::PAGES`: its type: it was `u16`, and is now `i16` fixtures/lock/src/lib.rs:191:1: rust2-ffi: the API breaks the baseline at tuple `((u32,), u64, u8)`: its C name `lock_tuple_tuple_u32_u64_u8` is now `lock_tuple_tuple_u32_u64_u8_`, and `lock_tuple_tuple_u32_u64_u8` names tuple `((u32, u64), u8)` +fixtures/lock/src/lib.rs:202:1: rust2-ffi: the API breaks the baseline at struct `lock::Tick`: its bytes: 4 aligned to 4, and now 8 aligned to 4 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:30:1: rust2-ffi: the API breaks the baseline at fn `lock::removed`: it is removed 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 diff --git a/ffi/fixtures/lock/src/lib.rs b/ffi/fixtures/lock/src/lib.rs index beb7104af..9d5f083d1 100644 --- a/ffi/fixtures/lock/src/lib.rs +++ b/ffi/fixtures/lock/src/lib.rs @@ -196,3 +196,30 @@ pub fn nested(x: ((u32,), u64, u8)) -> u32 { pub fn flat(x: ((u32, u64), u8)) -> u32 { x.0.0 } + +/// A `Copy` value C holds as its bytes, which `next` lengthens. +#[derive(Clone, Copy)] +pub struct Tick { + at: u32, + #[cfg(feature = "next")] + late: u32, +} + +impl Tick { + pub fn new(at: u32) -> Self { + Self { + at, + #[cfg(feature = "next")] + late: 0, + } + } + + pub fn at(&self) -> u32 { + self.at + } + + #[cfg(feature = "next")] + pub fn late(&self) -> u32 { + self.late + } +} diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 28ec92688..54899964d 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -844,7 +844,13 @@ impl<'tcx> Collector<'tcx> { /// Whether the foreign type `did` is a value C holds as its bytes, as a `Copy` one is. fn foreign_value(&self, did: DefId) -> bool { let tcx = self.tcx; - !self.opaque(did) && implements(tcx, tcx.type_of(did).instantiate_identity().skip_norm_wip(), did, sym::Copy) + !self.opaque(did) + && implements( + tcx, + tcx.type_of(did).instantiate_identity().skip_norm_wip(), + did, + sym::Copy, + ) } /// Each type a signature the export maps names, bounds included, at every choice @@ -2033,10 +2039,12 @@ impl<'tcx> Collector<'tcx> { let used = api.functions.iter().any(signature) || (api.records.iter().enumerate()).any(|(i, r)| { (!value || i != gone) && (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)| (value || i != gone) && (args(&o.args) || o.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)| (value || i != gone) && (args(&o.args) || o.methods.iter().any(signature))) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if used { continue; @@ -2047,7 +2055,9 @@ impl<'tcx> Collector<'tcx> { false => drop(self.api.objects.remove(gone)), } self.foreign.remove(n); - let values: Vec = (self.foreign[n..].iter()).map(|&(did, _)| self.foreign_value(did)).collect(); + let values: Vec = (self.foreign[n..].iter()) + .map(|&(did, _)| self.foreign_value(did)) + .collect(); for (later, later_value) in self.foreign[n..].iter_mut().zip(values) { if later_value == value { later.1 -= 1; @@ -5059,8 +5069,10 @@ fn opaque<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, lifetimes: usize, foreign: bool }; let mut covered = 0; for field in adt.all_fields() { - let Ok(field) = tcx.try_normalize_erasing_regions(env, ty::Unnormalized::new_wip(field.ty(tcx, args).skip_norm_wip())) - else { + let Ok(field) = tcx.try_normalize_erasing_regions( + env, + ty::Unnormalized::new_wip(field.ty(tcx, args).skip_norm_wip()), + ) else { return false; }; let Ok(layout) = tcx.layout_of(env.as_query_input(field)) else { diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 04894fec5..7001f673c 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -147,14 +147,14 @@ impl<'a> Header<'a> { match ty { Type::Option(inner) if Abi::is_buf(inner) => format!("::std::optional<::{}::view>", self.ns), // Moved out of, each handle as a handle taken by value is. - Type::Option(inner) if matches!(inner.base(), Type::List(_, elem) if moves(abi, elem)) => { + Type::Option(inner) if matches!(inner.base(), Type::List(_, elem) if moves(elem)) => { format!("::std::optional<{}>", self.items(inner.base())) } Type::Option(inner) if matches!(inner.base(), Type::List(..)) => { format!("const ::std::optional<{}> &", self.items(inner.base())) } // Moved out of, each handle as a handle taken by value is. - Type::List(_, inner) if moves(abi, inner) => self.items(ty), + Type::List(_, inner) if moves(inner) => self.items(ty), Type::List(..) => format!("const {} &", self.items(ty)), Type::Option(inner) => match &**inner { // Lent, or copied out: a pointer, null for none. @@ -166,7 +166,7 @@ impl<'a> Header<'a> { format!("{constant}{} *", self.owned(object)) } // Moved out of, as a handle taken by value is. - _ if moves(abi, inner) => format!("::std::optional<{}>", self.held(inner)), + _ if moves(inner) => format!("::std::optional<{}>", self.held(inner)), _ => format!("const ::std::optional<{}> &", self.held(inner)), }, Type::Waker => format!("const ::{}::waker &", self.ns), @@ -180,7 +180,7 @@ impl<'a> Header<'a> { } Type::Enum(i) if !abi.api.enums[*i].plain() => format!("const {} &", self.owned(ty)), // Moved out of, each handle as a handle taken by value is. - Type::Tuple(_) if moves(abi, ty) => self.held(ty), + Type::Tuple(_) if moves(ty) => self.held(ty), Type::Tuple(_) => format!("const {} &", self.held(ty)), ty => self.owned(ty), } @@ -1149,9 +1149,7 @@ impl<'a> Header<'a> { let (_, ty) = c_params.next().expect("a method has its receiver"); match (ty, free) { // Moved out of, as a handle taken by value is. - (Ty::Handle { .. }, _) if receiver == Some(Receiver::Owned) => { - args.push("raw_.own()".to_string()) - } + (Ty::Handle { .. }, _) if receiver == Some(Receiver::Owned) => args.push("raw_.own()".to_string()), (Ty::Handle { .. }, _) => args.push("raw_.get()".to_string()), (_, false) => { let ty = record(abi, owner_c.as_deref().expect("a member method has its record")); @@ -1552,11 +1550,11 @@ fn record(abi: &Abi<'_>, c: &str) -> Type { /// Whether passing `ty` moves a handle out of a `Vec`, `Option`, or tuple, as a handle /// taken by value is. -fn moves(abi: &Abi<'_>, ty: &Type) -> bool { +fn moves(ty: &Type) -> bool { match ty { Type::Object(_) => true, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moves(abi, inner), - Type::Tuple(elements) => elements.iter().any(|e| moves(abi, e)), + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moves(inner), + Type::Tuple(elements) => elements.iter().any(moves), _ => false, } } diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index df37573e3..2a91235c7 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -1898,7 +1898,7 @@ impl<'a> Gen<'a> { Type::Box(inner) => self.close(inner, e), Type::Tuple(elements) => { let closes: Vec = (elements.iter().enumerate()) - .filter(|(_, ty)| moved(self.abi, ty)) + .filter(|(_, ty)| moved(ty)) .map(|(k, ty)| self.close(ty, &format!("{e}.${}", k + 1))) .collect(); closes.join(" ") @@ -2012,7 +2012,7 @@ impl<'a> Gen<'a> { continue; } params.push(format!("{} {name}", self.dt(¶m.ty))); - if moved(abi, ¶m.ty) { + if moved(¶m.ty) { after.push(self.close(¶m.ty, &name)); } match (¶m.ty, c) { @@ -2354,11 +2354,11 @@ 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, and never through a reference. -fn moved(abi: &Abi<'_>, ty: &Type) -> bool { +fn moved(ty: &Type) -> bool { match ty { Type::Object(_) => true, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), - Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(inner), + Type::Tuple(elements) => elements.iter().any(moved), _ => false, } } diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index cd7838f52..ad771a7be 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -579,10 +579,7 @@ impl Gen<'_> { let mut call = format!("unsafe {{ f({}) }}", args.join(", ")); for (n, ty) in closure.params.iter().enumerate().rev() { if let Some((i, lend)) = lent(ty) { - call = format!( - "rt::Object::<{}>::{lend}(c{n}, w, |h{n}| {call})", - self.object_path(i) - ); + call = format!("rt::Object::<{}>::{lend}(c{n}, w, |h{n}| {call})", self.object_path(i)); } } let ret = self.rust(&closure.ok); diff --git a/ffi/src/go.rs b/ffi/src/go.rs index acfc4f30e..b512b6a62 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -1573,7 +1573,10 @@ impl<'a> Gen<'a> { let ty = Type::Record(i); let name = self.qualified(&ty); if let Some(opaque) = &record.opaque { - let (local, bytes) = (self.named(&ty).1, format!("[{}]uint{}", opaque.units(), opaque.unit() * 8)); + let (local, bytes) = ( + self.named(&ty).1, + format!("[{}]uint{}", opaque.units(), opaque.unit() * 8), + ); return format!( "func lift_{c}(c C.{c}) {name} {{\n\treturn {name}{{Bytes_: *(*{bytes})(unsafe.Pointer(&c)), Made_: true}}\n}}\n\n\ func lower_{c}(v {name}) C.{c} {{\n\tif !v.Made_ {{\n\t\tpanic(\"a zero {local}, which only Rust makes\")\n\t}}\n\treturn *(*C.{c})(unsafe.Pointer(&v.Bytes_))\n}}\n\n" @@ -2108,7 +2111,7 @@ impl<'a> Gen<'a> { 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)) + .filter(|(_, ty)| moved(ty)) .map(|(k, ty)| self.close(ty, &format!("{e}.V{k}"), depth)) .collect(); closes.join("; ") @@ -2263,7 +2266,7 @@ impl<'a> Gen<'a> { continue; } params.push((name.clone(), self.go_param(¶m.ty))); - if moved(abi, ¶m.ty) { + if moved(¶m.ty) { after.push(self.close(¶m.ty, &name, 0)); } match (¶m.ty, c) { @@ -2574,11 +2577,11 @@ fn poll_error(abi: &Abi<'_>) -> String { /// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, /// a list, or a tuple, and never through a reference. -fn moved(abi: &Abi<'_>, ty: &Type) -> bool { +fn moved(ty: &Type) -> bool { match ty { Type::Object(_) => true, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), - Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(inner), + Type::Tuple(elements) => elements.iter().any(moved), _ => false, } } diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 8780b457c..0809dc10a 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -751,7 +751,7 @@ impl<'a> Gen<'a> { false => format!("{e}.handle.hold {{ {h} ->"), }); // Taken by value, the value moves out, leaving the handle only to free. - if moved(abi, ty) { + if moved(ty) { arg.after.push(match option { true => format!("{e}?.close()"), false => format!("{e}.close()"), @@ -788,7 +788,7 @@ 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) { + if moved(ty) { let close = self.close(ty, e); arg.after.push(close); } @@ -1020,7 +1020,13 @@ impl<'a> Gen<'a> { ), (false, Some(opaque)) => { let (jni, native) = jni_type(self.abi, &Ty::Struct(c.to_string()), ty); - (jni, native, "run {".to_string(), holder(&opaque).to_string(), format!("&{}", from_kotlin(&opaque, "p0"))) + ( + jni, + native, + "run {".to_string(), + holder(&opaque).to_string(), + format!("&{}", from_kotlin(&opaque, "p0")), + ) } (false, None) => { let write = self.write_value(ty, "w", "this"); @@ -2171,7 +2177,7 @@ impl<'a> Gen<'a> { Type::Tuple(elements) => { let t = self.fresh("t"); let closes: Vec = (elements.iter().enumerate()) - .filter(|(_, ty)| moved(self.abi, ty)) + .filter(|(_, ty)| moved(ty)) .map(|(k, ty)| self.close(ty, &format!("{t}.{}", ordinal(k)))) .collect(); format!("{e}.let {{ {t} -> {} }}", closes.join("; ")) @@ -2299,11 +2305,11 @@ fn bytes(ty: &Type) -> bool { /// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, /// and never through a reference. -fn moved(abi: &Abi<'_>, ty: &Type) -> bool { +fn moved(ty: &Type) -> bool { match ty { Type::Object(_) => true, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), - Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(inner), + Type::Tuple(elements) => elements.iter().any(moved), _ => false, } } diff --git a/ffi/src/lock.rs b/ffi/src/lock.rs index 0d2f62c36..a139022b1 100644 --- a/ffi/src/lock.rs +++ b/ffi/src/lock.rs @@ -369,6 +369,15 @@ fn changed(base: &Index<'_>, now: &Index<'_>, maps: &Maps, ours: &Item<'_>, thei match (*ours, *theirs) { (Item::Record(i), Item::Record(j)) => { traits(b.records[i].traits, n.records[j].traits); + // An opaque value's bytes are its C struct, which every binding sizes. + if let (Some(o), Some(p)) = (b.records[i].opaque, n.records[j].opaque) + && (o.size, o.align) != (p.size, p.align) + { + changes.push(format!( + "its bytes: {} aligned to {}, and now {} aligned to {}", + o.size, o.align, p.size, p.align + )); + } let mut ours = merge::bare_record(&b.records[i]); maps.fields(&mut ours.fields); maps.types(&mut ours.args); diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 50f3561a2..4a2e5fa9d 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -930,7 +930,9 @@ impl<'a> Gen<'a> { self.traits(&ty, &c, record.traits, &mut body, &mut members, &mut protocols); self.methods(&record.methods, &Ty::Struct(c.clone()), &ty, &mut body, &mut members); self.distinct(&rust, &members); - body.push(format!("\tstatic func _lift(_ c: {c}) -> {name} {{\n\t\t{name}(c_: c)\n\t}}\n")); + body.push(format!( + "\tstatic func _lift(_ c: {c}) -> {name} {{\n\t\t{name}(c_: c)\n\t}}\n" + )); body.push(format!("\tfunc _lower() -> {c} {{\n\t\tc_\n\t}}\n")); let text = format!("{rust}\n\n{}", record.docs); let _ = write!( @@ -1862,9 +1864,7 @@ fn structs(abi: &Abi<'_>) -> BTreeSet { _ => None, }) .collect(); - (0..api.objects.len()) - .filter(|i| !thrown.contains(i)) - .collect() + (0..api.objects.len()).filter(|i| !thrown.contains(i)).collect() } /// `ty` returned: a borrow is returned as a clone, so owned. From e11059ec9cd2639fb885389c50a62ca81658f17c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 03:19:05 -0800 Subject: [PATCH 06/24] docs(ffi): measure Copy values, and leave the quest what remains The stamped write now costs C, C++, and Swift nothing over the write, and Python 40 ns less than a handle did on the same host. Copy types that change keep their handles, now the quest's goal, and Kotlin's LongArray becomes a speed follow-up. Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 35 ++++++++++--- quest/m1/ffi-export/checks-raise.md | 1 - quest/m1/ffi-export/copy-values.md | 71 +++++++++++--------------- quest/m1/ffi-export/numeric-slices.md | 4 +- quest/m1/ffi-export/ship.md | 2 +- quest/m1/ffi-speed/kotlin-values.md | 23 +++++++++ quest/m1/ffi-speed/object-ownership.md | 2 - 7 files changed, 85 insertions(+), 53 deletions(-) create mode 100644 quest/m1/ffi-speed/kotlin-values.md diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 957020807..13cb86a20 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -23,7 +23,7 @@ the run's summary; nothing fails on a number. | u64 getter | `demand.sequence()`: one borrowed handle and a field read, so all but 0.4 ns is the binding's | `group.sequence()` | | record return | `track.largest()`: an `Option` of two `u64`s, copied across | `track.info()`, a record | | object return | `track.info()`: a new handle, freed | none | -| write | `group.write_frame(at, frame)` with one `Timestamp` handle and 100 B, copied into Rust | `write_frame` | +| write | `group.write_frame(at, frame)` with one `Timestamp` and 100 B, copied into Rust | `write_frame` | | stamped write | the same with a new `Timestamp` per frame, as a caller holding a `u64` must | none | | ready read | a 100 B frame written, then read through a new future ready at its first poll | `await noop_async()`, plus a write | | woken read | a read started before its frame is written, as the language starts work concurrently, so it suspends and the write's wake resumes it | `await yield_once()`, plus a write | @@ -132,10 +132,12 @@ the export's default `release` build, which links all of moq's crates; "stripped From the CPU tables, read against the code each call runs. In order of what it costs moq's frame path: -1. **A handle per frame timestamp.** `Timestamp` is `Copy` in Rust, but its private fields - make it an object, so each stamped frame makes and frees a handle: 21 ns in C, 27 in - Swift, 140 in Python, 250 in Kotlin, 260 in Go, and 245 in Dart (stamped write minus - write). In Go, Dart, and Kotlin that doubles the write. +1. **A call per frame timestamp.** `Timestamp` is `Copy` with private fields, so it + crosses as its 16 bytes, and a stamped frame allocates and frees nothing in Rust. As + a handle, it cost 21 ns in C, 27 in Swift, 140 in Python, 250 in Kotlin, 260 in Go, + and 245 in Dart (stamped write minus write, in the tables above); as a value, see + [Copy values](#copy-values): what remains is each language's call and, in Kotlin and + Python, an object of its own. 2. **Ownership of a returned object.** An object return costs C 25 ns, but Go 263 and Dart 242, ten times C: each new Go object registers `runtime.AddCleanup` and each Dart one a `NativeFinalizer`, beside a handle allocation. Kotlin's 137 ns wall (210 CPU) is a phantom @@ -169,12 +171,33 @@ adding two more. Kotlin: 24 wall, Go 41, Dart 29, Python 47, each with its call' 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 +a class: one thrown as an error. A `Copy` type is a value since [Copy values](#copy-values). 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. +### Copy values + +A `Copy` type with private fields, such as `Timestamp`, crosses by value: a struct of its +bytes in C, C++, Swift, Go, and Dart, a class over a `LongArray` in Kotlin (a word value +class when it fits one), and a class of the runtime's `Value` holding its bytes in Python. +Measured 2026-11-13 at rust2 `7766768f`, moq `ffcb7cd9`, on an AMD Ryzen 9 9950X (32 threads), +Linux, at a load average of 13 to 15, the median of 3 rounds, in CPU time: + +| | C | C++ | Swift | Go | Dart | Kotlin | Python | +|---|---|---|---|---|---|---|---| +| write | 163 | 165 | 173 | 260 | 251 | 280 | 224 | +| stamped write | 161 | 168 | 174 | 293 | 296 | 430 | 384 | +| stamped minus write | 0 | 3 | 1 | 33 | 45 | 150 (35 wall) | 160 | + +C, C++, and Swift pay nothing for the timestamp; Go and Dart pay about their getter's call, +the `from_micros` itself. Kotlin's 35 ns wall is its call, and its CPU column adds the +collector's work on each `LongArray`. Python's is a call and a new object. The handle's cost +in the tables above came from another load, so Python's was also measured against main's +build on the same host, interleaved over 5 rounds at a load of 10 to 12: stamped minus write +fell from 205 ns to 166, and the stamped write from 441 to 391. + ### Against the targets Where the cases match, on wall time: Kotlin's getter costs 24 ns against BoltFFI's 5.8, diff --git a/quest/m1/ffi-export/checks-raise.md b/quest/m1/ffi-export/checks-raise.md index d54ac3930..c71fb7144 100644 --- a/quest/m1/ffi-export/checks-raise.md +++ b/quest/m1/ffi-export/checks-raise.md @@ -58,5 +58,4 @@ error types the runtimes add, and the status the C functions return. ## 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 - [Cheaper returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) - rewrites the same handle word, so its baselines move with this diff --git a/quest/m1/ffi-export/copy-values.md b/quest/m1/ffi-export/copy-values.md index 04171b5f1..e45c06138 100644 --- a/quest/m1/ffi-export/copy-values.md +++ b/quest/m1/ffi-export/copy-values.md @@ -1,50 +1,39 @@ -# [L] Copy types cross by value +# [M] Copy types that change cross by value ## Goal -A [rust2-ffi](/ffi/README.md) export of a type that is `Copy` and has private fields, such -as moq's `Timestamp`, crosses as an opaque value copied across the C ABI instead of an -`Arc` handle, in every generator. Making and passing one allocates and frees nothing on -either side. [The binding benchmark](/bench/ffi/README.md)'s stamped write then costs -within a few ns of its plain write in each language, against 140 to 256 ns of CPU time -more today in Kotlin, Python, Go, and Dart. +A [rust2-ffi](/ffi/README.md) export's `Copy` type with a `pub` field or a `pub` method +taking `&mut self` crosses as an opaque value, as every other `Copy` type with private +fields does since [#742](https://github.com/kixel-ai/rust2/pull/742), instead of an `Arc` +handle. A setter or `&mut self` method writes the new bytes back into the language's own +copy, so `x.add(other)` changes `x` and only `x`, as in Rust. moq has five today, each +still a handle: `stats::Traffic`, `stats::Presence`, `stats::Failures`, and +`stats::Refusals` (`add`), and `clock::WallMap` (`sample`). ## Plan -- The C ABI emits a fixed-size, aligned struct of opaque bytes with the Rust type's size - and alignment, read and written only by Rust glue; the language sees no fields, so - privacy holds. Recommended over a `#[repr(C)]` mirror of the fields, which would expose - them and break on a private layout change. -- Each language holds it as its own value type: a C struct, a C++ trivially copyable - class, a Swift struct, a Go struct, a Kotlin value class, a Python object holding bytes, - and a Dart `Struct`. Methods on it call Rust with the bytes. -- A type whose size changes is an ABI change of the export, as a field change already is. -- Prove it with the api fixture's differential tests and re-measure the bench. - -Decided with the maintainer on 2026-11-12, in the quest audit, for a size that differs by -target: since [#700](https://github.com/kixel-ai/rust2/pull/700) one binding serves every -target, moq ships 32-bit `armv7-linux-androideabi` beside 64-bit ones, and Kotlin, Python, -and Dart share one source across them. - -- ✅ The merge picks the representation from the type's size on every target. At most 8 - bytes on all of them: an inline word, such as a Kotlin value class over a `Long`, with no - allocation. Otherwise: bytes sized by the library when it loads. Rejected: always sized - at load, which allocates a small array per value in Kotlin where a `Long` is free; - refusing a type whose layout differs between targets, which would make moq change the - type, against "rust2 asks moq for no source changes". -- The IR carries no type's layout today: `ffi/src/collect.rs` reads a scalar's size and - the target's pointer width, and nothing records a struct's size or alignment. The - staged API (`--api`) gains each such type's size and alignment per target, and the merge - reads them. - -What this replaces, from [#710](https://github.com/kixel-ai/rust2/pull/710): the C ABI's -`_copy`, the IR's `Object.copy`, and Swift's class for a `Copy` object. - -Public API: how every generated API holds a `Copy` type (breaking), and the staged API's -layout fields. +- C takes `T *` for `&mut self` and writes the value back in place; C++ makes the method + non-`const`; Swift makes it `mutating`; Go takes a pointer receiver. Each writes only + the caller's copy. +- A `pub` field gets a getter and a setter over the bytes, through Rust, as an object's + field does; the bytes stay opaque. +- Prove it with the api fixture's differential tests: a `Copy` type with a `pub` field and + a `&mut self` method, mutated through a copy and through the original, and the copy left + as it was. +- Then `ffi/src/collect.rs`'s `changed`, which keeps these types objects today, goes. + +Public API: how every generated API holds these types (breaking). + +## Waiting + +- Maintainer: how Kotlin, Dart, and Python, whose values are immutable, write a + `&mut self` method's result back. Recommended: the method returns the new value, + `x = x.add(o)`, with Rust's own return, if any, as a pair, since Python's `Value` and + Kotlin's class over words are shared by reference, and mutating one in place would + change every alias, unlike a Rust copy. Rejected: keeping the type a handle in these + three only, which splits one Rust type across languages. Done when the maintainer + picks, recorded here. ## 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 +- [Kotlin values without a LongArray](/quest/m1/ffi-speed/kotlin-values.md) - how Kotlin holds the bytes this writes back diff --git a/quest/m1/ffi-export/numeric-slices.md b/quest/m1/ffi-export/numeric-slices.md index d343f3696..2c93a7def 100644 --- a/quest/m1/ffi-export/numeric-slices.md +++ b/quest/m1/ffi-export/numeric-slices.md @@ -23,8 +23,8 @@ The api fixture has no numeric slice parameter, so the path is untested. unsigned items, and a Dart typed list whose element width matches. Where a language has none that is exact, such as a 128-bit item, the list stays as it is. - `usize` and `isize` items differ in width by target, so they stay item by item unless - [copy-values](/quest/m1/ffi-export/copy-values.md)'s per-target layout makes one form - exact on every target. + a per-target layout, as the staged API records for a `Copy` value's size, makes one + form exact on every target. - Add numeric slice and vector parameters, returns, and fields to the api fixture, with each type's edge values, and a bench case for a slice of 1,000 `u32`s in and out. diff --git a/quest/m1/ffi-export/ship.md b/quest/m1/ffi-export/ship.md index fa4c84e41..6c6f9fd06 100644 --- a/quest/m1/ffi-export/ship.md +++ b/quest/m1/ffi-export/ship.md @@ -22,7 +22,7 @@ then see change. A sibling planned later adds itself to `Required` here. - [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 +- [Copy types that change cross by value](/quest/m1/ffi-export/copy-values.md) - how moq's stats and `WallMap` are held in every language - [Not Send](/quest/m1/ffi-export/not-send.md) - the types and futures that export checked by thread - [Export moq for every target](/quest/m1/ffi-export/moq-targets.md) - one binding across the targets moq ships - [Fused async start](/quest/m1/ffi-export/async-start.md) - every future's C signature diff --git a/quest/m1/ffi-speed/kotlin-values.md b/quest/m1/ffi-speed/kotlin-values.md new file mode 100644 index 000000000..12ca762a8 --- /dev/null +++ b/quest/m1/ffi-speed/kotlin-values.md @@ -0,0 +1,23 @@ +# [S] Kotlin values without a LongArray + +## Goal + +A [rust2-ffi](/ffi/README.md) Kotlin binding holds a `Copy` value over 8 bytes, such as +moq's 16-byte `Timestamp`, in one object with no array beside it. Today it is a class +over a `LongArray`, two allocations per value, so +[the binding benchmark](/bench/ffi/README.md)'s stamped write costs 150 ns of CPU more +than the plain write in Kotlin (35 wall), against Swift's 1. The stamped write then costs +within its `from_micros` call of the plain write, as Go's and Dart's do. + +## Plan + +- One `Long` field per 8-byte word in the value's class, passed to and from JNI as that + many `jlong` arguments and a returned struct's words written into the class's fields, + so HotSpot's escape analysis can drop a value that does not escape. +- `equals` and `hashCode` keep today's rules, so only the storage changes. +- Re-measure the stamped write, CPU and wall, on HotSpot and on ART in the nightly + emulator job. + +## Related + +- [Copy types that change cross by value](/quest/m1/ffi-export/copy-values.md) - writes a value's bytes back, through the same fields diff --git a/quest/m1/ffi-speed/object-ownership.md b/quest/m1/ffi-speed/object-ownership.md index c5c3e6b4d..57b4b17fe 100644 --- a/quest/m1/ffi-speed/object-ownership.md +++ b/quest/m1/ffi-speed/object-ownership.md @@ -23,5 +23,3 @@ Ownership stays automatic: a dropped object is still freed without a call from t ## 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 753dc1656693e1bbe62439f4a6936ec6e2661695 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 04:38:13 -0800 Subject: [PATCH 07/24] fix(ffi): a Copy object taken by value is copied, and review's other findings A Copy type kept a handle, for a pub field or a &mut self method, is copied out of its handle again where Rust takes it by value, as on main, so moq's Traffic::add leaves its argument usable; the api fixture's Score proves it in every language. Also: Kotlin's codec holds a value's padding as uninitialized bytes, C++ declares an opaque value a class, a value's lifetimes count as a returned object's, a closure may not return a borrowed value, and the lock catches a change to how Kotlin holds a value. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 13 +++++--- ffi/fixtures/api/examples/transcript.rs | 13 ++++++++ ffi/fixtures/api/src/lib.rs | 42 +++++++++++++++++++++++ ffi/fixtures/lock/expected.txt | 1 + ffi/fixtures/lock/src/lib.rs | 21 ++++++++++++ ffi/fixtures/reject/expected.txt | 3 ++ ffi/fixtures/reject/src/lib.rs | 43 ++++++++++++++++++++++++ ffi/runtime/jni.rs | 44 +++++++++++++++++-------- ffi/src/abi.rs | 22 +++++++++++-- ffi/src/collect.rs | 19 +++++++---- ffi/src/cpp.rs | 42 +++++++++++++++-------- ffi/src/dart.rs | 17 +++++----- ffi/src/glue.rs | 9 ++++- ffi/src/go.rs | 17 +++++----- ffi/src/ir.rs | 11 +++++++ ffi/src/kotlin.rs | 21 ++++++------ ffi/src/lock.rs | 26 ++++++++++----- ffi/src/python.rs | 10 ++++-- ffi/src/swift.rs | 27 ++++++++------- ffi/tests/api.c | 16 +++++++++ ffi/tests/api.cpp | 11 +++++++ ffi/tests/api.dart | 9 +++++ ffi/tests/api.go | 11 +++++++ ffi/tests/api.kt | 9 +++++ ffi/tests/api.py | 9 +++++ ffi/tests/api.swift | 9 +++++ ffi/tests/fixtures.rs | 25 +++++++++----- 27 files changed, 403 insertions(+), 97 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 19290ebfd..e1b8458b2 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -72,7 +72,7 @@ re-exports its items instead, which export. A type no path reaches is refused. | a struct whose fields are all `pub`, not `#[non_exhaustive]`, which map, with no `Drop` of its own | a record: a struct, copied across | | an enum with variants whose fields map, with no `Drop` of its own | a C enum without fields, or a struct of a tag and a union | | any other type that is `Copy` and `Send`, with no `pub` field and no `pub` method taking `&mut self`, such as moq's `Timestamp` or std's `Instant` | an opaque value: a struct of its bytes, copied across, which only Rust makes or reads; one no export returns is left out, since C could never make one | -| any other type, if `Send` | an object: an opaque handle to an `Arc`, freed with `_free` | +| any other type, if `Send` | an object: an opaque handle to an `Arc`, freed with `_free`; one that is `Copy`, since C may change it in place, is copied out of its handle where Rust takes it by value, and a closure neither takes nor returns one by value | | a type whose only generic parameters are lifetimes, such as `Path<'a>` | an object holding its `'static` form | | a generic type, at each instance it infers | that instance as any other type, with each method whose impl and own bounds it meets; any other instance is refused | | a `pub` field of an object | a getter returning a copy, and a setter assigning in place, as `x.field = value` does | @@ -131,7 +131,7 @@ maps, which may name more. Inference finishes before any type is mapped, so mapp never adds an instance. An instance that holds an instance of a type inside it, such as `Cell>`, is refused, as it could name ever deeper ones. -A record or enum holds only values: one with a field that does not map, +A record or enum holds only values and `Copy` handles: one with a field that does not map, or that holds itself, is an object instead, and the census lists it. A borrowed return is returned as a clone, so it must be `Clone`, and a handle in it is a new handle to the clone, but for an object a `&self` method of an object returns borrowed from its receiver @@ -225,7 +225,9 @@ where it was while its file is unchanged. - **Handles** are `Arc`s, and a call borrows the value as Rust's receiver or parameter 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 value that is not `Sync` is shared on one thread at a time, and held exclusively by a + `T`, after which the handle is only freed. A `Copy` object, one with a `pub` field or a + `pub` method taking `&mut self`, is copied out instead, leaving its handle as it was. 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 @@ -630,8 +632,9 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod `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 thrown as an error, which `Swift.Error` - requires be copyable, is a final class instead, whose handle a call + conforms no noncopyable type to yet. A `Copy` object, which Rust copies where it takes + one by value, and an object thrown as an error, which `Swift.Error` requires be + copyable, are final classes instead, whose handle a call holds until it returns, and moving its value out closes it, after which a call traps. 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. diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 065403cd8..8dadde7ea 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -318,6 +318,19 @@ fn main() { println!("shifted {} {}", shifted.millis(), shifted == moment.after(500)); println!("wrapped {} {}", moment.after(u64::MAX).millis(), moment == shifted); + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + let mut score = api::Score::of(2); + let step = api::Score::of(3); + score.add(step); + score.add(step); + println!("score {} {} {}", score.count(), step.doubled().count(), step.count()); + let weighted = api::Weighted { score: step, weight: 4 }; + println!( + "weighted {} {}", + weighted.total(), + weighted.score.count() + step.count() + ); + println!("kept {}", api::emit(Print, vec![1, 2, 3, 4])); let logger = Logger::new(Box::new(Print)); println!("write {}", logger.write("hi")); diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 011a35838..1b98614f6 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -981,6 +981,48 @@ pub fn shifted(moment: Moment, shift: impl Fn(Moment) -> Moment) -> Moment { shift(moment) } +/// A running count, as moq's `stats::Traffic`: `Copy` with private fields, but changed in +/// place through `&mut self`, which a copy could not write back, so a handle. A call taking +/// one by value copies it out of its handle, leaving the caller's as it was. +#[derive(Clone, Copy, Debug)] +pub struct Score { + count: u64, +} + +impl Score { + pub fn of(count: u64) -> Self { + Self { count } + } + + pub fn count(&self) -> u64 { + self.count + } + + /// Adds `other`'s count to this one, as Rust copies `other`. + pub fn add(&mut self, other: Score) { + self.count = self.count.wrapping_add(other.count); + } + + /// Twice the count, as Rust copies `self`. + pub fn doubled(self) -> Score { + Self { + count: self.count.wrapping_mul(2), + } + } +} + +/// A score and its weight: a record holding a `Copy` handle, copied in and out. +pub struct Weighted { + pub score: Score, + pub weight: u32, +} + +impl Weighted { + pub fn total(&self) -> u64 { + self.score.count.wrapping_mul(u64::from(self.weight)) + } +} + /// A stamp and a count: a record holding a value only Rust makes, whose method reads it. pub struct Stamped { pub at: Stamp, diff --git a/ffi/fixtures/lock/expected.txt b/ffi/fixtures/lock/expected.txt index cf7d9d9f6..ef51020d5 100644 --- a/ffi/fixtures/lock/expected.txt +++ b/ffi/fixtures/lock/expected.txt @@ -7,6 +7,7 @@ fixtures/lock/src/lib.rs:182:1: rust2-ffi: the API breaks the baseline at const fixtures/lock/src/lib.rs:191:1: rust2-ffi: the API breaks the baseline at tuple `((u32,), u64, u8)`: its C name `lock_tuple_tuple_u32_u64_u8` is now `lock_tuple_tuple_u32_u64_u8_`, and `lock_tuple_tuple_u32_u64_u8` names tuple `((u32, u64), u8)` fixtures/lock/src/lib.rs:202:1: rust2-ffi: the API breaks the baseline at struct `lock::Tick`: its bytes: 4 aligned to 4, and now 8 aligned to 4 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:231:1: rust2-ffi: the API breaks the baseline at struct `lock::Beat`: how it is held: a word compared by its bits, and now bytes compared by Rust 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: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 diff --git a/ffi/fixtures/lock/src/lib.rs b/ffi/fixtures/lock/src/lib.rs index 9d5f083d1..f73d06114 100644 --- a/ffi/fixtures/lock/src/lib.rs +++ b/ffi/fixtures/lock/src/lib.rs @@ -223,3 +223,24 @@ impl Tick { self.late } } + +/// A `Copy` value in a word, whose `PartialEq` `next` writes by hand, so Kotlin can no +/// longer compare its bits. +#[derive(Clone, Copy)] +#[cfg_attr(not(feature = "next"), derive(PartialEq))] +pub struct Beat { + at: u32, +} + +#[cfg(feature = "next")] +impl PartialEq for Beat { + fn eq(&self, other: &Self) -> bool { + self.at / 2 == other.at / 2 + } +} + +impl Beat { + pub fn new(at: u32) -> Self { + Self { at } + } +} diff --git a/ffi/fixtures/reject/expected.txt b/ffi/fixtures/reject/expected.txt index bd611ea1e..b09ef38cc 100644 --- a/ffi/fixtures/reject/expected.txt +++ b/ffi/fixtures/reject/expected.txt @@ -6,6 +6,9 @@ fixtures/reject/src/lib.rs:1016:1: rust2-ffi: cannot export fn `trust`: trait ob fixtures/reject/src/lib.rs:1021:1: rust2-ffi: cannot export fn `hazard`: trait object `dyn Hazard`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:1011:1: note: C cannot implement `Hazard`: unsafe fn `risk` fixtures/reject/src/lib.rs:1026:1: rust2-ffi: cannot export fn `wide_non_zero`: NonZero `u128`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1048:1: rust2-ffi: cannot export fn `wrap`: returned object that borrows `View<'_>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1064:1: rust2-ffi: cannot export fn `picked`: closure signature `&'static Mark`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1069:1: rust2-ffi: cannot export fn `maybe_picked`: closure signature `std::option::Option<&'static Mark>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:106:2: rust2-ffi: cannot export method `Text::later`: returned object that borrows `impl std::future::Future>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:112:1: rust2-ffi: cannot export fn `forever`: 'static borrow `&'static [u8]`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:118:1: rust2-ffi: cannot export fn `boxed`: reference inside a parameter `std::boxed::Box<&str>`; exclude it in the config to leave it out diff --git a/ffi/fixtures/reject/src/lib.rs b/ffi/fixtures/reject/src/lib.rs index 1e8db5272..c04cd9b8c 100644 --- a/ffi/fixtures/reject/src/lib.rs +++ b/ffi/fixtures/reject/src/lib.rs @@ -1026,3 +1026,46 @@ pub fn hazard(hazard: Box) -> u32 { pub fn wide_non_zero(n: std::num::NonZero) -> u128 { n.get() } + +/// A `Copy` view of bytes it borrows, held across as its bytes. +#[derive(Clone, Copy)] +pub struct View<'a> { + bytes: &'a [u8], +} + +impl View<'_> { + pub fn len(&self) -> usize { + self.bytes.len() + } +} + +/// A view of nothing, which borrows nothing, so an export returns a `View`. +pub fn empty_view() -> View<'static> { + View { bytes: &[] } +} + +/// Returns a value that borrows its parameter, which C could keep past the borrow. +pub fn wrap(bytes: &[u8]) -> View<'_> { + View { bytes } +} + +/// A `Copy` mark held across as its bytes. +#[derive(Clone, Copy)] +pub struct Mark { + at: u64, +} + +/// A mark at `at`, so an export returns one. +pub fn mark(at: u64) -> Mark { + Mark { at } +} + +/// A closure returning a borrowed value, which C has no reference to hand back. +pub fn picked(pick: impl Fn() -> &'static Mark) -> u64 { + pick().at +} + +/// The same, in an `Option`. +pub fn maybe_picked(pick: impl Fn() -> Option<&'static Mark>) -> u64 { + pick().map_or(0, |mark| mark.at) +} diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index 33cc7eb28..f7eaecbdb 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -10,6 +10,7 @@ use std::cell::Cell; use std::ffi::{CStr, c_void}; +use std::mem::MaybeUninit; use std::sync::OnceLock; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -147,16 +148,26 @@ pub fn view(bytes: &Option>) -> rt::Buf { /// A new `byte[]` holding `bytes`. pub fn array(env: *mut JNIEnv, bytes: &[u8]) -> jbyteArray { - let Ok(len) = jsize::try_from(bytes.len()) else { + // SAFETY: `bytes` holds its length in bytes. + unsafe { raw_array(env, bytes.as_ptr(), bytes.len()) } +} + +/// A new `byte[]` of the `len` bytes at `bytes`, which JNI copies as they are, so any may +/// be uninitialized. +/// +/// # Safety +/// `bytes` reaches `len` bytes. +unsafe fn raw_array(env: *mut JNIEnv, bytes: *const u8, len: usize) -> jbyteArray { + let Ok(len) = jsize::try_from(len) else { rt::fail("jni", "a buffer is too long for a byte[]") }; - // SAFETY: `env` is this thread's, and `bytes` holds `len` bytes. + // SAFETY: `env` is this thread's, and `bytes` reaches `len` bytes. unsafe { let array = ((**env).v1_2.NewByteArray)(env, len); if array.is_null() { fail(env, "jni", "a byte[] is out of memory"); } - ((**env).v1_2.SetByteArrayRegion)(env, array, 0, len, bytes.as_ptr().cast()); + ((**env).v1_2.SetByteArrayRegion)(env, array, 0, len, bytes.cast()); array } } @@ -661,11 +672,12 @@ impl<'a> Reader<'a> { thread_local! { /// The buffer a writer encodes into, kept between calls so a result allocates nothing /// but its `byte[]`. - static SCRATCH: Cell> = const { Cell::new(Vec::new()) }; + static SCRATCH: Cell>> = const { Cell::new(Vec::new()) }; } -/// Values encoded into a `byte[]` for Kotlin. -pub struct Writer(Vec); +/// Values encoded into a `byte[]` for Kotlin. Its bytes may be uninitialized, as an opaque +/// value's padding is, so it holds them as such and hands JNI them by pointer. +pub struct Writer(Vec>); impl Default for Writer { fn default() -> Self { @@ -690,7 +702,13 @@ impl Writer { } pub fn array(self, env: *mut JNIEnv) -> jbyteArray { - array(env, &self.0) + // SAFETY: the vector holds its length in bytes, which JNI copies as they are. + unsafe { raw_array(env, self.0.as_ptr().cast(), self.0.len()) } + } + + fn extend(&mut self, bytes: &[u8]) { + // SAFETY: a `u8` is a `MaybeUninit`. + self.0.extend_from_slice(unsafe { &*(std::ptr::from_ref(bytes) as *const [MaybeUninit]) }); } } @@ -766,7 +784,7 @@ pub fn read_words(r: &mut Reader<'_>) -> T { } /// Encode an opaque value's C struct as the `W` words Kotlin holds it in: its bytes, then -/// zeros. Its padding crosses as the bytes it holds, which Kotlin never reads. +/// zeros. Its padding stays uninitialized, as the writer allows, and Kotlin never reads it. pub fn write_words(w: &mut Writer, value: T) { const { assert!(size_of::() <= W * size_of::() && cfg!(target_endian = "little")) }; let n = W * size_of::(); @@ -774,7 +792,7 @@ pub fn write_words(w: &mut Writer, value: T) { let start = w.0.len(); // SAFETY: the vector has room for `n` more bytes, each written before its length grows. unsafe { - let at = w.0.as_mut_ptr().add(start); + let at = w.0.as_mut_ptr().add(start).cast::(); std::ptr::write_bytes(at, 0, n); std::ptr::copy_nonoverlapping((&raw const value).cast::(), at, size_of::()); w.0.set_len(start + n); @@ -796,7 +814,7 @@ macro_rules! scalar { } fn write(self, w: &mut Writer) { - w.0.extend_from_slice(&self.to_le_bytes()); + w.extend(&self.to_le_bytes()); } } )*}; @@ -810,7 +828,7 @@ impl Wire for [u8; N] { } fn write(self, w: &mut Writer) { - w.0.extend_from_slice(&self); + w.extend(&self); } } @@ -905,7 +923,7 @@ impl Wire for rt::Buf { rt::fail("jni", "a buffer is too long for a byte[]") }; w.put(len); - w.0.extend_from_slice(bytes); + w.extend(bytes); // SAFETY: as above. unsafe { self.free() }; } @@ -940,7 +958,7 @@ impl Wire for Owned { w.put(len); // SAFETY: a buffer Rust returned or lent, freed here once copied. unsafe { - w.0.extend_from_slice(buf.bytes("jni")); + w.extend(buf.bytes("jni")); buf.free(); } } diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index 010c3d530..c616c7d06 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -695,7 +695,10 @@ impl<'a> Abi<'a> { let ty = match owner { Ty::Handle { name, .. } => Ty::Handle { name: name.clone(), - constant: receiver == Receiver::Ref, + constant: receiver == Receiver::Ref + || (receiver == Receiver::Owned + && (0..self.api.objects.len()) + .any(|i| self.api.objects[i].copy && &self.object(i) == name)), }, // A value's receiver is read through a pointer, and never changed. value => Ty::In(Box::new(value.clone())), @@ -705,7 +708,22 @@ impl<'a> Abi<'a> { // What Rust writes back for each value C passes for `&mut`, after every parameter. let mut written = Vec::new(); for param in &function.params { - let ty = self.ty(¶m.ty); + let copied = match ¶m.ty { + Type::Object(i) => Some(*i), + Type::Option(inner) => match **inner { + Type::Object(i) => Some(i), + _ => None, + }, + _ => None, + }; + let ty = match copied { + // Taken by value, a `Copy` object is copied out, leaving its handle as it was. + Some(i) if self.api.objects[i].copy => Ty::Handle { + name: self.object(i), + constant: true, + }, + _ => self.ty(¶m.ty), + }; let name = escape(¶m.name); if let Type::Closure(closure) = ¶m.ty { // Named apart from every parameter of the function, which C requires. diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 54899964d..ae2707400 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1334,6 +1334,7 @@ impl<'tcx> Collector<'tcx> { *i = api.objects.len(); let ty = tcx.type_of(def_id).instantiate(tcx, site.args).skip_norm_wip(); let sync = implements(tcx, ty, def_id, sym::Sync); + let copy = implements(tcx, ty, def_id, sym::Copy); let lifetimes = tcx.generics_of(def_id).own_counts().lifetimes; api.objects.push(Object { path, @@ -1344,6 +1345,7 @@ impl<'tcx> Collector<'tcx> { on: On::ALL, docs, sync, + copy, shared: false, lifetimes, methods: Vec::new(), @@ -1465,6 +1467,7 @@ impl<'tcx> Collector<'tcx> { tcx.def_path_str(def_id) ), sync: true, + copy: false, shared: true, lifetimes: 0, methods: Vec::new(), @@ -1585,6 +1588,7 @@ impl<'tcx> Collector<'tcx> { on: On::ALL, docs: docs.to_string(), sync: true, + copy: implements(tcx, self_ty, def_id, sym::Copy), shared: traits.clone && self.clone_shares(self_ty, def_id), lifetimes: 0, methods, @@ -3584,14 +3588,16 @@ impl<'tcx> Collector<'tcx> { } _ => (ret, None), }; - // What C hands Rust: a scalar, a `String`, or an opaque value or an `Option` of one. + // What C hands Rust: a scalar, a `String`, or an opaque value or an `Option` of one, + // owned, as C has no reference to hand back. + let owned = |ty: Ty<'tcx>, mapped: &Type| !matches!(mapped, Type::Ref(..)) && copied(ty, mapped); let ok = match self.ty(ok, Pos::Return) { Ok(mapped) if foreign_return(&mapped) || mapped == Type::String || match (&mapped, ok.kind()) { - (Type::Option(inner), ty::Adt(_, args)) => copied(args.type_at(0), inner), - (mapped, _) => copied(ok, mapped), + (Type::Option(inner), ty::Adt(_, args)) => owned(args.type_at(0), inner), + (mapped, _) => owned(ok, mapped), } => { mapped @@ -4055,7 +4061,8 @@ impl<'tcx> Collector<'tcx> { for ty in ty.walk().filter_map(|arg| arg.as_type()) { match ty.kind() { ty::Ref(region, ..) => lent.push(*region), - ty::Adt(adt, args) if matches!(self.decl(adt.did(), args), Some(Decl::Object(_))) => { + // An object or an opaque value holds what its lifetime parameters name. + ty::Adt(adt, args) if matches!(self.decl(adt.did(), args), Some(Decl::Object(_) | Decl::Value(_))) => { held.extend(args.regions()) } // An `impl Future` holds what its bounds name, as an `async fn` its output. @@ -4634,8 +4641,8 @@ impl<'tcx> Collector<'tcx> { match self.decls.get(&key) { Some(Decl::Record(i) | Decl::Value(i)) => Type::Record(*i), Some(Decl::Enum(i)) => Type::Enum(*i), - // A value holds only values. - Some(Decl::Object(_)) if pos == Pos::Field => { + // A value holds only values, and handles it can copy. + Some(Decl::Object(_)) if pos == Pos::Field && !implements(tcx, ty, did, sym::Copy) => { return Err(why("object in a value", name)); } Some(Decl::Object(i)) => Type::Object(*i), diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 7001f673c..da8600efe 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -147,14 +147,14 @@ impl<'a> Header<'a> { match ty { Type::Option(inner) if Abi::is_buf(inner) => format!("::std::optional<::{}::view>", self.ns), // Moved out of, each handle as a handle taken by value is. - Type::Option(inner) if matches!(inner.base(), Type::List(_, elem) if moves(elem)) => { + Type::Option(inner) if matches!(inner.base(), Type::List(_, elem) if moves(abi, elem)) => { format!("::std::optional<{}>", self.items(inner.base())) } Type::Option(inner) if matches!(inner.base(), Type::List(..)) => { format!("const ::std::optional<{}> &", self.items(inner.base())) } // Moved out of, each handle as a handle taken by value is. - Type::List(_, inner) if moves(inner) => self.items(ty), + Type::List(_, inner) if moves(abi, inner) => self.items(ty), Type::List(..) => format!("const {} &", self.items(ty)), Type::Option(inner) => match &**inner { // Lent, or copied out: a pointer, null for none. @@ -165,8 +165,9 @@ impl<'a> Header<'a> { }; format!("{constant}{} *", self.owned(object)) } + Type::Object(i) if abi.api.objects[*i].copy => format!("const {} *", self.owned(inner)), // Moved out of, as a handle taken by value is. - _ if moves(inner) => format!("::std::optional<{}>", self.held(inner)), + _ if moves(abi, inner) => format!("::std::optional<{}>", self.held(inner)), _ => format!("const ::std::optional<{}> &", self.held(inner)), }, Type::Waker => format!("const ::{}::waker &", self.ns), @@ -175,12 +176,13 @@ impl<'a> Header<'a> { crate::ir::Mutability::Mut => format!("{} &", self.owned(inner)), }, Type::Ref(_, inner) | Type::Box(inner) => self.param(inner), + Type::Object(i) if abi.api.objects[*i].copy => format!("const {} &", self.owned(ty)), Type::Record(_) | Type::Array(_) | Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => { format!("const {} &", self.owned(ty)) } Type::Enum(i) if !abi.api.enums[*i].plain() => format!("const {} &", self.owned(ty)), // Moved out of, each handle as a handle taken by value is. - Type::Tuple(_) if moves(ty) => self.held(ty), + Type::Tuple(_) if moves(abi, ty) => self.held(ty), Type::Tuple(_) => format!("const {} &", self.held(ty)), ty => self.owned(ty), } @@ -289,7 +291,8 @@ impl<'a> Header<'a> { true => format!("static_cast<::{}>({e})", abi.enumeration(*i)), false => format!("{d}::lower({e})"), }, - // Moved out. + // A `Copy` value is copied out, and any other moved out. + Type::Object(i) if abi.api.objects[*i].copy => format!("{d}::access::raw({e})"), Type::Object(_) => format!("{d}::access::own({e})"), Type::Callback(..) => format!("{d}::lower(::std::move({e}))"), Type::Closure(..) | Type::Future | Type::Reader => { @@ -469,9 +472,14 @@ impl<'a> Header<'a> { } // Every class and struct, declared so any signature may name it. - for i in 0..api.records.len() { + for (i, record) in api.records.iter().enumerate() { let name = self.global(&self.name(&abi.record(i))); - self.line(format!("struct {name};")); + // The key its definition uses, which MSVC's C4099 and Clang's + // `-Wmismatched-tags` require match. + match record.opaque { + Some(_) => self.line(format!("class {name};")), + None => self.line(format!("struct {name};")), + } } for i in 0..api.objects.len() { let name = self.global(&self.name(&abi.object(i))); @@ -1149,7 +1157,9 @@ impl<'a> Header<'a> { let (_, ty) = c_params.next().expect("a method has its receiver"); match (ty, free) { // Moved out of, as a handle taken by value is. - (Ty::Handle { .. }, _) if receiver == Some(Receiver::Owned) => args.push("raw_.own()".to_string()), + (Ty::Handle { name, .. }, _) if receiver == Some(Receiver::Owned) && !copy(abi, name) => { + args.push("raw_.own()".to_string()) + } (Ty::Handle { .. }, _) => args.push("raw_.get()".to_string()), (_, false) => { let ty = record(abi, owner_c.as_deref().expect("a member method has its record")); @@ -1279,7 +1289,7 @@ impl<'a> Header<'a> { (Some(Receiver::Ref), _) => " const".to_string(), (Some(Receiver::Mut), _) => String::new(), (Some(Receiver::Owned), _) => match owner { - Some(Ty::Handle { .. }) => " &&".to_string(), + Some(Ty::Handle { name, .. }) if !copy(abi, name) => " &&".to_string(), _ => " const".to_string(), }, }; @@ -1390,6 +1400,7 @@ impl<'a> Header<'a> { Type::Ref(crate::ir::Mutability::Mut, _) => { format!("::{}::borrow<{}>(l{k}_.get())", self.ns, self.owned(object)) } + // A `Copy` object Rust copied for the call, which its class reads in place. _ => format!("::std::as_const(l{k}_.get())"), }); } @@ -1550,15 +1561,20 @@ fn record(abi: &Abi<'_>, c: &str) -> Type { /// Whether passing `ty` moves a handle out of a `Vec`, `Option`, or tuple, as a handle /// taken by value is. -fn moves(ty: &Type) -> bool { +fn moves(abi: &Abi<'_>, ty: &Type) -> bool { match ty { - Type::Object(_) => true, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moves(inner), - Type::Tuple(elements) => elements.iter().any(moves), + Type::Object(i) => !abi.api.objects[*i].copy, + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moves(abi, inner), + Type::Tuple(elements) => elements.iter().any(|e| moves(abi, e)), _ => false, } } +/// Whether the object named `c` in C is `Copy`. +fn copy(abi: &Abi<'_>, c: &str) -> bool { + (0..abi.api.objects.len()).any(|i| abi.api.objects[i].copy && abi.object(i) == c) +} + /// `ty` returned: a borrow is returned as a clone, so owned. fn strip_refs(ty: &Type) -> Type { match ty { diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 2a91235c7..ede147534 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -1898,7 +1898,7 @@ impl<'a> Gen<'a> { Type::Box(inner) => self.close(inner, e), Type::Tuple(elements) => { let closes: Vec = (elements.iter().enumerate()) - .filter(|(_, ty)| moved(ty)) + .filter(|(_, ty)| moved(self.abi, ty)) .map(|(k, ty)| self.close(ty, &format!("{e}.${}", k + 1))) .collect(); closes.join(" ") @@ -1969,7 +1969,8 @@ impl<'a> Gen<'a> { match c { Ty::Handle { .. } => { args.push("a_.hold(this)".to_string()); - if receiver == Receiver::Owned { + 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()); } @@ -2012,7 +2013,7 @@ impl<'a> Gen<'a> { continue; } params.push(format!("{} {name}", self.dt(¶m.ty))); - if moved(¶m.ty) { + if moved(abi, ¶m.ty) { after.push(self.close(¶m.ty, &name)); } match (¶m.ty, c) { @@ -2353,12 +2354,12 @@ 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, and never through a reference. -fn moved(ty: &Type) -> bool { +/// a list, or a tuple, unless `Copy`, and never through a reference. +fn moved(abi: &Abi<'_>, ty: &Type) -> bool { match ty { - Type::Object(_) => true, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(inner), - Type::Tuple(elements) => elements.iter().any(moved), + 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, } } diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index ad771a7be..2e83d2041 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -522,7 +522,14 @@ impl Gen<'_> { Type::Object(i) if self.abi.api.objects[*i].opaque => { format!("unsafe {{ rt::Object::::take({e}, w) }}.arc(w)") } - Type::Object(i) => format!("unsafe {{ rt::Object::<{}>::take({e}, w) }}", self.object_path(*i)), + 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::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()"), diff --git a/ffi/src/go.rs b/ffi/src/go.rs index b512b6a62..d891b7f9d 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -2111,7 +2111,7 @@ impl<'a> Gen<'a> { 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(ty)) + .filter(|(_, ty)| moved(self.abi, ty)) .map(|(k, ty)| self.close(ty, &format!("{e}.V{k}"), depth)) .collect(); closes.join("; ") @@ -2222,7 +2222,8 @@ impl<'a> Gen<'a> { fail("self.Handle_.Closed()") )); args.push(format!("(*C.{name})(h_)")); - if receiver == Receiver::Owned { + let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); + if receiver == Receiver::Owned && !copy { after.push("self.Close()".to_string()); } } @@ -2266,7 +2267,7 @@ impl<'a> Gen<'a> { continue; } params.push((name.clone(), self.go_param(¶m.ty))); - if moved(¶m.ty) { + if moved(abi, ¶m.ty) { after.push(self.close(¶m.ty, &name, 0)); } match (¶m.ty, c) { @@ -2576,12 +2577,12 @@ fn poll_error(abi: &Abi<'_>) -> String { } /// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, -/// a list, or a tuple, and never through a reference. -fn moved(ty: &Type) -> bool { +/// a list, or a tuple, unless `Copy`, and never through a reference. +fn moved(abi: &Abi<'_>, ty: &Type) -> bool { match ty { - Type::Object(_) => true, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(inner), - Type::Tuple(elements) => elements.iter().any(moved), + 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, } } diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index 63971a19f..2694632ed 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -152,6 +152,13 @@ impl Opaque { self.size <= 8 } + /// Whether a language may hold it in a word and compare that word: it fits one and is + /// dense, so values Rust calls equal have equal bits. Kotlin's type for it turns on + /// this, so a change of it breaks Kotlin's compiled callers. + pub fn compared(&self) -> bool { + self.word() && self.dense + } + /// The bytes of each unit its C struct holds it in, by its alignment, at most 8. pub fn unit(&self) -> u64 { self.align.clamp(1, 8) @@ -233,6 +240,10 @@ pub struct Object { pub docs: String, /// Whether it is `Sync`: if not, `&self` borrows it on one thread at a time. pub sync: bool, + /// Whether it is `Copy`, which only one with a `pub` field or a `&mut self` method + /// is, any other being a value: taken by value, it is copied out of its handle, as + /// Rust copies it, rather than moved out. + pub copy: bool, /// Whether copies of its handle may share one value, since no call tells that apart /// from `Clone` but by an `Arc`'s or `Rc`'s count: it is `Clone` and `Sync`, its clone owns no state that changes /// behind `&` or counts clones, and no export borrows it mutably. diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 0809dc10a..1c4b67941 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -751,7 +751,7 @@ impl<'a> Gen<'a> { false => format!("{e}.handle.hold {{ {h} ->"), }); // Taken by value, the value moves out, leaving the handle only to free. - if moved(ty) { + if moved(abi, ty) { arg.after.push(match option { true => format!("{e}?.close()"), false => format!("{e}.close()"), @@ -788,7 +788,7 @@ 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(ty) { + if moved(abi, ty) { let close = self.close(ty, e); arg.after.push(close); } @@ -1847,7 +1847,8 @@ 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 _")); - if receiver == Receiver::Owned { + 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()); } arg @@ -2177,7 +2178,7 @@ impl<'a> Gen<'a> { Type::Tuple(elements) => { let t = self.fresh("t"); let closes: Vec = (elements.iter().enumerate()) - .filter(|(_, ty)| moved(ty)) + .filter(|(_, ty)| moved(self.abi, ty)) .map(|(k, ty)| self.close(ty, &format!("{t}.{}", ordinal(k)))) .collect(); format!("{e}.let {{ {t} -> {} }}", closes.join("; ")) @@ -2304,12 +2305,12 @@ fn bytes(ty: &Type) -> bool { } /// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, -/// and never through a reference. -fn moved(ty: &Type) -> bool { +/// unless `Copy`, and never through a reference. +fn moved(abi: &Abi<'_>, ty: &Type) -> bool { match ty { - Type::Object(_) => true, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(inner), - Type::Tuple(elements) => elements.iter().any(moved), + 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, } } @@ -2391,7 +2392,7 @@ fn holder(opaque: &Opaque) -> &'static str { /// Whether Kotlin holds `opaque` in a `Long`, with no allocation: it fits one and is /// dense, so values Rust calls equal have equal bits, as the value class compares them. fn word(opaque: &Opaque) -> bool { - opaque.word() && opaque.dense + opaque.compared() } /// The Rust expression taking the opaque value Kotlin passed as `p`, as its C struct. diff --git a/ffi/src/lock.rs b/ffi/src/lock.rs index a139022b1..bf62366e2 100644 --- a/ffi/src/lock.rs +++ b/ffi/src/lock.rs @@ -15,7 +15,7 @@ use std::collections::BTreeMap; use rustc_span::{DUMMY_SP, Span}; use crate::abi::{Abi, Ty}; -use crate::ir::{Access, Api, Constant, Function, Literal, On, Traits, Type}; +use crate::ir::{Access, Api, Constant, Function, Literal, On, Opaque, Traits, Type}; use crate::merge::{self, Maps}; /// An item the export's API breaks the baseline's at: where, and how. @@ -369,14 +369,22 @@ fn changed(base: &Index<'_>, now: &Index<'_>, maps: &Maps, ours: &Item<'_>, thei match (*ours, *theirs) { (Item::Record(i), Item::Record(j)) => { traits(b.records[i].traits, n.records[j].traits); - // An opaque value's bytes are its C struct, which every binding sizes. - if let (Some(o), Some(p)) = (b.records[i].opaque, n.records[j].opaque) - && (o.size, o.align) != (p.size, p.align) - { - changes.push(format!( - "its bytes: {} aligned to {}, and now {} aligned to {}", - o.size, o.align, p.size, p.align - )); + // An opaque value's bytes are its C struct, which every binding sizes, and + // whether a word holds it and compares it is Kotlin's type for it. + if let (Some(o), Some(p)) = (b.records[i].opaque, n.records[j].opaque) { + if (o.size, o.align) != (p.size, p.align) { + changes.push(format!( + "its bytes: {} aligned to {}, and now {} aligned to {}", + o.size, o.align, p.size, p.align + )); + } + let held = |o: Opaque| match o.compared() { + true => "a word compared by its bits", + false => "bytes compared by Rust", + }; + if o.compared() != p.compared() { + changes.push(format!("how it is held: {}, and now {}", held(o), held(p))); + } } let mut ours = merge::bare_record(&b.records[i]); maps.fields(&mut ours.fields); diff --git a/ffi/src/python.rs b/ffi/src/python.rs index 6e7c51383..41251f3dc 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -501,7 +501,7 @@ impl<'a> Gen<'a> { Type::List(..) => format!("r_{}({o}, s, {m})", self.c_struct(ty)), Type::Option(inner) => match inner.base() { Type::Object(i) => { - let m = match matches!(**inner, Type::Ref(..)) { + let m = match abi.api.objects[*i].copy || matches!(**inner, Type::Ref(..)) { true => "false", false => m, }; @@ -532,7 +532,13 @@ 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) => format!("rt::hold({o}, {}, s, {m})", self.t(&abi.object(*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::Callback(i, _) => { let c = abi.callback(*i); format!( diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 4a2e5fa9d..af36fe2b7 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -8,11 +8,11 @@ //! handle, freed in `deinit`, so Swift's compiler checks each move and borrow Rust makes: //! `self` is `consuming`, `&mut self` `mutating`, a parameter `consuming`, `borrowing`, or //! `inout`, a tuple holding one a struct of its own, and an array of them swift-collections' -//! `UniqueArray`. An object thrown as an error is a final class instead, since `Swift.Error` -//! requires a copyable type, whose handle a call holds at run time. A record is a struct, -//! and a `Copy` type with private fields a struct over the bytes of its C struct, which -//! only Rust makes or reads; an enum a Swift enum, with -//! its data as associated values; a trait a protocol; a closure a function type; a future +//! `UniqueArray`. An object that is `Copy`, which only one C may change is, or thrown as an +//! error is a final class instead, since `Swift.Error` requires a copyable type, whose +//! handle a call holds at run time. A record is a struct, and any other `Copy` type with +//! private fields a struct over the bytes of its C struct, which only Rust makes or reads; +//! an enum a Swift enum, with its data as associated values; a trait a protocol; a closure a function type; a future //! an `async` function; an error a thrown `Swift.Error`. The runtime they share, //! [`runtime/rt.swift`](../runtime/rt.swift), holds the classes' handles, the arena a call //! lowers nested values into, and the signal a future parks on. @@ -207,7 +207,7 @@ struct Gen<'a> { thrown: BTreeMap, /// The exported types thrown as errors, which conform to `Swift.Error`. conforms: BTreeSet, - /// The objects that are noncopyable structs: each but those thrown. + /// The objects that are noncopyable structs: each but those `Copy` or thrown. structs: BTreeSet, fresh: usize, /// The targets of the item being written, whose calls check theirs. @@ -293,11 +293,11 @@ impl<'a> Gen<'a> { } /// Whether passing `ty` moves the value of a class's handle out, so the call closes it: - /// by value, boxed, or in an `Option`, a list, or a tuple, and never through a - /// reference. A struct needs no close: Swift's compiler ends it. + /// by value, boxed, or in an `Option`, a list, or a tuple, unless `Copy`, and never + /// through a reference. A struct needs no close: Swift's compiler ends it. fn moved(&self, ty: &Type) -> bool { match ty { - Type::Object(i) => !self.structs.contains(i), + Type::Object(i) => !self.abi.api.objects[*i].copy && !self.structs.contains(i), Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => self.moved(inner), Type::Tuple(elements) => elements.iter().any(|ty| self.moved(ty)), _ => false, @@ -1468,7 +1468,8 @@ impl<'a> Gen<'a> { Ty::Handle { .. } => { arg.open = Some((format!("{tr}self.handle.with {{ h_ in"), "}".to_string())); arg.exprs.push("h_".to_string()); - if receiver == Receiver::Owned { + let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); + if receiver == Receiver::Owned && !copy { arg.after.push("self.handle.close()".to_string()); } } @@ -1851,7 +1852,7 @@ fn lent_option(structs: &BTreeSet, ty: &Type) -> bool { matches!(ty, Type::Option(inner) if matches!(&**inner, Type::Ref(Mutability::Not, object) if unique(structs, object))) } -/// The objects Swift holds as noncopyable structs: each but those thrown +/// The objects Swift holds as noncopyable structs: each but those `Copy`, and those thrown /// as errors, which `Swift.Error` requires be copyable. fn structs(abi: &Abi<'_>) -> BTreeSet { let api = abi.api; @@ -1864,7 +1865,9 @@ fn structs(abi: &Abi<'_>) -> BTreeSet { _ => None, }) .collect(); - (0..api.objects.len()).filter(|i| !thrown.contains(i)).collect() + (0..api.objects.len()) + .filter(|i| !api.objects[*i].copy && !thrown.contains(i)) + .collect() } /// `ty` returned: a borrow is returned as a clone, so owned. diff --git a/ffi/tests/api.c b/ffi/tests/api.c index a2d05a148..9e7c5cdbf 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -903,6 +903,22 @@ static int script(void) { printf("wrapped %llu %s\n", (unsigned long long)api_Moment_millis(&wrapped), api_Moment_eq(&moment, &shifted) ? "true" : "false"); + /* A `Copy` type changed in place is a handle, which a call taking it by value copies. */ + api_Score *score = api_Score_of(2); + api_Score *step = api_Score_of(3); + api_Score_add(score, step); + api_Score_add(score, step); + api_Score *doubled_score = api_Score_doubled(step); + printf("score %llu %llu %llu\n", (unsigned long long)api_Score_count(score), + (unsigned long long)api_Score_count(doubled_score), (unsigned long long)api_Score_count(step)); + api_Weighted weighted = {.score = api_Score_clone(step), .weight = 4}; + printf("weighted %llu %llu\n", (unsigned long long)api_Weighted_total(&weighted), + (unsigned long long)(api_Score_count(weighted.score) + api_Score_count(step))); + api_Weighted_free(&weighted); + api_Score_free(doubled_score); + api_Score_free(step); + api_Score_free(score); + static const api_Log_vtable print_log = {.free = log_free, .log = log_line}; uint8_t levels[] = {1, 2, 3, 4}; api_buf lines = {.ptr = levels, .len = sizeof levels}; diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 31325f509..6a187e80e 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -532,6 +532,17 @@ int script() { std::printf("wrapped %llu %s\n", ull(moment.after(UINT64_MAX).millis()), yes(moment == shifted)); } + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + { + api::Score score = api::Score::of(2); + api::Score step = api::Score::of(3); + score.add(step); + score.add(step); + std::printf("score %llu %llu %llu\n", ull(score.count()), ull(step.doubled().count()), ull(step.count())); + api::Weighted weighted{step.clone(), 4}; + std::printf("weighted %llu %llu\n", ull(weighted.total()), ull(weighted.score.count() + step.count())); + } + std::uint8_t levels[] = {1, 2, 3, 4}; std::printf("kept %u\n", api::emit(std::make_unique(), api::view(levels, sizeof levels))); { diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index ab230d77f..231fbd538 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -329,6 +329,15 @@ Future script() async { print('shifted ${shifted.millis()} ${shifted == moment.after(500)}'); print('wrapped ${moment.after(-1).millis()} ${moment == shifted}'); + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + final score = api.Score.of(2); + final increment = api.Score.of(3); + score.add(increment); + score.add(increment); + print('score ${score.count()} ${increment.doubled().count()} ${increment.count()}'); + final weighted = api.Weighted(score: increment, weight: 4); + print('weighted ${weighted.total()} ${weighted.score.count() + increment.count()}'); + print('kept ${api.emit(PrintLog(), bytes([1, 2, 3, 4]))}'); final logger = api.Logger(PrintLog()); print('write ${logger.write('hi')}'); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index c71828ad2..56bbeeb30 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -418,6 +418,17 @@ func script() { fmt.Printf("shifted %d %t\n", shifted.Millis(), shifted.Equal(moment.After(500))) fmt.Printf("wrapped %d %t\n", moment.After(math.MaxUint64).Millis(), moment.Equal(shifted)) + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + score := api.ScoreOf(2) + step := api.ScoreOf(3) + ok(score.Add(step)) + ok(score.Add(step)) + fmt.Printf("score %d %d %d\n", must(score.Count()), must(must(step.Doubled()).Count()), must(step.Count())) + weighted := api.Weighted{Score: step, Weight: 4} + fmt.Printf("weighted %d %d\n", must(weighted.Total()), must(weighted.Score.Count())+must(step.Count())) + score.Close() + step.Close() + // Rust frees the implementation once it drops it, releasing Go's. fmt.Printf("kept %d\n", api.Emit(printLog{}, []byte{1, 2, 3, 4})) logger := api.NewLogger(printLog{}) diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 3d03a4c9c..aa950659b 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -260,6 +260,15 @@ fun script() { println("shifted ${shifted.millis()} ${shifted == moment.after(500u)}") println("wrapped ${moment.after(ULong.MAX_VALUE).millis()} ${moment == shifted}") + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + val score = Score.of(2u) + val step = Score.of(3u) + score.add(step) + score.add(step) + println("score ${score.count()} ${step.doubled().count()} ${step.count()}") + val weighted = Weighted(step, 4u) + println("weighted ${weighted.total()} ${weighted.score.count() + step.count()}") + println("kept ${emit(PrintLog(), byteArrayOf(1, 2, 3, 4))}") Logger.new(PrintLog()).use { logger -> println("write ${logger.write("hi")}") } println("dropped") diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 68eaff2c4..206253224 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -324,6 +324,15 @@ def edit(counter): except TypeError: pass + # A `Copy` type changed in place is a handle, which a call taking it by value copies. + score = api.Score.of(2) + increment = api.Score.of(3) + score.add(increment) + score.add(increment) + print(f"score {score.count()} {increment.doubled().count()} {increment.count()}") + weighted = api.Weighted(increment, 4) + print(f"weighted {weighted.total()} {weighted.score.count() + increment.count()}") + print(f"kept {api.emit(PrintLog(), bytes([1, 2, 3, 4]))}") logger = api.Logger.new(PrintLog()) print(f"write {flag(logger.write('hi'))}") diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index 2a51ad5c2..459e3d091 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -406,6 +406,15 @@ func script() { print("shifted \(shifted.millis()) \(shifted == moment.after(500))") print("wrapped \(moment.after(UInt64.max).millis()) \(moment == shifted)") + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + let score = Score.of(2) + let step = Score.of(3) + score.add(step) + score.add(step) + print("score \(score.count()) \(step.doubled().count()) \(step.count())") + let weighted = Weighted(score: step, weight: 4) + print("weighted \(weighted.total()) \(weighted.score.count() + step.count())") + // Bound first, so Swift's reference is gone before the print. let kept = emit(PrintLog(), Data([1, 2, 3, 4])) print("kept \(kept)") diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index d15ef1084..e45543d02 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -386,14 +386,23 @@ fn cxx(api: &Api, standard: u8, plain: bool, src: &Path, exe: &Path) -> Command if plain { cmd.args(["-fno-exceptions", "-fno-rtti"]); } - // -Wshadow stands in for MSVC's warnings that a name hides another. - cmd.args(["-pedantic", "-Wall", "-Wextra", "-Wshadow", "-Werror", "-I"]) - .arg(include) - .arg(src) - .arg(api.staticlib()) - .args(&api.native) - .arg("-o") - .arg(exe); + // -Wshadow stands in for MSVC's warnings that a name hides another, and + // -Wmismatched-tags for C4099, a class declared `struct` or a struct `class`. + cmd.args([ + "-pedantic", + "-Wall", + "-Wextra", + "-Wshadow", + "-Wmismatched-tags", + "-Werror", + "-I", + ]) + .arg(include) + .arg(src) + .arg(api.staticlib()) + .args(&api.native) + .arg("-o") + .arg(exe); cmd } From f15b0b6f73b69c7f4703697d693ae2aab4889926 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 04:44:21 -0800 Subject: [PATCH 08/24] docs(quest): zero an opaque value's padding, as a follow-up Co-Authored-By: Claude Opus 5.5 --- quest/m1/ffi-export/copy-values.md | 1 + quest/m1/ffi-export/value-padding.md | 35 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 quest/m1/ffi-export/value-padding.md diff --git a/quest/m1/ffi-export/copy-values.md b/quest/m1/ffi-export/copy-values.md index e45c06138..ee79d4fef 100644 --- a/quest/m1/ffi-export/copy-values.md +++ b/quest/m1/ffi-export/copy-values.md @@ -37,3 +37,4 @@ Public API: how every generated API holds these types (breaking). ## Related - [Kotlin values without a LongArray](/quest/m1/ffi-speed/kotlin-values.md) - how Kotlin holds the bytes this writes back +- [Opaque values cross with zeroed padding](/quest/m1/ffi-export/value-padding.md) - zeroes what a value's bytes leave uninitialized diff --git a/quest/m1/ffi-export/value-padding.md b/quest/m1/ffi-export/value-padding.md new file mode 100644 index 000000000..c8e7b86bf --- /dev/null +++ b/quest/m1/ffi-export/value-padding.md @@ -0,0 +1,35 @@ +# [M] Opaque values cross with zeroed padding + +## Goal + +A [rust2-ffi](/ffi/README.md) opaque value, a `Copy` type with private fields held as its +bytes, crosses with every padding byte zero, so no stale Rust stack byte reaches the memory +of the language holding it. Today a padded value, such as std's `Instant` on 64-bit Linux +(an `i64` and a `u32`, then 4 bytes of padding), copies its uninitialized padding into the C +struct: C and C++ see it as any struct's padding, and Kotlin's `LongArray`, Go's `Bytes_`, +Dart's `int`s, and Python's `Value` hold it as ordinary bytes. No API reads them, and no +Rust undefined behavior remains, since the codec holds them as `MaybeUninit`, but they are +readable in the language's own process memory. + +## Plan + +- The collector records, per target, the byte ranges a value's fields initialize, from + rustc's layout of its fields, through structs, tuples, and arrays. The glue cannot name + private fields, so it reads the ranges for the library's own target from a table. +- The glue lowers a value into a zeroed C struct, copying only those ranges with an + untyped copy, which keeps a reference's provenance. Every lowering of a value goes + through it: returns, outs, fields, options, lists, and closure arguments. +- Prove it with a padded fixture value, checked in each language to hold zeros where + Rust has padding, and re-measure the bench's stamped write. + +## Waiting + +- Maintainer: a value whose initialized bytes depend on its data, such as one holding an + enum with fields, whose payload's padding differs by variant. Recommended: such a type + stays an object, an `Arc` handle, as before values crossed by value. Rejected: refusing + it, which would refuse a type that exports today; masking by variant, which needs the + discriminant read through Rust on each lowering. + +## Related + +- [Copy types that change cross by value](/quest/m1/ffi-export/copy-values.md) - lowers values the same way From f2e2b2b927a5eb6a99e0fb7712ef6343bc267572 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 05:01:05 -0800 Subject: [PATCH 09/24] fix(ffi): an opaque value's padding crosses zeroed The collector reads each value's padding on its target from rustc's layout, and the glue zeroes it after the typed write, so no stale Rust byte reaches a language: Go would serialize Bytes_, and Kotlin holds the words. A Copy type whose initialized bytes depend on its value, as an enum's variants or a union make them, stays a handle. The api fixture's Gap, made with 0xff padding, proves it in C and Go; with the zeroing off, C sees byte 9 nonzero. In moq, Instant, tokio's Duration, and Specificity had padding. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 6 +- ffi/fixtures/api/examples/transcript.rs | 6 ++ ffi/fixtures/api/src/lib.rs | 39 ++++++++ ffi/src/collect.rs | 123 +++++++++++++++++++++--- ffi/src/glue.rs | 16 ++- ffi/src/ir.rs | 4 + ffi/src/merge.rs | 5 +- ffi/tests/api.c | 18 ++++ ffi/tests/api.cpp | 8 ++ ffi/tests/api.dart | 6 ++ ffi/tests/api.go | 23 +++++ ffi/tests/api.kt | 6 ++ ffi/tests/api.py | 6 ++ ffi/tests/api.swift | 6 ++ quest/m1/ffi-export/copy-values.md | 1 - quest/m1/ffi-export/value-padding.md | 35 ------- 16 files changed, 256 insertions(+), 52 deletions(-) delete mode 100644 quest/m1/ffi-export/value-padding.md diff --git a/ffi/README.md b/ffi/README.md index e1b8458b2..5778cf782 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -71,7 +71,7 @@ re-exports its items instead, which export. A type no path reaches is refused. | --- | --- | | a struct whose fields are all `pub`, not `#[non_exhaustive]`, which map, with no `Drop` of its own | a record: a struct, copied across | | an enum with variants whose fields map, with no `Drop` of its own | a C enum without fields, or a struct of a tag and a union | -| any other type that is `Copy` and `Send`, with no `pub` field and no `pub` method taking `&mut self`, such as moq's `Timestamp` or std's `Instant` | an opaque value: a struct of its bytes, copied across, which only Rust makes or reads; one no export returns is left out, since C could never make one | +| any other type that is `Copy` and `Send`, with no `pub` field, no `pub` method taking `&mut self`, and no union or enum whose variants initialize different bytes, such as an `Option`, in it, such as moq's `Timestamp` or std's `Instant` | an opaque value: a struct of its bytes, copied across, which only Rust makes or reads, with its padding zeroed; one no export returns is left out, since C could never make one | | any other type, if `Send` | an object: an opaque handle to an `Arc`, freed with `_free`; one that is `Copy`, since C may change it in place, is copied out of its handle where Rust takes it by value, and a closure neither takes nor returns one by value | | a type whose only generic parameters are lifetimes, such as `Path<'a>` | an object holding its `'static` form | | a generic type, at each instance it infers | that instance as any other type, with each method whose impl and own bounds it meets; any other instance is refused | @@ -238,7 +238,9 @@ where it was while its file is unchanged. - **Opaque values** are `typedef struct { uintN_t opaque[n]; } ;`, the bytes of a `Copy` type with private fields, in units of its alignment up to 8 bytes: C copies one as any struct, and passes `&self` as a `const *`, but never makes or changes one, - and frees none. Its size and alignment are the largest of the export's targets, so one + and frees none. Its padding is zero, from a table of each target's padding rustc's + layout gives, so no byte Rust left uninitialized reaches a language, which may + serialize what it holds. Its size and alignment are the largest of the export's targets, so one header serves them all, a few bytes past a 32-bit target's own. A change of size is an ABI change, as a record's field change is. - **In and out**: a `&mut` of a value is two parameters, the value passed in as one taken diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 8dadde7ea..84c20d387 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -318,6 +318,12 @@ fn main() { println!("shifted {} {}", shifted.millis(), shifted == moment.after(500)); println!("wrapped {} {}", moment.after(u64::MAX).millis(), moment == shifted); + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + let gap = api::Gap::new(7, 3); + println!("gap {} {}", gap.sum(), gap == api::Gap::new(7, 3)); + let berth = api::Berth::new(Some(4)); + println!("berth {:?} {:?}", berth.at(), api::Berth::new(None).at()); + // A `Copy` type changed in place is a handle, which a call taking it by value copies. let mut score = api::Score::of(2); let step = api::Score::of(3); diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 1b98614f6..a86dbaf6d 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -981,6 +981,45 @@ pub fn shifted(moment: Moment, shift: impl Fn(Moment) -> Moment) -> Moment { shift(moment) } +/// A value with padding, after its `u8`: made from bytes all `0xff`, so its padding likely +/// holds them, which the export zeroes before any language sees it. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Gap { + wide: u64, + narrow: u8, +} + +impl Gap { + pub fn new(wide: u64, narrow: u8) -> Self { + // SAFETY: every byte pattern is a `Gap`, whose fields are integers. + let mut gap: Gap = unsafe { std::mem::transmute([0xffu8; std::mem::size_of::()]) }; + gap.wide = wide; + gap.narrow = narrow; + gap + } + + pub fn sum(&self) -> u64 { + self.wide.wrapping_add(u64::from(self.narrow)) + } +} + +/// A `Copy` type whose `None` leaves its payload's bytes uninitialized, so no padding table +/// covers it: a handle, rather than a value. +#[derive(Clone, Copy, Debug)] +pub struct Berth { + at: Option, +} + +impl Berth { + pub fn new(at: Option) -> Self { + Self { at } + } + + pub fn at(&self) -> Option { + self.at + } +} + /// A running count, as moq's `stats::Traffic`: `Copy` with private fields, but changed in /// place through `&mut self`, which a copy could not write back, so a handle. A call taking /// one by value copies it out of its handle, leaving the caller's as it was. diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index ae2707400..2dcbddf71 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -379,7 +379,7 @@ pub fn collect<'tcx>(tcx: TyCtxt<'tcx>, facade: &Facade<'_>) -> Collected { functions: Vec::new(), constants: Vec::new(), crates: Vec::new(), - targets: vec![tcx.sess.opts.target_triple.tuple().to_string()], + targets: vec![target(tcx)], }, }; c.walk(); @@ -844,13 +844,8 @@ impl<'tcx> Collector<'tcx> { /// Whether the foreign type `did` is a value C holds as its bytes, as a `Copy` one is. fn foreign_value(&self, did: DefId) -> bool { let tcx = self.tcx; - !self.opaque(did) - && implements( - tcx, - tcx.type_of(did).instantiate_identity().skip_norm_wip(), - did, - sym::Copy, - ) + let ty = tcx.type_of(did).instantiate_identity().skip_norm_wip(); + !self.opaque(did) && implements(tcx, ty, did, sym::Copy) && padding(tcx, ty).is_some() } /// Each type a signature the export maps names, bounds included, at every choice @@ -1291,6 +1286,7 @@ impl<'tcx> Collector<'tcx> { methods: Vec::new(), traits: Traits::default(), opaque: None, + padding: Default::default(), }); } Decl::Enum(i) => { @@ -1315,6 +1311,7 @@ impl<'tcx> Collector<'tcx> { let ty = tcx.type_of(def_id).instantiate(tcx, site.args).skip_norm_wip(); let lifetimes = tcx.generics_of(def_id).own_counts().lifetimes; let opaque = opaque(tcx, ty, lifetimes, false); + let padding = padding(tcx, ty).expect("a value's bytes do not depend on it"); api.records.push(Record { path, home: home.clone(), @@ -1328,6 +1325,7 @@ impl<'tcx> Collector<'tcx> { methods: Vec::new(), traits: Traits::default(), opaque: Some(opaque), + padding: [(target(tcx), padding)].into(), }); } Decl::Object(i) => { @@ -1380,6 +1378,7 @@ impl<'tcx> Collector<'tcx> { self.foreign[n].1 = self.api.records.len(); let ty = tcx.type_of(def_id).instantiate_identity().skip_norm_wip(); let opaque = opaque(tcx, ty, 0, true); + let padding = padding(tcx, ty).expect("a value's bytes do not depend on it"); self.api.records.push(Record { path: object.path, home: object.home, @@ -1393,6 +1392,7 @@ impl<'tcx> Collector<'tcx> { methods: object.methods, traits: object.traits, opaque: Some(opaque), + padding: [(target(tcx), padding)].into(), }); } false => { @@ -1676,11 +1676,12 @@ impl<'tcx> Collector<'tcx> { } /// An object, if the type can move across threads, or if it is also `Copy`, a value C - /// holds as its bytes, unless C changes it in place, which a copy could not write back. - /// Shared borrows of an object that is not `Sync` are exclusive. + /// holds as its bytes, unless C changes it in place, which a copy could not write back, + /// or which of its bytes it initializes depends on its value, so its padding could not + /// be zeroed. Shared borrows of an object that is not `Sync` are exclusive. fn object(&self, site: &Site<'tcx>) -> Decl { let (tcx, ty, def_id) = (self.tcx, self.site_ty(site), site.key.def_id); - let copy = implements(tcx, ty, def_id, sym::Copy) && !self.changed(def_id); + let copy = implements(tcx, ty, def_id, sym::Copy) && !self.changed(def_id) && padding(tcx, ty).is_some(); if self.orphans.contains(&site.key) { let construct = match copy { true => "value no export returns", @@ -5104,6 +5105,106 @@ fn opaque<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, lifetimes: usize, foreign: bool } } +/// The target the export is collected for. +fn target(tcx: TyCtxt<'_>) -> String { + tcx.sess.opts.target_triple.tuple().to_string() +} + +/// The padding of `ty` on this target, as `(offset, length)` ranges, which the glue zeroes +/// so no byte Rust leaves uninitialized reaches a language; or none when which bytes it +/// initializes depends on its value: a union, such as `MaybeUninit`, or an enum whose +/// variants initialize different bytes, such as `Option`, whose `None` leaves its +/// payload uninitialized. +fn padding<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option> { + use rustc_abi::{FieldsShape, Variants}; + use rustc_middle::ty::layout::{LayoutCx, TyAndLayout}; + let env = TypingEnv::fully_monomorphized(); + let cx = LayoutCx::new(tcx, env); + let ty = tcx.erase_and_anonymize_regions(ty); + let layout = tcx.layout_of(env.as_query_input(ty)).ok()?; + // Marks in `bytes` each byte at `at` on that `layout` initializes. + fn init<'tcx>(cx: &LayoutCx<'tcx>, layout: TyAndLayout<'tcx>, at: u64, bytes: &mut [bool]) -> Option<()> { + let whole = |bytes: &mut [bool]| bytes[at as usize..(at + layout.size.bytes()) as usize].fill(true); + match layout.ty.kind() { + ty::Adt(adt, _) if adt.is_union() => return None, + ty::Int(_) + | ty::Uint(_) + | ty::Float(_) + | ty::Bool + | ty::Char + | ty::RawPtr(..) + | ty::Ref(..) + | ty::FnPtr(..) => { + whole(bytes); + return Some(()); + } + _ => {} + } + match &layout.variants { + Variants::Empty => Some(()), + Variants::Single { .. } => fields(cx, layout, at, bytes), + Variants::Multiple { + tag, + tag_field, + variants, + .. + } => { + let tag_at = at + layout.fields.offset(tag_field.as_usize()).bytes(); + let tag_len = tag.size(cx).bytes(); + let mut each: Option> = None; + for variant in variants.indices() { + let mut mine = vec![false; bytes.len()]; + mine[tag_at as usize..(tag_at + tag_len) as usize].fill(true); + fields(cx, layout.for_variant(cx, variant), at, &mut mine)?; + match &each { + Some(first) if *first != mine => return None, + Some(_) => {} + None => each = Some(mine), + } + } + for (byte, mine) in bytes.iter_mut().zip(each.unwrap_or_default()) { + *byte |= mine; + } + Some(()) + } + } + } + fn fields<'tcx>(cx: &LayoutCx<'tcx>, layout: TyAndLayout<'tcx>, at: u64, bytes: &mut [bool]) -> Option<()> { + match &layout.fields { + FieldsShape::Primitive => { + bytes[at as usize..(at + layout.size.bytes()) as usize].fill(true); + Some(()) + } + FieldsShape::Union(_) => None, + FieldsShape::Array { stride, count } => { + if *count > 0 { + let element = layout.field(cx, 0); + for n in 0..*count { + init(cx, element, at + n * stride.bytes(), bytes)?; + } + } + Some(()) + } + FieldsShape::Arbitrary { .. } => { + for n in 0..layout.fields.count() { + init(cx, layout.field(cx, n), at + layout.fields.offset(n).bytes(), bytes)?; + } + Some(()) + } + } + } + let mut bytes = vec![false; layout.size.bytes() as usize]; + init(&cx, layout, 0, &mut bytes)?; + let mut ranges: Vec<(u64, u64)> = Vec::new(); + for (n, _) in bytes.iter().enumerate().filter(|(_, init)| !**init) { + match ranges.last_mut() { + Some((start, len)) if *start + *len == n as u64 => *len += 1, + _ => ranges.push((n as u64, 1)), + } + } + Some(ranges) +} + /// Whether `ty`, the type `def_id` at some arguments, implements the std trait `item`. fn implements<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, def_id: DefId, item: rustc_span::Symbol) -> bool { let Some(tr) = tcx.get_diagnostic_item(item) else { diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 2e83d2041..6dd5ec942 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -1050,9 +1050,21 @@ impl Gen<'_> { self.item(format!( "fn lift_{name}(c: &{name}, w: &str) -> {path} {{\n\tlet _ = w;\n\tunsafe {{ c.opaque.as_ptr().cast::<{path}>().read_unaligned() }}\n}}\n" )); - // Zeroed past its own bytes, so a value with no padding has no uninitialized byte. + // Its padding on each target, which the write below leaves uninitialized, so no + // stale byte of Rust's reaches a language, which may serialize what it holds. + let tables: Vec = (record.padding.iter()) + .map(|(target, ranges)| { + let ranges: Vec = ranges.iter().map(|(at, len)| format!("({at}, {len})")).collect(); + format!( + "\t#[cfg({CFG} = {target:?})]\n\tconst PADDING: &[(usize, usize)] = &[{}];\n", + ranges.join(", ") + ) + }) + .collect(); + self.item(format!("impl {name} {{\n{}}}\n", tables.join("\n"))); + // Zeroed past its own bytes, and its padding zeroed after the write. self.item(format!( - "fn lower_{name}(v: {path}) -> {name} {{\n\tlet mut c = {name} {{ opaque: [::std::mem::MaybeUninit::zeroed(); {units}] }};\n\tunsafe {{ c.opaque.as_mut_ptr().cast::<{path}>().write_unaligned(v) }};\n\tc\n}}\n" + "fn lower_{name}(v: {path}) -> {name} {{\n\tlet mut c = {name} {{ opaque: [::std::mem::MaybeUninit::zeroed(); {units}] }};\n\tlet at = c.opaque.as_mut_ptr();\n\tunsafe {{ at.cast::<{path}>().write_unaligned(v) }};\n\tfor &(start, len) in {name}::PADDING {{\n\t\tunsafe {{ at.cast::().add(start).write_bytes(0, len) }};\n\t}}\n\tc\n}}\n" )); self.value_traits(&name, &path, &ty, record.traits); let owner = Ty::Struct(name.clone()); diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index 2694632ed..c50871c17 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -126,6 +126,10 @@ pub struct Record { /// For a `Copy` type with private fields, which has no `fields`: its bytes, which only /// Rust reads. pub opaque: Option, + /// For an opaque value, its padding on each target, by target, as `(offset, length)` + /// ranges, which the glue zeroes, so no byte Rust leaves uninitialized reaches a + /// language. + pub padding: std::collections::BTreeMap>, } /// A `Copy` type held as its bytes, which every language copies and none reads. diff --git a/ffi/src/merge.rs b/ffi/src/merge.rs index a58597159..613b93f17 100644 --- a/ffi/src/merge.rs +++ b/ffi/src/merge.rs @@ -199,12 +199,14 @@ pub fn bare_record(r: &Record) -> Record { r.on = On::ALL; r.methods.clear(); r.fields.iter_mut().for_each(|f| f.docs.clear()); - // Merged apart: one binding holds the largest. + // Merged apart: one binding holds the largest, and each target zeroes its own + // padding. if let Some(opaque) = &mut r.opaque { opaque.size = 0; opaque.align = 0; opaque.dense = true; } + r.padding.clear(); }) } @@ -366,6 +368,7 @@ impl Merged { ours.align = ours.align.max(theirs.align); ours.dense &= theirs.dense; } + self.api.records[at].padding.extend(record.padding); let methods = std::mem::take(&mut self.api.records[at].methods); self.api.records[at].methods = self.functions(methods, record.methods); } diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 9e7c5cdbf..0f7fa2775 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -903,6 +903,24 @@ static int script(void) { printf("wrapped %llu %s\n", (unsigned long long)api_Moment_millis(&wrapped), api_Moment_eq(&moment, &shifted) ? "true" : "false"); + /* A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. */ + api_Gap gap = api_Gap_new(7, 3); + api_Gap same_gap = api_Gap_new(7, 3); + printf("gap %llu %s\n", (unsigned long long)api_Gap_sum(&gap), api_Gap_eq(&gap, &same_gap) ? "true" : "false"); + /* Its `u64` then its `u8`, so every byte past the ninth is padding Rust made `0xff`. */ + for (size_t n = 9; n < sizeof gap; n++) { + if (((const unsigned char *)&gap)[n] != 0) { + fprintf(stderr, "gap's padding byte %zu is not zero\n", n); + return 1; + } + } + api_Berth *berth = api_Berth_new((api_option_u32){.some = true, .value = 4}); + api_Berth *empty_berth = api_Berth_new((api_option_u32){0}); + api_option_u32 berth_at = api_Berth_at(berth); + printf("berth Some(%u) %s\n", berth_at.value, api_Berth_at(empty_berth).some ? "Some" : "None"); + api_Berth_free(empty_berth); + api_Berth_free(berth); + /* A `Copy` type changed in place is a handle, which a call taking it by value copies. */ api_Score *score = api_Score_of(2); api_Score *step = api_Score_of(3); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 6a187e80e..aa32f571b 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -532,6 +532,14 @@ int script() { std::printf("wrapped %llu %s\n", ull(moment.after(UINT64_MAX).millis()), yes(moment == shifted)); } + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + { + api::Gap gap = api::Gap::new_(7, 3); + std::printf("gap %llu %s\n", ull(gap.sum()), yes(gap == api::Gap::new_(7, 3))); + api::Berth berth = api::Berth::new_(4u); + std::printf("berth Some(%u) %s\n", *berth.at(), api::Berth::new_(std::nullopt).at() ? "Some" : "None"); + } + // A `Copy` type changed in place is a handle, which a call taking it by value copies. { api::Score score = api::Score::of(2); diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 231fbd538..64334a6c1 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -329,6 +329,12 @@ Future script() async { print('shifted ${shifted.millis()} ${shifted == moment.after(500)}'); print('wrapped ${moment.after(-1).millis()} ${moment == shifted}'); + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + final gap = api.Gap(7, 3); + print('gap ${gap.sum()} ${gap == api.Gap(7, 3)}'); + final berth = api.Berth(4); + print('berth Some(${berth.at()}) ${api.Berth(null).at() == null ? 'None' : 'Some'}'); + // A `Copy` type changed in place is a handle, which a call taking it by value copies. final score = api.Score.of(2); final increment = api.Score.of(3); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 56bbeeb30..b2fffbc7d 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -21,6 +21,7 @@ import ( "sync" "sync/atomic" "time" + "unsafe" "api/api" "api/dep" @@ -418,6 +419,28 @@ func script() { fmt.Printf("shifted %d %t\n", shifted.Millis(), shifted.Equal(moment.After(500))) fmt.Printf("wrapped %d %t\n", moment.After(math.MaxUint64).Millis(), moment.Equal(shifted)) + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + gap := api.NewGap(7, 3) + fmt.Printf("gap %d %t\n", gap.Sum(), gap.Equal(api.NewGap(7, 3))) + // Its `u64` then its `u8`, so every byte past the ninth is padding Rust made `0xff`. + padding := unsafe.Slice((*byte)(unsafe.Pointer(&gap.Bytes_)), unsafe.Sizeof(gap.Bytes_))[9:] + for n, b := range padding { + if b != 0 { + panic(fmt.Sprintf("gap's padding byte %d is not zero", n+9)) + } + } + four := uint32(4) + berth := api.NewBerth(&four) + empty := api.NewBerth(nil) + berthAt := *must(berth.At()) + emptyAt := "Some" + if must(empty.At()) == nil { + emptyAt = "None" + } + fmt.Printf("berth Some(%d) %s\n", berthAt, emptyAt) + berth.Close() + empty.Close() + // A `Copy` type changed in place is a handle, which a call taking it by value copies. score := api.ScoreOf(2) step := api.ScoreOf(3) diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index aa950659b..d349d45eb 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -260,6 +260,12 @@ fun script() { println("shifted ${shifted.millis()} ${shifted == moment.after(500u)}") println("wrapped ${moment.after(ULong.MAX_VALUE).millis()} ${moment == shifted}") + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + val gap = Gap.new(7u, 3u) + println("gap ${gap.sum()} ${gap == Gap.new(7u, 3u)}") + val berth = Berth.new(4u) + println("berth Some(${berth.at()}) ${if (Berth.new(null).at() == null) "None" else "Some"}") + // A `Copy` type changed in place is a handle, which a call taking it by value copies. val score = Score.of(2u) val step = Score.of(3u) diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 206253224..0aeaa9f4f 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -324,6 +324,12 @@ def edit(counter): except TypeError: pass + # A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + gap = api.Gap.new(7, 3) + print(f"gap {gap.sum()} {flag(gap == api.Gap.new(7, 3))}") + berth = api.Berth.new(4) + print(f"berth Some({berth.at()}) {'None' if api.Berth.new(None).at() is None else 'Some'}") + # A `Copy` type changed in place is a handle, which a call taking it by value copies. score = api.Score.of(2) increment = api.Score.of(3) diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index 459e3d091..739630754 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -406,6 +406,12 @@ func script() { print("shifted \(shifted.millis()) \(shifted == moment.after(500))") print("wrapped \(moment.after(UInt64.max).millis()) \(moment == shifted)") + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + let gap = Gap.new(7, 3) + print("gap \(gap.sum()) \(gap == Gap.new(7, 3))") + let berth = Berth.new(4) + print("berth Some(\(berth.at()!)) \(Berth.new(nil).at() == nil ? "None" : "Some")") + // A `Copy` type changed in place is a handle, which a call taking it by value copies. let score = Score.of(2) let step = Score.of(3) diff --git a/quest/m1/ffi-export/copy-values.md b/quest/m1/ffi-export/copy-values.md index ee79d4fef..e45c06138 100644 --- a/quest/m1/ffi-export/copy-values.md +++ b/quest/m1/ffi-export/copy-values.md @@ -37,4 +37,3 @@ Public API: how every generated API holds these types (breaking). ## Related - [Kotlin values without a LongArray](/quest/m1/ffi-speed/kotlin-values.md) - how Kotlin holds the bytes this writes back -- [Opaque values cross with zeroed padding](/quest/m1/ffi-export/value-padding.md) - zeroes what a value's bytes leave uninitialized diff --git a/quest/m1/ffi-export/value-padding.md b/quest/m1/ffi-export/value-padding.md deleted file mode 100644 index c8e7b86bf..000000000 --- a/quest/m1/ffi-export/value-padding.md +++ /dev/null @@ -1,35 +0,0 @@ -# [M] Opaque values cross with zeroed padding - -## Goal - -A [rust2-ffi](/ffi/README.md) opaque value, a `Copy` type with private fields held as its -bytes, crosses with every padding byte zero, so no stale Rust stack byte reaches the memory -of the language holding it. Today a padded value, such as std's `Instant` on 64-bit Linux -(an `i64` and a `u32`, then 4 bytes of padding), copies its uninitialized padding into the C -struct: C and C++ see it as any struct's padding, and Kotlin's `LongArray`, Go's `Bytes_`, -Dart's `int`s, and Python's `Value` hold it as ordinary bytes. No API reads them, and no -Rust undefined behavior remains, since the codec holds them as `MaybeUninit`, but they are -readable in the language's own process memory. - -## Plan - -- The collector records, per target, the byte ranges a value's fields initialize, from - rustc's layout of its fields, through structs, tuples, and arrays. The glue cannot name - private fields, so it reads the ranges for the library's own target from a table. -- The glue lowers a value into a zeroed C struct, copying only those ranges with an - untyped copy, which keeps a reference's provenance. Every lowering of a value goes - through it: returns, outs, fields, options, lists, and closure arguments. -- Prove it with a padded fixture value, checked in each language to hold zeros where - Rust has padding, and re-measure the bench's stamped write. - -## Waiting - -- Maintainer: a value whose initialized bytes depend on its data, such as one holding an - enum with fields, whose payload's padding differs by variant. Recommended: such a type - stays an object, an `Arc` handle, as before values crossed by value. Rejected: refusing - it, which would refuse a type that exports today; masking by variant, which needs the - discriminant read through Rust on each lowering. - -## Related - -- [Copy types that change cross by value](/quest/m1/ffi-export/copy-values.md) - lowers values the same way From 5936da33b7fa653b28625ceff57a85b2940e6f4d Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:20:35 -0800 Subject: [PATCH 10/24] fix(ffi): tag Python objects with their class, and keep generated names apart Python can reassign `__class__` between classes of one layout, so a `Value` or `Object` now records the class Rust made it as, and a method refuses one reclassed: a reclassed `Stamp` read past its bytes as a `Moment`, and a reclassed handle segfaulted. C++ reserves an opaque value's `c_` member, and Kotlin the `LongArray` its words are held in. The README's closures return opaque values, not handles, and copy-values records the maintainer's answer. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 8 ++++---- ffi/fixtures/api/src/lib.rs | 17 ++++++++++++++++ ffi/runtime/python.rs | 32 +++++++++++++++++++++++++----- ffi/src/cpp.rs | 5 +++++ ffi/src/kotlin.rs | 1 + ffi/tests/api.py | 8 ++++++++ quest/m1/ffi-export/copy-values.md | 25 ++++++++++------------- 7 files changed, 73 insertions(+), 23 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 5778cf782..2b386beab 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -574,8 +574,8 @@ caller imports it with no ignores. - **Closures** are any callable, which Rust calls from any thread, one call at a time, taking the GIL: one lent for a call only during the call, and one Rust keeps until it drops it, when it lets go of its reference. An object it lends is closed once that call - of it returns. What it returns Rust owns: a `str` is copied, - and an object's handle cloned, so the caller's stays its own. One returning a `Result` + of it returns. What it returns Rust owns: a `str` is copied, and an opaque value's + bytes, so the caller's stays its own. One returning a `Result` fails by raising the exception of its error, such as `StringException(message)`, whose `error` Rust takes. Any other exception a closure or trait method raises prints its traceback and aborts, since Rust has nowhere to unwind it to. @@ -787,8 +787,8 @@ reads a go.mod. which releases it to the GC. An object it lends by reference is closed once the func returns. A `Result` returns an `error` last, whose `Error()` crosses as Rust's `String`. What it returns crosses as Rust's: a - `string` copied, and an object as a clone of its handle, so the func keeps its own; a - closed one panics. + `string` copied, and an opaque value as its bytes, so the func keeps its own; a zero one + panics. - **Readers**, `Read + Seek` behind `&mut`, are an `io.ReadSeeker`, such as an `*os.File`, which Rust reads and seeks only during the call, one call at a time, and never closes. A read of bytes and an error hands Rust the bytes, then the error on the next read, as diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index a86dbaf6d..1412b83f4 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -1003,6 +1003,23 @@ impl Gap { } } +/// A value named as Kotlin's `LongArray`, which holds a value's words, with a method named +/// as C++'s member holding its bytes: each generator keeps its own apart. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct LongArray { + words: [u64; 2], +} + +impl LongArray { + pub fn new(low: u64, high: u64) -> Self { + LongArray { words: [low, high] } + } + + pub fn c_(&self) -> u64 { + self.words[0].wrapping_add(self.words[1]) + } +} + /// A `Copy` type whose `None` leaves its payload's bytes uninitialized, so no padding table /// covers it: a handle, rather than a value. #[derive(Clone, Copy, Debug)] diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 85698689b..d0df30dd5 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -818,15 +818,30 @@ pub fn array_out(items: &[u8], index: usize) -> R { call_py(ty(index), vec![bytes]) } +/// A `Value`'s header, before its bytes. +#[repr(C)] +struct ValueHead { + ob_base: PyVarObject, + /// The class registered at this index made it: Python can reassign `__class__`, to + /// any class of `Value`'s layout, whose bytes may be fewer or another type's. + index: usize, +} + /// The `Copy` value a `Value` of the class registered at `index` holds, which only Rust /// made: Python makes none, as `Value` disallows it. pub fn value_in(obj: *mut PyObject, index: usize) -> R { if !is(obj, index) { return expected(obj, index); } - // SAFETY: the GIL is held, and `obj` a `Value` `value_out` made of a `T`, which its - // class always holds; `T` is a struct of `MaybeUninit` units. - unsafe { Ok(obj.cast::().add(size_of::()).cast::().read_unaligned()) } + // SAFETY: the GIL is held, and `obj` a `Value`; once its index matches, one + // `value_out` made of a `T`, which its class always holds; `T` is a struct of + // `MaybeUninit` units. + unsafe { + if (*obj.cast::()).index != index { + return expected(obj, index); + } + Ok(obj.cast::().add(size_of::()).cast::().read_unaligned()) + } } /// A new `Value` of the class registered at `index`, holding `value` in place. @@ -837,7 +852,8 @@ pub fn value_out(index: usize, value: T) -> R { unsafe { let alloc: allocfunc = std::mem::transmute(PyType_GetSlot(class.cast(), Py_tp_alloc)); let obj = Obj::new(alloc(class.cast(), size_of::() as Py_ssize_t))?; - obj.ptr().cast::().add(size_of::()).cast::().write_unaligned(value); + (*obj.ptr().cast::()).index = index; + obj.ptr().cast::().add(size_of::()).cast::().write_unaligned(value); Ok(obj) } } @@ -1442,6 +1458,8 @@ pub unsafe extern "C" fn free_callback(data: *mut c_void) { #[repr(C)] pub struct Handle { ob_base: PyObject, + /// The class registered at this index made it: Python can reassign `__class__`. + index: usize, raw: *const c_void, /// What frees the handle: none for one Rust lends, or once freed. free: Option, @@ -1484,6 +1502,7 @@ fn handle(raw: *const c_void, index: usize, free: Option(); + (*handle).index = index; (*handle).raw = raw; (*handle).free = free; (*handle).holds = 0; @@ -1499,6 +1518,9 @@ pub fn hold(obj: *mut PyObject, index: usize, s: &mut Scope, moved: bool) -> R<* let handle = obj.cast::(); // SAFETY: an object of a class that subclasses `Object`, and the GIL is held. unsafe { + if (*handle).index != index { + return expected(obj, index); + } if (*handle).closed { return value_error(&format!("{} is closed", type_name(ty(index)))); } @@ -2580,7 +2602,7 @@ unsafe fn runtime(module: *mut PyObject) -> R<()> { slot(0, null_mut()), ])?; // Its items are a value's bytes, past the header. - add_sized(module, &VALUE_BASE, c"Value", size_of::(), 1, base, &mut [ + add_sized(module, &VALUE_BASE, c"Value", size_of::(), 1, base, &mut [ slot( Py_tp_doc, c"A Rust `Copy` value's bytes, which only Rust makes or reads.".as_ptr().cast_mut().cast(), diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index da8600efe..047c5e273 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -873,6 +873,11 @@ impl<'a> Header<'a> { let owner = Ty::Struct(c.clone()); let mut taken = fields.clone(); taken.push(name.clone()); + // An opaque value's bytes are its private member `c_`. + if record.opaque.is_some() { + taken.push("c_".to_string()); + members.push("c_".to_string()); + } for method in &record.methods { let (decl, member) = self.function(method, Some(&owner), Some((&taken, Owner::Member))); body.push(decl); diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 1c4b67941..74457f518 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -332,6 +332,7 @@ const TAKEN: &[&str] = &[ "Float", "Int", "Long", + "LongArray", "Short", "StartSeekable", "String", diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 0aeaa9f4f..82a8aaa4d 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -1197,6 +1197,14 @@ def raises(error, f, what): raises(OverflowError, lambda: api.scaled(2**128, 1), "an int past u128 raises") raises(OverflowError, lambda: api.scaled(-1, 1), "a negative int for a u128 raises") + # An object reclassed in Python is still what Rust made: its methods refuse it. + stamp = api.Stamp.from_millis(5) + stamp.__class__ = api.Moment + raises(TypeError, lambda: stamp.millis(), "a Stamp reclassed as a Moment is no Moment") + counter = api.Counter.new("r", 1) + counter.__class__ = api.Berth + raises(TypeError, lambda: counter.at(), "a Counter reclassed as a Berth is no Berth") + # A borrowed tuple is one C value, not one out per element. check(api.corner(True) == (1, 1), "a borrowed tuple returns whole") raises(api.ErrorException, lambda: api.corner(False), "a borrowed tuple's error raises") diff --git a/quest/m1/ffi-export/copy-values.md b/quest/m1/ffi-export/copy-values.md index e45c06138..f4aab9b5e 100644 --- a/quest/m1/ffi-export/copy-values.md +++ b/quest/m1/ffi-export/copy-values.md @@ -3,10 +3,11 @@ ## Goal A [rust2-ffi](/ffi/README.md) export's `Copy` type with a `pub` field or a `pub` method -taking `&mut self` crosses as an opaque value, as every other `Copy` type with private -fields does since [#742](https://github.com/kixel-ai/rust2/pull/742), instead of an `Arc` -handle. A setter or `&mut self` method writes the new bytes back into the language's own -copy, so `x.add(other)` changes `x` and only `x`, as in Rust. moq has five today, each +taking `&mut self` crosses as an opaque value, as a `Send` `Copy` type with only private +fields and value-independent padding does since +[#742](https://github.com/kixel-ai/rust2/pull/742), instead of an `Arc` handle. A setter +or `&mut self` method writes the new bytes back into the language's own copy, so +`x.add(other)` changes `x` and only `x`, as in Rust. moq has five today, each still a handle: `stats::Traffic`, `stats::Presence`, `stats::Failures`, and `stats::Refusals` (`add`), and `clock::WallMap` (`sample`). @@ -20,20 +21,16 @@ still a handle: `stats::Traffic`, `stats::Presence`, `stats::Failures`, and - Prove it with the api fixture's differential tests: a `Copy` type with a `pub` field and a `&mut self` method, mutated through a copy and through the original, and the copy left as it was. +- Kotlin, Dart, and Python, whose values are immutable, hand back a `&mut self` method's + result as its new value, `x = x.add(o)`, with Rust's own return, if any, as a pair: + Python's `Value` and Kotlin's class over words are shared by reference, so mutating one + in place would change every alias, unlike a Rust copy. ✅ the method returns the new + value (rejected: a mutable wrapper type, which allocates; keeping such types as handles + in these three, which splits one Rust type across languages). - Then `ffi/src/collect.rs`'s `changed`, which keeps these types objects today, goes. Public API: how every generated API holds these types (breaking). -## Waiting - -- Maintainer: how Kotlin, Dart, and Python, whose values are immutable, write a - `&mut self` method's result back. Recommended: the method returns the new value, - `x = x.add(o)`, with Rust's own return, if any, as a pair, since Python's `Value` and - Kotlin's class over words are shared by reference, and mutating one in place would - change every alias, unlike a Rust copy. Rejected: keeping the type a handle in these - three only, which splits one Rust type across languages. Done when the maintainer - picks, recorded here. - ## Related - [Kotlin values without a LongArray](/quest/m1/ffi-speed/kotlin-values.md) - how Kotlin holds the bytes this writes back From 01bca97ec05951cb915c1fc8fb7a6a0c33794a00 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:46:15 -0800 Subject: [PATCH 11/24] fix(ffi): read a baseline whose records have no padding Co-Authored-By: Claude Opus 5.5 --- ffi/src/ir.rs | 3 ++- ffi/tests/fixtures.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index c50871c17..1aff61932 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -128,7 +128,8 @@ pub struct Record { pub opaque: Option, /// For an opaque value, its padding on each target, by target, as `(offset, length)` /// ranges, which the glue zeroes, so no byte Rust leaves uninitialized reaches a - /// language. + /// language. A baseline written before it existed has none, as its records had. + #[serde(default)] pub padding: std::collections::BTreeMap>, } diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index e45543d02..43e262e3b 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1435,6 +1435,23 @@ fn lock() { stderr.contains("library `other`: it is now `lock`"), "no library error:\n{stderr}" ); + // A baseline an earlier export wrote, before records had their padding, still reads. + let older = dir.join("older.json"); + let mut json = String::from_utf8(before.clone()).unwrap(); + while let Some(at) = json.find(",\"padding\":{") { + let end = at + json[at..].find('}').unwrap() + 1; + json.replace_range(at..end, ""); + } + std::fs::write(&older, json).unwrap(); + let args = ["--baseline".as_ref(), older.as_os_str()]; + let read = exporter("lock", &dir.join("older"), &targets, &args, &[]) + .output() + .expect("running rust2-ffi"); + assert!( + read.status.success(), + "a baseline without padding failed:\n{}", + String::from_utf8_lossy(&read.stderr) + ); let locked = |feature: &str| { let args = ["--baseline".as_ref(), baseline.as_os_str()]; exporter("lock", &dir.join(feature), &targets, &args, &["--features", feature]) From 30af352cb37bdb584630b5a4d1adb9f39bca78ba Mon Sep 17 00:00:00 2001 From: quest fixer Date: Fri, 13 Nov 2026 11:26:39 -0800 Subject: [PATCH 12/24] fix(ffi): preserve opaque conversions beside record flattening --- ffi/src/kotlin.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 100f6fd0d..658eb5d66 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -1994,7 +1994,7 @@ impl<'a> Gen<'a> { // A record returned directly crosses as its scalars, set into slots the call takes, // and its other fields encoded into the `byte[]` it returns. let flat = match (&export.future, outs.is_empty()) { - (None, true) if self.room > 0 => returned_record(&ok), + (None, true) if self.room > 0 => returned_record(&ok).filter(|(i, _)| abi.api.records[*i].opaque.is_none()), _ => None, }; let flat = flat.map(|(i, option)| { @@ -2411,7 +2411,9 @@ impl<'a> Gen<'a> { for (k, field) in abi.api.records[i].fields.iter().enumerate() { let e = format!("{e}.{}", field_name(&field.name)); let value = match &field.ty { - Type::Record(j) => self.flat_in(*j, &e, (&mut *arg, &mut *n), (w, &mut *writes), r), + Type::Record(j) if abi.api.records[*j].opaque.is_none() => { + self.flat_in(*j, &e, (&mut *arg, &mut *n), (w, &mut *writes), r) + } ty if scalar(abi, ty) => { let p = format!("p{n}"); *n += 1; @@ -2456,7 +2458,9 @@ impl<'a> Gen<'a> { for (k, field) in abi.api.records[i].fields.iter().enumerate() { let v = format!("{v}.f{k}"); fields.push(match &field.ty { - Type::Record(j) => self.flat_out(*j, &v, (a, &mut *slots), (r, &mut *puts)), + Type::Record(j) if abi.api.records[*j].opaque.is_none() => { + self.flat_out(*j, &v, (a, &mut *slots), (r, &mut *puts)) + } ty if scalar(abi, ty) => { let slot = format!("{a}[{}]", slots.len()); let (rust, kt) = match ty { @@ -2507,7 +2511,7 @@ fn scalar(abi: &Abi<'_>, ty: &Type) -> bool { /// Whether the record `i`, or one it nests, holds a field that crosses encoded. fn rest(abi: &Abi<'_>, i: usize) -> bool { abi.api.records[i].fields.iter().any(|f| match &f.ty { - Type::Record(j) => rest(abi, *j), + Type::Record(j) if abi.api.records[*j].opaque.is_none() => rest(abi, *j), ty => !scalar(abi, ty), }) } @@ -2516,7 +2520,7 @@ fn rest(abi: &Abi<'_>, i: usize) -> bool { /// scalar, one for any other, and one for the `byte[]` of its other fields, if any. fn weight(abi: &Abi<'_>, i: usize) -> usize { let fields = abi.api.records[i].fields.iter().map(|f| match &f.ty { - Type::Record(j) => weight(abi, *j) - usize::from(rest(abi, *j)), + Type::Record(j) if abi.api.records[*j].opaque.is_none() => weight(abi, *j) - usize::from(rest(abi, *j)), Type::F64 => 2, Type::Int(int) | Type::NonZero(int) if jni_int(*int) == "jlong" => 2, ty if scalar(abi, ty) => 1, From fcdcc56c8b82fed2eaebe9f3902cf6507bb3ebb8 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:43:03 -0800 Subject: [PATCH 13/24] fix(ffi): count a word opaque value as the two JVM slots its long takes c_slots counted every struct as one slot, so a call near the 255-slot limit flattened a record past it and the JVM refused the class. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/src/lib.rs | 14 ++++++++++++++ ffi/src/kotlin.rs | 8 +++++--- ffi/tests/api.kt | 8 ++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 7160f1219..4175b4420 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -111,6 +111,20 @@ pub fn reading_echo(reading: Reading) -> Reading { reading } +/// Declares `crowded`, whose parameters take 243 of a JVM method's 255 slots, so flattening +/// its `Reading` would take 15 more: a binding that counts a `Stamp` as one slot, not the +/// two of the `long` it crosses as, flattens it past the limit. +macro_rules! crowded { + ($($n:ident)*) => { + /// The sum of the reading's count, both stamps' millis, and every number. + pub fn crowded(reading: Reading, a: Stamp, b: Stamp, $($n: u64),*) -> u64 { + reading.count.wrapping_add(a.millis).wrapping_add(b.millis)$(.wrapping_add($n))* + } + }; +} + +crowded!(n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13 n14 n15 n16 n17 n18 n19 n20 n21 n22 n23 n24 n25 n26 n27 n28 n29 n30 n31 n32 n33 n34 n35 n36 n37 n38 n39 n40 n41 n42 n43 n44 n45 n46 n47 n48 n49 n50 n51 n52 n53 n54 n55 n56 n57 n58 n59 n60 n61 n62 n63 n64 n65 n66 n67 n68 n69 n70 n71 n72 n73 n74 n75 n76 n77 n78 n79 n80 n81 n82 n83 n84 n85 n86 n87 n88 n89 n90 n91 n92 n93 n94 n95 n96 n97 n98 n99 n100 n101 n102 n103 n104 n105 n106 n107 n108 n109 n110 n111 n112 n113 n114 n115 n116 n117 n118); + /// A plain enumeration. #[derive(Clone, Copy, Debug, PartialEq)] pub enum Shape { diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 4eec062ba..d6125240b 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -1977,7 +1977,7 @@ impl<'a> Gen<'a> { // The JVM slots of the 255 a method takes left past what each C parameter takes // unflattened and a future's waker: what flattening records, then the slots a // returned record crosses in, may spend. - let unflattened: usize = export.call.params.iter().map(|(_, c)| c_slots(c)).sum(); + let unflattened: usize = export.call.params.iter().map(|(_, c)| c_slots(abi, c)).sum(); self.room = 255usize.saturating_sub(unflattened + usize::from(export.future.is_some())); let c_name = export.call.name.clone(); let bare = self.bare(&c_name).to_string(); @@ -2656,10 +2656,12 @@ fn weight(abi: &Abi<'_>, i: usize) -> usize { } /// The JVM parameter slots the C parameter `c` takes as a native method's: two for a -/// `long` or `double`, none for what never crosses, and one for anything else. -fn c_slots(c: &Ty) -> usize { +/// `long` or `double`, as an opaque value Kotlin holds in a word is, none for what never +/// crosses, and one for anything else. +fn c_slots(abi: &Abi<'_>, c: &Ty) -> usize { match c { Ty::Void | Ty::Out(_) | Ty::Data | Ty::Poll => 0, + Ty::Struct(_) | Ty::In(_) if opaque_of(abi, c).is_some_and(|o| word(&o)) => 2, Ty::Int(int) if jni_int(*int) == "jlong" => 2, Ty::F64 | Ty::Handle { .. } | Ty::Future(_) => 2, _ => 1, diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 07f82def8..3b9eab38a 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -1041,8 +1041,16 @@ fun readings() { } } +/** A call near the JVM's 255 parameter slots, which a record's flattening must not pass. */ +fun crowding() { + val r = reading(1u)!! + val sum = crowded(r, Stamp.fromMillis(2uL), Stamp.fromMillis(3uL), 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL) + check(sum == r.count + 2uL + 3uL + 119uL, "a crowded call crosses every parameter: $sum") +} + fun owned() { readings() + crowding() budgeted() for (n in 1023..1025) { val payload = ByteArray(n) { it.toByte() } From 7ce117f6d97731ab0f46d178b29540b6d93283aa Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:43:03 -0800 Subject: [PATCH 14/24] fix(ffi): qualify JvmInline, document opaque handles and Kotlin's word class Adds the padding-layout follow-up quest. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 13 ++++++---- ffi/src/kotlin.rs | 2 +- quest/m1/ffi-export/padding-layout.md | 34 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 quest/m1/ffi-export/padding-layout.md diff --git a/ffi/README.md b/ffi/README.md index 4ebdc2410..dc0eb62bc 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -234,8 +234,9 @@ where it was while its file is unchanged. - **Handles** are `Arc`s, and a call borrows the value as Rust's receiver or parameter 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` object, one with a `pub` field or a - `pub` method taking `&mut self`, is copied out instead, leaving its handle as it was. A + `T`, after which the handle is only freed. A `Copy` object, one with a `pub` field, a + `pub` method taking `&mut self`, or bytes its value decides are initialized, as an + `Option` in it does, is copied out instead, leaving its handle as it was. 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: @@ -484,9 +485,11 @@ out, with only its other fields encoded. 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()`. An **opaque value** is a class with an internal constructor, so only Rust makes one: of no more than - 8 bytes on every target, and no padding, a value class over a `Long`, which allocates - nothing; otherwise one over a `LongArray`, whose `==` is Rust's, and whose `hashCode` - is 0, since none Kotlin computes agrees with it. + 8 bytes on every target, with no padding, fields of integers, `bool`s, and `char`s alone, nested or not, and a + derived or std `PartialEq`, a value class over a `Long`, compared by its bits, which + allocates nothing unless Kotlin boxes it, as in a `List` or a nullable; otherwise one over + a `LongArray`, whose `==` is Rust's, and whose `hashCode` is 0, since none Kotlin + computes agrees with it. - **Bytes** passed in are a `ByteArray`, copied, as are those Rust lends a closure or trait method. Bytes Rust returns, or a record holds, are a `Bytes`, read only through `size`, `get`, `slice`, `copyInto`, and `toByteArray()`, so no view of Rust's memory escapes and diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index d6125240b..d27266730 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -1415,7 +1415,7 @@ impl<'a> Gen<'a> { let words = opaque.words(); let (head, write, read) = match word(opaque) { true => ( - format!("@JvmInline\nvalue class {name} internal constructor(internal val bits: Long)"), + format!("@kotlin.jvm.JvmInline\nvalue class {name} internal constructor(internal val bits: Long)"), "\tw.i64(v.bits)\n".to_string(), format!("{name}(r.i64())"), ), diff --git a/quest/m1/ffi-export/padding-layout.md b/quest/m1/ffi-export/padding-layout.md new file mode 100644 index 000000000..d7994553b --- /dev/null +++ b/quest/m1/ffi-export/padding-layout.md @@ -0,0 +1,34 @@ +# [M] Opaque padding from the rustc the glue builds with + +## Goal + +A [rust2-ffi](/ffi/README.md) export zeroes an opaque value's padding from a table only +when the table describes the layout the glue is compiled with. Today `collect.rs` reads +each target's padding from the exporter's own nightly `rustc`, while the glue builds with +the `rustc` cargo resolved, and Rust promises no `repr(Rust)` layout across compilers. A +compiler that orders a type's fields differently makes `lower_` zero a live field on +every write, silently. That mismatch fails the glue's build instead. + +## Plan + +- The glue can't name a private field, so it can't check an offset itself; the table's + provenance has to be pinned instead. +- Recommended: the export records the commit hash of the `rustc` cargo resolved, the one + it already runs for a build script's probe, beside each table, and the glue's build + script refuses any other `rustc` with an error naming both. The table itself then has + to come from that `rustc`'s layout, such as its `-Zprint-type-sizes` field offsets under + `RUSTC_BOOTSTRAP`, rather than the exporter's nightly. +- Prove it with a test that builds the glue with a `rustc` whose hash differs from the + recorded one and expects the refusal, and the api fixture's `Gap` padding test still + passing with the matching one. +- The API lock leaves padding out: it is no part of what a binding's caller compiles + against, so a change of it is no break. + +## Waiting + +- Maintainer: how the table is tied to the glue's layout. Options: pin the glue's `rustc` + to the one whose layout the export read (recommended: exact, and fails loudly at + build); compare only each target's size and alignment exactly (rejected: misses a + reorder of the same size); stop zeroing padding (rejected: stale Rust bytes reach + languages that serialize a value). An agent cannot pick, since pinning the glue's + `rustc` constrains every consumer's toolchain. From 11c4e559b91f74dd1ecc9cb4f400bd1f70456bf3 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:51:04 -0800 Subject: [PATCH 15/24] fix(ffi): finish the merge: lock test records and fixture lines Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/lock/expected.txt | 2 +- ffi/fixtures/reject/expected.txt | 2 +- ffi/src/lock.rs | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ffi/fixtures/lock/expected.txt b/ffi/fixtures/lock/expected.txt index b5cd947ec..5ac7bfb49 100644 --- a/ffi/fixtures/lock/expected.txt +++ b/ffi/fixtures/lock/expected.txt @@ -8,8 +8,8 @@ fixtures/lock/src/lib.rs:191:1: rust2-ffi: the API breaks the baseline at tuple fixtures/lock/src/lib.rs:202:1: rust2-ffi: the API breaks the baseline at struct `lock::Tick`: its bytes: 4 aligned to 4, and now 8 aligned to 4 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:231:1: rust2-ffi: the API breaks the baseline at struct `lock::Beat`: how it is held: a word compared by its bits, and now bytes compared by Rust -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:356: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: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/reject/expected.txt b/ffi/fixtures/reject/expected.txt index 69d49fe33..f4426cc25 100644 --- a/ffi/fixtures/reject/expected.txt +++ b/ffi/fixtures/reject/expected.txt @@ -9,8 +9,8 @@ fixtures/reject/src/lib.rs:1026:1: rust2-ffi: cannot export fn `wide_non_zero`: fixtures/reject/src/lib.rs:1048:1: rust2-ffi: cannot export fn `wrap`: returned object that borrows `View<'_>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:1064:1: rust2-ffi: cannot export fn `picked`: closure signature `&'static Mark`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:1069:1: rust2-ffi: cannot export fn `maybe_picked`: closure signature `std::option::Option<&'static Mark>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1031:1: rust2-ffi: cannot export fn `boxed_bytes`: boxed slice of bytes `std::boxed::Box<[u8]>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:106:2: rust2-ffi: cannot export method `Text::later`: returned object that borrows `impl std::future::Future>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1074:1: rust2-ffi: cannot export fn `boxed_bytes`: boxed slice of bytes `std::boxed::Box<[u8]>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:112:1: rust2-ffi: cannot export fn `forever`: 'static borrow `&'static [u8]`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:118:1: rust2-ffi: cannot export fn `boxed`: reference inside a parameter `std::boxed::Box<&str>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:11:1: rust2-ffi: cannot export fn `map`: foreign type `std::collections::HashMap`; exclude it in the config to leave it out diff --git a/ffi/src/lock.rs b/ffi/src/lock.rs index 0120b4267..f49126e12 100644 --- a/ffi/src/lock.rs +++ b/ffi/src/lock.rs @@ -854,6 +854,8 @@ mod tests { tuple: false, methods: vec![], traits: Traits::default(), + opaque: None, + padding: Default::default(), } } From b882efb7cdbf7fa041f588f7bd40f82ad175c0b9 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:00:36 -0800 Subject: [PATCH 16/24] quest: record padding-layout follow-up answers Co-Authored-By: Claude Opus 5.5 --- quest/m1/ffi-export/padding-layout.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/quest/m1/ffi-export/padding-layout.md b/quest/m1/ffi-export/padding-layout.md index d7994553b..671aa60f9 100644 --- a/quest/m1/ffi-export/padding-layout.md +++ b/quest/m1/ffi-export/padding-layout.md @@ -11,6 +11,13 @@ every write, silently. That mismatch fails the glue's build instead. ## Plan +- The maintainer settled on 2026-11-13: ✅ keep the quest (rejected: drop). How the table + is tied to the glue's layout: ✅ decide later (rejected for now: pin the glue's `rustc`; + padding-free types only). The decision stays under Waiting. +- First step, before the maintainer picks: measure which of moq's opaque values have + padding, and report it on the decision. +- Pinning would make every consumer build the glue with rust2's nightly, which today only + the exporter needs. - The glue can't name a private field, so it can't check an offset itself; the table's provenance has to be pinned instead. - Recommended: the export records the commit hash of the `rustc` cargo resolved, the one @@ -30,5 +37,9 @@ every write, silently. That mismatch fails the glue's build instead. to the one whose layout the export read (recommended: exact, and fails loudly at build); compare only each target's size and alignment exactly (rejected: misses a reorder of the same size); stop zeroing padding (rejected: stale Rust bytes reach - languages that serialize a value). An agent cannot pick, since pinning the glue's + languages that serialize a value); cross as raw bytes only types whose leaf field sizes + add up to their size, so no padding exists under any field order, assert that size in + the glue, and cross padded types as objects (handles): sound on any `rustc` with no + table and no pin, but padded types such as `Instant` pay a handle. Measure moq's padded + opaque values first; the maintainer picks after. An agent cannot pick, since pinning the glue's `rustc` constrains every consumer's toolchain. From ea888ad0836a5dbdcb22a3397abe40f2a0462be6 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:47:29 -0800 Subject: [PATCH 17/24] fix(ffi): know std by its crates in dense, assert Copy in the glue, require padding-layout before ship --- ffi/src/collect.rs | 24 +++++++++++++++--------- ffi/src/glue.rs | 2 ++ quest/m1/ffi-export/ship.md | 1 + 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 660c10c1e..d2fcd2451 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1493,7 +1493,7 @@ impl<'tcx> Collector<'tcx> { *i = api.records.len(); let ty = tcx.type_of(def_id).instantiate(tcx, site.args).skip_norm_wip(); let lifetimes = tcx.generics_of(def_id).own_counts().lifetimes; - let opaque = opaque(tcx, ty, lifetimes, false); + let opaque = opaque(tcx, &self.sysroot, ty, lifetimes, false); let padding = padding(tcx, ty).expect("a value's bytes do not depend on it"); api.records.push(Record { path, @@ -1560,7 +1560,7 @@ impl<'tcx> Collector<'tcx> { true => { self.foreign[n].1 = self.api.records.len(); let ty = tcx.type_of(def_id).instantiate_identity().skip_norm_wip(); - let opaque = opaque(tcx, ty, 0, true); + let opaque = opaque(tcx, &self.sysroot, ty, 0, true); let padding = padding(tcx, ty).expect("a value's bytes do not depend on it"); self.api.records.push(Record { path: object.path, @@ -5331,7 +5331,7 @@ fn marker(tcx: TyCtxt<'_>, tr: DefId) -> bool { } /// How C holds the `Copy` type `ty`, with `lifetimes` lifetime parameters, as its bytes. -fn opaque<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, lifetimes: usize, foreign: bool) -> crate::ir::Opaque { +fn opaque<'tcx>(tcx: TyCtxt<'tcx>, sysroot: &[CrateNum], ty: Ty<'tcx>, lifetimes: usize, foreign: bool) -> crate::ir::Opaque { let env = TypingEnv::fully_monomorphized(); let ty = tcx.erase_and_anonymize_regions(ty); // A type that has a layout on its target, as every exported `Copy` type does. @@ -5341,13 +5341,19 @@ fn opaque<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, lifetimes: usize, foreign: bool .and_then(|tr| (tcx.associated_item_def_ids(tr).iter().copied()).find(|&f| tcx.item_name(f) == sym::eq)); // Whether every byte of `ty` belongs to an integer, `bool`, or `char` in it, and its // `PartialEq`, if any, is derived or std's, so equal values have equal bytes. - fn dense<'tcx>(tcx: TyCtxt<'tcx>, env: TypingEnv<'tcx>, eq: Option, ty: Ty<'tcx>) -> bool { + fn dense<'tcx>( + tcx: TyCtxt<'tcx>, + sysroot: &[CrateNum], + env: TypingEnv<'tcx>, + eq: Option, + ty: Ty<'tcx>, + ) -> bool { match ty.kind() { ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char => true, - ty::Pat(base, _) => dense(tcx, env, eq, *base), - ty::Array(element, _) => dense(tcx, env, eq, *element), + ty::Pat(base, _) => dense(tcx, sysroot, env, eq, *base), + ty::Array(element, _) => dense(tcx, sysroot, env, eq, *element), ty::Adt(adt, args) if adt.is_struct() => { - let std = matches!(tcx.crate_name(adt.did().krate).as_str(), "core" | "alloc" | "std"); + let std = sysroot.contains(&adt.did().krate); let derived = || { let Some(eq) = eq else { return true }; match ty::Instance::try_resolve(tcx, env, eq, tcx.mk_args(&[ty.into(), ty.into()])) { @@ -5374,7 +5380,7 @@ fn opaque<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, lifetimes: usize, foreign: bool let Ok(layout) = tcx.layout_of(env.as_query_input(field)) else { return false; }; - if !dense(tcx, env, eq, field) { + if !dense(tcx, sysroot, env, eq, field) { return false; } covered += layout.size.bytes(); @@ -5387,7 +5393,7 @@ fn opaque<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, lifetimes: usize, foreign: bool crate::ir::Opaque { size: own.size().bytes(), align: own.align().bytes(), - dense: dense(tcx, env, eq, ty), + dense: dense(tcx, sysroot, env, eq, ty), lifetimes, foreign, } diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 779bfe828..5264331b9 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -1185,6 +1185,8 @@ impl Gen<'_> { self.item(format!( "const _: () = assert!(::std::mem::size_of::<{path}>() <= ::std::mem::size_of::<{name}>(), \"{name} is smaller than its Rust type\");\n" )); + // A layout check alone passes a type that lost `Copy`, which `lift_` would then drop twice. + self.item(format!("const _: fn() = {{\n\tfn copy() {{}}\n\tcopy::<{path}>\n}};\n")); self.item(format!( "fn lift_{name}(c: &{name}, w: &str) -> {path} {{\n\tlet _ = w;\n\tunsafe {{ c.opaque.as_ptr().cast::<{path}>().read_unaligned() }}\n}}\n" )); diff --git a/quest/m1/ffi-export/ship.md b/quest/m1/ffi-export/ship.md index 777d9dafb..ae1ff4770 100644 --- a/quest/m1/ffi-export/ship.md +++ b/quest/m1/ffi-export/ship.md @@ -23,6 +23,7 @@ then see change. A sibling planned later adds itself to `Required` here. - [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 that change cross by value](/quest/m1/ffi-export/copy-values.md) - how moq's stats and `WallMap` are held in every language +- [Opaque padding from the glue's rustc](/quest/m1/ffi-export/padding-layout.md) - that zeroing an opaque value's padding never zeroes a live field - [Not Send](/quest/m1/ffi-export/not-send.md) - the types and futures that export checked by thread - [Export moq for every target](/quest/m1/ffi-export/moq-targets.md) - one binding across the targets moq ships - [Owned buffers](/quest/m1/ffi-export/owned-bufs.md) - what a buffer's owner means on the way in From 4e1fd3f0295f49444f599c79bcb51d015a985068 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:48:14 -0800 Subject: [PATCH 18/24] style(ffi): fmt --- ffi/src/collect.rs | 8 +++++++- ffi/src/glue.rs | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index d2fcd2451..f6bedeebe 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -5331,7 +5331,13 @@ fn marker(tcx: TyCtxt<'_>, tr: DefId) -> bool { } /// How C holds the `Copy` type `ty`, with `lifetimes` lifetime parameters, as its bytes. -fn opaque<'tcx>(tcx: TyCtxt<'tcx>, sysroot: &[CrateNum], ty: Ty<'tcx>, lifetimes: usize, foreign: bool) -> crate::ir::Opaque { +fn opaque<'tcx>( + tcx: TyCtxt<'tcx>, + sysroot: &[CrateNum], + ty: Ty<'tcx>, + lifetimes: usize, + foreign: bool, +) -> crate::ir::Opaque { let env = TypingEnv::fully_monomorphized(); let ty = tcx.erase_and_anonymize_regions(ty); // A type that has a layout on its target, as every exported `Copy` type does. diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 5264331b9..9a82d1e44 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -1186,7 +1186,9 @@ impl Gen<'_> { "const _: () = assert!(::std::mem::size_of::<{path}>() <= ::std::mem::size_of::<{name}>(), \"{name} is smaller than its Rust type\");\n" )); // A layout check alone passes a type that lost `Copy`, which `lift_` would then drop twice. - self.item(format!("const _: fn() = {{\n\tfn copy() {{}}\n\tcopy::<{path}>\n}};\n")); + self.item(format!( + "const _: fn() = {{\n\tfn copy() {{}}\n\tcopy::<{path}>\n}};\n" + )); self.item(format!( "fn lift_{name}(c: &{name}, w: &str) -> {path} {{\n\tlet _ = w;\n\tunsafe {{ c.opaque.as_ptr().cast::<{path}>().read_unaligned() }}\n}}\n" )); From 8c79bccfdc0a4339fd7a6fc9b4bc160a1dea19d6 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:08:45 -0800 Subject: [PATCH 19/24] fix(ffi): an impl Trait return hiding a value crosses as its bytes --- ffi/fixtures/api/examples/transcript.rs | 2 ++ ffi/fixtures/api/src/lib.rs | 5 +++++ ffi/src/collect.rs | 10 +++++----- ffi/src/glue.rs | 5 +++-- ffi/tests/api.c | 2 ++ ffi/tests/api.cpp | 1 + ffi/tests/api.dart | 1 + ffi/tests/api.go | 1 + ffi/tests/api.kt | 1 + ffi/tests/api.py | 1 + ffi/tests/api.swift | 1 + 11 files changed, 23 insertions(+), 7 deletions(-) diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 56eba5d32..9a6e6fe38 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -299,6 +299,8 @@ fn main() { println!("secs {}", stamp.secs()); println!("later {}", Stamp::from_millis(3000).later(stamp)); println!("secs {}", stamp.secs()); + let whole: Stamp = stamp.whole().into(); + println!("whole {whole:?}"); let mut message = Message::new(Some(stamp), Bytes::from_static(b"body")); println!("payload {}", String::from_utf8_lossy(&message.payload)); println!("stamp {:?}", message.stamp.unwrap()); diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 5933ec886..9784c418c 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -1022,6 +1022,11 @@ impl Stamp { self.millis / 1000 } + /// It rounded down to whole seconds, through an `impl Trait`, so it crosses as its bytes. + pub fn whole(self) -> impl Into + use<> { + Self { millis: self.millis / 1000 * 1000 } + } + pub fn later(self, other: Stamp) -> bool { self.millis > other.millis } diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index f6bedeebe..1666c6c90 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -2740,7 +2740,7 @@ impl<'tcx> Collector<'tcx> { let lendable = match unbound.inputs().first().map(|ty| ty.kind()) { Some(&ty::Ref(region, inner, ty::Mutability::Not)) if has_self - && matches!(inner.kind(), ty::Adt(adt, args) if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_)))) => + && matches!(inner.kind(), ty::Adt(adt, args) if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_)))) => { let regions: Vec<_> = (unbound.output().walk()).filter_map(|arg| arg.as_region()).collect(); !regions.is_empty() @@ -2755,7 +2755,7 @@ impl<'tcx> Collector<'tcx> { let written: Vec<_> = (unbound.inputs().iter()) .filter_map(|input| match input.kind() { ty::Ref(region, inner, ty::Mutability::Mut) - if !matches!(inner.kind(), ty::Adt(adt, args) if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_)))) => + if !matches!(inner.kind(), ty::Adt(adt, args) if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_)))) => { Some(*region) } @@ -2997,7 +2997,7 @@ impl<'tcx> Collector<'tcx> { _ => output, }; // The glue cannot name an `impl Trait`'s hidden type, so one in a return that maps - // crosses only as what the glue passes on whole, an object's handle. A future and a + // crosses only as what the glue passes on whole: an object's handle, or a value's bytes. A future and a // collected iterator are taken apart instead, so it is what their output or items // name that crosses. let unrevealed = sig.output(); @@ -3025,7 +3025,7 @@ impl<'tcx> Collector<'tcx> { } let hidden = normalize(ty); if !matches!(hidden.kind(), ty::Adt(adt, args) - if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_)))) + if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_)))) { errs.push(why("opaque type", ty.to_string())); } @@ -4339,7 +4339,7 @@ impl<'tcx> Collector<'tcx> { // since they may be the signature's bound ones, which a key never names. let erased = ty::fold_regions(tcx, hidden, |_, _| tcx.lifetimes.re_erased); let object = matches!(erased.kind(), ty::Adt(adt, args) - if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_)))); + if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_)))); if object { held.extend(args.iter().flat_map(|arg| arg.walk()).filter_map(|arg| arg.as_region())); } diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 9a82d1e44..6c8d984dd 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -1204,9 +1204,10 @@ impl Gen<'_> { }) .collect(); self.item(format!("impl {name} {{\n{}}}\n", tables.join("\n"))); - // Zeroed past its own bytes, and its padding zeroed after the write. + // Zeroed past its own bytes, and its padding zeroed after the write. Generic, since + // an `impl Trait` return hides the type it writes, which the collector checked is this. self.item(format!( - "fn lower_{name}(v: {path}) -> {name} {{\n\tlet mut c = {name} {{ opaque: [::std::mem::MaybeUninit::zeroed(); {units}] }};\n\tlet at = c.opaque.as_mut_ptr();\n\tunsafe {{ at.cast::<{path}>().write_unaligned(v) }};\n\tfor &(start, len) in {name}::PADDING {{\n\t\tunsafe {{ at.cast::().add(start).write_bytes(0, len) }};\n\t}}\n\tc\n}}\n" + "fn lower_{name}(v: T) -> {name} {{\n\tconst {{ assert!(::std::mem::size_of::() == ::std::mem::size_of::<{path}>() && ::std::mem::align_of::() == ::std::mem::align_of::<{path}>()) }};\n\tlet mut c = {name} {{ opaque: [::std::mem::MaybeUninit::zeroed(); {units}] }};\n\tlet at = c.opaque.as_mut_ptr();\n\tunsafe {{ at.cast::().write_unaligned(v) }};\n\tfor &(start, len) in {name}::PADDING {{\n\t\tunsafe {{ at.cast::().add(start).write_bytes(0, len) }};\n\t}}\n\tc\n}}\n" )); self.value_traits(&name, &path, &ty, record.traits); let owner = Ty::Struct(name.clone()); diff --git a/ffi/tests/api.c b/ffi/tests/api.c index bffe3a54a..54363165c 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -984,6 +984,8 @@ static int script(void) { api_Stamp later = api_Stamp_from_millis(3000); printf("later %s\n", api_Stamp_later(&later, stamp) ? "true" : "false"); printf("secs %llu\n", (unsigned long long)api_Stamp_secs(&stamp)); + api_Stamp rounded = api_Stamp_whole(&stamp); + print("whole ", api_Stamp_debug(&rounded), "\n"); api_Message *message = api_Message_new((api_option_Stamp){.some = true, .value = stamp}, str("body")); print("payload ", api_Message_payload_get(message), "\n"); api_option_Stamp read = api_Message_stamp_get(message); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index cdce9e5cd..f46cbe5c1 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -565,6 +565,7 @@ int script() { std::printf("secs %llu\n", ull(stamp.secs())); std::printf("later %s\n", yes(api::Stamp::from_millis(3000).later(stamp))); std::printf("secs %llu\n", ull(stamp.secs())); + print("whole ", stamp.whole().debug(), "\n"); api::Message message = api::Message::new_(stamp, "body"); print("payload ", message.payload_get(), "\n"); print("stamp ", message.stamp_get()->debug(), "\n"); diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 74bfa650e..f78e7c8f8 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -337,6 +337,7 @@ Future script() async { print('secs ${stamp.secs()}'); print('later ${api.Stamp.fromMillis(3000).later(stamp)}'); print('secs ${stamp.secs()}'); + print('whole ${stamp.whole().debug()}'); final message = api.Message(stamp, utf8.encode('body')); print('payload ${text(message.payloadGet)}'); print('stamp ${message.stampGet!.debug()}'); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 422457e6d..084bc7540 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -423,6 +423,7 @@ func script() { fmt.Printf("secs %d\n", stamp.Secs()) fmt.Printf("later %t\n", api.StampFromMillis(3000).Later(stamp)) fmt.Printf("secs %d\n", stamp.Secs()) + fmt.Printf("whole %s\n", stamp.Whole().GoString()) message := api.NewMessage(&stamp, []byte("body")) fmt.Printf("payload %s\n", must(message.PayloadGet()).Copy()) fmt.Printf("stamp %s\n", must(message.StampGet()).GoString()) diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 4ccf94a2d..e325b8300 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -261,6 +261,7 @@ fun script() { println("secs ${stamp.secs()}") println("later ${Stamp.fromMillis(3000u).later(stamp)}") println("secs ${stamp.secs()}") + println("whole ${stamp.whole().debug()}") val message = Message.new(stamp, "body".encodeToByteArray()) println("payload ${message.payloadGet.text()}") println("stamp ${message.stampGet!!.debug()}") diff --git a/ffi/tests/api.py b/ffi/tests/api.py index ef5ea67ad..ea6efceb5 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -325,6 +325,7 @@ def edit(counter): print(f"secs {stamp.secs()}") print(f"later {flag(api.Stamp.from_millis(3000).later(stamp))}") print(f"secs {stamp.secs()}") + print(f"whole {stamp.whole()!r}") message = api.Message.new(stamp, b"body") print(f"payload {text(message.payload_get)}") print(f"stamp {message.stamp_get!r}") diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index b4b39a3fc..a6f13b753 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -403,6 +403,7 @@ func script() { print("secs \(stamp.secs())") print("later \(Stamp.fromMillis(3000).later(stamp))") print("secs \(stamp.secs())") + print("whole \(debug(stamp.whole()))") var message = Message.new(stamp, Data("body".utf8)) print("payload \(text(message.payloadGet))") print("stamp \(debug(message.stampGet!))") From 086dc3527d0f54c02cbc72f1cd01b78e4ea47b65 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:09:28 -0800 Subject: [PATCH 20/24] style(ffi): fmt --- ffi/fixtures/api/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 9784c418c..392511d1c 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -1024,7 +1024,9 @@ impl Stamp { /// It rounded down to whole seconds, through an `impl Trait`, so it crosses as its bytes. pub fn whole(self) -> impl Into + use<> { - Self { millis: self.millis / 1000 * 1000 } + Self { + millis: self.millis / 1000 * 1000, + } } pub fn later(self, other: Stamp) -> bool { From f314f58d318c19c098f923cab9a649826ad7d5ac Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:16:55 -0800 Subject: [PATCH 21/24] fix(ffi): keep lending and write-back guards to objects --- ffi/src/collect.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 1666c6c90..67838c6f3 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -2740,7 +2740,7 @@ impl<'tcx> Collector<'tcx> { let lendable = match unbound.inputs().first().map(|ty| ty.kind()) { Some(&ty::Ref(region, inner, ty::Mutability::Not)) if has_self - && matches!(inner.kind(), ty::Adt(adt, args) if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_)))) => + && matches!(inner.kind(), ty::Adt(adt, args) if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_)))) => { let regions: Vec<_> = (unbound.output().walk()).filter_map(|arg| arg.as_region()).collect(); !regions.is_empty() @@ -2755,7 +2755,7 @@ impl<'tcx> Collector<'tcx> { let written: Vec<_> = (unbound.inputs().iter()) .filter_map(|input| match input.kind() { ty::Ref(region, inner, ty::Mutability::Mut) - if !matches!(inner.kind(), ty::Adt(adt, args) if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_)))) => + if !matches!(inner.kind(), ty::Adt(adt, args) if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_)))) => { Some(*region) } @@ -2997,8 +2997,8 @@ impl<'tcx> Collector<'tcx> { _ => output, }; // The glue cannot name an `impl Trait`'s hidden type, so one in a return that maps - // crosses only as what the glue passes on whole: an object's handle, or a value's bytes. A future and a - // collected iterator are taken apart instead, so it is what their output or items + // crosses only as what the glue passes on whole: an object's handle, or a value's + // bytes. A future and a collected iterator are taken apart instead, so it is what their output or items // name that crosses. let unrevealed = sig.output(); let mut named = if ok.is_ok() { vec![unrevealed] } else { Vec::new() }; @@ -4339,7 +4339,7 @@ impl<'tcx> Collector<'tcx> { // since they may be the signature's bound ones, which a key never names. let erased = ty::fold_regions(tcx, hidden, |_, _| tcx.lifetimes.re_erased); let object = matches!(erased.kind(), ty::Adt(adt, args) - if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_)))); + if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_)))); if object { held.extend(args.iter().flat_map(|arg| arg.walk()).filter_map(|arg| arg.as_region())); } From 530c7f71f12c4960aa5246082ca6e83614306761 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:23:31 -0800 Subject: [PATCH 22/24] fix(ffi): an impl Trait hiding a value holds what it captures --- ffi/fixtures/reject/expected.txt | 13 +++++++------ ffi/fixtures/reject/src/lib.rs | 5 +++++ ffi/src/collect.rs | 15 ++++++++------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/ffi/fixtures/reject/expected.txt b/ffi/fixtures/reject/expected.txt index 6f6e23448..1cc7b8121 100644 --- a/ffi/fixtures/reject/expected.txt +++ b/ffi/fixtures/reject/expected.txt @@ -7,13 +7,14 @@ fixtures/reject/src/lib.rs:1021:1: rust2-ffi: cannot export fn `hazard`: trait o fixtures/reject/src/lib.rs:1011:1: note: C cannot implement `Hazard`: unsafe fn `risk` fixtures/reject/src/lib.rs:1026:1: rust2-ffi: cannot export fn `wide_non_zero`: NonZero `u128`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:1048:1: rust2-ffi: cannot export fn `wrap`: returned object that borrows `View<'_>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1064:1: rust2-ffi: cannot export fn `picked`: closure signature `&'static Mark`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1069:1: rust2-ffi: cannot export fn `maybe_picked`: closure signature `std::option::Option<&'static Mark>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1053:1: rust2-ffi: cannot export fn `hidden_wrap`: returned object that borrows `impl std::clone::Clone + '_`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1069:1: rust2-ffi: cannot export fn `picked`: closure signature `&'static Mark`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:106:2: rust2-ffi: cannot export method `Text::later`: returned object that borrows `impl std::future::Future>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1074:1: rust2-ffi: cannot export fn `hidden_answer`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1079:1: rust2-ffi: cannot export fn `hidden_items`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1084:1: rust2-ffi: cannot export fn `hidden_option`: opaque type `impl std::clone::Clone`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1089:1: rust2-ffi: cannot export fn `boxed_bytes`: boxed slice of bytes `std::boxed::Box<[u8]>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1074:1: rust2-ffi: cannot export fn `maybe_picked`: closure signature `std::option::Option<&'static Mark>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1079:1: rust2-ffi: cannot export fn `hidden_answer`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1084:1: rust2-ffi: cannot export fn `hidden_items`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1089:1: rust2-ffi: cannot export fn `hidden_option`: opaque type `impl std::clone::Clone`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1094:1: rust2-ffi: cannot export fn `boxed_bytes`: boxed slice of bytes `std::boxed::Box<[u8]>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:112:1: rust2-ffi: cannot export fn `forever`: 'static borrow `&'static [u8]`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:118:1: rust2-ffi: cannot export fn `boxed`: reference inside a parameter `std::boxed::Box<&str>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:11:1: rust2-ffi: cannot export fn `map`: foreign type `std::collections::HashMap`; exclude it in the config to leave it out diff --git a/ffi/fixtures/reject/src/lib.rs b/ffi/fixtures/reject/src/lib.rs index 0529aa575..a6312acbb 100644 --- a/ffi/fixtures/reject/src/lib.rs +++ b/ffi/fixtures/reject/src/lib.rs @@ -1049,6 +1049,11 @@ pub fn wrap(bytes: &[u8]) -> View<'_> { View { bytes } } +/// The same, through an `impl Trait` hiding the value, which holds what it captures. +pub fn hidden_wrap(bytes: &[u8]) -> impl Clone + '_ { + View { bytes } +} + /// A `Copy` mark held across as its bytes. #[derive(Clone, Copy)] pub struct Mark { diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 67838c6f3..3d88be9a2 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -2998,8 +2998,8 @@ impl<'tcx> Collector<'tcx> { }; // The glue cannot name an `impl Trait`'s hidden type, so one in a return that maps // crosses only as what the glue passes on whole: an object's handle, or a value's - // bytes. A future and a collected iterator are taken apart instead, so it is what their output or items - // name that crosses. + // bytes. A future and a collected iterator are taken apart instead, so it is what + // their output or items name that crosses. let unrevealed = sig.output(); let mut named = if ok.is_ok() { vec![unrevealed] } else { Vec::new() }; while let Some(ty) = named.pop() { @@ -4333,13 +4333,14 @@ impl<'tcx> Collector<'tcx> { hidden = tcx.type_of(*def_id).instantiate(tcx, args).skip_norm_wip(); } // One whose hidden type is an object crosses as its handle, which the glue - // keeps as the opaque type itself, so it holds every lifetime it captures, - // even one its hidden type does not borrow; one collected at the call holds - // nothing past it, and any other is refused. Looked up with regions erased, - // since they may be the signature's bound ones, which a key never names. + // keeps as the opaque type itself, and one whose hidden type is a value as + // its bytes, so each holds every lifetime it captures, even one its hidden + // type does not borrow; one collected at the call holds nothing past it, + // and any other is refused. Looked up with regions erased, since they may + // be the signature's bound ones, which a key never names. let erased = ty::fold_regions(tcx, hidden, |_, _| tcx.lifetimes.re_erased); let object = matches!(erased.kind(), ty::Adt(adt, args) - if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_)))); + if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_)))); if object { held.extend(args.iter().flat_map(|arg| arg.walk()).filter_map(|arg| arg.as_region())); } From 12cf91ade9338da0a9bf14857035075d26fbc159 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:15:15 -0800 Subject: [PATCH 23/24] fix(ffi): read an array's element once for its padding A Copy value holding [(); usize::MAX] made the padding table walk every element, so its export never finished (CodeRabbit). One element is read and its initialized bytes stamped at each stride, and a zero-sized one is skipped. Co-Authored-By: Claude Opus 5.5 --- ffi/src/collect.rs | 17 ++++++++++++----- ffi/tests/fixtures.rs | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 3d88be9a2..9b7a96aa8 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -5478,11 +5478,18 @@ fn padding<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option> { } FieldsShape::Union(_) => None, FieldsShape::Array { stride, count } => { - if *count > 0 { - let element = layout.field(cx, 0); - for n in 0..*count { - init(cx, element, at + n * stride.bytes(), bytes)?; - } + // Every element initializes the same bytes, so one is read and stamped at + // each stride: walking each would never end for `[(); usize::MAX]`. + let element = layout.field(cx, 0); + let size = element.size.bytes() as usize; + if *count == 0 || size == 0 { + return Some(()); + } + let mut mine = vec![false; size]; + init(cx, element, 0, &mut mine)?; + for n in 0..*count { + let start = (at + n * stride.bytes()) as usize; + bytes[start..start + size].copy_from_slice(&mine); } Some(()) } diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 32b802fe8..f0d8e0cf5 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -2261,6 +2261,21 @@ fn standard_names() { } } +/// A `Copy` value of a zero-sized element repeated `usize::MAX` times exports, as its +/// padding table reads the element once rather than walking every one of its elements. +#[test] +fn zero_sized_elements() { + let package = package("zero_sized_elements"); + let lib = "#[derive(Clone, Copy)]\npub struct Marker([(); usize::MAX]);\n\nimpl Marker {\n\tpub fn new() -> Self {\n\t\tSelf([(); usize::MAX])\n\t}\n}\n\npub fn take(marker: Marker) -> u32 {\n\tmarker.0.len().count_ones()\n}\n"; + std::fs::write(package.join("src/lib.rs"), lib).unwrap(); + export_and_build(&package, &["--offline"]); + let header = std::fs::read_to_string(package.join("out/include/zero_sized_elements.h")).unwrap(); + assert!( + header.contains("\tuint8_t opaque[1];\n} zero_sized_elements_Marker;"), + "Marker is no opaque value:\n{header}" + ); +} + /// A facade whose lib takes the name of a foreign type's crate maps that crate's type /// through a dependency of its own, apart from the glue's dependency on the facade. #[test] From 50b88c07fd359d06ae1d285638cb27955fddeb4e Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:21:00 -0800 Subject: [PATCH 24/24] fix(ffi): keep Writer::value in its impl after merging main Git's merge placed #750's Writer::value inside the branch's write_words, which broke every Kotlin glue build. Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/jni.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index ee58a411a..2aad369d4 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -841,6 +841,27 @@ impl Writer { // SAFETY: a `u8` is a `MaybeUninit`. self.0.extend_from_slice(unsafe { &*(std::ptr::from_ref(bytes) as *const [MaybeUninit]) }); } + + /// Construct a Kotlin value while this writer's native buffer is alive. The generated + /// factory reads it completely and retains neither the buffer nor its reader. + pub fn value(self, env: *mut JNIEnv, cache: &OnceLock, name: &CStr, back: jobject) -> jobject { + let id = method(env, cache, name, c"(Ljava/nio/ByteBuffer;[Ljava/lang/Object;)Ljava/lang/Object;"); + let len = i32::try_from(self.0.len()).unwrap_or_else(|_| rt::fail("jni", "a value is too large for a ByteBuffer")); + // SAFETY: this writer owns the allocation through the synchronous factory call. + // Even an empty Vec has a non-null aligned pointer. Kotlin cannot retain the view. + unsafe { + let buffer = ((**env).v1_4.NewDirectByteBuffer)(env, self.0.as_ptr() as *mut c_void, len as jlong); + if buffer.is_null() { + fail(env, "jni", "cannot make a return buffer"); + } + let args = [jvalue { l: buffer }, jvalue { l: back }]; + let result = ((**env).v1_2.CallStaticObjectMethodA)(env, class(), id, args.as_ptr()); + // A declared Rust error is thrown by the factory, after applying writebacks. + // Leave it pending for the JVM, including a null success such as Option::None. + ((**env).v1_2.DeleteLocalRef)(env, buffer); + result + } + } } /// An opaque value's C struct from the `long` Kotlin holds it in: its bytes, at the word's @@ -928,27 +949,6 @@ pub fn write_words(w: &mut Writer, value: T) { std::ptr::copy_nonoverlapping((&raw const value).cast::(), at, size_of::()); w.0.set_len(start + n); } - - /// Construct a Kotlin value while this writer's native buffer is alive. The generated - /// factory reads it completely and retains neither the buffer nor its reader. - pub fn value(self, env: *mut JNIEnv, cache: &OnceLock, name: &CStr, back: jobject) -> jobject { - let id = method(env, cache, name, c"(Ljava/nio/ByteBuffer;[Ljava/lang/Object;)Ljava/lang/Object;"); - let len = i32::try_from(self.0.len()).unwrap_or_else(|_| rt::fail("jni", "a value is too large for a ByteBuffer")); - // SAFETY: this writer owns the allocation through the synchronous factory call. - // Even an empty Vec has a non-null aligned pointer. Kotlin cannot retain the view. - unsafe { - let buffer = ((**env).v1_4.NewDirectByteBuffer)(env, self.0.as_ptr() as *mut c_void, len as jlong); - if buffer.is_null() { - fail(env, "jni", "cannot make a return buffer"); - } - let args = [jvalue { l: buffer }, jvalue { l: back }]; - let result = ((**env).v1_2.CallStaticObjectMethodA)(env, class(), id, args.as_ptr()); - // A declared Rust error is thrown by the factory, after applying writebacks. - // Leave it pending for the JVM, including a null success such as Option::None. - ((**env).v1_2.DeleteLocalRef)(env, buffer); - result - } - } } /// A C value as it crosses encoded: Kotlin writes one Rust reads, and Rust writes one it