Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors are ugly when trying to write to a const buffer #931

Closed
maldag opened this issue Apr 18, 2024 · 8 comments · Fixed by #933
Closed

Errors are ugly when trying to write to a const buffer #931

maldag opened this issue Apr 18, 2024 · 8 comments · Fixed by #933
Labels
enhancement New feature or request

Comments

@maldag
Copy link

maldag commented Apr 18, 2024

Another issue arised:

I'm trying to get the example working:

struct my_struct
{
  int i = 287;
  double d = 3.14;
  std::string hello = "Hello World";
  std::array<uint64_t, 3> arr = { 1, 2, 3 };
  std::map<std::string, int> map{{"one", 1}, {"two", 2}};
};
TEST(glaze_test,test_with_prettier){
    my_struct s{};
    const std::string buffer;
    glz::write<glz::opts{.prettify = true}>(s, buffer);
    ASSERT_STREQ(buffer.c_str(),R"({
        "i": 287,
        "d": 3.14,
        "hello": "Hello World",
        "arr": [
            1,
            2,
            3
        ],
        "map": {
            "one": 1,
            "two": 2
        }
        })"
    );
}

I've deactivated clang-tidy and using clang++ version 19 now.

The errorlog is as follows:

In file included from /project/test/unittest/glazeJson/test_json.cpp:7:
In file included from /project/build_gtest/_deps/glaze-src/include/glaze/glaze.hpp:36:
In file included from /project/build_gtest/_deps/glaze-src/include/glaze/binary/beve_to_json.hpp:7:
/project/build_gtest/_deps/glaze-src/include/glaze/json/write.hpp:1116:16: error: no matching function for call to 'dump'
 1116 |                dump<'{'>(b, ix);
      |                ^~~~~~~~~
/project/build_gtest/_deps/glaze-src/include/glaze/json/write.hpp:1107:16: note: in instantiation of function template specialization 'glz::detail::to_json<my_struct>::op_base<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, glz::context &, const std::basic_string<char> &, unsigned long &>' requested here
 1107 |                op_base<write_unknown_on<Options>()>(std::forward<V>(value), ctx, b, ix);
      |                ^
/project/build_gtest/_deps/glaze-src/include/glaze/json/write.hpp:30:55: note: in instantiation of function template specialization 'glz::detail::to_json<my_struct>::op<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, glz::context &, const std::basic_string<char> &, unsigned long &>' requested here
   30 |             to_json<std::remove_cvref_t<T>>::template op<Opts>(std::forward<T>(value), std::forward<Ctx>(ctx),
      |                                                       ^
/project/build_gtest/_deps/glaze-src/include/glaze/core/write.hpp:30:44: note: in instantiation of function template specialization 'glz::detail::write<10>::op<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, glz::context &, const std::basic_string<char> &, unsigned long &>' requested here
   30 |       detail::write<Opts.format>::template op<Opts>(std::forward<T>(value), ctx, buffer, ix);
      |                                            ^
/project/build_gtest/_deps/glaze-src/include/glaze/core/write.hpp:60:7: note: in instantiation of function template specialization 'glz::write<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, const std::basic_string<char>, glz::context &>' requested here
   60 |       write<Opts>(std::forward<T>(value), buffer, ctx);
      |       ^
/project/test/unittest/glazeJson/test_json.cpp:26:10: note: in instantiation of function template specialization 'glz::write<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, const std::basic_string<char>>' requested here
   26 |     glz::write<glz::opts{.prettify = true}>(s, buffer);
      |          ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:43:27: note: candidate template ignored: constraints not satisfied [with c = '{', b:auto = const std::basic_string<char>, ix:auto = unsigned long]
   43 |    GLZ_ALWAYS_INLINE void dump(vector_like auto& b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:43:32: note: because 'const std::basic_string<char>' does not satisfy 'vector_like'
   43 |    GLZ_ALWAYS_INLINE void dump(vector_like auto& b, auto& ix) noexcept
      |                                ^
/project/build_gtest/_deps/glaze-src/include/glaze/concepts/container_concepts.hpp:138:26: note: because 'const std::basic_string<char>' does not satisfy 'resizeable'
  138 |    concept vector_like = resizeable<T> && accessible<T> && has_data<T>;
      |                          ^
/project/build_gtest/_deps/glaze-src/include/glaze/concepts/container_concepts.hpp:96:59: note: because 'container.resize(0)' would be invalid: no matching member function for call to 'resize'
   96 |    concept resizeable = requires(T container) { container.resize(0); };
      | 
                                                         ^/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:60:27: note: candidate template ignored: could not match 'auto *' against 'std::basic_string<char>'
   60 |    GLZ_ALWAYS_INLINE void dump(auto* b, auto& ix) noexcept

I attached the rest of the log file.
error.log

This error occurs using the write<> Method with all string_literals within dump.hpp

@stephenberry
Copy link
Owner

Strange, I tested with Clang 18, but I'll have to set up an environment to test with Clang 19. The behavior seems strange.

Can you try using the latest release (v2.5.2)? Because the main branch has some #include refactoring associated with dump.hpp.

@maldag
Copy link
Author

maldag commented Apr 18, 2024

I was using the main branch at commit cdd097a

Using the tag 2.5.2 with hash 4749325 also yields errors:

In file included from /project/test/unittest/glazeJson/test_json.cpp:7:
In file included from /project/build_gtest/_deps/glaze-src/include/glaze/glaze.hpp:36:
In file included from /project/build_gtest/_deps/glaze-src/include/glaze/binary/beve_to_json.hpp:7:
/project/build_gtest/_deps/glaze-src/include/glaze/json/write.hpp:1116:16: error: no matching function for call to 'dump'
 1116 |                dump<'{'>(b, ix);
      |                ^~~~~~~~~
/project/build_gtest/_deps/glaze-src/include/glaze/json/write.hpp:1107:16: note: in instantiation of function template specialization 'glz::detail::to_json<my_struct>::op_base<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, glz::context &, const std::basic_string<char> &, unsigned long &>' requested here
 1107 |                op_base<write_unknown_on<Options>()>(std::forward<V>(value), ctx, b, ix);
      |                ^
/project/build_gtest/_deps/glaze-src/include/glaze/json/write.hpp:30:55: note: in instantiation of function template specialization 'glz::detail::to_json<my_struct>::op<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, glz::context &, const std::basic_string<char> &, unsigned long &>' requested here
   30 |             to_json<std::remove_cvref_t<T>>::template op<Opts>(std::forward<T>(value), std::forward<Ctx>(ctx),
      |                                                       ^
/project/build_gtest/_deps/glaze-src/include/glaze/core/write.hpp:42:44: note: in instantiation of function template specialization 'glz::detail::write<10>::op<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, glz::context &, const std::basic_string<char> &, unsigned long &>' requested here
   42 |       detail::write<Opts.format>::template op<Opts>(std::forward<T>(value), ctx, buffer, ix);
      |                                            ^
/project/build_gtest/_deps/glaze-src/include/glaze/core/write.hpp:72:7: note: in instantiation of function template specialization 'glz::write<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, const std::basic_string<char>, glz::context &>' requested here
   72 |       rite<Opts>(std::forward<T>(value), buffer, ctx);
      |       ^
/project/test/unittest/glazeJson/test_json.cpp:28:10: note: in instantiation of function template specialization 'glz::write<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, const std::basic_string<char>>' requested here
   28 |     glz::write<glz::opts{.prettify = true}>(s, buffer);
      |          ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:31:27: note: candidate template ignored: constraints not satisfied [with c = '{', b:auto = const std::basic_string<char>, ix:auto = unsigned long]
   31 |    GLZ_ALWAYS_INLINE void dump(vector_like auto& b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:31:32: note: because 'const std::basic_string<char>' does not satisfy 'vector_like'
   31 |    GLZ_ALWAYS_INLINE void dump(vector_like auto& b, auto& ix) noexcept
      |                                ^
/project/build_gtest/_deps/glaze-src/include/glaze/core/common.hpp:339:29: note: because 'const std::basic_string<char>' does not satisfy 'resizeable'
  339 |       concept vector_like = resizeable<T> && accessible<T> && has_data<T>;
      |                             ^
/project/build_gtest/_deps/glaze-src/include/glaze/concepts/container_concepts.hpp:69:59: note: because 'container.resize(0)' would be invalid: no matching member function for call to 'resize'
   69 |    concept resizable = requires(T container) { container.resize(0); };
      |                                                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:48:27: note: candidate template ignored: could not match 'auto *' against 'std::basic_string<char>'
   48 |    GLZ_ALWAYS_INLINE void dump(auto* b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:110:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'str'
  110 |    GLZ_ALWAYS_INLINE void dump(vector_like auto& b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:124:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'str'
  124 |    GLZ_ALWAYS_INLINE void dump(auto* b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:151:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'str'
  151 |    GLZ_ALWAYS_INLINE void dump(vector_like auto& b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:165:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'str'
  165 |    GLZ_ALWAYS_INLINE void dump(auto* b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:215:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'c'
  215 |    GLZ_ALWAYS_INLINE void dump(B&& b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:233:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'B'
  233 |    GLZ_ALWAYS_INLINE void dump(std::byte c, B&& b) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:264:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'B'
  264 |    GLZ_ALWAYS_INLINE void dump(const std::span<const std::byte> bytes, B&& b) noexcept
      |  

In file included from /project/test/unittest/glazeJson/test_json.cpp:7:
In file included from /project/build_gtest/_deps/glaze-src/include/glaze/glaze.hpp:36:
In file included from /project/build_gtest/_deps/glaze-src/include/glaze/binary/beve_to_json.hpp:7:
/project/build_gtest/_deps/glaze-src/include/glaze/json/write.hpp:1215:22: error: no matching function for call to 'dump'
 1215 |                      dump<':'>(b, ix);
      |                      ^~~~~~~~~
/project/build_gtest/_deps/glaze-src/include/glaze/json/write.hpp:1157:37: note: while substituting into a lambda expression here
 1157 |             for_each<N>([&](auto I) {
      |                                     ^
/project/build_gtest/_deps/glaze-src/include/glaze/json/write.hpp:1107:16: note: in instantiation of function template specialization 'glz::detail::to_json<my_struct>::op_base<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, glz::context &, const std::basic_string<char> &, unsigned long &>' requested here
 1107 |                op_base<write_unknown_on<Options>()>(std::forward<V>(value), ctx, b, ix);
      |                ^
/project/build_gtest/_deps/glaze-src/include/glaze/json/write.hpp:30:55: note: in instantiation of function template specialization 'glz::detail::to_json<my_struct>::op<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, glz::context &, const std::basic_string<char> &, unsigned long &>' requested here
   30 |             to_json<std::remove_cvref_t<T>>::template op<Opts>(std::forward<T>(value), std::forward<Ctx>(ctx),
      |                                                       ^
/project/build_gtest/_deps/glaze-src/include/glaze/core/write.hpp:42:44: note: in instantiation of function template specialization 'glz::detail::write<10>::op<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, glz::context &, const std::basic_string<char> &, unsigned long &>' requested here
   42 |       detail::write<Opts.format>::template op<Opts>(std::forward<T>(value), ctx, buffer, ix);
      |                                            ^
/project/build_gtest/_deps/glaze-src/include/glaze/core/write.hpp:72:7: note: in instantiation of function template specialization 'glz::write<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, const std::basic_string<char>, glz::context &>' requested here
72 |       write<Opts>(std::forward<T>(value), buffer, ctx);
      |       ^
/project/test/unittest/glazeJson/test_json.cpp:28:10: note: in instantiation of function template specialization 'glz::write<opts{10, false, true, true, true, true, 32, 3, true, false, true, false, false, false, 0, false, false, false, false, false, false, true, true, false, false, false, false, true}, my_struct &, const std::basic_string<char>>' requested here
   28 |     glz::write<glz::opts{.prettify = true}>(s, buffer);
      |          ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:31:27: note: candidate template ignored: constraints not satisfied [with c = ':', b:auto = const std::basic_string<char>, ix:auto = unsigned long]
   31 |    GLZ_ALWAYS_INLINE void dump(vector_like auto& b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:31:32: note: because 'const std::basic_string<char>' does not satisfy 'vector_like'
   31 |    GLZ_ALWAYS_INLINE void dump(vector_like auto& b, auto& ix) noexcept
      |                                ^
/project/build_gtest/_deps/glaze-src/include/glaze/core/common.hpp:339:29: note: because 'const std::basic_string<char>' does not satisfy 'resizeable'
  339 |       concept vector_like = resizeable<T> && accessible<T> && has_data<T>;
      |                             ^
/project/build_gtest/_deps/glaze-src/include/glaze/concepts/container_concepts.hpp:69:59: note: because 'container.resize(0)' would be invalid: no matching member function for call to 'resize'
   69 |    concept resizeable = requires(T container) { container.resize(0); };
      |                                                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:48:27: note: candidate template ignored: could not match 'auto *' against 'std::basic_string<char>'
   48 |    GLZ_ALWAYS_INLINE void dump(auto* b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:110:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'str'
  110 |    GLZ_ALWAYS_INLINE void dump(vector_like auto& b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:124:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'str'
  124 |    GLZ_ALWAYS_INLINE void dump(auto* b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:151:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'str'
  151 |    GLZ_ALWAYS_INLINE void dump(vector_like auto& b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:165:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'str'
  165 |    GLZ_ALWAYS_INLINE void dump(auto* b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:215:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'c'
  215 |    GLZ_ALWAYS_INLINE void dump(B&& b, auto& ix) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:233:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'B'
  233 |    GLZ_ALWAYS_INLINE void dump(std::byte c, B&& b) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:264:27: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'B'
  264 |    GLZ_ALWAYS_INLINE void dump(const std::span<const std::byte> bytes, B&& b) noexcept
      |                           ^
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:94:27: note: candidate function template not viable: requires single argument 'b', but 2 arguments were provided
   94 |    GLZ_ALWAYS_INLINE void dump(char*&b) noexcept
      |                           ^    ~~~~~~~~
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:101:27: note: candidate function template not viable: requires single argument 'b', but 2 arguments were provided
  101 |    GLZ_ALWAYS_INLINE void dump(char*& ) noexcept
      |                           ^    ~~~~~~~~
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:202:27: note: candidate function template not viable: requires single argument 'b', but 2 arguments were provided
  202 |    GLZ_ALWAYS_INLINE void dump(B&& b) noexcept
      | 
                         ^    ~~~~~/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:14:27: note: candidate function template not viable: requires 3 arguments, but 2 were provided
   14 |    GLZ_ALWAYS_INLINE void dump(const char c, vector_like auto& b, auto& ix) noexcept
      |                           ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:285:27: note: candidate function template not viable: requires 3 arguments, but 2 were provided
  285 |    GLZ_ALWAYS_INLINE void dump(const std::array<uint8_t, N>& bytes, B&& b, auto& ix) noexcept
      |                           ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:174:27: note: candidate function template not viable: requires 3 arguments, but 2 were provided
  174 |    GLZ_ALWAYS_INLINE void dump(const sv str, vector_like auto& b, auto& ix) noexcept
      |                           ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:185:27: note: candidate function template not viable: requires 3 arguments, but 2 were provided
  185 |    GLZ_ALWAYS_INLINE void dump(const sv str, auto* b, auto& ix) noexcept
      |                           ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:273:27: note: candidate function template not viable: requires 3 arguments, but 2 were provided
  273 |    GLZ_ALWAYSINLINE void dump(const std::span<const std::byte> bytes, B&& b, auto& ix) noexcept
      |                           ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/project/build_gtest/_deps/glaze-src/include/glaze/util/dump.hpp:246:27: note: candidate function template not viable: requires 3 arguments, but 2 were provided
  246 |    GLZ_ALWAYS_INLINE void dump(std::byte c, auto&& b, auto& ix) noexcept
      |                           ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error.log

@stephenberry
Copy link
Owner

I would be surprised if Clang 19 introduced significant compiler bugs. It's probably something wrong with your setup. I'll build a dev container with Clang 19 to test. But, the code builds with Clang 15,16,17, and 18

@stephenberry
Copy link
Owner

Clang 19 works on compiler explorer: https://gcc.godbolt.org/z/WzzshzffM

@maldag
Copy link
Author

maldag commented Apr 18, 2024

Well, testing with clang 19 it does not compile using write-Method with prettier: https://gcc.godbolt.org/z/9d4M1Yh1f

@stephenberry
Copy link
Owner

Oh, thank you! Will fix ASAP.

@stephenberry stephenberry added bug Something isn't working and removed bug Something isn't working labels Apr 18, 2024
@stephenberry
Copy link
Owner

The issue is actually not a bug, but that you're trying to write to a const std::string buffer. If you remove const, the code will build and run as intended.

This does indicate that we need much better error messages when someone tries to write to a const qualified buffer. I'll keep this issue open until that is resolved.

@stephenberry stephenberry changed the title Testing with json and prettier using example Errors are ugly when trying to write to a const buffer Apr 18, 2024
@stephenberry stephenberry added the enhancement New feature or request label Apr 18, 2024
@stephenberry stephenberry linked a pull request Apr 18, 2024 that will close this issue
@stephenberry
Copy link
Owner

Better erroring when trying to write to a const buffer has been added.

Thanks for bringing this up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants