Skip to content

feat(ts): translate zero-sized values MIR never assigns - #304

Merged
kixelated merged 6 commits into
mainfrom
quest/m3/ts-zero-sized
Oct 30, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m3/ts-zero-sized

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

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:

  • A function or closure returning a unit struct or an empty array, such as #[derive(Clone)] of a unit struct or |_| [0u8; 0], emitted return _0 of an undeclared _0, and tsc rejected it (hints.ts(1903,12): Cannot find name '_023'). unassigned skipped the return place.
  • iter::from_fn(|| None::<u8>) was refused as "constructing std::iter::FromFn<..> from another crate" (hints.rs:213). The call was mapped, but the later read of the zero-sized local is a constant FromFn(ZeroSized), which translation tried to build by its fields.

An audit of the same paths found more of each:

  • Results: [Unit; 3], a struct of zero-sized fields, a one-variant enum, and a tuple of them.
  • Constants: Map<Empty<u8>, F> and the other closure adaptors over Empty, and a PhantomData field in a value-tree constant.
  • Runtime: once VecDeque<Unit> translated, reserves showed that std's deque checks len + lower + 1 for 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

  • unassigned makes any zero-sized, inhabited local that is read and never assigned, the return place included, through const_value(ZeroSized), as a constant of its type is. This replaces its special cases for unit structs, empty arrays, fmt::Error, and ParseBoolError. A closure's local keeps its declaration at entry.
  • Body::zero_sized_std makes std's zero-sized values for both constant paths (Fx::adt and the value-tree Body::value):
    • PhantomData and Empty, as before.
    • Any other std iterator of zero-sized fields becomes the call that makes it: iter::maker finds the function, or the Iterator method, named for the type (FromFn to from_fn, Map to map), 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.dequeReserve makes std's first-item reserve in dequeExtend.

Tests, differential against native wasm32:

  • hints::hints: a flatten of [u8; 0], the hint of a flat_map to [u64; 0] over up to 2^60 items, and from_fn of a closure capturing nothing beside the capturing one.
  • hints::reserves: a unit struct element. Its limits were measured on wasm32 with try_reserve: Vec usize::MAX, Vec of pairs 0x1fff_ffff, set 0x1fff_ffff, map 0x0e00_0000. Each collection is collected or extended at u32::MAX and usize::MAX + 1 items without allocating them.
  • New zeros::zeros: unit structs, [T; 0], [Unit; 3], a struct of a unit and PhantomData, a one-variant enum, and a tuple, each returned. Closures capturing nothing, returned, moved, and held in a field. Vec<()> and Vec<Unit>. Drop order of zero-sized Drop types: locals, a result, a Vec's items, a struct before its fields, a moved value, an array, and mem::forget. from_fn capturing and not, and Map, Copied, Rev, and Filter over Empty.

Impact

  • Public API: none; the translator's output only. Code that was refused or failed tsc now 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.dequeReserve is new in the runtime.
  • The reject fixture no longer refuses Folds.map(..) and Backs.rev() as "constructing Map/Rev from another crate". Those refusals were this bug, recorded since Lower bodies from MIR #67. The refusals the fixture means, of overriding fold and implementing DoubleEndedIterator, still fire, and its census drops from 129 to 127.

Alternatives

  • Disabling rustc's zero-sized operand rewrite for the crate (-Zmir-enable-passes): std's and dependencies' MIR keeps it, so translation must handle it anyway.
  • A table of std adaptors and their constructors: the name and signature match covers each adaptor without one, and refuses any whose maker doesn't take exactly its fields.
  • Keeping the typed [] as T[] for empty arrays: the declared form type-checks and reads as well.

Follow-ups

  • std's impls of its zero-sized types: PhantomData's derived Debug, Default, and PartialEq are unsupported calls, and () is refused as a hash or B-tree key and in collect::<Option<_>>. Both were found by this audit.
  • Vec::capacity, reserve, and try_reserve are unsupported; std-rest already plans them.

Choices made without asking: the follow-up quest above; a new zeros fixture module beside the hints cases the quest named.

Review

Codex (quest review), gpt-6.1-sol at medium effort, session 01a18ec9. Reviewed head 6b8c12438ffb56cfc81450c317c9fe4bafceb8de against origin/main a0195648.

Local checks: just ci passes on 6b8c1243, 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

kixelated and others added 6 commits October 30, 2026 13:20
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 kixelated changed the title Zero-sized values made out of sight feat(ts): translate zero-sized values MIR never assigns Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 22:00
@coderabbitai

coderabbitai Bot commented Oct 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b8440dd9-37c3-4748-9c59-794c6077147e
Commits

Reviewing files that changed from the base of the PR and between a019564 and 6b8c124.

Files selected for processing: 33
  • quest/m3/README.md
  • quest/m3/ts-zero-sized-impls.md
  • quest/m3/ts-zero-sized.md
  • ts/fixtures/lang/caller.ts
  • ts/fixtures/lang/expected/arrays.ts
  • ts/fixtures/lang/expected/core/option.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/hints.Unit.ts
  • ts/fixtures/lang/expected/hints.ts
  • ts/fixtures/lang/expected/iters.ts
  • ts/fixtures/lang/expected/strs.ts
  • ts/fixtures/lang/expected/zeros.Both.ts
  • ts/fixtures/lang/expected/zeros.Holds.ts
  • ts/fixtures/lang/expected/zeros.Loud.ts
  • ts/fixtures/lang/expected/zeros.One.ts
  • ts/fixtures/lang/expected/zeros.Pair.ts
  • ts/fixtures/lang/expected/zeros.Quiet.ts
  • ts/fixtures/lang/expected/zeros.Unit.ts
  • ts/fixtures/lang/expected/zeros.ts
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/hints.rs
  • ts/fixtures/lang/src/lib.rs
  • ts/fixtures/lang/src/zeros.rs
  • ts/fixtures/reject/census.md
  • ts/fixtures/reject/expected.txt
  • ts/fixtures/task/expected/model.ts
  • ts/runtime/rt/collections.ts
  • ts/runtime/rt/iter.ts
  • ts/src/lower/body.rs
  • ts/src/lower/consts.rs
  • ts/src/lower/iter.rs
  • ts/src/lower/mir/mod.rs
  • ts/src/lower/mir/rvalue.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kixelated
kixelated merged commit 02f8380 into main Oct 30, 2026
2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-zero-sized branch October 30, 2026 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant