json icon indicating copy to clipboard operation
json copied to clipboard

compiler error using clang-16.0.5 when using gcc-13.1 standard library

Open janwilmans opened this issue 2 years ago • 4 comments

Description

https://godbolt.org/z/jnxMe6veT

workaround: -DJSON_HAS_RANGES=0

Reproduction steps

na.

Expected vs. actual results

no compilation error vs. compilation error

Minimal code example

#include <nlohmann/json.hpp>
#include <sstream>
#include <iomanip>

using nlohmann::json;

int main()
{
    std::stringstream ss;
    json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
    ss << std::setw(1) << std::setfill('\t') << j;

}

Error messages

In file included from <source>:1:
In file included from /opt/compiler-explorer/libs/nlohmann_json/trunk/single_include/nlohmann/json.hpp:5097:
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:6151:14: error: requires clause differs in template redeclaration
    requires forward_range<_Vp>
             ^
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:5850:14: note: previous template declaration is here
    requires input_range<_Vp>
             ^
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:8846:20: error: type-id cannot have a name
    { return (auto(__x) += __y); }
                   ^
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:8846:25: error: expected ')'
    { return (auto(__x) += __y); }
                        ^
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:8846:14: note: to match this '('
    { return (auto(__x) += __y); }
             ^
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:8846:32: error: expected expression
    { return (auto(__x) += __y); }
                               ^
4 errors generated.
ASM generation compiler returned: 1
In file included from <source>:1:
In file included from /opt/compiler-explorer/libs/nlohmann_json/trunk/single_include/nlohmann/json.hpp:5097:
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:6151:14: error: requires clause differs in template redeclaration
    requires forward_range<_Vp>
             ^
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:5850:14: note: previous template declaration is here
    requires input_range<_Vp>
             ^
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:8846:20: error: type-id cannot have a name
    { return (auto(__x) += __y); }
                   ^
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:8846:25: error: expected ')'
    { return (auto(__x) += __y); }
                        ^
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:8846:14: note: to match this '('
    { return (auto(__x) += __y); }
             ^
/opt/compiler-explorer/gcc-13.1.0/lib/gcc/x86_64-linux-gnu/13.1.0/../../../../include/c++/13.1.0/ranges:8846:32: error: expected expression
    { return (auto(__x) += __y); }
                               ^
4 errors generated.
Execution build compiler returned: 1

Compiler and operating system

Linux, clang-16.0.5+gcc-13.1 standard library

Library version

3.11.2

Validation

janwilmans avatar Jun 09 '23 11:06 janwilmans

We use GCC 13.3.0 in the CI. Can you please re-test with the latest develop branch?

nlohmann avatar Nov 29 '24 13:11 nlohmann

the problem persists, I think it is triggered by clang lacking support for some ranges feature. see: https://godbolt.org/z/cbEK3s1W9, which tests clang 16 + gcc 13.3

image

janwilmans avatar Dec 02 '24 08:12 janwilmans

The same setting for Clang 17.0.1 works https://godbolt.org/z/fPocKaT5j

I think the ranges-detection asks the standard library 'what it supports', but then there is compiler support in clang-16 missing that the standard library from gcc 13.1 relies on. So maybe this this not a bug, but something that can't be reliably queried?

janwilmans avatar Dec 02 '24 08:12 janwilmans

Sorry, I missed the point about mixing Clang with GCC's STL. We do check Clang with both libc++ and libstdc++, but only for the latest Clang version (20.0.0). I'm not sure to what extend I can test all combinations of Clang/GCC.

With #4440 we have a similar issue with ranges.

Maybe we need to refine this detection code to incorporate the experiences from the compiler combinations. Apparently, it is insufficient to "trust" defined(__cpp_lib_ranges):

#ifndef JSON_HAS_RANGES
    // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error
    #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
        #define JSON_HAS_RANGES 0
    #elif defined(__cpp_lib_ranges)
        #define JSON_HAS_RANGES 1
    #else
        #define JSON_HAS_RANGES 0
    #endif
#endif

nlohmann avatar Dec 05 '24 21:12 nlohmann

I did some more testing, it is indeed very specific for some combinations and I did not find a reliable way to detect this. Let's just close this as 'won't fix'

janwilmans avatar Dec 09 '24 16:12 janwilmans