Repository navigation
feat(ffi)!: Copy types changed in place cross by value - #808
Conversation
Quest-Claim: 0b2d5c60a2d501fa
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
I reviewed the whole PR: a Copy type changed in place now crosses as an opaque value, and its &mut self methods and field setters are written back in each language.
- Values with
pubfields or&mut selfmethods:changed()is gone, so these types are nowDecl::Value. A value struct's fields get accessors.written_backrefuses, at its span, a&mut selfmethod that returns a future or aResult. Ty::InOutin the C ABI: a value's&mut selfis passed as<T> *self. The glue lifts amutcopy, calls with&mut a_self, and writes it back throughthisbeforefinish.- In-place languages: C++ assigns
*this. Swift ismutatingwith adefer { self = … }. Go uses a pointer receiver and assigns*self. - Hand-back languages: in Kotlin, Python, and Dart,
Abi::handed_backbuilds a&selfclone that returns(Self, …ret). It keeps the real method's renamed C name. Aback_<C name>::callshim copies*p0, calls the real C function, and writes the copy to the first out. - Docs and quest: I checked the README rules table and each language's section against the generators. The quest file and its links are removed.
I checked the shim's parameter mapping by exporting a scratch crate whose value had &mut self methods returning Option<u64>, String, and a 4-tuple, plus ones taking closures and &mut Self with &str. Real params are [self, params, written outs, tuple outs]; the back shim's are [self, params, written outs, out_v, …]. Every index lined up. The glue built with --features kotlin,python,dart. A &mut self -> &u64 on a value is already refused at its span (returned borrow of a value written back), so the new in-place write-back can't hit a glue borrow conflict.
claude-opus-5-5 | 𝕏
|
Maintainer decision (2026-11-14, via /quest-run):
(Written by Claude Opus 5.5 with high effort) |
… Rust shim Kotlin's, Python's, and Dart's glue now call a value's in-place C function on their own copy and write that copy back inline. Abi::handed_back no longer builds the back_<C name>::call module. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Applied the maintainer's decision on head 7e82a7a: the Rust (Written by Claude Opus 5.5) |
# Conflicts: # ffi/fixtures/reject/expected.txt
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Since d318c87, the Rust back_<C name> shim is replaced by an expression each generator writes into its own glue call. A reject case also now covers a value's &mut self that returns a borrow of itself.
abi::Handedreplaces the shim:handed_backreturns aHanded { name, lead, unit, tuple }instead of shim source.Handed::callemits an inlineunsafeblock that copies*args[0], calls the real C function with&mut handed_, and writes the copy (and Rust's return) through the outs. I compared it with the removed shim branch by branch. The argument slicing (args[1..=lead], then the tuple outs fromlead + 2) and the three return shapes are unchanged. It still uses the real, possibly renamed, C name.- Kotlin, Python, and Dart call sites: the Kotlin
rust_callclosure, Python'srt::call(|| …), and Dart'sservedthunk (keyed by C name in the newBTreeMap) useHanded::call. Each argument expression still appears exactly once, so it is evaluated once, as it was when passed to the shim. Theunsafeblock nested inside Dart's outerunsafeis covered by the glue's crate-wide#![allow(unused_unsafe)]. Counter::bump_refreject case: the fixture line (1218) and the expected diagnostic, including its place in the sorted output, matchcollect.rs's "returned borrow of a value written back" refusal.
I checked this by reading the code. No nightly toolchain was cached in this shell, so I did not rebuild the glue. The PR body reports the api transcripts passing in all seven languages on this head.
claude-opus-5-5 | 𝕏
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Since 7e82a7a, the only new commit merges origin/main (0f2331b) into the branch.
- Regenerated
reject/expected.txt: lines thatmainremoved moved the fixture source up by 9 lines. I checked every entry the range-diff touched against the mergedffi/fixtures/reject/src/lib.rs.Counter::try_bumpis at 1199,bump_laterat 1204, andbump_refat 1209.hidden_*andboxed_bytesare at 1168 to 1183,poll_readyat 127,Link::sessionat 144, andmeasureat 161. All of them match, and their order follows the file's existing byte-wise sort. kotlin.rscontext: the range-diff's only other change ismainrenaming_Native.Pollableto_Native.__Pollable. It sits next to this PR's Kotlin code but changes none of the PR's own lines.
claude-opus-5-5 | 𝕏
Main's #808 makes Score an opaque value, so the refusal fixtures hold a new checked Copy handle, Mark. Its handed-back `&mut self` calls (#808) now pass a status, which `Handed` places last; `Score::note(&Mark)` covers it. #807's underscore names extend to `__Close` and `__Refused`, and Swift's moq program drops the `try`s #808's values made unneeded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Finishes Copy types that change cross by value. A
Copytype with apubfield or apub&mut selfmethod now crosses as an opaque value, as otherCopytypes have since #742. It is no longer anArchandle. A change reaches only the copy it was made on, as in Rust.Changes
collect.rs:changedis gone, so such a type is a value. A value'spubfields export as a getter and setter, as an object's do. Its&mut selfmethods export too, except two that are refused at their span (written_back): one returning a future and one returning aResult.&mut self, its field setters included, takes<T> *self(the newTy::InOut). The glue writes back what the call left through it.constand assigns*this. Swift: the method ismutatingand assignsself. Go: the method has a pointer receiver and assigns*self.x = x.add(o). If Rust also returns something, the method returns aPair, atuple, or a record, with each element of a returned tuple after the value.Abi::handed_backgives each generator that function's signature and aHanded, whosecallwrites the glue's call inline: it copies the value, calls the in-place C function on the copy, and writes the copy (and Rust's return) to the outs. There is no separate Rust shim. All three generators' fast paths (flat records, tuples, written-backMutarguments) work unchanged.ship.mdandkotlin-values.md.Public API
Breaking (
!). Every generated binding holds these types differently:stats::Traffic,stats::Presence,stats::Failures, andstats::Refusalsare now values. Their fields are<field>_get/<field>_setaccessors, andaddwrites back in place in C, C++, Swift, and Go.addreturns the new value:t = t.add(o).<T>_freeand<T>_cloneof these types are gone.Migration: in C, hold
moq_stats_Trafficby value and pass&t. Drop frees and clones. In Kotlin, Python, and Dart, reassign the method's result. No wire impact.Decisions
Settled by the maintainer on 2026-11-14 (comment):
&mut selfmethod returning aResultor a future at its span in every language (rejected: refuse only in Kotlin, Python, and Dart). In those three the error would be raised as an exception, losing a change Rust made before failing, and a future outlives the call that has to hand the value back. Refusing in one language only would split one Rust type's API. None of moq's methods is affected; the reject fixture covers both refusals.absorb -> (u64, u64)returnsTriple<Score, ULong, ULong>. Nesting would need a C tuple struct the API never declares.back_<C name>::callshim in the glue). Each generator's glue call (the JNI native, the Python glue function, the Dart serve thunk) now copies the value, calls the in-place C function on the copy, and writes the copy back inline.Handed::callin abi.rs builds that expression once for all three, so the argument mapping lives in one place.Validation
api_py_types), and Dart.Score::take(&mut selfreturning a value), mutated on a copy, with the original left as it was.Score::absorb(&mut selfplus a written-back&mut Scoreargument, returning a tuple).Hits:#[non_exhaustive]with all fieldspub, as moq's stats are. It is changed byaddand by a setter, and a copy kept before the change is unchanged.Extent: apubfield beside a private one, withslidereturning the old start, and a setter.Hits::eq(&mut self, …): its C name clashes withPartialEq'sapi_Hits_eq, so it is renamedapi_Hits_eq_. The hand-back must keep the renamed name; the local review found this case.just ffi test(all fixture tests, includingrejectwith its two new refusals) passes.just ffi moqexports moq at its pin, builds the export, and runs its C, C++, Swift, Kotlin, Python, Go, and Dart programs and linters. All pass. In the header,moq_stats_{Traffic,Presence,Failures,Refusals}_add(moq_stats_X *self, moq_stats_X other)take the value in place.just cipasses on the head.just ffi moqandjust cipass. The generated Kotlin, Python, and Dart glue has noback_module. Its inline calls keep the renamedapi_Hits_eq_and passabsorb's tuple outs after the value's.quest review --harness codex(gpt-6.1-sol, high). On the earlier shim head, round 1 found one medium issue (a renamed C name lost in the hand-back), fixed in d318c87. On this head, a new session's round 1 flagged that a value's&mut selfreturning a borrow of itself would not compile in the glue. That was refuted:collect.rsalready refuses it at its span. A reject fixture case,Counter::bump_ref, now covers it. Round 2 found nothing (10/10). Main was then merged in (onlyreject/expected.txtconflicted; it was regenerated), and round 3 on the merge found nothing (10/10).just ciandjust ffi moqpass on the merge.just ffi sanitize(it needs the sanitize shell) and the nightly shapes run. The bench was not re-measured, since this PR makes no performance claim.Follow-ups
clock::WallMapstays aCopyhandle. ItsOption<(Instant, Wall)>leaves bytes uninitialized forNone, so it fails the "value-independent padding" rule that this quest's Goal assumes. The quest listed it among the five, but the padding rule, not&mut self, is what keeps it a handle. Making it a value would take a padding rule that handles enum variants, which is close to padding-layout's open decision. No new quest is added here.Models
Size: M
Implementer: Claude Code, claude-opus-5-5, unknown effort
Implementer: Claude Code, claude-opus-5-5, unknown effort (iterate, M)
🤖 Generated with Claude Code
(Written by Claude Opus 5.5)
Summary by CodeRabbit
Review
Rewritten by
quest reviewafter each round.Head: 82443af
Base: origin/main at 0f2331b
Risk: medium,
the base has no RISK.md to name what is always high or customer-facing, so every change is mediumClassified: medium by default
Reviewer: Codex, gpt-6.1-sol, high effort
Session:
01a1db7c-8957-7873-9e46-5226f94a8527, round 3Score: 10/10,
The merge preserves the reviewed behavior and correctly updates rejection diagnostics, with no new findings from code inspection; builds and tests were not run as instructed.Rounds: 3; first round 1 (medium 1); findings 1 (medium 1): fixed 0, refuted 1, open 0
Findings: none