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

Unevaluated lambda in assert breaks gcc 7 build #705

Closed
chuckatkins opened this issue Aug 22, 2017 · 8 comments
Closed

Unevaluated lambda in assert breaks gcc 7 build #705

chuckatkins opened this issue Aug 22, 2017 · 8 comments
Labels
solution: invalid the issue is not related to the library

Comments

@chuckatkins
Copy link
Contributor

$ cmake ../../source/devel
-- The CXX compiler identification is GNU 7.1.1
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chuckatkins/Code/nljson/build/devel
$ make
Scanning dependencies of target catch_main
[  2%] Building CXX object test/CMakeFiles/catch_main.dir/src/unit.cpp.o
[  2%] Built target catch_main
Scanning dependencies of target json_unit
[  5%] Building CXX object test/CMakeFiles/json_unit.dir/src/unit-algorithms.cpp.o
In file included from /usr/include/c++/7/cassert:44:0,
                 from /home/chuckatkins/Code/nljson/source/devel/src/json.hpp:34,
                 from /home/chuckatkins/Code/nljson/source/devel/test/src/unit-algorithms.cpp:31:
/home/chuckatkins/Code/nljson/source/devel/src/json.hpp: In constructor ‘nlohmann::detail::input_adapter::input_adapter(IteratorType, IteratorType)’:
/home/chuckatkins/Code/nljson/source/devel/src/json.hpp:1500:20: error: lambda-expression in unevaluated context
                    [&first](std::pair<bool, int> res, decltype(*first) val)
                    ^
make[2]: *** [test/CMakeFiles/json_unit.dir/build.make:63: test/CMakeFiles/json_unit.dir/src/unit-algorithms.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:88: test/CMakeFiles/json_unit.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
@nlohmann
Copy link
Owner

It's odd I never experienced this error. What kind of compiler flags or Cmake options are you using?

@chuckatkins
Copy link
Contributor Author

Nothing special. I do tend to use a bleeding edge development build of CMake but that's it. No CFLAGS, or CXX flags, etc. This is a current Fedora 26 machine with my own CMake built from cmake/master and the system gcc 7.1.1. As you can see from the verbose output, no special flags are being passed.

$ cmake --version
cmake version 3.9.20170822-g8a0ed
...
$ echo $CFLAGS $CXXFLAGS

$ cmake ../../source/devel
-- The CXX compiler identification is GNU 7.1.1
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chuckatkins/Code/nljson/build/devel
$ make VERBOSE=1
...
[  5%] Building CXX object test/CMakeFiles/json_unit.dir/src/unit-algorithms.cpp.o
cd /home/chuckatkins/Code/nljson/build/devel/test && /usr/bin/c++   -I/home/chuckatkins/Code/nljson/source/devel/src -I/home/chuckatkins/Code/nljson/source/devel/test/src -I/home/chuckatkins/Code/nljson/source/devel/test/thirdparty/catch  -std=gnu++11 -o CMakeFiles/json_unit.dir/src/unit-algorithms.cpp.o -c /home/chuckatkins/Code/nljson/source/devel/test/src/unit-algorithms.cpp
In file included from /usr/include/c++/7/cassert:44:0,
                 from /home/chuckatkins/Code/nljson/source/devel/src/json.hpp:34,
                 from /home/chuckatkins/Code/nljson/source/devel/test/src/unit-algorithms.cpp:31:
/home/chuckatkins/Code/nljson/source/devel/src/json.hpp: In constructor ‘nlohmann::detail::input_adapter::input_adapter(IteratorType, IteratorType)’:
/home/chuckatkins/Code/nljson/source/devel/src/json.hpp:1500:20: error: lambda-expression in unevaluated context
                    [&first](std::pair<bool, int> res, decltype(*first) val)
                    ^
make[2]: *** [test/CMakeFiles/json_unit.dir/build.make:63: test/CMakeFiles/json_unit.dir/src/unit-algorithms.cpp.o] Error 1
make[2]: Leaving directory '/home/chuckatkins/Code/nljson/build/devel'
make[1]: *** [CMakeFiles/Makefile2:88: test/CMakeFiles/json_unit.dir/all] Error 2
make[1]: Leaving directory '/home/chuckatkins/Code/nljson/build/devel'
make: *** [Makefile:141: all] Error 2
$ 

FWIW, I believe it's likely an issue with compiler sensitivity since I didn't get this with gcc6 when I was running Fedora 24. I can reproduce this with a dummy example by putting a lamda inside an assert and evaluating it:

// foo.cxx
#include <cassert>
int main(int argc, char **argv)
{ 
  assert([](int x) { return x == 0; }(argc));
  return 0;
} 
$ c++ -o foo -DDEBUG foo.cxx 
In file included from /usr/include/c++/7/cassert:44:0,
                 from foo.cxx:1:
foo.cxx: In function ‘int main(int, char**)’:
foo.cxx:5:10: error: lambda-expression in unevaluated context
   assert([](int x) { return x == 0; }(argc));
          ^
$ 

@theodelrieu
Copy link
Contributor

Isn't that a bug of GCC? I don't object to the fix, but it'd be great to report it if it's indeed a bug.

@chuckatkins
Copy link
Contributor Author

I thought so too at first but it's not. It has to do with how assert( foo ) is implemented and expanded by the preprocessor. The result of the preprocessor expansion of assert(foo) for a failing build is, amongst other things, an expression with sizeof( foo ) and per the C++ standard, the operand to sizeof( ... ) is an unevaluated expression. In turn, also per the C++ standard, lambda expressions are not allowed to appear in unevaluated expressions. After digging through the preprocessor output, I've been able to reproduce the error without the assert:

int main(int argc, char **argv)
{
  (void) sizeof( [] { return true; } () );
  return 0;
}

Which, indeed, is invalid because of the lambda expression being in the unevaluated operand of sizeof. The bug is not actually a gcc/g++ one but a glibc issue in that the implementation of assert() (I just figured this out btw) in assert.h which is incompatible with c++ lambda expressions. I tracked down the issue to a recent glibc commit which introduced the error by changing the assert implementation to use sizeofin order to squelch some pedantic warnings and another about two weeks later that subsequenty fixed it by not using sizeof for the implemention of assert when using C++. Unfortunately the first patch seems to have propagated to many distro's glibc releases while the later has not. So, I guess it's up to you as to whether or not to take #706 which I now realize is really a workaround for a glibc bug in Fedora 26, which is already patched upstream.

@theodelrieu
Copy link
Contributor

Wow, congrats for the impressive digging!

Personally, I would not add a workaround to an already patched issue, but that is up to @nlohmann.

Anyway, thanks a lot for your thorough research :)

@nlohmann
Copy link
Owner

Thanks a lot for the research. I think merging the PR makes little sense here. If anyone encounters the same combination of problems, I think Google will send her/him here. :)

@nlohmann nlohmann added solution: invalid the issue is not related to the library and removed kind: bug labels Aug 22, 2017
@markand
Copy link

markand commented Aug 26, 2017

And I also was bitten by this bug as well :)

halstead pushed a commit to openembedded/openembedded-core that referenced this issue Jan 3, 2018
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
halstead pushed a commit to openembedded/openembedded-core that referenced this issue Jan 3, 2018
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
kraj pushed a commit to YoeDistro/poky-old that referenced this issue Jan 4, 2018
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

(From OE-Core rev: e80d3cb89aacc6ffb6630a106387e08483628950)

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
kraj pushed a commit to YoeDistro/poky-old that referenced this issue Jan 4, 2018
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

(From OE-Core rev: e80d3cb89aacc6ffb6630a106387e08483628950)

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
halstead pushed a commit to openembedded/openembedded-core that referenced this issue Jan 4, 2018
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
kraj pushed a commit to YoeDistro/poky-old that referenced this issue Jan 4, 2018
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

(From OE-Core rev: d9583296be58f02912abc4fd19f576b3f89107ff)

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
otavio pushed a commit to OSSystems/oe-core that referenced this issue Jan 23, 2018
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
(cherry picked from commit d958329)
Signed-off-by: Armin Kuster <akuster808@gmail.com>
halstead pushed a commit to openembedded/openembedded-core that referenced this issue Jan 25, 2018
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
(cherry picked from commit d958329)
Signed-off-by: Armin Kuster <akuster808@gmail.com>
kraj pushed a commit to YoeDistro/poky-old that referenced this issue Jan 25, 2018
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

(From OE-Core rev: ecc789663fe83b388ecdf1b0958a2990773f7487)

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
(cherry picked from commit d9583296be58f02912abc4fd19f576b3f89107ff)
Signed-off-by: Armin Kuster <akuster808@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
@vimpunk
Copy link
Contributor

vimpunk commented Sep 4, 2018

Has this been fixed? I'm unfortunately not on bleeding edge (xtensa gcc 5.2.0), and this error popped up on line 2165 in single_header/nlohmann/json.hpp. Would like to use this library as it looks really good, but can't currently due to this and I'd like to avoid forking it.

daregit pushed a commit to daregit/yocto-combined that referenced this issue May 22, 2024
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

(From OE-Core rev: d9583296be58f02912abc4fd19f576b3f89107ff)

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
daregit pushed a commit to daregit/yocto-combined that referenced this issue May 22, 2024
* fixes "lambda-expression in unevaluated context" compile failures such as
  nlohmann/json#705

* fixes "no match for 'operator==" compile failures such as
  https://bugzilla.redhat.com/show_bug.cgi?id=1482990

(From OE-Core rev: d9583296be58f02912abc4fd19f576b3f89107ff)

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

5 participants