util: Make Assert work with any value #19491

pull MarcoFalke wants to merge 1 commits into bitcoin:master from MarcoFalke:2007-utilAssert changing 2 files +11 −1
  1. MarcoFalke commented at 1:04 pm on July 11, 2020: member
    Goal is to avoid compile failures
  2. util: Make Assert work with any value fa53635381
  3. MarcoFalke added the label Refactoring on Jul 11, 2020
  4. MarcoFalke commented at 1:05 pm on July 11, 2020: member
    Can be tested by reverting the changes to the header and observing compile failures in the test
  5. practicalswift commented at 5:43 pm on July 11, 2020: contributor
    Concept ACK
  6. jonatack commented at 6:33 pm on July 11, 2020: member

    Interesting idea.

    ACK fa5363538125d996ae5cede55f7f05e88701ace2

     0  CXX      wallet/test/test_test_bitcoin-coinselector_tests.o
     1In file included from ./test/util/setup_common.h:15,
     2                 from test/util_tests.cpp:12:
     3test/util_tests.cpp: In lambda function:
     4test/util_tests.cpp:47:62: error: cannot bind non-const lvalue reference of type std::unique_ptr<int>& to an rvalue of type std::unique_ptr<int>
     5   47 |     const std::unique_ptr<int> p_two = Assert(MakeUnique<int>(2));
     6./util/check.h:57:80: note: in definition of macro Assert
     7   57 | #define Assert(val) [&]() -> decltype(get_pure_r_value(val))& { auto& check = (val); assert(#val && check); return check; }()
     8      |                                                                                ^~~
     9test/util_tests.cpp: In member function void util_tests::util_check::test_method():
    10./util/check.h:57:125: error: use of deleted function std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]
    11   57 | #define Assert(val) [&]() -> decltype(get_pure_r_value(val))& { auto& check = (val); assert(#val && check); return check; }()
    12      |                                                                                                                             ^
    13test/util_tests.cpp:47:40: note: in expansion of macro Assert
    14   47 |     const std::unique_ptr<int> p_two = Assert(MakeUnique<int>(2));
    15      |                                        ^~~~~~
    16In file included from /usr/include/c++/9/memory:80,
    17                 from /usr/include/boost/system/detail/std_interoperability.hpp:12,
    18                 from /usr/include/boost/system/error_code.hpp:959,
    19                 from /usr/include/boost/filesystem/path_traits.hpp:22,
    20                 from /usr/include/boost/filesystem/path.hpp:26,
    21                 from /usr/include/boost/filesystem.hpp:16,
    22                 from ./fs.h:14,
    23                 from ./util/system.h:20,
    24                 from test/util_tests.cpp:5:
    25/usr/include/c++/9/bits/unique_ptr.h:414:7: note: declared here
    26  414 |       unique_ptr(const unique_ptr&) = delete;
    27      |       ^~~~~~~~~~
    28In file included from ./test/util/setup_common.h:15,
    29                 from test/util_tests.cpp:12:
    30test/util_tests.cpp: In lambda function:
    31test/util_tests.cpp:50:16: error: cannot bind non-const lvalue reference of type bool& to an rvalue of type bool
    32   50 |     Assert(two == 2);
    33./util/check.h:57:80: note: in definition of macro Assert
    34   57 | #define Assert(val) [&]() -> decltype(get_pure_r_value(val))& { auto& check = (val); assert(#val && check); return check; }()
    35      |                                                                                ^~~
    36test/util_tests.cpp: In lambda function:
    37./util/check.h:57:83: error: cannot bind non-const lvalue reference of type bool& to an rvalue of type bool
    38   57 | #define Assert(val) [&]() -> decltype(get_pure_r_value(val))& { auto& check = (val); assert(#val && check); return check; }()
    39      |                                                                                   ^
    40test/util_tests.cpp:51:5: note: in expansion of macro Assert
    41   51 |     Assert(true);
    42      |     ^~~~~~
    43make[2]: *** [Makefile:17997: test/test_bitcoin-util_tests.o] Error 1
    44make[2]: *** Waiting for unfinished jobs....
    45make[2]: Leaving directory '/home/jon/projects/bitcoin/bitcoin/src'
    46make[1]: *** [Makefile:18328: all-recursive] Error 1
    47make[1]: Leaving directory '/home/jon/projects/bitcoin/bitcoin/src'
    48make: *** [Makefile:788: all-recursive] Error 1
    
     0  CXX      test/test_bitcoin-validation_flush_tests.o
     1test/util_tests.cpp:47:40: error: non-const lvalue reference to type 'unique_ptr<...>' cannot bind to a temporary of type 'unique_ptr<...>'
     2    const std::unique_ptr<int> p_two = Assert(MakeUnique<int>(2));
     3                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~
     4./util/check.h:57:71: note: expanded from macro 'Assert'
     5#define Assert(val) [&]() -> decltype(get_pure_r_value(val))& { auto& check = (val); assert(#val && check); return check; }()
     6                                                                      ^       ~~~~~
     7test/util_tests.cpp:47:32: error: call to deleted constructor of 'const std::unique_ptr<int>'
     8    const std::unique_ptr<int> p_two = Assert(MakeUnique<int>(2));
     9                               ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~
    10/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unique_ptr.h:414:7: note: 'unique_ptr' has been explicitly marked deleted here
    11      unique_ptr(const unique_ptr&) = delete;
    12      ^
    13test/util_tests.cpp:50:5: error: non-const lvalue reference to type 'bool' cannot bind to a temporary of type 'bool'
    14    Assert(two == 2);
    15    ^~~~~~~~~~~~~~~~
    16./util/check.h:57:71: note: expanded from macro 'Assert'
    17#define Assert(val) [&]() -> decltype(get_pure_r_value(val))& { auto& check = (val); assert(#val && check); return check; }()
    18                                                                      ^       ~~~~~
    19test/util_tests.cpp:51:5: error: non-const lvalue reference to type 'bool' cannot bind to a temporary of type 'bool'
    20    Assert(true);
    21    ^~~~~~~~~~~~
    22./util/check.h:57:71: note: expanded from macro 'Assert'
    23#define Assert(val) [&]() -> decltype(get_pure_r_value(val))& { auto& check = (val); assert(#val && check); return check; }()
    24                                                                      ^       ~~~~~
    254 errors generated.
    26make[2]: *** [Makefile:17997: test/test_bitcoin-util_tests.o] Error 1
    27make[2]: *** Waiting for unfinished jobs....
    28make[2]: Leaving directory '/home/jon/projects/bitcoin/bitcoin/src'
    29make[1]: *** [Makefile:18328: all-recursive] Error 1
    30make[1]: Leaving directory '/home/jon/projects/bitcoin/bitcoin/src'
    31make: *** [Makefile:788: all-recursive] Error 1
    
  7. ryanofsky approved
  8. ryanofsky commented at 7:29 pm on July 14, 2020: member

    Code review ACK fa5363538125d996ae5cede55f7f05e88701ace2. Looks like if argument is an lvalue this effectively does:

    0T& Assert(T& check) { assert(check); return check; }
    

    and if argument is an rvalue it does:

    0T Assert(T&& check) { assert(check); return std::move(check); }
    
  9. MarcoFalke merged this on Jul 15, 2020
  10. MarcoFalke closed this on Jul 15, 2020

  11. MarcoFalke deleted the branch on Jul 15, 2020
  12. sidhujag referenced this in commit c7466e7847 on Jul 16, 2020
  13. Fabcien referenced this in commit bf30e25f2b on Jun 15, 2021
  14. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-17 06:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me