Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CXX / class / class.compare / class.compare.default / p3.cpp
blob166bd97e2731cb01d4fc084d6e6a9d4f5f4d7e3c
1 // This test is for the [class.compare.default]p3 added by P2002R0
2 // Also covers modifications made by P2448R2 and extension warnings
4 // RUN: %clang_cc1 -std=c++2a -verify %s
5 // RUN: %clang_cc1 -std=c++2a -Wc++23-default-comp-relaxed-constexpr -verify=expected,extension %s
7 namespace std {
8 struct strong_ordering {
9 int n;
10 constexpr operator int() const { return n; }
11 static const strong_ordering less, equal, greater;
13 constexpr strong_ordering strong_ordering::less = {-1};
14 constexpr strong_ordering strong_ordering::equal = {0};
15 constexpr strong_ordering strong_ordering::greater = {1};
18 struct A {
19 friend bool operator==(const A&, const A&) = default;
20 friend bool operator!=(const A&, const A&) = default;
22 friend std::strong_ordering operator<=>(const A&, const A&) = default;
23 friend bool operator<(const A&, const A&) = default;
24 friend bool operator<=(const A&, const A&) = default;
25 friend bool operator>(const A&, const A&) = default;
26 friend bool operator>=(const A&, const A&) = default;
28 struct TestA {
29 friend constexpr bool operator==(const A&, const A&) noexcept;
30 friend constexpr bool operator!=(const A&, const A&) noexcept;
32 friend constexpr std::strong_ordering operator<=>(const A&, const A&) noexcept;
33 friend constexpr bool operator<(const A&, const A&);
34 friend constexpr bool operator<=(const A&, const A&);
35 friend constexpr bool operator>(const A&, const A&);
36 friend constexpr bool operator>=(const A&, const A&);
39 // Declaration order doesn't matter, even though the secondary operators need
40 // to know whether the primary ones are constexpr.
41 struct ReversedA {
42 friend bool operator>=(const ReversedA&, const ReversedA&) = default;
43 friend bool operator>(const ReversedA&, const ReversedA&) = default;
44 friend bool operator<=(const ReversedA&, const ReversedA&) = default;
45 friend bool operator<(const ReversedA&, const ReversedA&) = default;
46 friend std::strong_ordering operator<=>(const ReversedA&, const ReversedA&) = default;
48 friend bool operator!=(const ReversedA&, const ReversedA&) = default;
49 friend bool operator==(const ReversedA&, const ReversedA&) = default;
51 struct TestReversedA {
52 friend constexpr bool operator>=(const ReversedA&, const ReversedA&);
53 friend constexpr bool operator>(const ReversedA&, const ReversedA&);
54 friend constexpr bool operator<=(const ReversedA&, const ReversedA&);
55 friend constexpr bool operator<(const ReversedA&, const ReversedA&);
56 friend constexpr std::strong_ordering operator<=>(const ReversedA&, const ReversedA&) noexcept;
58 friend constexpr bool operator!=(const ReversedA&, const ReversedA&) noexcept;
59 friend constexpr bool operator==(const ReversedA&, const ReversedA&) noexcept;
62 struct B {
63 A a;
64 friend bool operator==(const B&, const B&) = default;
65 friend bool operator!=(const B&, const B&) = default;
67 friend std::strong_ordering operator<=>(const B&, const B&) = default;
68 friend bool operator<(const B&, const B&) = default;
69 friend bool operator<=(const B&, const B&) = default;
70 friend bool operator>(const B&, const B&) = default;
71 friend bool operator>=(const B&, const B&) = default;
73 struct TestB {
74 friend constexpr bool operator==(const B&, const B&) noexcept;
75 friend constexpr bool operator!=(const B&, const B&) noexcept;
77 friend constexpr std::strong_ordering operator<=>(const B&, const B&);
78 friend constexpr bool operator<(const B&, const B&);
79 friend constexpr bool operator<=(const B&, const B&);
80 friend constexpr bool operator>(const B&, const B&);
81 friend constexpr bool operator>=(const B&, const B&);
84 struct C {
85 friend bool operator==(const C&, const C&); // expected-note {{previous}} extension-note 2{{non-constexpr comparison function declared here}}
86 friend bool operator!=(const C&, const C&) = default; // expected-note {{previous}}
88 friend std::strong_ordering operator<=>(const C&, const C&); // expected-note {{previous}} extension-note 2{{non-constexpr comparison function declared here}}
89 friend bool operator<(const C&, const C&) = default; // expected-note {{previous}}
90 friend bool operator<=(const C&, const C&) = default; // expected-note {{previous}}
91 friend bool operator>(const C&, const C&) = default; // expected-note {{previous}}
92 friend bool operator>=(const C&, const C&) = default; // expected-note {{previous}}
94 struct TestC {
95 friend constexpr bool operator==(const C&, const C&); // expected-error {{non-constexpr}}
96 friend constexpr bool operator!=(const C&, const C&); // expected-error {{non-constexpr}}
98 friend constexpr std::strong_ordering operator<=>(const C&, const C&); // expected-error {{non-constexpr}}
99 friend constexpr bool operator<(const C&, const C&); // expected-error {{non-constexpr}}
100 friend constexpr bool operator<=(const C&, const C&); // expected-error {{non-constexpr}}
101 friend constexpr bool operator>(const C&, const C&); // expected-error {{non-constexpr}}
102 friend constexpr bool operator>=(const C&, const C&); // expected-error {{non-constexpr}}
105 struct D {
106 A a;
107 C c;
108 A b;
109 friend bool operator==(const D&, const D&) = default; // expected-note {{previous}}
110 friend bool operator!=(const D&, const D&) = default; // expected-note {{previous}}
112 friend std::strong_ordering operator<=>(const D&, const D&) = default; // expected-note {{previous}}
113 friend bool operator<(const D&, const D&) = default; // expected-note {{previous}}
114 friend bool operator<=(const D&, const D&) = default; // expected-note {{previous}}
115 friend bool operator>(const D&, const D&) = default; // expected-note {{previous}}
116 friend bool operator>=(const D&, const D&) = default; // expected-note {{previous}}
118 struct TestD {
119 friend constexpr bool operator==(const D&, const D&); // expected-error {{non-constexpr}}
120 friend constexpr bool operator!=(const D&, const D&); // expected-error {{non-constexpr}}
122 friend constexpr std::strong_ordering operator<=>(const D&, const D&); // expected-error {{non-constexpr}}
123 friend constexpr bool operator<(const D&, const D&); // expected-error {{non-constexpr}}
124 friend constexpr bool operator<=(const D&, const D&); // expected-error {{non-constexpr}}
125 friend constexpr bool operator>(const D&, const D&); // expected-error {{non-constexpr}}
126 friend constexpr bool operator>=(const D&, const D&); // expected-error {{non-constexpr}}
130 struct E {
131 A a;
132 C c; // extension-note 2{{non-constexpr comparison function would be used to compare member 'c'}}
133 A b;
134 friend constexpr bool operator==(const E&, const E&) = default; // extension-warning {{declared constexpr but invokes a non-constexpr comparison function is a C++23 extension}}
135 friend constexpr bool operator!=(const E&, const E&) = default;
137 friend constexpr std::strong_ordering operator<=>(const E&, const E&) = default; // extension-warning {{declared constexpr but invokes a non-constexpr comparison function is a C++23 extension}}
138 friend constexpr bool operator<(const E&, const E&) = default;
139 friend constexpr bool operator<=(const E&, const E&) = default;
140 friend constexpr bool operator>(const E&, const E&) = default;
141 friend constexpr bool operator>=(const E&, const E&) = default;
144 struct E2 : A, C { // extension-note 2{{non-constexpr comparison function would be used to compare base class 'C'}}
145 friend constexpr bool operator==(const E2&, const E2&) = default; // extension-warning {{declared constexpr but invokes a non-constexpr comparison function is a C++23 extension}}
146 friend constexpr bool operator!=(const E2&, const E2&) = default;
148 friend constexpr std::strong_ordering operator<=>(const E2&, const E2&) = default; // extension-warning {{declared constexpr but invokes a non-constexpr comparison function is a C++23 extension}}
149 friend constexpr bool operator<(const E2&, const E2&) = default;
150 friend constexpr bool operator<=(const E2&, const E2&) = default;
151 friend constexpr bool operator>(const E2&, const E2&) = default;
152 friend constexpr bool operator>=(const E2&, const E2&) = default;
155 struct F {
156 friend bool operator==(const F&, const F&); // extension-note {{non-constexpr comparison function declared here}}
157 friend constexpr bool operator!=(const F&, const F&) = default; // extension-warning {{declared constexpr but invokes a non-constexpr comparison function is a C++23 extension}}
159 friend std::strong_ordering operator<=>(const F&, const F&); // extension-note 4{{non-constexpr comparison function declared here}}
160 friend constexpr bool operator<(const F&, const F&) = default; // extension-warning {{declared constexpr but invokes a non-constexpr comparison function is a C++23 extension}}
161 friend constexpr bool operator<=(const F&, const F&) = default; // extension-warning {{declared constexpr but invokes a non-constexpr comparison function is a C++23 extension}}
162 friend constexpr bool operator>(const F&, const F&) = default; // extension-warning {{declared constexpr but invokes a non-constexpr comparison function is a C++23 extension}}
163 friend constexpr bool operator>=(const F&, const F&) = default; // extension-warning {{declared constexpr but invokes a non-constexpr comparison function is a C++23 extension}}
166 // No implicit 'constexpr' if it's not the first declaration.
167 // FIXME: This rule creates problems for reordering of declarations; is this
168 // really the right model?
169 struct G;
170 bool operator==(const G&, const G&); // expected-note {{previous declaration}}
171 bool operator!=(const G&, const G&); // expected-note {{previous declaration}}
172 std::strong_ordering operator<=>(const G&, const G&); // expected-note {{previous declaration}}
173 bool operator<(const G&, const G&); // expected-note {{previous declaration}}
174 bool operator<=(const G&, const G&); // expected-note {{previous declaration}}
175 bool operator>(const G&, const G&); // expected-note {{previous declaration}}
176 bool operator>=(const G&, const G&); // expected-note {{previous declaration}}
177 struct G {
178 friend bool operator==(const G&, const G&) = default; // expected-error {{because it was already declared outside}}
179 friend bool operator!=(const G&, const G&) = default; // expected-error {{because it was already declared outside}}
181 friend std::strong_ordering operator<=>(const G&, const G&) = default; // expected-error {{because it was already declared outside}}
182 friend bool operator<(const G&, const G&) = default; // expected-error {{because it was already declared outside}}
183 friend bool operator<=(const G&, const G&) = default; // expected-error {{because it was already declared outside}}
184 friend bool operator>(const G&, const G&) = default; // expected-error {{because it was already declared outside}}
185 friend bool operator>=(const G&, const G&) = default; // expected-error {{because it was already declared outside}}
187 bool operator==(const G&, const G&);
188 bool operator!=(const G&, const G&);
190 std::strong_ordering operator<=>(const G&, const G&);
191 bool operator<(const G&, const G&);
192 bool operator<=(const G&, const G&);
193 bool operator>(const G&, const G&);
194 bool operator>=(const G&, const G&);
196 namespace PR44721 {
197 template <typename T> bool operator==(T const &, T const &) { return true; }
198 template <typename T, typename U> bool operator!=(T const &, U const &) { return true; }
199 template <typename T> int operator<=>(T const &, T const &) { return 0; }
201 struct S {
202 friend bool operator==(const S &, const S &) = default;
203 friend bool operator<=>(const S &, const S &) = default;
204 int x;