Skip to content

Make Rust JSG Nullable<T> follow dont_substitute_null_on_type_error - #8646

Merged
littledivy merged 4 commits into
mainfrom
littledivy/2026-11-19-rjsg-nullable-flag
Nov 19, 2026
Merged

littledivy merged 4 commits into
mainfrom
littledivy/2026-11-19-rjsg-nullable-flag

Conversation

@littledivy

Copy link
Copy Markdown
Member

For a Worker without dont_substitute_null_on_type_error (dated before 2022-06-01), C++ JSG converts a value that T does not take to null for a kj::Maybe<T> (MaybeWrapper::tryUnwrap in value.h). Only with the flag is it a TypeError. Rust's Nullable<T> always threw. Nothing in Rust uses it yet, but a C++ API with a kj::Maybe parameter ported to Rust would start throwing for those Workers.

In this patch Nullable<T> reads the flag when T does not take a value. Without the flag the result is Null. With it, the conversion throws T's error as before. NullOrUndefined<T> goes through Nullable<T> for anything but undefined, as C++ jsg::Optional<kj::Maybe<T>> does. Only the top-level type counts, so an element of the wrong type in a Nullable<Vec<T>> is still an error. The flag is read only on a mismatch.

The new tests follow "maybes - don't substitute null" in value-test.c++, for Nullable of a non-coercible string, of a union, and in a sequence, with and without the flag. The tests that expect the TypeError now set the flag.

Refs #8057

For a Worker without `dont_substitute_null_on_type_error` (dated before 2022-06-01), C++ JSG converts a value that `T` does not take to `null` for a `kj::Maybe<T>` (value.h, `MaybeWrapper::tryUnwrap`), and only with the flag is it a TypeError. Rust's `Nullable<T>` always threw. Nothing in Rust uses it yet, but a C++ API with a `kj::Maybe` parameter ported to Rust would start throwing for those Workers.

In this patch `Nullable<T>` reads the flag when `T` does not take a value: without it the result is `Null`, and with it the conversion throws `T`'s error as before. `NullOrUndefined<T>` goes through `Nullable<T>` for anything but `undefined`, as C++ `jsg::Optional<kj::Maybe<T>>` does. Only the top-level type counts, so an element of the wrong type in a `Nullable<Vec<T>>` is still an error. The flag is read only on a mismatch.

The new tests follow "maybes - don't substitute null" in value-test.c++, for `Nullable` of a non-coercible string, of a union, and in a sequence, with and without the flag. The tests that expect the TypeError now set the flag.

Refs #8057
@littledivy
littledivy requested review from a team as code owners November 19, 2026 04:22

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 277e10a6.

This review, of d91e6709

✅ Approved — No issues found. The Rust conversion now matches C++ kj::Maybe handling under dont_substitute_null_on_type_error, and tests cover the case with and without the flag.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found. The feature flag is read only when T does not take the value.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: d91e6709

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 8bba5b78.

This review, of 277e10a6

✅ Approved — No issues found. The Rust conversion now matches C++ kj::Maybe handling under dont_substitute_null_on_type_error, and tests cover the case with and without the flag.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found. The feature flag is read only when T does not take the value.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 277e10a6 — same changes as d91e6709, so this is that commit's review (no new review was run; line numbers are as of d91e6709)

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 45e5c645.

This review, of 8bba5b78

✅ Approved — No issues found. The Rust conversion now matches C++ kj::Maybe handling under dont_substitute_null_on_type_error, and tests cover the case with and without the flag.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found. The feature flag is read only when T does not take the value.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 8bba5b78 — same changes as d91e6709, so this is that commit's review (no new review was run; line numbers are as of d91e6709)

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found. The Rust Nullable<T> conversion now follows the C++ kj::Maybe behaviour under dont_substitute_null_on_type_error, and tests cover it with and without the flag.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns. The change only affects how values convert, and it follows the existing C++ compatibility behaviour.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The changes to Nullable<T> and NullOrUndefined<T> in src/rust/jsg/wrappable.rs match the C++ kj::Maybe behaviour under dont_substitute_null_on_type_error. Without the flag a value of the wrong type becomes Null. With it, the conversion throws T's own error. Tests cover the top-level mismatch, a value inside a union, an element of a sequence, and an element mismatch nested inside a Nullable<Vec<T>>, which stays an error.


⚡ Performance: ✅ LGTM

The flag is read only when T does not take the value, so a successful conversion costs nothing extra.


📝 Documentation: ✅ LGTM

The README now describes how a type mismatch converts with and without the flag, and the section on union conversion links to it.


Reviewed commit: 45e5c645

@littledivy
littledivy merged commit 9471353 into main Nov 19, 2026
36 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-19-rjsg-nullable-flag branch November 19, 2026 11:59
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.

2 participants