1 // RUN: %clang_cc1 -std=c++17 -verify %s
2 
use_from_own_init()3 void use_from_own_init() {
4   auto [a] = a; // expected-error {{binding 'a' cannot appear in the initializer of its own decomposition declaration}}
5 }
6 
num_elems()7 void num_elems() {
8   struct A0 {} a0;
9   int a1[1], a2[2];
10 
11   auto [] = a0; // expected-warning {{does not allow a decomposition group to be empty}}
12   auto [v1] = a0; // expected-error {{type '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}}
20 }
21 
22 // As a Clang extension, _Complex can be decomposed.
decompose_complex(_Complex float cf)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);
29 
30   auto [re, im] = cf;
31   return re*re + im*im;
32 }
33 
34 // As a Clang extension, vector types can be decomposed.
35 typedef float vf3 __attribute__((ext_vector_type(3)));
decompose_vector(vf3 v)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;
40 }
41 
42 struct S { int a, b; };
f(S s)43 constexpr int f(S s) {
44   auto &[a, b] = s;
45   return a * 10 + b;
46 }
47 static_assert(f({1, 2}) == 12);
48 
g(S && s)49 constexpr bool g(S &&s) {
50   auto &[a, b] = s;
51   return &a == &s.a && &b == &s.b && &a != &b;
52 }
53 static_assert(g({1, 2}));
54 
55 auto [outer1, outer2] = S{1, 2};
enclosing()56 void enclosing() {
57   struct S { int a = outer1; };
58   auto [n] = S(); // expected-note 2{{'n' declared here}}
59 
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}}
63 
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}}
68 }
69 
bitfield()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 {{decomposes into 2 elements, but 3 names were provided}}
74 }
75 
for_range()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++;
80   }
81 
82   int y[5];
83   for (auto[c] : y) { // expected-error {{cannot decompose non-class, non-array type 'int'}}
84     c++;
85   }
86 }
87 
error_recovery()88 int error_recovery() {
89   auto [foobar]; // expected-error {{requires an initializer}}
90   return foobar_; // expected-error {{undeclared identifier 'foobar_'}}
91 }
92 
93 // PR32172
dependent_foreach(T t)94 template <class T> void dependent_foreach(T t) {
95   for (auto [a,b,c] : t)
96     a,b,c;
97 }
98 
99 struct PR37352 {
100   int n;
fPR37352101   void f() { static auto [a] = *this; } // expected-warning {{C++20 extension}}
102 };
103 
104 namespace instantiate_template {
105 
106 template <typename T1, typename T2>
107 struct pair {
108   T1 a;
109   T2 b;
110 };
111 
112 const pair<int, int> &f1();
113 
f2()114 int f2() {
115   const auto &[a, b] = f1();
116   return a + b;
117 }
118 
119 } // namespace instantiate_template
120 
121 namespace lambdas {
f()122   void f() {
123     int n;
124     auto [a] =  // expected-error {{cannot decompose lambda closure type}}
125         [n] {}; // expected-note {{lambda expression}}
126   }
127 
__anonfd50df4a0702null128   auto [] = []{}; // expected-warning {{ISO C++17 does not allow a decomposition group to be empty}}
129 
g()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;
136     };
137     return a(a); // expected-note {{in instantiation of}}
138   }
139 
h()140   int h() {
141     auto x = [] {};
142     struct A : decltype(x) {
143       int n;
144     };
145     auto &&[r] = A{x, 0}; // OK (presumably), non-capturing lambda has no non-static data members
146     return r;
147   }
148 
i()149   int i() {
150     int n;
151     auto x = [n] {};
152     struct A : decltype(x) {
153       int n;
154     };
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;
157   }
158 
j()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}}
163   }
164 }
165 
166 // FIXME: by-value array copies
167