Skip to content

Commit 0e5104f

Browse files
committed
[libc++] Further constrain comparison against foo_ordering types
This fixes an issue reported in llvm#79465.
1 parent 5953e5a commit 0e5104f

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

libcxx/include/__compare/ordering.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ struct _CmpUnspecifiedParam {
4949
{
5050
(void)__zero;
5151
}
52+
53+
// Reject any other type and reject int lvalues.
54+
template <class T>
55+
_CmpUnspecifiedParam(T&&) = delete;
56+
_CmpUnspecifiedParam(const volatile int&) = delete;
5257
};
5358

5459
class partial_ordering {

libcxx/test/std/language.support/cmp/cmp.categories.pre/reject-other-than-literal-zero.verify.cpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,33 @@
2222

2323
#include "test_macros.h"
2424

25+
struct AnyType {
26+
operator int() const { return 0; }
27+
};
28+
2529
#define TEST_FAIL(v, op) \
2630
do { \
2731
/* invalid types */ \
32+
constexpr AnyType t; \
2833
void(v op 0L); \
2934
void(0L op v); \
3035
void(v op 0.0); \
3136
void(0.0 op v); \
3237
void(v op nullptr); \
3338
void(nullptr op v); \
39+
void(v op t); \
40+
void(t op v); \
3441
/* invalid value */ \
3542
void(v op 1); \
3643
void(1 op v); \
37-
/* value not known at compile-time */ \
44+
/* lvalue reference (also, value is not known at compile-time) */ \
3845
int i = 0; \
3946
void(v op i); \
4047
void(i op v); \
48+
/* value known at compile time, but still a lvalue */ \
49+
constexpr int j = 0; \
50+
void(v op j); \
51+
void(j op v); \
4152
} while (false)
4253

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

5162
template <typename T>
5263
void test_category(T v) {
53-
TEST_FAIL(v, ==); // expected-error 30 {{invalid operands to binary expression}}
54-
TEST_FAIL(v, !=); // expected-error 30 {{invalid operands to binary expression}}
55-
TEST_FAIL(v, <); // expected-error 30 {{invalid operands to binary expression}}
56-
TEST_FAIL(v, <=); // expected-error 30 {{invalid operands to binary expression}}
57-
TEST_FAIL(v, >); // expected-error 30 {{invalid operands to binary expression}}
58-
TEST_FAIL(v, >=); // expected-error 30 {{invalid operands to binary expression}}
59-
TEST_FAIL(v, <=>); // expected-error 30 {{invalid operands to binary expression}}
64+
TEST_FAIL(v, ==);
65+
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
66+
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}
67+
68+
TEST_FAIL(v, !=);
69+
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
70+
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}
71+
72+
TEST_FAIL(v, <);
73+
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
74+
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}
75+
76+
TEST_FAIL(v, <=);
77+
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
78+
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}
79+
80+
TEST_FAIL(v, >);
81+
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
82+
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}
83+
84+
TEST_FAIL(v, >=);
85+
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
86+
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}
87+
88+
TEST_FAIL(v, <=>);
89+
// expected-error-re@-1 36 {{conversion function from '{{.+}}' to '_CmpUnspecifiedParam' invokes a deleted function}}
90+
// expected-error-re@-2 6 {{conversion from '{{.+}}' to '_CmpUnspecifiedParam' is ambiguous}}
6091

6192
TEST_PASS(v, ==);
6293
TEST_PASS(v, !=);

0 commit comments

Comments
 (0)