Repository navigation
feat(ts): translate zero-sized values MIR never assigns - #304
Merged
Merged
Conversation
Quest-Claim: 57911bd6ae413a20
A zero-sized result or local that nothing assigns, the return place included, is made as a constant of it is, and a constant of std's iterators of zero-sized fields, as `from_fn` of a closure capturing nothing makes, is the call that makes it. A VecDeque reserves at the first item it extends by, as std's does for zero-sized elements. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # ts/fixtures/lang/caller.ts # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/src/harness.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/src/harness.rs # ts/fixtures/lang/src/lib.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated
marked this pull request as ready for review
October 30, 2026 22:00
|
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 5 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 |
This was referenced Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
rustc's MIR never assigns a zero-sized place. It drops the assignment and reads each zero-sized operand as a constant. Two shapes failed in
hints.rs:#[derive(Clone)]of a unit struct or|_| [0u8; 0], emittedreturn _0of an undeclared_0, andtscrejected it (hints.ts(1903,12): Cannot find name '_023').unassignedskipped the return place.iter::from_fn(|| None::<u8>)was refused as "constructingstd::iter::FromFn<..>from another crate" (hints.rs:213). The call was mapped, but the later read of the zero-sized local is a constantFromFn(ZeroSized), which translation tried to build by its fields.An audit of the same paths found more of each:
[Unit; 3], a struct of zero-sized fields, a one-variant enum, and a tuple of them.Map<Empty<u8>, F>and the other closure adaptors overEmpty, and aPhantomDatafield in a value-tree constant.VecDeque<Unit>translated,reservesshowed that std's deque checkslen + lower + 1for overflow at the first item it extends by. It does this even for zero-sized elements, whose capacity never fills, and the runtime didn't.Approach
unassignedmakes any zero-sized, inhabited local that is read and never assigned, the return place included, throughconst_value(ZeroSized), as a constant of its type is. This replaces its special cases for unit structs, empty arrays,fmt::Error, andParseBoolError. A closure's local keeps its declaration at entry.Body::zero_sized_stdmakes std's zero-sized values for both constant paths (Fx::adtand the value-treeBody::value):PhantomDataandEmpty, as before.iter::makerfinds the function, or theIteratormethod, named for the type (FromFntofrom_fn,Maptomap), and takes its generic arguments from its result type, with the item for the rest. Only a maker whose inputs are exactly the type's fields, in order, is used.rt.dequeReservemakes std's first-item reserve indequeExtend.Tests, differential against native wasm32:
hints::hints: a flatten of[u8; 0], the hint of aflat_mapto[u64; 0]over up to 2^60 items, andfrom_fnof a closure capturing nothing beside the capturing one.hints::reserves: a unit struct element. Its limits were measured on wasm32 withtry_reserve:Vecusize::MAX,Vecof pairs0x1fff_ffff, set0x1fff_ffff, map0x0e00_0000. Each collection is collected or extended atu32::MAXandusize::MAX + 1items without allocating them.zeros::zeros: unit structs,[T; 0],[Unit; 3], a struct of a unit andPhantomData, a one-variant enum, and a tuple, each returned. Closures capturing nothing, returned, moved, and held in a field.Vec<()>andVec<Unit>. Drop order of zero-sizedDroptypes: locals, a result, aVec's items, a struct before its fields, a moved value, an array, andmem::forget.from_fncapturing and not, andMap,Copied,Rev, andFilteroverEmpty.Impact
tscnow translates.rt.iterEmpty()passed as a constant is now cast to its iterator type, and an unassigned empty array local is now declared rather than inlined, as the snapshot diffs show.rt.dequeReserveis new in the runtime.Folds.map(..)andBacks.rev()as "constructingMap/Revfrom another crate". Those refusals were this bug, recorded since Lower bodies from MIR #67. The refusals the fixture means, of overridingfoldand implementingDoubleEndedIterator, still fire, and its census drops from 129 to 127.Alternatives
-Zmir-enable-passes): std's and dependencies' MIR keeps it, so translation must handle it anyway.[] as T[]for empty arrays: the declared form type-checks and reads as well.Follow-ups
PhantomData's derivedDebug,Default, andPartialEqare unsupported calls, and()is refused as a hash or B-tree key and incollect::<Option<_>>. Both were found by this audit.Vec::capacity,reserve, andtry_reserveare unsupported; std-rest already plans them.Choices made without asking: the follow-up quest above; a new
zerosfixture module beside thehintscases the quest named.Review
Codex (
quest review), gpt-6.1-sol at medium effort, session01a18ec9. Reviewed head6b8c12438ffb56cfc81450c317c9fe4bafceb8deagainstorigin/maina0195648.9e6140b9): 3 findings, all refuted. Each described fix(ts): fold an inclusive range through std's adaptors as core does #301's inclusive-range folds (rt/iter.tsfolded()and reverse folds,IntoIteratorof&mutinlower/iter.rs) as removed. fix(ts): fold an inclusive range through std's adaptors as core does #301 had landed on main after this branch's last merge, and the reviewer compared trees.git log HEAD..origin/mainlisted onlya0195648. Merging origin/main (3f697a93) restored fix(ts): fold an inclusive range through std's adaptors as core does #301's code unchanged.6b8c1243): no findings.Local checks:
just cipasses on6b8c1243, including the lang, task, reject, and census fixtures and the 50,000-case lang differential against wasm32.(written by Claude Opus 5.5)
🤖 Generated with Claude Code