Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions src/catch2/internal/catch_decomposer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ namespace Catch {
// This was added in C++20, but we require only C++14 for now.
template <typename T>
using RemoveCVRef_t = std::remove_cv_t<std::remove_reference_t<T>>;

// Perform ordinary lookup before decomposition operators can hide
// global user-defined comparisons. Decomposition uses lvalues.
#define CATCH_INTERNAL_COMPARISON_HELPERS(id, op) \
template <typename L, typename R> \
constexpr bool compare_##id(L& lhs, R& rhs) { \
return static_cast<bool>(lhs op rhs); \
} \
template <typename T> \
constexpr bool compare_##id##_zero(T& lhs) { \
return static_cast<bool>(lhs op 0); \
} \
template <typename T> \
constexpr bool compare_zero_##id(T& rhs) { \
return static_cast<bool>(0 op rhs); \
}
CATCH_INTERNAL_COMPARISON_HELPERS(eq, ==)
CATCH_INTERNAL_COMPARISON_HELPERS(ne, !=)
CATCH_INTERNAL_COMPARISON_HELPERS(lt, <)
CATCH_INTERNAL_COMPARISON_HELPERS(le, <=)
CATCH_INTERNAL_COMPARISON_HELPERS(gt, >)
CATCH_INTERNAL_COMPARISON_HELPERS(ge, >=)
#undef CATCH_INTERNAL_COMPARISON_HELPERS
}

// Note: This is about as much as we can currently reasonably support.
Expand Down Expand Up @@ -294,7 +317,7 @@ namespace Catch {
Detail::negation<capture_by_value< \
Detail::RemoveCVRef_t<RhsT>>>>::value, \
BinaryExpr<LhsT, RhsT const&>> { \
return { static_cast<bool>( lhs.getLhs() op rhs ), \
return { Detail::compare_##id( lhs.getLhs(), rhs ), \
lhs.getLhs(), \
#op##_sr, \
rhs }; \
Expand All @@ -305,7 +328,7 @@ namespace Catch {
Detail::conjunction<Detail::is_##id##_comparable<LhsT, RhsT>, \
capture_by_value<RhsT>>::value, \
BinaryExpr<LhsT, RhsT>> { \
return { static_cast<bool>( lhs.getLhs() op rhs ), \
return { Detail::compare_##id( lhs.getLhs(), rhs ), \
lhs.getLhs(), \
#op##_sr, \
rhs }; \
Expand All @@ -322,7 +345,7 @@ namespace Catch {
std::is_same<RhsT, long>>>::value, \
BinaryExpr<LhsT, RhsT>> { \
if ( rhs != 0 ) { throw_test_failure_exception(); } \
return { static_cast<bool>( lhs.getLhs() op 0 ), \
return { Detail::compare_##id##_zero( lhs.getLhs() ), \
lhs.getLhs(), \
#op##_sr, \
rhs }; \
Expand All @@ -339,7 +362,7 @@ namespace Catch {
std::is_same<LhsT, long>>>::value, \
BinaryExpr<LhsT, RhsT>> { \
if ( lhs.getLhs() != 0 ) { throw_test_failure_exception(); } \
return { static_cast<bool>( 0 op rhs ), lhs.getLhs(), #op##_sr, rhs }; \
return { Detail::compare_zero_##id( rhs ), lhs.getLhs(), #op##_sr, rhs }; \
}

CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( eq, == )
Expand All @@ -355,7 +378,7 @@ namespace Catch {
Detail::negation<capture_by_value< \
Detail::RemoveCVRef_t<RhsT>>>>::value, \
BinaryExpr<LhsT, RhsT const&>> { \
return { static_cast<bool>( lhs.getLhs() op rhs ), \
return { Detail::compare_##id( lhs.getLhs(), rhs ), \
lhs.getLhs(), \
#op##_sr, \
rhs }; \
Expand All @@ -366,7 +389,7 @@ namespace Catch {
Detail::conjunction<Detail::is_##id##_comparable<LhsT, RhsT>, \
capture_by_value<RhsT>>::value, \
BinaryExpr<LhsT, RhsT>> { \
return { static_cast<bool>( lhs.getLhs() op rhs ), \
return { Detail::compare_##id( lhs.getLhs(), rhs ), \
lhs.getLhs(), \
#op##_sr, \
rhs }; \
Expand All @@ -380,7 +403,7 @@ namespace Catch {
std::is_same<RhsT, int>>::value, \
BinaryExpr<LhsT, RhsT>> { \
if ( rhs != 0 ) { throw_test_failure_exception(); } \
return { static_cast<bool>( lhs.getLhs() op 0 ), \
return { Detail::compare_##id##_zero( lhs.getLhs() ), \
lhs.getLhs(), \
#op##_sr, \
rhs }; \
Expand All @@ -394,7 +417,7 @@ namespace Catch {
std::is_same<LhsT, int>>::value, \
BinaryExpr<LhsT, RhsT>> { \
if ( lhs.getLhs() != 0 ) { throw_test_failure_exception(); } \
return { static_cast<bool>( 0 op rhs ), lhs.getLhs(), #op##_sr, rhs }; \
return { Detail::compare_zero_##id( rhs ), lhs.getLhs(), #op##_sr, rhs }; \
}

CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( lt, < )
Expand Down
57 changes: 57 additions & 0 deletions tests/SelfTest/UsageTests/Decomposition.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@

#include <iostream>
#include <cstdio>
#include <cstddef>

namespace regression_3221 {
struct Value { int value; };
struct NullValue {};
}

// These deliberately live outside Value's namespace, before Catch2 headers.
#define DEFINE_GLOBAL_COMPARISON(op) \
constexpr bool operator op(regression_3221::Value const& lhs, \
regression_3221::Value const& rhs) { \
return lhs.value op rhs.value; \
}
DEFINE_GLOBAL_COMPARISON(==)
DEFINE_GLOBAL_COMPARISON(!=)
DEFINE_GLOBAL_COMPARISON(<)
DEFINE_GLOBAL_COMPARISON(<=)
DEFINE_GLOBAL_COMPARISON(>)
DEFINE_GLOBAL_COMPARISON(>=)
#undef DEFINE_GLOBAL_COMPARISON

constexpr bool operator==(regression_3221::NullValue const&, std::nullptr_t) {
return true;
}
constexpr bool operator==(std::nullptr_t, regression_3221::NullValue const&) {
return true;
}
constexpr bool operator!=(regression_3221::NullValue const&, std::nullptr_t) {
return false;
}
constexpr bool operator!=(std::nullptr_t, regression_3221::NullValue const&) {
return false;
}

namespace {

Expand All @@ -28,6 +61,30 @@ std::ostream& operator<<(std::ostream& o, truthy) {

#include <catch2/catch_test_macros.hpp>

TEST_CASE("Global comparisons declared before Catch2 headers: #3221",
"[Decomposition][approvals]") {
regression_3221::Value const one{1}, two{2};
REQUIRE(one == one);
REQUIRE_FALSE(one == two);
REQUIRE(one != two);
REQUIRE_FALSE(one != one);
REQUIRE(one < two);
REQUIRE(one <= one);
REQUIRE(two > one);
REQUIRE(two >= two);
REQUIRE(regression_3221::Value{1} == regression_3221::Value{1});
REQUIRE(one != regression_3221::Value{2});
regression_3221::NullValue const null;
REQUIRE(null == 0);
REQUIRE(0 == null);
REQUIRE(null == 0L);
REQUIRE(0L == null);
REQUIRE_FALSE(null != 0);
REQUIRE_FALSE(0 != null);
REQUIRE_FALSE(null != 0L);
REQUIRE_FALSE(0L != null);
}

TEST_CASE( "Reconstruction should be based on stringification: #914" , "[Decomposition][failing][.]") {
CHECK(truthy(false));
}
Expand Down