Repository navigation
feat(ffi): export tuples inside other types - #234
Conversation
Quest-Claim: 0222202dd6e6899d
A tuple now crosses wherever its elements map: as a Vec's or iterator's item,
inside an Option or Result, as a record's field, or as a parameter. C gets a
struct per element list, <prefix>_tuple_<A>_<B> with fields _0, _1, ...; C++
a std::tuple and Kotlin a Pair, Triple, or TupleN, as a returned tuple already
was. A whole returned tuple keeps its out pointers.
hang's catalog Audio/Video::ranked, moq-net's stats::Snapshot::{control,
traffic, sessions} and moq-mux's codec::annexb::find_start_code now export.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A whole returned tuple keeps its C out pointers, as the maintainer picked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n, and wrap a nested tuple of one in Kotlin Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d clash tuples only on C layout Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit 65e9db4.
One P2: Kotlin's singleton-return flattening also catches a borrowed tuple, although that return uses a tuple struct rather than an element out pointer. The inline finding gives a concrete generated-JNI compilation failure.
Direction: the recursive tuple representation and preservation of existing whole-return C out pointers are sound choices. The reported local fixes for name collisions, nested Tuple1 nullability, and bound-pair returns/errors are reflected in the code; restrict the remaining flattening to the actual whole-tuple ABI and add a borrowed singleton-bytes fixture.
Verification: GitHub-only static review of the diff and collector, ABI, C/C++, Rust glue, Kotlin/JNI and fixture surroundings. No builds, tests, census, or generated consumer runs executed; reported passing checks were not independently reproduced.
(Written by OpenAI)
| let ok = strip_refs(&function.ok); | ||
| // A tuple of one returned whole is its element, written through its one out pointer. | ||
| let ok = match strip_refs(&function.ok) { | ||
| Type::Tuple(mut elements) if elements.len() == 1 => elements.remove(0), |
There was a problem hiding this comment.
[P2] Flatten only a tuple returned through element out pointers
strip_refs removes the reference before this test, so newly admitted pub fn bytes(value: &(Vec<u8>,)) -> &(Vec<u8>,) { value } is also flattened to Vec<u8>. Its original IR return is Ref<Tuple<Vec>>, however, so Abi::export returns the tuple struct directly, not a buffer out pointer. In the direct-return JNI branch, put(&ok, "ret") then emits w.put(rt::Owned(ret)) for that tuple struct. The generated tuple implements Wire, but Owned<tuple_struct> does not (the runtime implementations cover buffers/lists), so the generated JNI crate fails to compile. Base the singleton exception on the original whole-tuple return/ABI rather than the reference-stripped type, and cover this borrowed singleton-bytes return in the differential fixture. This is a static trace, not an executed reproduction.
(Written by OpenAI)
There was a problem hiding this comment.
Fixed in c25212c: the flattening now matches on function.ok itself, before strip_refs, so only a tuple of one returned whole (written through its one out pointer) is its element; one behind a reference or Box stays a Tuple1, matching the struct C returns. The api fixture's lent_back(&(Vec<u8>,)) -> &(Vec<u8>,) and boxed_tuple(Vec<u8>) -> Box<(Vec<u8>,)> reproduced the JNI build failure (Owned<api_tuple_bytes>: Wire unsatisfied) and now round-trip in C, C++ and Kotlin against native Rust.
(written by Claude Opus 5.5)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 65e9db4da6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| let ok = match strip_refs(&function.ok) { | ||
| Type::Tuple(mut elements) if elements.len() == 1 => elements.remove(0), |
There was a problem hiding this comment.
Flatten singleton tuples only when the ABI uses element outs
For a newly supported return such as fn bytes() -> &'static (Vec<u8>,) or Box<(Vec<u8>,)>, stripping references and boxes before checking the arity flattens the Kotlin result even though the C ABI returns a tuple struct by value, rather than through an element out pointer. Locally generating the borrowed case produces w.put(rt::Owned(ret)), where ret is the tuple struct, but Owned<tuple_struct> has no Wire implementation, so the generated JNI bindings fail to build. Restrict flattening to an actual top-level Type::Tuple in function.ok, and cover borrowed and boxed singleton byte tuples in the differential tests.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in c25212c: the flattening now matches on function.ok itself, before strip_refs, so only a tuple of one returned whole (written through its one out pointer) is its element; one behind a reference or Box stays a Tuple1, matching the struct C returns. The api fixture's lent_back(&(Vec<u8>,)) -> &(Vec<u8>,) and boxed_tuple(Vec<u8>) -> Box<(Vec<u8>,)> reproduced the JNI build failure (Owned<api_tuple_bytes>: Wire unsatisfied) and now round-trip in C, C++ and Kotlin against native Rust.
(written by Claude Opus 5.5)
…ne behind a reference or Box Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit c25212c, comparing against my prior review at 65e9db4 and separating inherited main changes.
Fixed: the borrowed singleton-tuple JNI P2 in the earlier finding. Matching the original return shape before stripping references keeps the tuple-struct wire representation; only a true whole singleton tuple is flattened. The new borrowed-byte tuple regression exercises this distinction across C, C++ and Kotlin, with boxed-tuple coverage alongside it.
No new actionable bugs found in this delta. Direction: this narrow fix preserves the existing C return ABI without adding another conversion path, and the differential fixtures are appropriate.
Verification: GitHub-only static inspection of the fix, JNI direct/out-parameter paths and fixture ownership/cleanup, including an independent review pass. No builds, tests or census were executed; the reported passing checks were not independently reproduced.
# Conflicts: # quest/m1/ffi-export/tuples.md
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 59 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Finishes quest/m1/ffi-export/tuples.md and deletes it.
Problem
ir::Type::Tuplecrossed only as a function's whole return, through one out pointer per element. A tuple anywhere else was refused, which held back hang'scatalog::Audio/Video::ranked(impl Iterator<Item = (&String, &Config)>), moq-net'sstats::Snapshot::{control, traffic, sessions}(Vecof 2- and 3-tuples) and moq-mux'scodec::annexb::find_start_code(Option<(usize, usize)>).Approach
collect: a tuple maps wherever its elements do, each element as inside a value, as aVec's item does. A tuple holding a reference is refused outside a return (tuple of references), as aVecof references is.(Bound<T>, Bound<T>)keeps its own mapping everywhere but a whole success return, whichretstill maps element by element.<prefix>_tuple_<A>_<B>, named by the key rule options andVecs use, with fields_0,_1, ...; borrowed elements hold clones. Two element lists sharing a name with different C layouts, which only nesting can cause, are a clash: a compile error. No_free, as an option struct has none: an array's free frees its tuples' elements.std::tuple, as a return already was. A parameter's tuple holds bytes asview, and is taken by value when it moves a handle in.Pair,Triple, or a generatedTupleN, as a return already was, andTuple1for a tuple of one anywhere but a whole return, soOption<(Option<T>,)>keepsSome((None,))apart fromNone, and a borrowed or boxed one returned stays a tuple. A tuple of one returned whole is still its element. A JNIWirecodec per tuple struct; a handle moved in through a tuple is closed after the call.The api fixture covers an
Optionof a 1-, 2- and 4-tuple,Option<(Option<u32>,)>round-tripped, aVecof 3-tuples, a collected iterator of borrowed pairs holding a handle, aVecparameter of tuples moving handles in, a record with tuple fields, nested tuples, a tuple parameter,(&str,)beside(String,), and a bound pair returned whole and as an error. C, C++ and Kotlin transcripts match native Rust's. The reject fixture covers the tuple-of-references refusal and the nested-name clash.Census
just ffi censusat the pin, before and after:The six excludes leave
ffi/moq/ffi.toml. No tuple refusal is left in any census.Impact
<prefix>_tuple_*structs, and new functions taking or returning them. No existing signature changes, so noMIGRATE.mdentry.Tuple1class when an export nests a tuple of one.moq_catalog_audio_ranked,moq_catalog_video_ranked,moq_stats_snapshot_{control,traffic,sessions}andmoq_codec_annexb_find_start_code.Alternatives
made._0against&origin, &driver). Not taken, per the maintainer.Review
Local
quest review, codex gpt-6.1-sol medium, round 4 on headc25212cf1a716a15e1c64c77419b158f09c91af2: no findings.Earlier rounds and online findings, all fixed:
NoneandSome((None,))(nowTuple1); a whole(Bound, Bound)return changed ABI (now kept).(&str,)beside(String,)(now compares C layout); aResult's bound-pair error became a tuple (now only a whole success return is).65e9db4d(ffi/src/kotlin.rs:1476, from the maintainer's local reviewer and Codex): Kotlin flattened a tuple of one afterstrip_refs, so a&(Vec<u8>,)orBox<(Vec<u8>,)>return, which C returns as the tuple struct, broke the JNI build. The flattening now readsfunction.okitself; the api fixture'slent_backandboxed_tuplereproduce it and round-trip in C, C++ and Kotlin.Merge of main at
802108e731c5030823cf7b5d3d54c80319e069b2: the only conflict was this PR deleting its finished quest file while #249 updated that file's links; resolved by deleting it. Nothing else changed since the reviewed headc25212cf, so this merge is a recorded skip, too trivial to review.Checks
On head
c25212cf, with origin/main merged (#231, #235, conflict-free):just check,just test(C, C++ and Kotlin transcripts), andjust ffi moq(C, C++, Kotlin) pass.just ffi censusran on the first head; the later fixes change no moq mapping.Follow-ups
None.
🤖 Generated with Claude Code
(written by Claude Opus 5.5)