Skip to content

Commit 3752006

Browse files
authored
Allow tag_invoke to throw without std::terminate (#2858) (#2872)
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.
1 parent a683f0a commit 3752006

13 files changed

Lines changed: 640 additions & 91 deletions

‎doc/basics.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,12 @@ The C++26 approach is even simpler.
982982

983983
### 1. Specialize `simdjson::ondemand::value::get` to get custom types (pre-C++20)
984984

985+
This approach is meant for compilers without C++20 support. It is not compatible with
986+
static reflection (C++26): when reflection is enabled, your structs are deserialized
987+
automatically and `get<T>()` is `noexcept(false)` for them, so an explicit `noexcept`
988+
specialization no longer compiles. Prefer [`tag_invoke`](#2-use-tag_invoke-for-custom-types-c20)
989+
whenever you can.
990+
985991
Suppose you have your own types, such as a `Car` struct:
986992

987993
```cpp
@@ -1334,6 +1340,25 @@ Let us explain each argument of `tag_invoke` function.
13341340
- `var`: It receives automatically a `simdjson` value type (document, value, document_reference).
13351341
- The third parameter is an instance of the type that you want to support.
13361342
1343+
Your `tag_invoke` function reports errors by returning an `error_code`, but it is also allowed
1344+
to throw an exception. The `get<T>()` methods (including those of `simdjson_result<T>`)
1345+
are `noexcept` only when the `tag_invoke` function for `T` is itself `noexcept`, so an
1346+
exception thrown from your `tag_invoke` propagates to your `try`/`catch` block. Whether you
1347+
return an error code or throw, the object being deserialized may be left partially
1348+
populated: when deserializing a `std::vector<T>`, for example, the elements that were parsed
1349+
before the failure are kept and no extra element is added. A `std::unique_ptr<T>` or
1350+
`std::shared_ptr<T>` is left unchanged (so it stays null if it was null).
1351+
If you never throw, mark your `tag_invoke` function `noexcept`: the `get<T>()` methods then
1352+
remain `noexcept` for your type.
1353+
1354+
Exceptions also propagate out of structs deserialized through static reflection (C++26):
1355+
if a member's type has a throwing `tag_invoke`, `get<T>()` for the enclosing struct is
1356+
`noexcept(false)` and the exception reaches your `try`/`catch` block. Note that this makes
1357+
`get<T>()` `noexcept(false)` for every reflected struct `T`, so a legacy explicit
1358+
specialization such as `template<> simdjson_result<Car> simdjson::ondemand::value::get() noexcept`
1359+
no longer matches the primary template when static reflection is enabled: remove it (the
1360+
struct is deserialized automatically) or replace it with a `tag_invoke` customization.
1361+
13371362
You can use it like so:
13381363
13391364
```cpp

‎include/simdjson/generic/ondemand/array.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class array {
160160
*/
161161
template <typename T>
162162
simdjson_warn_unused simdjson_inline error_code get(T &out)
163-
noexcept(custom_deserializable<T, array> ? nothrow_custom_deserializable<T, array> : true) {
163+
noexcept(nothrow_gettable<T, array>) {
164164
static_assert(custom_deserializable<T, array>);
165165
return deserialize(*this, out);
166166
}
@@ -172,7 +172,7 @@ class array {
172172
*/
173173
template <typename T>
174174
simdjson_inline simdjson_result<T> get()
175-
noexcept(custom_deserializable<T, value> ? nothrow_custom_deserializable<T, value> : true)
175+
noexcept(nothrow_gettable<T, array>)
176176
{
177177
static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
178178
T out{};
@@ -269,15 +269,15 @@ struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> : public SIMDJS
269269
// TODO: move this code into object-inl.h
270270

271271
template<typename T>
272-
simdjson_inline simdjson_result<T> get() noexcept {
272+
simdjson_inline simdjson_result<T> get() noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
273273
if (error()) { return error(); }
274274
if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
275275
return first;
276276
}
277277
return first.get<T>();
278278
}
279279
template<typename T>
280-
simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept {
280+
simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
281281
if (error()) { return error(); }
282282
if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
283283
out = first;

‎include/simdjson/generic/ondemand/deserialize.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ concept nothrow_custom_deserializable = nothrow_tag_invocable<deserialize_tag, V
3939
template <typename T, typename ValT = SIMDJSON_IMPLEMENTATION::ondemand::value>
4040
concept nothrow_deserializable = nothrow_custom_deserializable<T, ValT> || is_builtin_deserializable_v<T>;
4141

42+
// True iff get<T>() on ValT cannot throw: no custom tag_invoke, or that tag_invoke is noexcept.
43+
template <typename T, typename ValT = SIMDJSON_IMPLEMENTATION::ondemand::value>
44+
concept nothrow_gettable = !custom_deserializable<T, ValT> || nothrow_custom_deserializable<T, ValT>;
45+
4246
/// Deserialize Tag
4347
inline constexpr struct deserialize_tag {
4448
using array_type = SIMDJSON_IMPLEMENTATION::ondemand::array;

‎include/simdjson/generic/ondemand/document-inl.h‎

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -592,22 +592,46 @@ simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::o
592592
}
593593

594594
template<typename T>
595-
simdjson_inline simdjson_result<T> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document>::get() & noexcept {
595+
simdjson_inline simdjson_result<T> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document>::get() &
596+
#if SIMDJSON_SUPPORTS_CONCEPTS
597+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document>)
598+
#else
599+
noexcept
600+
#endif
601+
{
596602
if (error()) { return error(); }
597603
return first.get<T>();
598604
}
599605
template<typename T>
600-
simdjson_deprecated simdjson_inline simdjson_result<T> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document>::get() && noexcept {
606+
simdjson_deprecated simdjson_inline simdjson_result<T> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document>::get() &&
607+
#if SIMDJSON_SUPPORTS_CONCEPTS
608+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document>)
609+
#else
610+
noexcept
611+
#endif
612+
{
601613
if (error()) { return error(); }
602614
return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::document>(first).get<T>();
603615
}
604616
template<typename T>
605-
simdjson_warn_unused simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document>::get(T &out) & noexcept {
617+
simdjson_warn_unused simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document>::get(T &out) &
618+
#if SIMDJSON_SUPPORTS_CONCEPTS
619+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document>)
620+
#else
621+
noexcept
622+
#endif
623+
{
606624
if (error()) { return error(); }
607625
return first.get<T>(out);
608626
}
609627
template<typename T>
610-
simdjson_warn_unused simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document>::get(T &out) && noexcept {
628+
simdjson_warn_unused simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document>::get(T &out) &&
629+
#if SIMDJSON_SUPPORTS_CONCEPTS
630+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document>)
631+
#else
632+
noexcept
633+
#endif
634+
{
611635
if (error()) { return error(); }
612636
return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::document>(first).get<T>(out);
613637
}
@@ -1011,22 +1035,46 @@ simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::o
10111035
return first.is_null();
10121036
}
10131037
template<typename T>
1014-
simdjson_inline simdjson_result<T> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference>::get() & noexcept {
1038+
simdjson_inline simdjson_result<T> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference>::get() &
1039+
#if SIMDJSON_SUPPORTS_CONCEPTS
1040+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document_reference>)
1041+
#else
1042+
noexcept
1043+
#endif
1044+
{
10151045
if (error()) { return error(); }
10161046
return first.get<T>();
10171047
}
10181048
template<typename T>
1019-
simdjson_inline simdjson_result<T> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference>::get() && noexcept {
1049+
simdjson_inline simdjson_result<T> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference>::get() &&
1050+
#if SIMDJSON_SUPPORTS_CONCEPTS
1051+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document_reference>)
1052+
#else
1053+
noexcept
1054+
#endif
1055+
{
10201056
if (error()) { return error(); }
10211057
return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::document_reference>(first).get<T>();
10221058
}
10231059
template <class T>
1024-
simdjson_warn_unused simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference>::get(T &out) & noexcept {
1060+
simdjson_warn_unused simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference>::get(T &out) &
1061+
#if SIMDJSON_SUPPORTS_CONCEPTS
1062+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document_reference>)
1063+
#else
1064+
noexcept
1065+
#endif
1066+
{
10251067
if (error()) { return error(); }
10261068
return first.get<T>(out);
10271069
}
10281070
template <class T>
1029-
simdjson_warn_unused simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference>::get(T &out) && noexcept {
1071+
simdjson_warn_unused simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference>::get(T &out) &&
1072+
#if SIMDJSON_SUPPORTS_CONCEPTS
1073+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document_reference>)
1074+
#else
1075+
noexcept
1076+
#endif
1077+
{
10301078
if (error()) { return error(); }
10311079
return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::document_reference>(first).get<T>(out);
10321080
}

‎include/simdjson/generic/ondemand/document.h‎

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class document {
228228
template <typename T>
229229
simdjson_inline simdjson_result<T> get() &
230230
#if SIMDJSON_SUPPORTS_CONCEPTS
231-
noexcept(custom_deserializable<T, document> ? nothrow_custom_deserializable<T, document> : true)
231+
noexcept(nothrow_gettable<T, document>)
232232
#else
233233
noexcept
234234
#endif
@@ -251,7 +251,7 @@ class document {
251251
template<typename T>
252252
simdjson_inline simdjson_result<T> get() &&
253253
#if SIMDJSON_SUPPORTS_CONCEPTS
254-
noexcept(custom_deserializable<T, document> ? nothrow_custom_deserializable<T, document> : true)
254+
noexcept(nothrow_gettable<T, document>)
255255
#else
256256
noexcept
257257
#endif
@@ -274,7 +274,7 @@ class document {
274274
template<typename T>
275275
simdjson_warn_unused simdjson_inline error_code get(T &out) &
276276
#if SIMDJSON_SUPPORTS_CONCEPTS
277-
noexcept(custom_deserializable<T, document> ? nothrow_custom_deserializable<T, document> : true)
277+
noexcept(nothrow_gettable<T, document>)
278278
#else
279279
noexcept
280280
#endif
@@ -306,7 +306,12 @@ class document {
306306
}
307307

308308
/** @overload template<typename T> error_code get(T &out) & noexcept */
309-
template<typename T> simdjson_deprecated simdjson_inline error_code get(T &out) && noexcept;
309+
template<typename T> simdjson_deprecated simdjson_inline error_code get(T &out) &&
310+
#if SIMDJSON_SUPPORTS_CONCEPTS
311+
noexcept(nothrow_gettable<T, document>);
312+
#else
313+
noexcept;
314+
#endif
310315

311316
#if SIMDJSON_EXCEPTIONS
312317
/**
@@ -894,7 +899,7 @@ class document_reference {
894899
template <typename T>
895900
simdjson_inline simdjson_result<T> get() &
896901
#if SIMDJSON_SUPPORTS_CONCEPTS
897-
noexcept(custom_deserializable<T, document> ? nothrow_custom_deserializable<T, document> : true)
902+
noexcept(nothrow_gettable<T, document_reference>)
898903
#else
899904
noexcept
900905
#endif
@@ -907,7 +912,8 @@ class document_reference {
907912
template<typename T>
908913
simdjson_inline simdjson_result<T> get() &&
909914
#if SIMDJSON_SUPPORTS_CONCEPTS
910-
noexcept(custom_deserializable<T, document> ? nothrow_custom_deserializable<T, document> : true)
915+
// Forwards to document::get<T>(), so the document customization decides.
916+
noexcept(nothrow_gettable<T, document>)
911917
#else
912918
noexcept
913919
#endif
@@ -930,7 +936,7 @@ class document_reference {
930936
template<typename T>
931937
simdjson_warn_unused simdjson_inline error_code get(T &out) &
932938
#if SIMDJSON_SUPPORTS_CONCEPTS
933-
noexcept(custom_deserializable<T, document> ? nothrow_custom_deserializable<T, document_reference> : true)
939+
noexcept(nothrow_gettable<T, document_reference>)
934940
#else
935941
noexcept
936942
#endif
@@ -962,7 +968,12 @@ class document_reference {
962968
}
963969

964970
/** @overload template<typename T> error_code get(T &out) & noexcept */
965-
template<typename T> simdjson_inline error_code get(T &out) && noexcept;
971+
template<typename T> simdjson_inline error_code get(T &out) &&
972+
#if SIMDJSON_SUPPORTS_CONCEPTS
973+
noexcept(nothrow_gettable<T, document_reference>);
974+
#else
975+
noexcept;
976+
#endif
966977
simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
967978
#if SIMDJSON_STATIC_REFLECTION
968979
template<constevalutil::fixed_string... FieldNames, typename T>
@@ -1055,11 +1066,31 @@ struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document> : public SIM
10551066
simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> get_value() noexcept;
10561067
simdjson_inline simdjson_result<bool> is_null() noexcept;
10571068

1058-
template<typename T> simdjson_inline simdjson_result<T> get() & noexcept;
1059-
template<typename T> simdjson_deprecated simdjson_inline simdjson_result<T> get() && noexcept;
1069+
template<typename T> simdjson_inline simdjson_result<T> get() &
1070+
#if SIMDJSON_SUPPORTS_CONCEPTS
1071+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document>);
1072+
#else
1073+
noexcept;
1074+
#endif
1075+
template<typename T> simdjson_deprecated simdjson_inline simdjson_result<T> get() &&
1076+
#if SIMDJSON_SUPPORTS_CONCEPTS
1077+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document>);
1078+
#else
1079+
noexcept;
1080+
#endif
10601081

1061-
template<typename T> simdjson_inline error_code get(T &out) & noexcept;
1062-
template<typename T> simdjson_inline error_code get(T &out) && noexcept;
1082+
template<typename T> simdjson_inline error_code get(T &out) &
1083+
#if SIMDJSON_SUPPORTS_CONCEPTS
1084+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document>);
1085+
#else
1086+
noexcept;
1087+
#endif
1088+
template<typename T> simdjson_inline error_code get(T &out) &&
1089+
#if SIMDJSON_SUPPORTS_CONCEPTS
1090+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document>);
1091+
#else
1092+
noexcept;
1093+
#endif
10631094
#if SIMDJSON_EXCEPTIONS
10641095

10651096
using SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::document>::operator*;
@@ -1152,11 +1183,31 @@ struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference> :
11521183
simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> get_value() noexcept;
11531184
simdjson_inline simdjson_result<bool> is_null() noexcept;
11541185

1155-
template<typename T> simdjson_inline simdjson_result<T> get() & noexcept;
1156-
template<typename T> simdjson_inline simdjson_result<T> get() && noexcept;
1186+
template<typename T> simdjson_inline simdjson_result<T> get() &
1187+
#if SIMDJSON_SUPPORTS_CONCEPTS
1188+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document_reference>);
1189+
#else
1190+
noexcept;
1191+
#endif
1192+
template<typename T> simdjson_inline simdjson_result<T> get() &&
1193+
#if SIMDJSON_SUPPORTS_CONCEPTS
1194+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document_reference>);
1195+
#else
1196+
noexcept;
1197+
#endif
11571198

1158-
template<typename T> simdjson_inline error_code get(T &out) & noexcept;
1159-
template<typename T> simdjson_inline error_code get(T &out) && noexcept;
1199+
template<typename T> simdjson_inline error_code get(T &out) &
1200+
#if SIMDJSON_SUPPORTS_CONCEPTS
1201+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document_reference>);
1202+
#else
1203+
noexcept;
1204+
#endif
1205+
template<typename T> simdjson_inline error_code get(T &out) &&
1206+
#if SIMDJSON_SUPPORTS_CONCEPTS
1207+
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::document_reference>);
1208+
#else
1209+
noexcept;
1210+
#endif
11601211
#if SIMDJSON_EXCEPTIONS
11611212
template <class T>
11621213
explicit simdjson_inline operator T() noexcept(false);

‎include/simdjson/generic/ondemand/object.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ class object {
475475
*/
476476
template <typename T>
477477
simdjson_warn_unused simdjson_inline error_code get(T &out)
478-
noexcept(custom_deserializable<T, object> ? nothrow_custom_deserializable<T, object> : true) {
478+
noexcept(nothrow_gettable<T, object>) {
479479
static_assert(custom_deserializable<T, object>);
480480
return deserialize(*this, out);
481481
}
@@ -487,7 +487,7 @@ class object {
487487
*/
488488
template <typename T>
489489
simdjson_inline simdjson_result<T> get()
490-
noexcept(custom_deserializable<T, value> ? nothrow_custom_deserializable<T, value> : true)
490+
noexcept(nothrow_gettable<T, object>)
491491
{
492492
static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
493493
T out{};
@@ -593,15 +593,15 @@ struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> : public SIMDJ
593593
// TODO: move this code into object-inl.h
594594

595595
template<typename T>
596-
simdjson_inline simdjson_result<T> get() noexcept {
596+
simdjson_inline simdjson_result<T> get() noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
597597
if (error()) { return error(); }
598598
if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
599599
return first;
600600
}
601601
return first.get<T>();
602602
}
603603
template<typename T>
604-
simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept {
604+
simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
605605
if (error()) { return error(); }
606606
if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
607607
out = first;

0 commit comments

Comments
 (0)