Repository navigation
baml_cranelift: an optional union inside a union gets one flat type key - #5867
antoniosarosi wants to merge 5 commits into
Conversation
…86_64 AVX 128-bit atomics
…d baml pack --native
…ped JSON errors, and @@rust_type slots
- Fix ARM64 (FEAT_LRCPC) Dekker lost-wakeup races in bex2_scheduler by
upgrading wake_task, io_notified, CancelToken, Semaphore admission,
AtomicLockTable, Future settlement, and AwaitAnyGroup paired atomic
loads/stores/CASes to Ordering::SeqCst.
- Support uint8array (BytesObject) indexing in array_get, array_set
(with low-8-bit masking and COW protection on shared Arc), and array_len.
- Decode uint8array.to_string() lossily as UTF-8 via baml_rt_bytes_to_string_lossy
instead of returning debug '<uint8array len=N>'.
- Register and throw typed baml.json.{ParseError, DecodeError, SerializationError}
instances with jq-style paths and reason tags from baml_rt_json_* entrypoints.
- Emit synthetic handle slot 0 for @@rust_type classes with zero BAML fields in
rodata.rs and align Semaphore._admit/_admitted/_wait_permit with Permit handles.
- Add unit, Dekker stress, and Cranelift E2E regression tests (29/29 passing).
…t type key The type checker treats (int | string)? | bool, (int | string | bool)? and int | string | bool | null as one type, but normalize_union_keys pushed the inner type of an Optional member as a single member. When that inner type was a union, the key stayed nested: Optional(Union[Union[int, string], bool]), and (int | bool)? | bool listed bool twice. Equal types then got different static type objects, and anything reading union members (the bex2_boundary library) had to flatten them itself. normalize_union_keys now splices every Optional and Union in a member at any depth, depth first in declaration order, hoists null, and drops duplicates keeping the first occurrence. Union members are never Null, Optional or Union. The runtime tests and decodes union members in order and recursively, so the flat key matches the same values in the same order.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
@coderabbitai review |
Action performed
|
|
Found 18 test failures on Blacksmith runners: Failures
...and 8 more test failures. View all on Blacksmith
|
…rong
The end-to-end test passed on the base with the same output, so it did not
test the change. It now checks three things the nested keys did differently,
and all three lines fail on the base (checked by packing the same program
with a base build):
- decoding class Node { next: (Node | int)? | string } 40 levels deep
succeeds; the base throws a DecodeError, because each node used 4 of the
decoder's 128 depth levels through the nested key and uses 3 now;
- decoding into (A | B)? | A runs A's FromJson hook once when nothing
matches; the base tried A twice and ran it twice;
- the DecodeError for [1] into bool | (int | string)? wraps the member
error once; the base wrapped it twice, through the nested union.
The unit tests now use the member shapes the three callers really pass
(already normalised keys: Optional(Union), Union of plain members), including
a three-level type built level by level through the normaliser. Five of them
fail on the old normaliser; three guard shapes it already handled. Shapes the
callers never produce (a union holding null or an optional, Optional(Null),
a one-member union) moved to defensive_* tests, which the module comment
says.
|
[🤖 Claude on behalf of Antonio] An adversarial review built the base and this branch, packed 9 programs with It found that the change is observable in JSON decoding in three ways, all listed in the description now, and that the first end-to-end test passed on the base too. #5853 (the shared library) is retargeted onto this PR. |
9b60d93 to
fb530c3
Compare
![Fix with [code]smith](https://pr-comments-assets.blacksmith.sh/codesmith/fix-with-codesmith-light-v2-3x.png)
[🤖 Claude on behalf of Antonio]
The native backend gives an optional union inside a union one flat type key, as the type checker already treats it. On top of #5850.
Why
The type checker treats
(int | string)? | bool,(int | string | bool)?andint | string | bool | nullas one type. The backend builds its runtime type keys with its own normaliser, and that one left a case nested: for a member that is optional it hoisted the null and kept the inner type as one member, also when the inner type was a union. So the same type had different type objects depending on how it was written, and a member could appear twice.The shared library in #5853 reads those type objects, so every SDK back end met the nested shape and had to rebuild it.
What changes
baml_craneliftnormalize_union_keys, the only place that builds union and optional type keys, splices optional and union members at any depth, hoists null and drops duplicates, keeping the firstBefore and after
(int | string)? | boolOptional(Union[Union[int, string], bool])Optional(Union[int, string, bool])(int | bool)? | boolOptional(Union[Union[int, bool], bool]),booltwiceOptional(Union[int, bool])Member order is unchanged (depth first, as declared), so the runtime's first-match order for type tests and JSON decoding stays the same.
Observable changes
Type tests,
match, the JSON decoder and encoder and CLI argument parsing walk union members recursively and in order, and no value carries a member index, so no program matches another member than before. A review that ran the same programs on both sides found three differences, all in JSON decoding:class Node { next: (Node | int)? | string }used 4 of the decoder's 128 depth levels per node and now uses 3.FromJsonhook on it runs once where it ran twice ((A | B)? | A).DecodeErrortext: a nested union is no longer wrapped twice, and where a duplicate was dropped the message names another last member.Tests
cranelift_e2e: a program that takes and returns both types and matches on every member and null, plus the three cases above (a 40-level decode, a hook that runs once, an error wrapped once), each of which fails on a base build.baml_cranelift,cranelift_e2e,pack_native_e2eand the bex2 crates pass on arm64 and on x86-64 (Linux machine, Ryzen 9 5950X), except three failures that are identical on the base.