[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / clang / test / SemaCXX / cxx1z-decomposition.cpp
blob4c8c64177ed133e6b20a77c932737caceae65a96
1 // RUN: %clang_cc1 -std=c++17 -verify %s
3 void use_from_own_init() {
4 auto [a] = a; // expected-error {{binding 'a' cannot appear in the initializer of its own decomposition declaration}}
7 void num_elems() {
8 struct A0 {} a0;
9 int a1[1], a2[2];
11 auto [] = a0; // expected-warning {{does not allow a decomposition group to be empty}}
12 auto [v1] = a0; // expected-error {{type 'struct A0' decomposes into 0 elements, but 1 name was provided}}
13 auto [] = a1; // expected-error {{type 'int[1]' decomposes into 1 element, but no names were provided}} expected-warning {{empty}}
14 auto [v2] = a1;
15 auto [v3, v4] = a1; // expected-error {{type 'int[1]' decomposes into 1 element, but 2 names were provided}}
16 auto [] = a2; // expected-error {{type 'int[2]' decomposes into 2 elements, but no names were provided}} expected-warning {{empty}}
17 auto [v5] = a2; // expected-error {{type 'int[2]' decomposes into 2 elements, but only 1 name was provided}}
18 auto [v6, v7] = a2;
19 auto [v8, v9, v10] = a2; // expected-error {{type 'int[2]' decomposes into 2 elements, but 3 names were provided}}
22 // As a Clang extension, _Complex can be decomposed.
23 float decompose_complex(_Complex float cf) {
24 static _Complex float scf;
25 auto &[sre, sim] = scf;
26 // ok, this is references initialized by constant expressions all the way down
27 static_assert(&sre == &__real scf);
28 static_assert(&sim == &__imag scf);
30 auto [re, im] = cf;
31 return re*re + im*im;
34 // As a Clang extension, vector types can be decomposed.
35 typedef float vf3 __attribute__((ext_vector_type(3)));
36 float decompose_vector(vf3 v) {
37 auto [x, y, z] = v;
38 auto *p = &x; // expected-error {{address of vector element requested}}
39 return x + y + z;
42 struct S { int a, b; };
43 constexpr int f(S s) {
44 auto &[a, b] = s;
45 return a * 10 + b;
47 static_assert(f({1, 2}) == 12);
49 constexpr bool g(S &&s) {
50 auto &[a, b] = s;
51 return &a == &s.a && &b == &s.b && &a != &b;
53 static_assert(g({1, 2}));
55 auto [outer1, outer2] = S{1, 2};
56 void enclosing() {
57 struct S { int a = outer1; };
58 auto [n] = S(); // expected-note 2{{'n' declared here}}
60 struct Q { int f() { return n; } }; // expected-error {{reference to local binding 'n' declared in enclosing function}}
61 (void) [&] { return n; }; // expected-error {{reference to local binding 'n' declared in enclosing function}}
62 (void) [n] {}; // expected-error {{'n' in capture list does not name a variable}}
64 static auto [m] = S(); // expected-warning {{extension}}
65 struct R { int f() { return m; } };
66 (void) [&] { return m; };
67 (void) [m] {}; // expected-error {{'m' in capture list does not name a variable}}
70 void bitfield() {
71 struct { int a : 3, : 4, b : 5; } a;
72 auto &[x, y] = a;
73 auto &[p, q, r] = a; // expected-error-re {{type 'struct (unnamed struct at {{.*}})' decomposes into 2 elements, but 3 names were provided}}
76 void for_range() {
77 int x = 1;
78 for (auto[a, b] : x) { // expected-error {{invalid range expression of type 'int'; no viable 'begin' function available}}
79 a++;
82 int y[5];
83 for (auto[c] : y) { // expected-error {{cannot decompose non-class, non-array type 'int'}}
84 c++;
88 int error_recovery() {
89 auto [foobar]; // expected-error {{requires an initializer}}
90 return foobar_; // expected-error {{undeclared identifier 'foobar_'}}
93 // PR32172
94 template <class T> void dependent_foreach(T t) {
95 for (auto [a,b,c] : t)
96 a,b,c;
99 struct PR37352 {
100 int n;
101 void f() { static auto [a] = *this; } // expected-warning {{C++20 extension}}
104 namespace instantiate_template {
106 template <typename T1, typename T2>
107 struct pair {
108 T1 a;
109 T2 b;
112 const pair<int, int> &f1();
114 int f2() {
115 const auto &[a, b] = f1();
116 return a + b;
119 } // namespace instantiate_template
121 namespace lambdas {
122 void f() {
123 int n;
124 auto [a] = // expected-error {{cannot decompose lambda closure type}}
125 [n] {}; // expected-note {{lambda expression}}
128 auto [] = []{}; // expected-warning {{ISO C++17 does not allow a decomposition group to be empty}}
130 int g() {
131 int n = 0;
132 auto a = [=](auto &self) { // expected-note {{lambda expression}}
133 auto &[capture] = self; // expected-error {{cannot decompose lambda closure type}}
134 ++capture;
135 return n;
137 return a(a); // expected-note {{in instantiation of}}
140 int h() {
141 auto x = [] {};
142 struct A : decltype(x) {
143 int n;
145 auto &&[r] = A{x, 0}; // OK (presumably), non-capturing lambda has no non-static data members
146 return r;
149 int i() {
150 int n;
151 auto x = [n] {};
152 struct A : decltype(x) {
153 int n;
155 auto &&[r] = A{x, 0}; // expected-error-re {{cannot decompose class type 'A': both it and its base class 'decltype(x)' (aka '(lambda {{.*}})') have non-static data members}}
156 return r;
159 void j() {
160 auto x = [] {};
161 struct A : decltype(x) {};
162 auto &&[] = A{x}; // expected-warning {{ISO C++17 does not allow a decomposition group to be empty}}
166 // FIXME: by-value array copies