Skip to content

Allow tag_invoke to throw without std::terminate (#2858) - #2872

Merged
lemire merged 1 commit into
masterfrom
fix-2858-tag-invoke-exceptions
Sep 20, 2026
Merged

lemire merged 1 commit into
masterfrom
fix-2858-tag-invoke-exceptions

Conversation

@lemire

@lemire lemire commented Sep 20, 2026

Copy link
Copy Markdown
Member

A user tag_invoke customization is allowed to throw, but the exception could never reach the caller: every simdjson_result<...>::get() wrapper was unconditionally noexcept, so a throw hit std::terminate.

  • New concept nothrow_gettable<T, ValT>: true when there is no custom tag_invoke for T or that tag_invoke is noexcept. get() and get(T&) on value, array, object, document, document_reference and all their simdjson_result<...> wrappers are noexcept(nothrow_gettable). Built-in types and noexcept customizations keep every layer noexcept. document_reference::get was checking the document customization; fixed.
  • Allocating customizations (containers, maps, unique_ptr, shared_ptr, reflected structs) are noexcept(false) so bad_alloc and user exceptions propagate, and so that the exception specification of get() never recurses into T for self-referential types.
  • The appendable-container loop always deserializes into a temporary and moves it in: master's "in place" path already went through a simdjson_result temporary, a plain temporary is ~3% faster on the citm static-reflection benchmark, and it needs no cleanup on error or exception (the container keeps exactly the successfully parsed elements).
  • unique_ptr/shared_ptr customizations own the allocation before calling get(), so a throw cannot leak and the caller's pointer is unchanged.
  • Static reflection: exceptions propagate out of reflected structs too. The reflected-struct tag_invoke also matched simdjson's own types (array, object, number, document, ...), conflicting with their explicit noexcept get() specializations; they are excluded now. With reflection enabled get() is noexcept(false) for every struct T, so a legacy explicit specialization template<> simdjson_result value::get() noexcept { ... } no longer matches the primary template; such specializations are redundant under reflection and the two tests carrying them guard them with !SIMDJSON_STATIC_REFLECTION. Documented in basics.md.
  • New tests/ondemand/ondemand_tag_invoke_exception_tests.cpp.

Fixes #2858 reported by @maxbachmann

A user tag_invoke customization is allowed to throw, but the exception
could never reach the caller: every simdjson_result<...>::get<T>()
wrapper was unconditionally noexcept, so a throw hit std::terminate.

- New concept nothrow_gettable<T, ValT>: true when there is no custom
  tag_invoke for T or that tag_invoke is noexcept. get<T>() and
  get<T>(T&) on value, array, object, document, document_reference and
  all their simdjson_result<...> wrappers are noexcept(nothrow_gettable).
  Built-in types and noexcept customizations keep every layer noexcept.
  document_reference::get was checking the document customization; fixed.
- Allocating customizations (containers, maps, unique_ptr, shared_ptr,
  reflected structs) are noexcept(false) so bad_alloc and user
  exceptions propagate, and so that the exception specification of
  get<T>() never recurses into T for self-referential types.
- The appendable-container loop always deserializes into a temporary and
  moves it in: master's "in place" path already went through a
  simdjson_result<T> temporary, a plain temporary is ~3% faster on the
  citm static-reflection benchmark, and it needs no cleanup on error or
  exception (the container keeps exactly the successfully parsed
  elements).
- unique_ptr/shared_ptr customizations own the allocation before calling
  get(), so a throw cannot leak and the caller's pointer is unchanged.
- Static reflection: exceptions propagate out of reflected structs too.
  The reflected-struct tag_invoke also matched simdjson's own types
  (array, object, number, document, ...), conflicting with their
  explicit noexcept get<T>() specializations; they are excluded now.
  With reflection enabled get<T>() is noexcept(false) for every struct
  T, so a legacy explicit specialization
      template<> simdjson_result<Car> value::get() noexcept { ... }
  no longer matches the primary template; such specializations are
  redundant under reflection and the two tests carrying them guard them
  with !SIMDJSON_STATIC_REFLECTION. Documented in basics.md.
- New tests/ondemand/ondemand_tag_invoke_exception_tests.cpp.
@lemire lemire mentioned this pull request Sep 20, 2026
1 of 6 tasks
@lemire
lemire merged commit 3752006 into master Sep 20, 2026
215 checks passed
@lemire
lemire deleted the fix-2858-tag-invoke-exceptions branch September 20, 2026 16:45
lemire added a commit that referenced this pull request Sep 22, 2026
…-Wmaybe-uninitialized

GCC 11.4 (Ubuntu 22.04 CI, Debug/-Og developer build with -Werror) reports a
false-positive 'v may be used uninitialized' on the json_sequence subtest
since #2870 landed on top of #2872. The variable is only read after
get(v) returned SUCCESS, so this is purely a diagnostic issue; the warning
flips on unrelated call-graph changes in headers pulled in via simdjson.h.
Initialize all the int64_t v locals in the file so the other identical
sites cannot start failing the same way later.
lemire added a commit that referenced this pull request Sep 22, 2026
…-Wmaybe-uninitialized (#2875)

GCC 11.4 (Ubuntu 22.04 CI, Debug/-Og developer build with -Werror) reports a
false-positive 'v may be used uninitialized' on the json_sequence subtest
since #2870 landed on top of #2872. The variable is only read after
get(v) returned SUCCESS, so this is purely a diagnostic issue; the warning
flips on unrelated call-graph changes in headers pulled in via simdjson.h.
Initialize all the int64_t v locals in the file so the other identical
sites cannot start failing the same way later.
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.

Exception handling inside tag_invoke customizations

1 participant