Skip to content

fix(empty payload): a message with no fields works in every backend - #740

Merged
Andste82 merged 3 commits into
mainfrom
fix/empty-payload-738
Oct 12, 2026
Merged

Andste82 merged 3 commits into
mainfrom
fix/empty-payload-738

Conversation

@Andste82

Copy link
Copy Markdown
Member

Closes #738. The corelib half is already on main: sofa-buffers/corelib-c-cpp#195 and sofa-buffers/corelib-java#128.

payload: {} has always validated, and MESSAGE_SPEC §2 omits every field equal to its default — so the all-default message is the empty byte string, a message with no fields is a legal schema whose every value encodes to zero bytes (the ping, the signal, the keep-alive), and MAX_SIZE == 0 for it.

Nothing in tests/ or examples/ had one. So the shape reached eleven backends untested and got eleven answers. The declaration was not the coverage.

Per backend

backend was now
cpp the header did not compile under the -Wall -Wextra -Werror the suites build generated code with: deserialize's stream parameter is unused when a type binds no field. The harness's to_json the same. named only where an arm reads it — the rule _size and _count already follow — and the harness gets the discard from_json already had
typescript emit: project panicked (the bench sink folds one cheap scalar of the decoded message and indexed fields[0]), and the module named a visitor and a Decoder it did not emit the field-less fold reads the object itself; the decode surface is emitted for every message
zig the harness did not compile — an unused parameter and a var that is never mutated are errors there both discards where the alloc/obj ones already were
rust two warnings (unused variable: os, field m is never read) _os, and the destination borrow the visitor must still hold says why no arm reads it
c an empty struct and a zero-length descriptor table in the header, zero-size arrays in the harness — GCC extensions, not ISO C a placeholder member and a one-entry table the field count keeps unread; _SOFAB_BUF in the harness. Generated library and harness compile under -std=c11 -pedantic-errors

The TypeScript decode surface deserves its own line: the guard that suppressed it came from #160, where its only job was to leave an unused WireType import out — which the rendered-text import scan decides on its own now. By the time the flat-visitor surface landed, that guard was suppressing the very visitor and Decoder that decode() and decoder() name.

Tests

tests/conformance/lib/check_empty_payload.py, wired into all eleven suites (inside cpp's variant loop, rust's label loop and python's engine loop):

Verified by hand against all twelve target legs — c, cpp (both corelibs), csharp, dart, go, java, kotlin, python, rust, typescript, zig — both through this driver and through the corpus round-trip. The go and c suites were run in full.

The COMPILE half is tests/matrix/corpus/defs/empty_payload.yaml, which every suite's corpus leg builds and round-trips, as array_lengths.yaml is for #550.

And it immediately found one more hole — in the test tooling rather than a backend: check_corpus_roundtrip.py read named[key]["fields"] (--dump-ir omits an empty list) and demanded that a value-filled message encode to non-zero bytes, which the one message that has no values cannot. Both fixed, with the carve-out saying why.

Five backend unit tests pin the emitted shapes, each with the with-a-field case beside it, so the rule stays "unused", not "always".

`payload: {}` has always validated, and MESSAGE_SPEC §2 omits every field equal
to its default -- so the all-default message IS the empty byte string, a message
with no fields is a legal schema whose every value encodes to zero bytes (the
ping, the keep-alive), and MAX_SIZE is 0 for it. Nothing in tests/ or examples/
had one, so the shape reached eleven backends untested and got eleven answers
(#738). The declaration was not the coverage.

Per backend:

  cpp         the header did not compile under the -Wall -Wextra -Werror the
              suites build generated code with: deserialize's stream parameter
              is unused when a type binds no field. Named only where an arm
              reads it now -- the rule _size and _count already follow -- and
              the harness's to_json gets the discard from_json already had.
  typescript  `emit: project` PANICKED: the bench sink folds one cheap scalar of
              the decoded message and indexed fields[0]. And the module named a
              visitor and a Decoder it did not emit -- a guard kept from #160,
              where its only job was to leave an unused WireType import out,
              which the rendered-text import scan decides on its own now. By the
              time the flat-visitor surface landed, that guard was suppressing
              the very decode surface decode() and decoder() name.
  zig         the harness did not compile: an unused parameter and a `var` that
              is never mutated are errors there. Both discards now sit where the
              alloc/obj ones already did.
  rust        two warnings: serialize's stream is `_os` when nothing is written,
              and the visitor's destination borrow -- which it must still hold
              -- says why no arm reads it.
  c           the header declared an empty struct and a zero-length descriptor
              table: GCC extensions, not ISO C, and an empty struct's sizeof is
              0 in C and 1 in C++, so the two views of one type did not even
              agree. A placeholder member and a one-entry table the field COUNT
              keeps unread; the harness sizes its MAX_SIZE buffers through
              _SOFAB_BUF and discards what it does not read. Generated library
              and harness now compile under -std=c11 -pedantic-errors.

The two RUNTIME failures are the corelib half, fixed there because §5.1 puts the
rule there: corelib-c-cpp#195 (a zero-length buffer may be handed over as NULL;
std::vector::data() is null at size 0 and C++ encode() aborted in the install
assert) and corelib-java#128 (OStream.overScratch(0) threw). lang-cpp and
lang-java pin corelib main, so this needs both merged to go green.

Tests. tests/conformance/lib/check_empty_payload.py, wired into all eleven
suites (inside cpp's variant loop, rust's label loop and python's engine loop):
`{}` encodes to exactly zero bytes and the encode SUCCEEDS, zero bytes decode as
a complete message and re-encode to zero bytes -- the path any relay takes --
and a `{ s: struct {}, n: u32 }` message omits the field-less struct as any
default is, so `{"n": 7}` is 08 07 whether the struct is sent or not. Verified by
hand against all twelve target legs: c, cpp (both corelibs), csharp, dart, go,
java, kotlin, python, rust, typescript, zig.

The COMPILE half is tests/matrix/corpus/defs/empty_payload.yaml, which every
suite's corpus leg builds and round-trips -- and which found one more hole, in
the test tooling rather than a backend: check_corpus_roundtrip.py read
`named[key]["fields"]` (--dump-ir omits an empty list) and demanded that a
value-filled message encode to non-zero bytes, which the one message that has no
values cannot. Both fixed, and the carve-out says why.

Five backend unit tests pin the emitted shapes, each with the with-a-field case
beside it so the rule stays "unused", not "always".

Closes #738
lang-typescript on #740 was the one job the hand-verified legs missed: the suite
compiles every corpus definition with --noUnusedLocals --noUnusedParameters
(tsc_strict), and four things go unread in a type that declares no fields --
`serialize`'s stream, `fromJSON`'s object, the visitor's current-scope register
`_c`, and the location constants that exist for it.

The first two take the `_` spelling tsc exempts. The register joins the index and
row registers that are already emitted only where a hook READS them
(dropUnreadRegister), and the constants follow the register: the hook rendering
moved above them so the decision can be made from the rendered arms, which is
how every other "is this read" question in this file is answered. Emitted text is
unchanged for every type that has a field.

Verified with the suite's own flags: all 26 corpus definitions compile clean, and
the field-less one round-trips through check_empty_payload.py and
check_corpus_roundtrip.py as before. My earlier local check ran --strict without
the two --noUnused* flags, which is why CI found this and I did not.
lang-zig gate 10 regenerates every schema and checks it with `zig fmt
--check`: the blank line that separates a struct's member list from the
declarations below it opens the container when there is no member list, and the
formatter removes it (generator#738).

Emitted only where there is a list to separate. Verified with zig 0.16.0 --
`zig fmt --check` clean for every corpus definition and for example.yaml -- and
the zig conformance leg for the field-less message passed on CI already, so this
is the formatter gate alone.
@Andste82
Andste82 merged commit 1ab3628 into main Oct 12, 2026
16 checks passed
Andste82 added a commit that referenced this pull request Oct 12, 2026
lang-typescript on #740 was the one job the hand-verified legs missed: the suite
compiles every corpus definition with --noUnusedLocals --noUnusedParameters
(tsc_strict), and four things go unread in a type that declares no fields --
`serialize`'s stream, `fromJSON`'s object, the visitor's current-scope register
`_c`, and the location constants that exist for it.

The first two take the `_` spelling tsc exempts. The register joins the index and
row registers that are already emitted only where a hook READS them
(dropUnreadRegister), and the constants follow the register: the hook rendering
moved above them so the decision can be made from the rendered arms, which is
how every other "is this read" question in this file is answered. Emitted text is
unchanged for every type that has a field.

Verified with the suite's own flags: all 26 corpus definitions compile clean, and
the field-less one round-trips through check_empty_payload.py and
check_corpus_roundtrip.py as before. My earlier local check ran --strict without
the two --noUnused* flags, which is why CI found this and I did not.
@Andste82
Andste82 deleted the fix/empty-payload-738 branch October 12, 2026 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant