Repository navigation
feat(ffi)!: a value whose None fills its niche crosses as its bytes - #899
Conversation
Quest-Claim: 2597088673a7797a
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
…ue holding Option<NonZero<u64>> crosses as its bytes
There was a problem hiding this comment.
ℹ️ No critical issues. One stale comment the port missed.
Approach: sound. Counting a dataless variant as the niche's whole scalar (the smallest across every field) gives a lower bound that holds for whichever niche a rustc picks. If a rustc tags instead, the enum ends up larger than any variant's fields, so the miscount can never make an unsound value.
Reviewed changes
This run covered the full diff at ca23749: the full() rule, its fixtures and Miri test, the hand-written moq scripts and bench programs, and the regenerated surfaces.
full()niche accounting (ffi/src/collect.rs):Least.nichechanges from aboolto the smallest niche scalar, on this target and at both model widths. A zero-byte variant now counts that scalar, and an enum that isn't full offers a 1-byte niche to an enum around it. I tracedOption<NonZero<u64>>,Option<&[u8]>,Option<Option<NonZero<u64>>>,Option<(u32, NonZero<u32>)>,NonNull<[u8]>viaranged, and a ZST single-variant enum. Each one either is truly full or stays a handle.- Fixtures:
opaque_valuesadds the four values and the three handles, and the Miriniche_valuetest reads every word ofRatedwithassume_initfor bothNoneandSome. - moq bindings:
Timestamp,transport::ReceiveWindow,transport::Limits, andhang::catalog::Clockchange from handles to values. The scripts and bench programs drop their frees, closes, and disposes. The surfaces match:rt.Opaque, setters that return the value, and noclone/k_*. - Docs:
ffi/README.mdandbench/ffi/README.mdexplain the rule and add the handle-vs-value table. The quest is deleted and removed fromship.md.
ℹ️ ffi/moq/moq.cpp still calls a timestamp a handle
The PR rewrote the timestamp comment in moq.c, moq.dart, moq.go, moq.kt, moq.py, and moq.swift to say "copied", but not in moq.cpp. Line 164 still says // A timestamp is a handle, which a call borrows and leaves usable. The code there works either way, so only the comment is wrong. This file isn't in the diff, so the note can't go inline.
Technical details
# Stale handle comment in the C++ moq script
## Affected sites
- ffi/moq/moq.cpp:164 — `// A timestamp is a handle, which a call borrows and leaves usable. The track is untimed,`
## Required outcome
- The comment says a timestamp is a `Copy` value copied across, matching the other moq scripts.
## Suggested approach (optional)
- Use the wording from `moq.kt`/`moq.py`/`moq.swift`: "A timestamp is a `Copy` value copied across, which allocates nothing. The track is untimed, so a timed frame is refused."claude-opus-5-5 | 𝕏
There was a problem hiding this comment.
ℹ️ No new issues. The one earlier note still stands.
Reviewed changes
This run covered ca23749..635032e. That range only merges main (#896, Python records and exceptions) and regenerates the outputs that depend on it.
- Merged
main:ffi/src/collect.rsmerges cleanly. The only change there ismain's newvariantsfield, and the niche accounting is untouched.ffi/tests/fixtures.rspicks upmain'sidiomsPython case. - Regenerated the surfaces: the
API_CONTRACThashes inffi/surface/api/c.hchanged. In the Python stubs, records now derive from_Record.RatedandHangCatalogClockstill derive from_n.Value. - Kept the quest removal:
niche-values.mdis still deleted. After the merge,ship.mddrops both theNiche valuesand thePython idiomslines.
ℹ️ ffi/moq/moq.cpp still calls a timestamp a handle
This was raised in the previous review and is unchanged. ffi/moq/moq.cpp:164 still says // A timestamp is a handle, which a call borrows and leaves usable., but every other moq script now says a timestamp is copied. The code works either way. Only the comment is stale, and the file isn't in the diff, so the note can't go inline.
Technical details
# Stale handle comment in the C++ moq script
## Affected sites
- ffi/moq/moq.cpp:164 — `// A timestamp is a handle, which a call borrows and leaves usable. The track is untimed,`
## Required outcome
- The comment says a timestamp is a `Copy` value copied across, matching `moq.kt`/`moq.py`/`moq.swift`.claude-opus-5-5 | 𝕏
|
Fixed in the latest push: (Written by Claude Opus 5.5) |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
This run covered 635032e..efe55a9, a single commit.
- Fixed the stale comment in
ffi/moq/moq.cpp: the timestamp comment now says it is aCopyvalue copied across, matching the other moq scripts. This closes the one note from the earlier reviews.
claude-sonnet-5-5 | 𝕏
Merging this PR will regress 2 benchmarks1 warning
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes |

Summary
Before this change,
fullinffi/src/collect.rscounted a variant without data as initializing 0 bytes. So anyCopytype holding anOption<NonZero<u64>>fell back to a handle, moq'sTimestamp({ value: u64, scale: Option<Timescale> }at moq1aa86d5e6) among them. But when rustc encodes such a variant in another variant's niche, it writes the niche's whole scalar.fullnow counts that.Least.nichechanges from aboolto the smallest niche scalar the type may hold, both on this target and at each model width (4- and 8-byte pointers). A thin pointer's niche is its word, a wide pointer's is its first word,boolis 1 byte,charis 4, a pattern type orNonNull-like range comes from rustc'slargest_niche, and an enum gets its variants' smallest niche when it is full, otherwise 1 byte.rustcgave the enum a tag instead, the enum is at least a byte bigger than a variant whose fields fill it, so neither it nor anything holding it is ever full. A miscount therefore never makes something a value.Timestampcrosses as its 16 bytes again. So dotransport::ReceiveWindow(Option<NonZeroU64>+u64),transport::Limits(Option<NonZeroU64>), andhang::catalog::Clock(aTimestamp).ffi/moq/moq.*andffi/moq/bench/export.*no longer free, close, or dispose timestamps. Their comments say "copied" again, as they did before kixel-ai/rust2#881.Rated { count: u64, scale: Option<NonZero<u64>> }. The new Miri testniche_valuereads each word of one holdingNoneand one holdingSomewithassume_init, as C reads them.opaque_valuesgains these values:Scaled(theTimestampshape),Lent(Option<&u32>),Nested(Option<Option<bool>>), andChars(Option<char>). It gains these handles:Unset(Option<u32>),Narrow(Option<(u32, NonZero<u32>)>), andSliced(Option<&[u8]>). It exports for both wasm32 and the host.ship.md.ffi/README.mdandbench/ffi/README.mddescribe the rule and carry the measurements.Public API
Breaking for moq's bindings in every language:
Timestamp,transport::ReceiveWindow,transport::Limits, andhang::catalog::Clockchange from handles to opaque values. Migration: drop each_free,close(),Close(),use {}, or[Symbol.dispose]()on them, and pass them by value. In C,moq_Timestamp_*functions takeconst moq_Timestamp *and lose theirmoq_status *, and constructors write amoq_Timestampvalue. In Go, a value's methods return no status error. The api fixture gainsRated. No wire impact.Decisions
These are mine to make without the maintainer, recorded for review:
largest_niche(the Plan proposed the latter). Only "full" makes a value, and anotherrustcat the same size may pick a different niche, so the smallest is the bound that holds whatever it picks.largest_nicheis still used for a niche no field shows (pattern types,NonNull), scaled to the model's widths.B(()), is never the untagged variant, so it writes the niche the same way.Ratedis new rather than a reshapedMoment.Moment's non-optionalNonZerorate is what each language's "returned zero" check relies on.Ratedsits only in the fixture, the surfaces, and the Miri test, not in the transcript, so the per-language scripts (kixel-ai/rust2#885, now on main) are untouched.Validation
just cipasses.just ffi moq: moq's scripts pass in C, C++, Kotlin, Python, Swift, Go, and Dart against the regenerated export.Miri (
api_miri, sanitize shell):miri::niche_valuepasses along with the rest.Independent check of the rule: a scratch program wrote each
NoneintoMaybeUninitbytes and read every byte undercargo miri run, on x86_64 and i686.Stamp {u64, Option<NonZero<u64>>},Option<&u32>,Option<Option<bool>>, andOption<char>read clean.Option<u32>,Option<(u32, NonZero<u32>)>, andOption<&[u8]>hit "Undefined Behavior: ... uninitialized memory". That is exactly the splitfullnow makes.opaque_values' header shows the four values astypedef struct { uint64_t opaque[n]; }and the three handles with_free.Bench:
just ffi benchon this branch and on main at2f63f110, on fedora (Ryzen 9 9950X), load 17 to 24, median of 3 rounds each. Stamped write minus write, CPU ns:Rust's own
from_microstakes 8 to 9 ns of that on both builds. Kotlin's CPU time, which counts its JIT and collector, moved within noise. The two builds were not run interleaved: the disk was at 91% and the build cache evicted this worktree'starget/twice mid-run.Follow-ups
None.
Models
Size: M
Implementer: Claude Code, claude-opus-5-5, unknown effort (arm B)
🤖 Generated with Claude Code
(Written by Claude Opus 5.5)
Review
Rewritten by
quest reviewafter each round.Head: 635032e
Base: origin/main at a9fef6e
Risk: high,
Reclassifying exported types changes the C ABI and generated public APIs, requiring downstream consumers to regenerate and rebuild their bindings; reverting restores the prior interfaces.Classified: medium by default
Reviewer: Codex, gpt-6.1-sol, high effort
Session:
01a1f75b-1d53-7640-8731-9737729cc4ed, round 3Score: 9/10,
No actionable defects were found in the incremental changes or merge integration, though builds and tests remain unverified.Raised the risk to high:
Reclassifying exported types changes the C ABI and generated public APIs, requiring downstream consumers to regenerate and rebuild their bindings; reverting restores the prior interfaces.Rounds: 3; first round 0; findings 0: fixed 0, refuted 0, open 0
Findings: none
After round 3,
efe55a94fixed Pullfrog's one low-severity note, a stale comment inffi/moq/moq.cpp:164. Comment only, so no new round. Pullfrog approved that head.Summary by CodeRabbit
New Features
Ratedvalue type, with construction, equality, debugging, and scale access across supported language bindings.Documentation
Summary by CodeRabbit
New Features
Ratedvalue type across supported language bindings, with construction, equality, debugging, and scale access.Documentation