Skip to content

[libc++] Further constrain comparison against foo_ordering types #127311

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions libcxx/include/__compare/ordering.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ struct _CmpUnspecifiedParam {
{
(void)__zero;
}

// Reject any other type and reject int lvalues.
template <class T>
_CmpUnspecifiedParam(T&&) = delete;
_CmpUnspecifiedParam(const volatile int&) = delete;
};

class partial_ordering {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,33 @@

#include "test_macros.h"

struct AnyType {
operator int() const { return 0; }
};

#define TEST_FAIL(v, op) \
do { \
/* invalid types */ \
constexpr AnyType t; \
void(v op 0L); \
void(0L op v); \
void(v op 0.0); \
void(0.0 op v); \
void(v op nullptr); \
void(nullptr op v); \
void(v op t); \
void(t op v); \
/* invalid value */ \
void(v op 1); \
void(1 op v); \
/* value not known at compile-time */ \
/* lvalue reference (also, value is not known at compile-time) */ \
int i = 0; \
void(v op i); \
void(i op v); \
/* value known at compile time, but still a lvalue */ \
constexpr int j = 0; \
void(v op j); \
void(j op v); \
} while (false)

#define TEST_PASS(v, op) \
Expand All @@ -50,13 +61,33 @@

template <typename T>
void test_category(T v) {
TEST_FAIL(v, ==); // expected-error 30 {{invalid operands to binary expression}}
TEST_FAIL(v, !=); // expected-error 30 {{invalid operands to binary expression}}
TEST_FAIL(v, <); // expected-error 30 {{invalid operands to binary expression}}
TEST_FAIL(v, <=); // expected-error 30 {{invalid operands to binary expression}}
TEST_FAIL(v, >); // expected-error 30 {{invalid operands to binary expression}}
TEST_FAIL(v, >=); // expected-error 30 {{invalid operands to binary expression}}
TEST_FAIL(v, <=>); // expected-error 30 {{invalid operands to binary expression}}
TEST_FAIL(v, ==);
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}

TEST_FAIL(v, !=);
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}

TEST_FAIL(v, <);
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}

TEST_FAIL(v, <=);
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}

TEST_FAIL(v, >);
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}

TEST_FAIL(v, >=);
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}

TEST_FAIL(v, <=>);
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}

TEST_PASS(v, ==);
TEST_PASS(v, !=);
Expand Down
Loading