From d17734ad2ad8bb09868a2a0f1e46fc092fb59823 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:07:24 -0700 Subject: [PATCH 1/5] chore(quest): claim quest/m1/ffi-export/payload-bytes.md Quest-Claim: 83816b94a63b1f42 From 7b696daf11352fed323f84555079d9e564180e4f Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:35:06 -0700 Subject: [PATCH 2/5] fix(ffi): rank the bytes a parameter's supertraits imply ahead of text Co-Authored-By: Claude Opus 5.5 --- ffi/MIGRATE.md | 4 ++ ffi/README.md | 2 +- ffi/fixtures/api/examples/transcript.rs | 1 + ffi/fixtures/api/ffi.toml | 4 +- ffi/fixtures/api/src/lib.rs | 35 ++++++++++++++++ ffi/moq/bench/export.kt | 4 +- ffi/moq/moq.c | 6 +-- ffi/moq/moq.cpp | 7 ++-- ffi/moq/moq.kt | 13 +++--- ffi/src/collect.rs | 56 ++++++++++++++++--------- ffi/tests/api.c | 1 + ffi/tests/api.cpp | 2 + ffi/tests/api.kt | 1 + quest/m1/ffi-export/README.md | 1 - quest/m1/ffi-export/payload-bytes.md | 24 ----------- 15 files changed, 101 insertions(+), 60 deletions(-) delete mode 100644 quest/m1/ffi-export/payload-bytes.md diff --git a/ffi/MIGRATE.md b/ffi/MIGRATE.md index 9d40b452b..adf7ae273 100644 --- a/ffi/MIGRATE.md +++ b/ffi/MIGRATE.md @@ -2,6 +2,10 @@ ## Unreleased +- A type parameter whose bounds `Bytes` or `Vec` meet as well as `String`, such as + moq's `IntoBytes` frame payloads, exports as bytes rather than text. Kotlin takes a + `ByteArray` there: pass text as `"text".encodeToByteArray()`. C and C++ keep their + types, and accept a payload that is not UTF-8 rather than aborting. - In `exclude` and `rename`, a path that a field shares with a method or constant of its type, such as `Backoff::initial`, names only the method, as in rustdoc. Name the field `field@Backoff::initial`: an `exclude` that left out both now needs both entries. diff --git a/ffi/README.md b/ffi/README.md index f3e779d8e..c9bfe8dd2 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -78,7 +78,7 @@ export's, and adds nothing; another crate's type is refused. | `impl RangeBounds` of an integer | `_bounds_`: its start and end bounds | | `Option` | a null handle or buffer, or `_option_` | | a `pub const` | a `#define` if a `bool` or integer, else a function returning it | -| `impl Into`, `impl AsRef` | `X` | +| `impl Into`, `impl AsRef`, or a bound implying one, such as `IntoBytes: AsRef<[u8]>` | `X` | | another type parameter | the first of `String`, `Bytes`, and `Vec` meeting its bounds, or a foreign implementation of its one exported trait; else an instance of the function at each type the exports implement a trait bounding it for, as `&X` where that meets its bounds too, so C keeps its handle, and, as an argument of a generic type, at each argument of an instance that meets its bounds and makes each generic type the signature names an instance | A generic type infers the instances it exports at, with no config: the one at its diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 9b172f883..c48f1473c 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -148,6 +148,7 @@ fn main() { println!("{}", api_lib::greet("x", Shape::Square)); println!("len {}", api_lib::len(b"abc")); + println!("hex {}", api_lib::hex(Bytes::from_static(b"\xff\x00a"))); for event in [Event::Moved(p), Event::Named { name: "n".to_string() }, Event::Done] { println!("{}", api_lib::describe(&event)); } diff --git a/ffi/fixtures/api/ffi.toml b/ffi/fixtures/api/ffi.toml index c972c5cf8..0033be4d3 100644 --- a/ffi/fixtures/api/ffi.toml +++ b/ffi/fixtures/api/ffi.toml @@ -1,13 +1,15 @@ package = "api-fixture" prefix = "api" # The list's tail is not `Clone`, so its getter has no copy to return, one instance leaves -# out a method the others keep, `Seed` is generic, so no callback implements it, and +# out a method the others keep, `Seed` is generic, so no callback implements it, `Payload` +# consumes itself, which no callback can, and # `field@` leaves out a field alone where a method takes its path, as `Retry`'s and # `Slot`'s do. `Slot` has no such method, so its path alone names its field. A # function instance is left out by its path, as `depth_past` at `polar::Point` is. exclude = [ "Node::next", "Pair::swap", + "Payload", "Seed", "Slot::value", "field@Retry::delay", diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 7b37e7f95..8692e206a 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -589,6 +589,41 @@ pub fn len(payload: &[u8]) -> usize { payload.len() } +/// Converts into owned bytes, as moq's `IntoBytes` does: text implements it too, but its +/// supertrait makes a parameter it bounds bytes. +pub trait Payload: AsRef<[u8]> { + fn into_bytes(self) -> Bytes; +} + +impl Payload for Bytes { + fn into_bytes(self) -> Bytes { + self + } +} + +impl Payload for Vec { + fn into_bytes(self) -> Bytes { + self.into() + } +} + +impl Payload for String { + fn into_bytes(self) -> Bytes { + self.into() + } +} + +impl Payload for &str { + fn into_bytes(self) -> Bytes { + Bytes::copy_from_slice(self.as_bytes()) + } +} + +/// A payload's bytes in hex, which need not be UTF-8. +pub fn hex(payload: impl Payload) -> String { + payload.into_bytes().iter().map(|b| format!("{b:02x}")).collect() +} + pub fn greet(name: impl Into, shape: Shape) -> String { format!("hello {}, a {shape:?}", name.into()) } diff --git a/ffi/moq/bench/export.kt b/ffi/moq/bench/export.kt index 6c67eb58f..2f484cc59 100644 --- a/ffi/moq/bench/export.kt +++ b/ffi/moq/bench/export.kt @@ -16,8 +16,8 @@ fun round(): String { broadcast.createTrack("bench", TrackInfo.default().withTimescale(micro)) } - // The C ABI takes moq's `impl Into` frame as a string. - val frame = "x".repeat(100) + // A frame of 100 zero bytes, as bench/export.c writes. + val frame = ByteArray(100) var micros = 0uL // Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. diff --git a/ffi/moq/moq.c b/ffi/moq/moq.c index 6fa284343..4dd222954 100644 --- a/ffi/moq/moq.c +++ b/ffi/moq/moq.c @@ -122,13 +122,13 @@ int main(void) { moq_error_free(err); CHECK(moq_timestamp_as_secs(at) == 1); - /* The last frame, then the group's end, which reads as None. */ - CHECK(moq_group_producer_write_frame(writer, NULL, str("world"), &err)); + /* The last frame, bytes that are not UTF-8, then the group's end, which reads as None. */ + CHECK(moq_group_producer_write_frame(writer, NULL, str("\xffworld"), &err)); CHECK(moq_group_producer_finish(writer, &err)); reading = moq_group_consumer_read_frame(group); CHECK(moq_group_consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_READY); moq_group_consumer_read_frame_future_free(reading); - CHECK(frame != NULL && is(moq_frame_frame_payload(frame), "world")); + CHECK(frame != NULL && is(moq_frame_frame_payload(frame), "\xffworld")); moq_frame_frame_free(frame); reading = moq_group_consumer_read_frame(group); frame = (moq_frame_frame *)1; diff --git a/ffi/moq/moq.cpp b/ffi/moq/moq.cpp index 782c214e8..14aa0a707 100644 --- a/ffi/moq/moq.cpp +++ b/ffi/moq/moq.cpp @@ -86,11 +86,12 @@ int main() { CHECK(!late && is(late.error().to_string(), "frame timestamp doesn't fit its track")); CHECK(at->as_secs() == 1); - // The last frame, then the group's end, which reads as None, both blocking. - CHECK(writer->write_frame(nullptr, "world")); + // 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(std::move(*writer).finish()); auto world = group.read_frame().get(); - CHECK(world && world.value() && is(world.value()->payload(), "world")); + CHECK(world && world.value() && is(world.value()->payload(), "\xffworld")); auto end = group.read_frame().get(); CHECK(end && !end.value()); diff --git a/ffi/moq/moq.kt b/ffi/moq/moq.kt index 824de3eda..4e6d764ed 100644 --- a/ffi/moq/moq.kt +++ b/ffi/moq/moq.kt @@ -75,7 +75,7 @@ fun main() { scope.launch { frame = first.readFrame() } q.drain() expect(frame == null, "a read waits for a frame") - writer.writeFrame(null, "hello") + writer.writeFrame(null, "hello".encodeToByteArray()) expect(q.size == 1, "a frame's arrival wakes the read") q.drain() val hello = frame!! @@ -89,18 +89,19 @@ fun main() { val at = Timestamp.fromMillis(1500u) expect(at.asSecs() == 1uL, "1500 ms is 1 s") try { - writer.writeFrame(at, "late") + writer.writeFrame(at, "late".encodeToByteArray()) expect(false, "a timestamped frame on an untimed track is an error") } catch (e: ErrorException) { expect(e.message == "frame timestamp doesn't fit its track", "the late frame's error is Rust's") } expect(at.asSecs() == 1uL, "a copied timestamp stays usable") - // The last frame, then the group's end, which reads as null. - writer.writeFrame(null, "world") + // The last frame, bytes that are not UTF-8, then the group's end, which reads as null. + val world = byteArrayOf(0xff.toByte()) + "world".encodeToByteArray() + writer.writeFrame(null, world) writer.finish() runBlocking { - expect(first.readFrame()?.payload?.decodeToString() == "world", "the last frame reads") + expect(first.readFrame()?.payload?.contentEquals(world) == true, "the last frame reads") expect(first.readFrame() == null, "a finished group reads null") } @@ -184,7 +185,7 @@ fun main() { val catalog = CatalogProducerExtra.new(publisher, extra) val snapshot = catalog.binarySnapshotConfig(publisher.createTrack("state", null), BinaryConfig.default()) expect(snapshot.name() == "state", "a snapshot has its track's name") - snapshot.update("latest") + snapshot.update("latest".encodeToByteArray()) var listed = 0 catalog.mutate { lent -> if (lent.toJson().contains("state")) listed++ diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 72be6a09e..b6961bac4 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -14,6 +14,7 @@ use std::collections::VecDeque; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{CrateNum, DefId}; use rustc_infer::infer::TyCtxtInferExt; +use rustc_infer::traits::util::elaborate; use rustc_infer::traits::{Obligation, ObligationCause}; use rustc_middle::ty::{ self, GenericArgKind, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingEnv, @@ -1742,7 +1743,8 @@ impl<'tcx> Collector<'tcx> { } /// The types a type parameter may be exported as, best first, which typing takes the - /// first of: `X` for `impl Into` or `impl AsRef`, then `String`, `Bytes`, and + /// first of: `X` for each `Into` or `AsRef` its bounds imply, or `&X` and then + /// `String` or `Vec` where `X` is `str` or `[T]`, then `String`, `Bytes`, and /// `Vec`, each that meets its bounds and maps. fn choose(&self, def_id: DefId, param: Ty<'tcx>, base: &[ty::GenericArg<'tcx>]) -> Result>, Why> { let tcx = self.tcx; @@ -1762,37 +1764,53 @@ impl<'tcx> Collector<'tcx> { .as_type_outlives_clause() .is_some_and(|pred| pred.skip_binder().0 == param) }); + let string = tcx + .lang_items() + .string() + .map(|string| Ty::new_adt(tcx, tcx.adt_def(string), ty::List::empty())); + let vec = |elem: Ty<'tcx>| { + let vec = tcx.get_diagnostic_item(sym::Vec)?; + let alloc = tcx.lang_items().global_alloc_ty()?; + let alloc = Ty::new_adt(tcx, tcx.adt_def(alloc), ty::List::empty()); + Some(Ty::new_adt( + tcx, + tcx.adt_def(vec), + tcx.mk_args(&[elem.into(), alloc.into()]), + )) + }; let mut candidates = Vec::new(); - for bound in &bounds { + // With the bounds they imply: `IntoBytes: AsRef<[u8]>` makes a parameter it bounds bytes. + let implied = elaborate(tcx, identity.clauses.iter().map(|clause| clause.skip_norm_wip())) + .filter_map(|clause| clause.as_trait_clause()) + .filter(|pred| pred.self_ty().skip_binder() == param); + for bound in implied { let tr = bound.def_id(); let target = || bound.skip_binder().trait_ref.args.type_at(1); if tcx.is_diagnostic_item(sym::Into, tr) || tcx.is_diagnostic_item(sym::AsRef, tr) { let target = target(); - match target.kind() { - ty::Str | ty::Slice(_) if outlives => {} - ty::Str | ty::Slice(_) => candidates.push(Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, target)), - _ => candidates.push(target), + let owned = match target.kind() { + ty::Str => string, + ty::Slice(elem) => vec(*elem), + _ => { + candidates.push(target); + continue; + } + }; + // A borrow of what C lends, unless the parameter outlives the call, then the + // owned form, which meets bounds that only owned types implement. + if !outlives { + candidates.push(Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, target)); } + candidates.extend(owned); } else if Some(tr) == self.range_bounds { candidates.extend(self.bounds(target())); } } - if let Some(string) = tcx.lang_items().string() { - candidates.push(Ty::new_adt(tcx, tcx.adt_def(string), ty::List::empty())); - } + candidates.extend(string); if let Some(bytes) = self.bytes { candidates.push(Ty::new_adt(tcx, tcx.adt_def(bytes), ty::List::empty())); } - if let Some(vec) = tcx.get_diagnostic_item(sym::Vec) { - let alloc = tcx.lang_items().global_alloc_ty(); - if let Some(alloc) = alloc { - let args = tcx.mk_args(&[ - tcx.types.u8.into(), - Ty::new_adt(tcx, tcx.adt_def(alloc), ty::List::empty()).into(), - ]); - candidates.push(Ty::new_adt(tcx, tcx.adt_def(vec), args)); - } - } + candidates.extend(vec(tcx.types.u8)); // `impl Into>` takes a `String` too, which maps where `Arc` does not. candidates .retain(|&candidate| self.at(def_id, param, base, candidate).is_some() && self.maps(candidate, Pos::Param)); diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 93cc2891e..663a74310 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -266,6 +266,7 @@ static int script(void) { print("", api_greet(str("x"), API_SHAPE_SQUARE), "\n"); printf("len %zu\n", api_len(str("abc"))); + print("hex ", api_hex((api_buf){.ptr = (const uint8_t *)"\xff\0a", .len = 3}), "\n"); api_event events[3] = { {.tag = API_EVENT_MOVED, .moved = {._0 = p}}, {.tag = API_EVENT_NAMED, .named = {.name = str("n")}}, diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index e2521af56..02ab1cde8 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -208,6 +208,8 @@ int script() { print("", api::greet("x", api::shape::square), "\n"); std::printf("len %zu\n", api::len("abc")); + const std::uint8_t payload[] = {0xff, 0x00, 'a'}; + print("hex ", api::hex(api::view(payload, sizeof payload)), "\n"); api::event events[] = {api::event_moved{p}, api::event_named{api::buf("n")}, api::event_done{}}; for (const auto &event : events) { print("", api::describe(event), "\n"); diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 3ecf4bb56..972c48cc0 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -130,6 +130,7 @@ fun script() { println(greet("x", Shape.SQUARE)) println("len ${len("abc".encodeToByteArray())}") + println("hex ${hex(byteArrayOf(0xff.toByte(), 0, 'a'.code.toByte()))}") for (event in listOf(Event.Moved(p), Event.Named("n"), Event.Done)) { println(describe(event)) } diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index 2b5000354..195971f4a 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -47,7 +47,6 @@ Every generator on the C ABI: ## Required -- [Payloads cross as bytes](/quest/m1/ffi-export/payload-bytes.md) - a payload that is not UTF-8 crosses unchanged, where today it aborts from C, C++, and Kotlin - [Iterator and JSON arguments the exporter can't map yet](/quest/m1/ffi-export/instance-keys.md) - `IntoIterator` as a `Vec`, and `serde_json::Value` - [Fail CI on an instance not inferred](/quest/m1/ffi-export/inference-gate.md) - a gap in instance inference fails CI instead of sitting in the census - [moq-clock's C names once it moves into kio](/quest/m1/ffi-export/crates.md) - `moq_now` and `moq_origin` become the clock's, after moq moves it into `kio::clock` diff --git a/quest/m1/ffi-export/payload-bytes.md b/quest/m1/ffi-export/payload-bytes.md deleted file mode 100644 index 110a522cb..000000000 --- a/quest/m1/ffi-export/payload-bytes.md +++ /dev/null @@ -1,24 +0,0 @@ -# [S] Payloads cross as bytes - -## Goal - -A parameter that takes bytes, such as moq's `impl Into` frame payloads -(`group::Producer::write_frame`), exports as bytes in the C ABI and every generator, so a -payload that is not valid UTF-8 crosses unchanged. Today it resolves to `String`: C and C++ -abort on such a payload, and Kotlin's `writeFrame` takes a `String`. - -## Plan - -Found by [#184](https://github.com/kixelated/rust2/pull/184) on 2026-10-28. This is a -correctness bug: moq's media payloads are binary. - -- Find why first. [`choose`](/ffi/src/collect.rs) puts `X` of `impl Into` ahead of - `String`, so the signature moq writes, or the bound typing reads, differs from what the - rule expects. -- Fix the rule, not moq's case: any parameter whose bounds `Bytes` or `Vec` meets but - whose meaning is bytes must not narrow to `String`. A `String` choice stays only where - the bound itself is about text. -- Prove it from C, C++, and Kotlin with a payload that is not UTF-8, and report the census - before and after. - -Public API: the C, C++, and Kotlin types of the affected parameters. From 179c4ccc7f7d40c8422bfc908856cd1d11c5c3b2 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:44:36 -0700 Subject: [PATCH 3/5] fix(ffi): take what a From implementation converts from for an unmapped Into target Co-Authored-By: Claude Opus 5.5 --- ffi/MIGRATE.md | 9 +-- ffi/README.md | 3 +- ffi/fixtures/api/examples/transcript.rs | 1 + ffi/fixtures/api/ffi.toml | 3 +- ffi/fixtures/api/src/lib.rs | 20 ++++++ ffi/src/collect.rs | 85 +++++++++++++++++++++---- ffi/tests/api.c | 4 +- ffi/tests/api.cpp | 5 +- ffi/tests/api.kt | 4 +- 9 files changed, 110 insertions(+), 24 deletions(-) diff --git a/ffi/MIGRATE.md b/ffi/MIGRATE.md index adf7ae273..48fdfacaf 100644 --- a/ffi/MIGRATE.md +++ b/ffi/MIGRATE.md @@ -2,10 +2,11 @@ ## Unreleased -- A type parameter whose bounds `Bytes` or `Vec` meet as well as `String`, such as - moq's `IntoBytes` frame payloads, exports as bytes rather than text. Kotlin takes a - `ByteArray` there: pass text as `"text".encodeToByteArray()`. C and C++ keep their - types, and accept a payload that is not UTF-8 rather than aborting. +- A type parameter whose bounds imply bytes, through a supertrait such as moq's + `IntoBytes: AsRef<[u8]>` or a `From` implementation such as moq's + `impl> From for Timed`, exports as bytes rather than + `String`. Kotlin takes a `ByteArray` there: pass text as `"text".encodeToByteArray()`. + C and C++ keep their types, and accept a payload that is not UTF-8 rather than aborting. - In `exclude` and `rename`, a path that a field shares with a method or constant of its type, such as `Backoff::initial`, names only the method, as in rustdoc. Name the field `field@Backoff::initial`: an `exclude` that left out both now needs both entries. diff --git a/ffi/README.md b/ffi/README.md index c9bfe8dd2..f920909a4 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -78,7 +78,8 @@ export's, and adds nothing; another crate's type is refused. | `impl RangeBounds` of an integer | `_bounds_`: its start and end bounds | | `Option` | a null handle or buffer, or `_option_` | | a `pub const` | a `#define` if a `bool` or integer, else a function returning it | -| `impl Into`, `impl AsRef`, or a bound implying one, such as `IntoBytes: AsRef<[u8]>` | `X` | +| `impl Into`, `impl AsRef`, or a bound implying one, as a supertrait does | `X`, or `&X` and then `String` or `Vec` where `X` is `str` or `[T]` | +| `impl Into`, where `X` does not map | what a `From` implementation into `X` takes, by its source parameter's bounds as above: `impl> From for Timed` takes `Bytes` | | another type parameter | the first of `String`, `Bytes`, and `Vec` meeting its bounds, or a foreign implementation of its one exported trait; else an instance of the function at each type the exports implement a trait bounding it for, as `&X` where that meets its bounds too, so C keeps its handle, and, as an argument of a generic type, at each argument of an instance that meets its bounds and makes each generic type the signature names an instance | A generic type infers the instances it exports at, with no config: the one at its diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index c48f1473c..027fbda09 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -149,6 +149,7 @@ fn main() { println!("{}", api_lib::greet("x", Shape::Square)); println!("len {}", api_lib::len(b"abc")); println!("hex {}", api_lib::hex(Bytes::from_static(b"\xff\x00a"))); + println!("timed hex {}", api_lib::timed_hex(Bytes::from_static(b"\xff\x00a"))); for event in [Event::Moved(p), Event::Named { name: "n".to_string() }, Event::Done] { println!("{}", api_lib::describe(&event)); } diff --git a/ffi/fixtures/api/ffi.toml b/ffi/fixtures/api/ffi.toml index 0033be4d3..91c65c77e 100644 --- a/ffi/fixtures/api/ffi.toml +++ b/ffi/fixtures/api/ffi.toml @@ -2,7 +2,7 @@ package = "api-fixture" prefix = "api" # The list's tail is not `Clone`, so its getter has no copy to return, one instance leaves # out a method the others keep, `Seed` is generic, so no callback implements it, `Payload` -# consumes itself, which no callback can, and +# consumes itself, which no callback can, `Timed` has no instance that exports, and # `field@` leaves out a field alone where a method takes its path, as `Retry`'s and # `Slot`'s do. `Slot` has no such method, so its path alone names its field. A # function instance is left out by its path, as `depth_past` at `polar::Point` is. @@ -15,6 +15,7 @@ exclude = [ "field@Retry::delay", "field@Slot::value", "Stack::depth_past", + "Timed", ] # The field `Retry::limit`, whose name its method takes. diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 8692e206a..cf59e2dbd 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -624,6 +624,26 @@ pub fn hex(payload: impl Payload) -> String { payload.into_bytes().iter().map(|b| format!("{b:02x}")).collect() } +/// A value and when it was stamped, as moq's `Timed` is, which bytes convert into. +pub struct Timed

{ + pub value: P, + pub at: Option, +} + +impl> From for Timed { + fn from(value: B) -> Self { + Self { + value: value.into(), + at: None, + } + } +} + +/// A timed payload's bytes in hex, which need not be UTF-8. +pub fn timed_hex(payload: impl Into>) -> String { + hex(payload.into().value) +} + pub fn greet(name: impl Into, shape: Shape) -> String { format!("hello {}, a {shape:?}", name.into()) } diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index b6961bac4..5cdae32b8 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -16,11 +16,13 @@ use rustc_hir::def_id::{CrateNum, DefId}; use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::traits::util::elaborate; use rustc_infer::traits::{Obligation, ObligationCause}; +use rustc_middle::ty::fast_reject::{TreatParams, simplify_type}; use rustc_middle::ty::{ self, GenericArgKind, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingEnv, }; -use rustc_span::{Span, sym}; +use rustc_span::{DUMMY_SP, Span, sym}; use rustc_trait_selection::infer::InferCtxtExt; +use rustc_trait_selection::traits::ObligationCtxt; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _; use crate::ir::{ @@ -1787,21 +1789,25 @@ impl<'tcx> Collector<'tcx> { let tr = bound.def_id(); let target = || bound.skip_binder().trait_ref.args.type_at(1); if tcx.is_diagnostic_item(sym::Into, tr) || tcx.is_diagnostic_item(sym::AsRef, tr) { - let target = target(); - let owned = match target.kind() { - ty::Str => string, - ty::Slice(elem) => vec(*elem), - _ => { - candidates.push(target); - continue; + // An `Into` target that does not map takes what converts into it instead. + let into = (tcx.is_diagnostic_item(sym::Into, tr) && !self.maps(target(), Pos::Param)) + .then(|| self.sources(def_id, target())); + for target in std::iter::once(target()).chain(into.into_iter().flatten()) { + let owned = match target.kind() { + ty::Str => string, + ty::Slice(elem) => vec(*elem), + _ => { + candidates.push(target); + continue; + } + }; + // A borrow of what C lends, unless the parameter outlives the call, then the + // owned form, which meets bounds that only owned types implement. + if !outlives { + candidates.push(Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, target)); } - }; - // A borrow of what C lends, unless the parameter outlives the call, then the - // owned form, which meets bounds that only owned types implement. - if !outlives { - candidates.push(Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, target)); + candidates.extend(owned); } - candidates.extend(owned); } else if Some(tr) == self.range_bounds { candidates.extend(self.bounds(target())); } @@ -1835,6 +1841,57 @@ impl<'tcx> Collector<'tcx> { } } + /// What `impl Into` takes through the `From` implementations into `target` + /// whose source is a type parameter: each `Into` or `AsRef` its bounds imply, so + /// `impl> From for Timed` makes `impl Into>` bytes. + fn sources(&self, def_id: DefId, target: Ty<'tcx>) -> Vec> { + let tcx = self.tcx; + let Some(from) = tcx.get_diagnostic_item(sym::From) else { + return Vec::new(); + }; + let Some(simple) = simplify_type(tcx, target, TreatParams::AsRigid) else { + return Vec::new(); + }; + let mut found = Vec::new(); + for &imp in tcx + .trait_impls_of(from) + .non_blanket_impls() + .get(&simple) + .into_iter() + .flatten() + { + let (infcx, env) = tcx + .infer_ctxt() + .build_with_typing_env(TypingEnv::post_analysis(tcx, def_id)); + let args = infcx.fresh_args_for_item(DUMMY_SP, imp); + let trait_ref = tcx.impl_trait_ref(imp).instantiate(tcx, args).skip_norm_wip(); + let source = trait_ref.args.type_at(1); + let ocx = ObligationCtxt::new(&infcx); + if !source.is_ty_var() + || ocx + .eq(&ObligationCause::dummy(), env, trait_ref.self_ty(), target) + .is_err() + { + continue; + } + let source = infcx.deeply_resolve_ignoring_regions(source); + let clauses = tcx.clauses_of(imp).instantiate(tcx, args).clauses; + for bound in elaborate(tcx, clauses.iter().map(|clause| clause.skip_norm_wip())) + .filter_map(|clause| clause.as_trait_clause()) + .filter(|bound| { + tcx.is_diagnostic_item(sym::Into, bound.def_id()) + || tcx.is_diagnostic_item(sym::AsRef, bound.def_id()) + }) { + let bound = infcx.deeply_resolve_ignoring_regions(bound.skip_binder()); + let x = tcx.erase_and_anonymize_regions(bound.trait_ref.args.type_at(1)); + if bound.self_ty() == source && !x.has_non_region_infer() && !found.contains(&x) { + found.push(x); + } + } + } + found + } + /// The arguments with `param` of `def_id` as `candidate`, if its bounds hold there. fn at( &self, diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 663a74310..e8147bf54 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -266,7 +266,9 @@ static int script(void) { print("", api_greet(str("x"), API_SHAPE_SQUARE), "\n"); printf("len %zu\n", api_len(str("abc"))); - print("hex ", api_hex((api_buf){.ptr = (const uint8_t *)"\xff\0a", .len = 3}), "\n"); + api_buf binary = {.ptr = (const uint8_t *)"\xff\0a", .len = 3}; + print("hex ", api_hex(binary), "\n"); + print("timed hex ", api_timed_hex(binary), "\n"); api_event events[3] = { {.tag = API_EVENT_MOVED, .moved = {._0 = p}}, {.tag = API_EVENT_NAMED, .named = {.name = str("n")}}, diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 02ab1cde8..198ce6cf7 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -208,8 +208,9 @@ int script() { print("", api::greet("x", api::shape::square), "\n"); std::printf("len %zu\n", api::len("abc")); - const std::uint8_t payload[] = {0xff, 0x00, 'a'}; - print("hex ", api::hex(api::view(payload, sizeof payload)), "\n"); + const std::uint8_t binary[] = {0xff, 0x00, 'a'}; + print("hex ", api::hex(api::view(binary, sizeof binary)), "\n"); + print("timed hex ", api::timed_hex(api::view(binary, sizeof binary)), "\n"); api::event events[] = {api::event_moved{p}, api::event_named{api::buf("n")}, api::event_done{}}; for (const auto &event : events) { print("", api::describe(event), "\n"); diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 972c48cc0..0acfcb7b2 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -130,7 +130,9 @@ fun script() { println(greet("x", Shape.SQUARE)) println("len ${len("abc".encodeToByteArray())}") - println("hex ${hex(byteArrayOf(0xff.toByte(), 0, 'a'.code.toByte()))}") + val binary = byteArrayOf(0xff.toByte(), 0, 'a'.code.toByte()) + println("hex ${hex(binary)}") + println("timed hex ${timedHex(binary)}") for (event in listOf(Event.Moved(p), Event.Named("n"), Event.Done)) { println(describe(event)) } From 129f16bfd54ae0c77a8237bff30a4974623247ba Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:58:50 -0700 Subject: [PATCH 4/5] fix(ffi): read a parameter's implied lifetime bound before borrowing for it Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/src/lib.rs | 20 +++++++++++++------- ffi/src/collect.rs | 29 ++++++++++++++++++----------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index cf59e2dbd..1eecdbd71 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -590,8 +590,8 @@ pub fn len(payload: &[u8]) -> usize { } /// Converts into owned bytes, as moq's `IntoBytes` does: text implements it too, but its -/// supertrait makes a parameter it bounds bytes. -pub trait Payload: AsRef<[u8]> { +/// supertrait makes a parameter it bounds bytes, and owned ones, as it must be `'static`. +pub trait Payload: AsRef<[u8]> + 'static { fn into_bytes(self) -> Bytes; } @@ -613,9 +613,15 @@ impl Payload for String { } } -impl Payload for &str { +impl Payload for &'static str { fn into_bytes(self) -> Bytes { - Bytes::copy_from_slice(self.as_bytes()) + Bytes::from_static(self.as_bytes()) + } +} + +impl Payload for &'static [u8] { + fn into_bytes(self) -> Bytes { + Bytes::from_static(self) } } @@ -624,16 +630,16 @@ pub fn hex(payload: impl Payload) -> String { payload.into_bytes().iter().map(|b| format!("{b:02x}")).collect() } -/// A value and when it was stamped, as moq's `Timed` is, which bytes convert into. +/// A value and when it was stamped, as moq's `Timed` is, which a payload converts into. pub struct Timed

{ pub value: P, pub at: Option, } -impl> From for Timed { +impl From for Timed { fn from(value: B) -> Self { Self { - value: value.into(), + value: value.into_bytes(), at: None, } } diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 6d9605f2e..8c8e7e640 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1794,10 +1794,11 @@ impl<'tcx> Collector<'tcx> { .filter_map(|clause| clause.as_trait_clause()) .filter(|pred| pred.self_ty().skip_binder() == param) .collect(); + // With the bounds they imply: `IntoBytes: AsRef<[u8]>` makes a parameter it bounds bytes. + let implied: Vec<_> = elaborate(tcx, identity.clauses.iter().map(|clause| clause.skip_norm_wip())).collect(); // A parameter that must outlive the call cannot borrow what C lends for it. - let outlives = identity.clauses.iter().any(|clause| { + let outlives = implied.iter().any(|clause| { clause - .skip_norm_wip() .as_type_outlives_clause() .is_some_and(|pred| pred.skip_binder().0 == param) }); @@ -1816,8 +1817,7 @@ impl<'tcx> Collector<'tcx> { )) }; let mut candidates = Vec::new(); - // With the bounds they imply: `IntoBytes: AsRef<[u8]>` makes a parameter it bounds bytes. - let implied = elaborate(tcx, identity.clauses.iter().map(|clause| clause.skip_norm_wip())) + let implied = (implied.iter()) .filter_map(|clause| clause.as_trait_clause()) .filter(|pred| pred.self_ty().skip_binder() == param); for bound in implied { @@ -1827,7 +1827,7 @@ impl<'tcx> Collector<'tcx> { // An `Into` target that does not map takes what converts into it instead. let into = (tcx.is_diagnostic_item(sym::Into, tr) && !self.maps(target(), Pos::Param)) .then(|| self.sources(def_id, target())); - for target in std::iter::once(target()).chain(into.into_iter().flatten()) { + for (target, outlives) in std::iter::once((target(), outlives)).chain(into.into_iter().flatten()) { let owned = match target.kind() { ty::Str => string, ty::Slice(elem) => vec(*elem), @@ -1877,9 +1877,10 @@ impl<'tcx> Collector<'tcx> { } /// What `impl Into` takes through the `From` implementations into `target` - /// whose source is a type parameter: each `Into` or `AsRef` its bounds imply, so - /// `impl> From for Timed` makes `impl Into>` bytes. - fn sources(&self, def_id: DefId, target: Ty<'tcx>) -> Vec> { + /// whose source is a type parameter: each `Into` or `AsRef` its bounds imply, and + /// whether they make it outlive the call, so `impl> From for + /// Timed` makes `impl Into>` bytes. + fn sources(&self, def_id: DefId, target: Ty<'tcx>) -> Vec<(Ty<'tcx>, bool)> { let tcx = self.tcx; let Some(from) = tcx.get_diagnostic_item(sym::From) else { return Vec::new(); @@ -1911,7 +1912,13 @@ impl<'tcx> Collector<'tcx> { } let source = infcx.deeply_resolve_ignoring_regions(source); let clauses = tcx.clauses_of(imp).instantiate(tcx, args).clauses; - for bound in elaborate(tcx, clauses.iter().map(|clause| clause.skip_norm_wip())) + let implied: Vec<_> = elaborate(tcx, clauses.iter().map(|clause| clause.skip_norm_wip())).collect(); + let outlives = implied.iter().any(|clause| { + clause + .as_type_outlives_clause() + .is_some_and(|pred| infcx.deeply_resolve_ignoring_regions(pred.skip_binder().0) == source) + }); + for bound in (implied.iter()) .filter_map(|clause| clause.as_trait_clause()) .filter(|bound| { tcx.is_diagnostic_item(sym::Into, bound.def_id()) @@ -1919,8 +1926,8 @@ impl<'tcx> Collector<'tcx> { }) { let bound = infcx.deeply_resolve_ignoring_regions(bound.skip_binder()); let x = tcx.erase_and_anonymize_regions(bound.trait_ref.args.type_at(1)); - if bound.self_ty() == source && !x.has_non_region_infer() && !found.contains(&x) { - found.push(x); + if bound.self_ty() == source && !x.has_non_region_infer() && !found.contains(&(x, outlives)) { + found.push((x, outlives)); } } } From a5bec55e77decef329b77b29fa0ac4e67a4c64e0 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 20:03:02 -0700 Subject: [PATCH 5/5] fix(ffi): own a From source when the parameter must outlive the call Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/src/lib.rs | 10 +++++----- ffi/src/collect.rs | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 1eecdbd71..2766c91bc 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -630,23 +630,23 @@ pub fn hex(payload: impl Payload) -> String { payload.into_bytes().iter().map(|b| format!("{b:02x}")).collect() } -/// A value and when it was stamped, as moq's `Timed` is, which a payload converts into. +/// A value and when it was stamped, as moq's `Timed` is, which bytes convert into. pub struct Timed

{ pub value: P, pub at: Option, } -impl From for Timed { +impl> From for Timed { fn from(value: B) -> Self { Self { - value: value.into_bytes(), + value: Bytes::copy_from_slice(value.as_ref()), at: None, } } } -/// A timed payload's bytes in hex, which need not be UTF-8. -pub fn timed_hex(payload: impl Into>) -> String { +/// A timed payload's bytes in hex, which need not be UTF-8, owned as it is `'static`. +pub fn timed_hex(payload: impl Into> + 'static) -> String { hex(payload.into().value) } diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 8c8e7e640..bd9393b13 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1827,7 +1827,9 @@ impl<'tcx> Collector<'tcx> { // An `Into` target that does not map takes what converts into it instead. let into = (tcx.is_diagnostic_item(sym::Into, tr) && !self.maps(target(), Pos::Param)) .then(|| self.sources(def_id, target())); - for (target, outlives) in std::iter::once((target(), outlives)).chain(into.into_iter().flatten()) { + // A source that must outlive the call, or converts into a parameter that must, owns it. + let sources = into.into_iter().flatten().map(|(x, source)| (x, outlives || source)); + for (target, outlives) in std::iter::once((target(), outlives)).chain(sources) { let owned = match target.kind() { ty::Str => string, ty::Slice(elem) => vec(*elem),