Skip to content

Commit dd84520

Browse files
committed
CWG 3131
1 parent 13c4298 commit dd84520

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

clang/test/SemaCXX/cxx2c-expansion-stmts.cpp

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ struct Private {
187187
friend constexpr int friend_func();
188188

189189
private:
190-
constexpr const int* begin() const { return integers.begin(); } // expected-note 2 {{declared private here}}
191-
constexpr const int* end() const { return integers.end(); } // expected-note 2 {{declared private here}}
190+
constexpr const int* begin() const { return integers.begin(); } // expected-note 3 {{declared private here}}
191+
constexpr const int* end() const { return integers.end(); } // expected-note 3 {{declared private here}}
192192

193193
public:
194194
static constexpr int member_func() {
@@ -203,8 +203,8 @@ struct Protected {
203203
friend constexpr int friend_func();
204204

205205
protected:
206-
constexpr const int* begin() const { return integers.begin(); } // expected-note 2 {{declared protected here}}
207-
constexpr const int* end() const { return integers.end(); } // expected-note 2 {{declared protected here}}
206+
constexpr const int* begin() const { return integers.begin(); } // expected-note 3 {{declared protected here}}
207+
constexpr const int* end() const { return integers.end(); } // expected-note 3 {{declared protected here}}
208208

209209
public:
210210
static constexpr int member_func() {
@@ -217,10 +217,10 @@ struct Protected {
217217

218218
void access_control() {
219219
static constexpr Private p1;
220-
template for (auto x : p1) g(x); // expected-error 2 {{'begin' is a private member of 'Private'}} expected-error 2 {{'end' is a private member of 'Private'}}
220+
template for (auto x : p1) g(x); // expected-error 3 {{'begin' is a private member of 'Private'}} expected-error 3 {{'end' is a private member of 'Private'}}
221221

222222
static constexpr Protected p2;
223-
template for (auto x : p2) g(x); // expected-error 2 {{'begin' is a protected member of 'Protected'}} expected-error 2 {{'end' is a protected member of 'Protected'}}
223+
template for (auto x : p2) g(x); // expected-error 3 {{'begin' is a protected member of 'Protected'}} expected-error 3 {{'end' is a protected member of 'Protected'}}
224224
}
225225

226226
constexpr int friend_func() {
@@ -634,8 +634,7 @@ consteval int f() {
634634
// expected-error@#invalid-ref {{constexpr variable '__begin1' must be initialized by a constant expression}}
635635
// expected-error@#invalid-ref {{constexpr variable '__end1' must be initialized by a constant expression}}
636636
// expected-error@#invalid-ref {{expansion size is not a constant expression}}
637-
// expected-note@#invalid-ref 2 {{member call on variable '__range1' whose value is not known}}
638-
// expected-note@#invalid-ref {{initializer of '__begin1' is not a constant expression}}
637+
// expected-note@#invalid-ref 3 {{member call on variable '__range1' whose value is not known}}
639638
// expected-note@#invalid-ref 3 {{declared here}}
640639
// expected-note@#invalid-ref {{reference to 'arr' is not a constant expression}}
641640
// expected-note@#invalid-ref {{in call to}}
@@ -1094,3 +1093,48 @@ void lambda_template(T a) {
10941093
void lambda_template_call() {
10951094
lambda_template([]{}); // expected-note {{in instantiation of function template specialization}}
10961095
}
1096+
1097+
// CWG 3131 makes it possible to expand over non-constexpr ranges.
1098+
namespace cwg3131 {
1099+
constexpr int f1() {
1100+
int j = 0;
1101+
template for (auto i : Array<int, 3>{1, 2, 3}) j +=i;
1102+
return j;
1103+
}
1104+
1105+
constexpr int f2() {
1106+
Array<int, 3> a{1, 2, 3};
1107+
int j = 0;
1108+
template for (auto i : a) j +=i;
1109+
return j;
1110+
}
1111+
1112+
static_assert(f1() == 6);
1113+
static_assert(f2() == 6);
1114+
1115+
template <typename T>
1116+
struct Span {
1117+
T* data;
1118+
__SIZE_TYPE__ size;
1119+
1120+
template <__SIZE_TYPE__ N>
1121+
constexpr Span(T(&a)[N]) : data{a}, size{N} {}
1122+
1123+
constexpr auto begin() const -> T* { return data; }
1124+
constexpr auto end() const -> T* { return data + size; }
1125+
};
1126+
1127+
constexpr int arr[3] = { 1, 2, 3 };
1128+
consteval Span<const int> foo() {
1129+
return Span<const int>(arr);
1130+
}
1131+
1132+
constexpr int f3() {
1133+
int r = 0;
1134+
template for (constexpr auto m : foo())
1135+
r += m;
1136+
return r;
1137+
}
1138+
1139+
static_assert(f3() == 6);
1140+
}

0 commit comments

Comments
 (0)