Repository navigation
feat(ts): translate Box::new_uninit, new_zeroed, write, and assume_init for one value - #639
Conversation
Quest-Claim: 88dc7439271105db
…it for one value `Box::new_uninit` holds what `MaybeUninit::uninit` makes, and `write` and `assume_init` pass the value through. `Box::new_zeroed` builds the value of zero bytes part by part: arrays, tuples, and the crate's structs field by field, an enum as the variant its zero tag or niche picks, and any other part rustc says zero bytes are valid for as that constant; a part they aren't valid for, such as a reference, stays unwritten for a write to fill. MIR constant lowering now reads a pattern type as its base and builds a `Duration` from its fields, which a zeroed `Duration` needs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Box::write now writes into the box's own object or array, as MaybeUninit::write does, so a pointer taken before it still points at what the box holds. Box::new_zeroed recurses through Wrapping, Saturating, Reverse, and AssertUnwindSafe, which JS keeps as the value they wrap, and through Cell, UnsafeCell, RefCell, RwLock, and Mutex, so a part zero bytes are a value of stays zero beside one they are not. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
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 14 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. |
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit a21768f against base efa6300.
No actionable findings identified in this static review. Extending the existing MaybeUninit representation and constant lowering is a consistent direction. I checked Box::write's in-place object/array write and pointer-analysis integration, recursive zero construction for aggregates and supported wrappers, validity-gated constant lowering, enum selection, pattern types and Duration, and the added initialization/drop/retained-pointer fixtures. The two defects described as fixed in the PR are addressed in the current code.
Verification limits: GitHub source/diff inspection only. I did not run the translator, native differential fixtures, or just ci, and have not independently reproduced the reported passing cases or checkout-dependent TypeId snapshot failures. This is a COMMENT, not an approval.
(Written by review (OpenAI))

Completes quest/m3/ts-unsafe/box-new-uninit.md.
Summary
rust2-ts now translates
Box::new_uninitandBox::new_zeroedfor one value, written throughMaybeUninit::write, its pointer,Box::write, or a raw pointer fromBox::into_raw, and taken byassume_init.Box::new_uninitholds whatMaybeUninit::uninitmakes.Box::writewrites into the box's own object or array, asMaybeUninit::writedoes, so a pointer taken before the write still points at what the box holds. The pointer analyses (derives,stores) treat it as they treatMaybeUninit::write.Box::assume_initpasses the value through. It used to be mapped for boxed slices only, and now covers one value too.Box::new_zeroedbuilds the value of zero bytes part by part. Arrays, tuples, and the crate's structs are built field by field.Wrapping,Saturating,Reverse, andAssertUnwindSafe, which JS keeps as the value they wrap, are zeroed through to that value.Cell/UnsafeCell,RefCell/RwLock, andMutexwrap their zeroed value, unborrowed and unlocked. An enum becomes the variant its zero tag or niche picks, with that variant's fields zeroed in turn. Any other part that rustc'sValidityRequirement::Zerocheck allows is lowered as a constant of zero bytes. A part that zero bytes aren't a value of, such as a reference or aString, stays unwritten (undefined, asMaybeUninit::uninitleaves it) until a write fills it.ty::Pat) as its base type and builds aDurationfrom its two fields. Both are needed to zero std types likeNonZero's inner pattern type andDuration.MaybeUninit<T>row documents both constructors.The new
lang/manualcasesuninit::boxed_oneanduninit::boxed_zeroedcover:Box::write, compared to the box afterwards, for a struct and an arrayassume_init, which drops nothingchar,f32Cell,AtomicU32,Wrapping,Duration,RefCell,MutexWrapping<(u32, &u8)>and anUnsafeCell<(u32, &u8)>with only the reference writtenBoth cases match native Rust.
Choices made without asking
Box::writeis mapped too. The quest asked to check whetherwritewas already covered. It wasn't, and it's the natural way to fill aBox<MaybeUninit<T>>, so it's included.Box::<(&u8, u32)>::new_zeroed()translates, and only an actual read of the unwritten part differs.Impact
Public API: the translator's output. Code using these four
Boxfunctions, which was refused before, now translates. Other output only gainsDurationand pattern-type constants. The runtime is unchanged.Validation
UPDATE_EXPECT=1 just ts test --test fixtures lang::manual: passes against native Rust. The snapshot diff adds only the zeroedCell,AtomicU32,Wrapping, andDurationcases, plus renumbered temporaries.quest check --fix: ok.just cion a21768f: everything passes exceptlang::dynsandlang::panics. Their diffs touch onlyTypeIdhash strings, and bare origin/main (efa6300cc) fails the same way in this checkout. moq-dev/rust2#633, which fixes those snapshots, is still open.Follow-ups
Each is recommended to plan:
MaybeUninit::zeroed,mem::zeroed, andBox::new_zeroed_slice, reusing the zero-value builder (Fx::zeroed) added here.Rc::new_uninit/Arc::new_uninitand theirnew_zeroed.&raw mut (*p)[1][0]. The fixture writes its zeroed grid through a slice instead.Models
Size: S
Implementer: Claude Code, Claude Opus 5.5, high effort
(Written by Claude Opus 5.5 with high effort)
🤖 Generated with Claude Code
Review
Rewritten by
quest reviewafter each round.01a1b61f-018c-7ae0-b705-eeecb4ea0dcd, round 2Both reported defects are fixed and covered by differential fixtures, and inspection found no additional concrete defects; tests were not rerun in the read-only environment.ts/src/lower/library.rs:2477:Preserve the destination allocation in Box::write;ts/src/lower/pointer.rs now writes object and array values into the original allocation and returns it, with retained-pointer comparisons for Pair and [u16; 3] in uninit::boxed_one.ts/src/lower/mir/place.rs:1628:Preserve zeroed fields inside partially invalid standard wrappers;ts/src/lower/mir/place.rs now recursively zeroes supported wrappers and cells, with partial-reference initialization cases for Wrapping and UnsafeCell in uninit::boxed_zeroed.