Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / sugared-auto.cpp
blob5fdfb09689b667286f7e4df215e71a842d5010ab
1 // RUN: %clang_cc1 -fsyntax-only -verify -xobjective-c++ %s -std=c++20 -fms-extensions -fblocks -fobjc-arc -fobjc-runtime-has-weak -fenable-matrix -Wno-dynamic-exception-spec -Wno-c++17-compat-mangling
2 // RUN: %clang_cc1 -fsyntax-only -verify -xobjective-c++ %s -std=c++14 -fms-extensions -fblocks -fobjc-arc -fobjc-runtime-has-weak -fenable-matrix -Wno-dynamic-exception-spec -Wno-c++17-compat-mangling
4 namespace std {
5 template<typename T> struct initializer_list {
6 const T *begin, *end;
7 initializer_list();
8 };
9 } // namespace std
11 enum class N {};
13 using Animal = int;
15 using AnimalPtr = Animal *;
17 using Man = Animal;
18 using Dog = Animal;
20 using ManPtr = Man *;
21 using DogPtr = Dog *;
23 using SocratesPtr = ManPtr;
25 using ConstMan = const Man;
26 using ConstDog = const Dog;
28 using Virus = void;
29 using SARS = Virus;
30 using Ebola = Virus;
32 using Bacteria = float;
33 using Bacilli = Bacteria;
34 using Vibrio = Bacteria;
36 struct Plant;
37 using Gymnosperm = Plant;
38 using Angiosperm = Plant;
40 namespace variable {
42 auto x1 = Animal();
43 N t1 = x1; // expected-error {{lvalue of type 'Animal' (aka 'int')}}
45 auto x2 = AnimalPtr();
46 N t2 = x2; // expected-error {{lvalue of type 'AnimalPtr' (aka 'int *')}}
48 auto *x3 = AnimalPtr();
49 N t3 = x3; // expected-error {{lvalue of type 'Animal *' (aka 'int *')}}
51 // Each variable deduces separately.
52 auto x4 = Man(), x5 = Dog();
53 N t4 = x4; // expected-error {{lvalue of type 'Man' (aka 'int')}}
54 N t5 = x5; // expected-error {{lvalue of type 'Dog' (aka 'int')}}
56 auto x6 = { Man(), Dog() };
57 N t6 = x6; // expected-error {{from 'std::initializer_list<Animal>' (aka 'initializer_list<int>')}}
59 } // namespace variable
61 namespace function_basic {
63 auto f1() { return Animal(); }
64 auto x1 = f1();
65 N t1 = x1; // expected-error {{lvalue of type 'Animal' (aka 'int')}}
67 decltype(auto) f2() { return Animal(); }
68 auto x2 = f2();
69 N t2 = x2; // expected-error {{lvalue of type 'Animal' (aka 'int')}}
71 auto x3 = [a = Animal()] { return a; }();
72 N t3 = x3; // expected-error {{lvalue of type 'Animal' (aka 'int')}}
74 } // namespace function_basic
76 namespace function_multiple_basic {
78 N t1 = [] { // expected-error {{rvalue of type 'Animal' (aka 'int')}}
79 if (true)
80 return Man();
81 return Dog();
82 }();
84 N t2 = []() -> decltype(auto) { // expected-error {{rvalue of type 'Animal' (aka 'int')}}
85 if (true)
86 return Man();
87 return Dog();
88 }();
90 N t3 = [] { // expected-error {{rvalue of type 'Animal' (aka 'int')}}
91 if (true)
92 return Dog();
93 auto x = Man();
94 return x;
95 }();
97 N t4 = [] { // expected-error {{rvalue of type 'int'}}
98 if (true)
99 return Dog();
100 return 1;
101 }();
103 N t5 = [] { // expected-error {{rvalue of type 'Virus' (aka 'void')}}
104 if (true)
105 return Ebola();
106 return SARS();
107 }();
109 N t6 = [] { // expected-error {{rvalue of type 'void'}}
110 if (true)
111 return SARS();
112 return;
113 }();
115 } // namespace function_multiple_basic
117 #define TEST_AUTO(X, A, B) \
118 static_assert(__is_same(A, B), ""); \
119 auto X(A a, B b) { \
120 if (0) \
121 return a; \
122 if (0) \
123 return b; \
124 return N(); \
126 #define TEST_DAUTO(X, A, B) \
127 static_assert(__is_same(A, B), ""); \
128 decltype(auto) X(A a, B b) { \
129 if (0) \
130 return static_cast<A>(a); \
131 if (0) \
132 return static_cast<B>(b); \
133 return N(); \
136 namespace misc {
138 TEST_AUTO(t1, ManPtr, DogPtr) // expected-error {{but deduced as 'Animal *' (aka 'int *')}}
139 TEST_AUTO(t2, ManPtr, int *) // expected-error {{but deduced as 'int *'}}
140 TEST_AUTO(t3, SocratesPtr, ManPtr) // expected-error {{but deduced as 'ManPtr' (aka 'int *')}}
142 TEST_AUTO(t4, _Atomic(Man), _Atomic(Dog)) // expected-error {{but deduced as '_Atomic(Animal)'}}
144 using block_man = void (^)(Man);
145 using block_dog = void (^)(Dog);
146 TEST_AUTO(t5, block_man, block_dog) // expected-error {{but deduced as 'void (^__strong)(Animal)'}}
148 #if __cplusplus >= 201500
149 using fp1 = SARS (*)(Man, DogPtr) throw(Vibrio);
150 using fp2 = Ebola (*)(Dog, ManPtr) throw(Bacilli);
151 TEST_AUTO(t6, fp1, fp2); // expected-error {{but deduced as 'Virus (*)(Animal, Animal *) throw(Bacteria)' (aka 'void (*)(int, int *) throw(Bacteria)')}}
153 using fp3 = SARS (*)() throw(Man);
154 using fp4 = Ebola (*)() throw(Vibrio);
155 auto t7(fp3 a, fp4 b) {
156 if (false)
157 return true ? a : b;
158 if (false)
159 return a;
160 return N(); // expected-error {{but deduced as 'Virus (*)() throw(Man, Vibrio)' (aka 'void (*)() throw(Man, Vibrio)')}}
162 #endif
164 using fp5 = void (*)(const Man);
165 using fp6 = void (*)(Dog);
166 TEST_AUTO(t8, fp5, fp6); // expected-error {{but deduced as 'void (*)(Animal)' (aka 'void (*)(int)')}}
168 using fp7 = void (*)(ConstMan);
169 using fp8 = void (*)(ConstDog);
170 TEST_AUTO(t9, fp7, fp8); // expected-error {{but deduced as 'void (*)(const Animal)' (aka 'void (*)(const int)')}}
172 using fp9 = void (*)(ConstMan);
173 using fp10 = void (*)(const Dog);
174 TEST_AUTO(t10, fp9, fp10); // expected-error {{but deduced as 'void (*)(const Animal)' (aka 'void (*)(const int)')}}
176 using fp11 = void (*)(__strong block_man);
177 using fp12 = void (*)(__weak block_dog);
178 TEST_AUTO(t11, fp11, fp12); // expected-error {{but deduced as 'void (*)(void (^)(Animal))'}}
180 TEST_AUTO(t12, Man Angiosperm::*, Dog Gymnosperm::*) // expected-error {{but deduced as 'Animal Plant::*'}}
182 TEST_DAUTO(t13, const Man &, const Dog &) // expected-error {{but deduced as 'const Animal &' (aka 'const int &')}}
184 TEST_DAUTO(t14, Man &&, Dog &&) // expected-error {{but deduced as 'Animal &&' (aka 'int &&')}}
186 using matrix_man = Man __attribute__((matrix_type(4, 4)));
187 using matrix_dog = Dog __attribute__((matrix_type(4, 4)));
188 TEST_AUTO(t15, matrix_man, matrix_dog) // expected-error {{but deduced as 'Animal __attribute__((matrix_type(4, 4)))'}}
190 using vector_man = Man __attribute__((vector_size(4)));
191 using vector_dog = Dog __attribute__((vector_size(4)));
192 TEST_AUTO(t16, vector_man, vector_dog) // expected-error {{but deduced as '__attribute__((__vector_size__(1 * sizeof(Animal)))) Animal' (vector of 1 'Animal' value)}}
194 using ext_vector_man = Man __attribute__((ext_vector_type(4)));
195 using ext_vector_dog = Dog __attribute__((ext_vector_type(4)));
196 TEST_AUTO(t17, ext_vector_man, ext_vector_dog) // expected-error {{but deduced as 'Animal __attribute__((ext_vector_type(4)))' (vector of 4 'Animal' values)}}
198 using TwoDogs = Dog[2];
199 using ConstTwoDogsPtr = const TwoDogs*;
200 using ConstTwoMenPtr = const Man(*)[2];
201 TEST_AUTO(t18, ConstTwoDogsPtr, ConstTwoMenPtr); // expected-error {{but deduced as 'const Animal (*)[2]' (aka 'const int (*)[2]')}}
203 } // namespace misc
205 namespace exception_spec {
207 void none();
208 void dyn_none() throw();
209 void dyn() throw(int);
210 void ms_any() throw(...);
211 void __declspec(nothrow) nothrow();
212 void noexcept_basic() noexcept;
213 void noexcept_true() noexcept(true);
214 void noexcept_false() noexcept(false);
216 #if __cplusplus < 201500
217 TEST_AUTO(t1, decltype(&noexcept_false), decltype(&noexcept_true)) // expected-error {{but deduced as 'void (*)() noexcept(false)'}}
218 TEST_AUTO(t2, decltype(&noexcept_basic), decltype(&noexcept_true)) // expected-error {{but deduced as 'void (*)() noexcept(true)'}}
219 TEST_AUTO(t3, decltype(&none), decltype(&ms_any)) // expected-error {{but deduced as 'void (*)()'}}
220 TEST_AUTO(t4, decltype(&noexcept_false), decltype(&ms_any)) // expected-error {{but deduced as 'void (*)() throw(...)'}}
221 TEST_AUTO(t5, decltype(&nothrow), decltype(&noexcept_false)) // expected-error {{but deduced as 'void (*)() noexcept(false)'}}
222 TEST_AUTO(t6, decltype(&dyn_none), decltype(&nothrow)) // expected-error {{but deduced as 'void (*)() throw()'}}
223 TEST_AUTO(t7, decltype(&noexcept_true), decltype(&dyn)) // expected-error {{but deduced as 'void (*)() throw(int)'}}
224 #endif
225 } // namespace exception_spec
227 namespace non_deduced {
228 void f();
229 void g();
230 void g(int);
231 auto h() {
232 if (false) return f;
233 return g;
234 // expected-error@-1 {{returned value of type '<overloaded function type>'}}
236 } // namespace non_deduced