daw_json_link icon indicating copy to clipboard operation
daw_json_link copied to clipboard

daw_to_json.h: unqualified json_details::serialization::set_bits is non-portable (fails on GCC <=14 and Clang)

Open marcelomatus opened this issue 5 months ago • 1 comments

In daw_to_json.h (lines 37 and 111, v3.31.0 / current master 3.31.1), inside namespace daw::json::DAW_JSON_VER::json_details, the call

json_details::serialization::set_bits( ... )

is non-portable: GCC 14, GCC 15, and Clang all resolve the leading json_details to the nested json_details::json_details (which has no serialization) rather than to the enclosing json_details:

error: 'serialization' is not a member of 'daw::json::v3_31_0d::json_details::json_details'

Minimal, self-contained reproducer

Normal usage — a json_data_contract plus a to_json_schema<T> call (the schema generator is what instantiates apply_policy_flags down the non-empty-PolicyFlags branch that contains set_bits):

// repro.cpp  —  compile with daw_json_link on the include path:
//   g++-14   -std=c++23 -I<daw_json_link>/include ... repro.cpp
//   g++-15   -std=c++23 -I<daw_json_link>/include ... repro.cpp
//   clang++  -std=c++23 -I<daw_json_link>/include ... repro.cpp
#include <daw/json/daw_json_schema.h>
#include <daw/json/daw_json_link.h>

#include <string>
#include <tuple>

struct Point
{
  int x;
  int y;
};

namespace daw::json
{
template<>
struct json_data_contract<Point>
{
  static constexpr char const x[] = "x";
  static constexpr char const y[] = "y";
  using type = json_member_list<json_number<x, int>, json_number<y, int>>;

  static constexpr auto to_json_data( Point const &p )
  {
    return std::forward_as_tuple( p.x, p.y );
  }
};
}  // namespace daw::json

int main()
{
  // Instantiates json_details::apply_policy_flags(...) -> set_bits(...)
  std::string const s = daw::json::to_json_schema<Point>( "Point", "demo" );
  return s.empty() ? 1 : 0;
}

Observed output (daw_json_link v3.31.0, unmodified):

GCC 14.3 / GCC 15.2

daw_to_json.h:37:91: error: 'serialization' is not a member of
  'daw::json::v3_31_0d::json_details::json_details'
   37 |   json_details::serialization::set_bits(
daw_to_json.h:39:95: error: template argument 2 is invalid

Clang 23

daw_to_json.h:37:35: error: no member named 'serialization' in namespace
  'daw::json::json_details::json_details'; did you mean simply 'serialization'?
   37 |   json_details::serialization::set_bits(
note: 'serialization' declared here
   25 |   namespace json_details::serialization {

Two notes that correct an earlier draft of this report

  1. It is to_json_schema (and to_json with explicit output flags) that trips, not plain to_json. A bare daw::json::to_json(value) keeps the set_bits call inside a discarded if constexpr( sizeof...(PolicyFlags) == 0 ) branch, so it compiles on all three compilers. The error only appears once a code path forces the non-empty-PolicyFlags branch to be instantiated — which to_json_schema<T> always does.

  2. GCC 15 is not immune. Its default-on -Wtemplate-body eager template-body checker performs the same incorrect lookup and emits the error above. GCC 15 will compile the file only with -Wno-template-body (at actual instantiation GCC 15's lookup is conformant — only the eager parse-time check is wrong). -Wno-template-body does not rescue GCC 14, where it is a genuine, unconditional lookup error.

Suggested fix

Fully-qualify both call sites with a leading :::

::daw::json::json_details::serialization::set_bits( ... )

Verified to compile cleanly on GCC 14.3, GCC 15.2 (no -Wno-template-body needed), and Clang 23. A leading-:: absolute qualification removes the dependence on how json_details resolves from inside itself.

Environment

Compiler Result on unmodified v3.31.0 With ::daw::json::… fix
GCC 15.2 ❌ error (default -Wtemplate-body); compiles with -Wno-template-body ✅ compiles
GCC 14.3 ❌ error (unconditional; -Wno-template-body does not help) ✅ compiles
Clang 23.0 ❌ error (unconditional) ✅ compiles

daw_json_link v3.31.0 (debug namespace v3_31_0d); the offending lines are unchanged on master (3.31.1). Both daw_to_json.h:37 and :111 live in apply_policy_flags() and share the identical pattern.

marcelomatus avatar Jun 12 '26 15:06 marcelomatus

I am not able to replicate this with gcc 14.3 or 15.2 https://jsonlink.godbolt.org/z/51To69YEK https://jsonlink.godbolt.org/z/oYYbvdv1s

beached avatar Jun 20 '26 00:06 beached