1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
3 // RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
8 for (int& it : x) { // expected-error {{invalid range expression of type 'int'; no viable 'begin' function available}}
19 auto begin(T &&t) -> decltype(t.begin()) { return t.begin(); } // expected-note 2{{ignored: substitution failure}}
21 auto end(T &&t) -> decltype(t.end()) { return t.end(); } // expected-note {{candidate template ignored: substitution failure [with T = }}
24 auto begin(T &&t) -> decltype(t.alt_begin()) { return t.alt_begin(); } // expected-note {{selected 'begin' template [with T = }} \
25 expected-note 2{{candidate template ignored: substitution failure [with T = }}
27 auto end(T &&t) -> decltype(t.alt_end()) { return t.alt_end(); } // expected-note {{candidate template ignored: substitution failure [with T = }}
30 // These should never be considered.
35 using namespace inner;
37 struct A { // expected-note 2 {{candidate constructor}}
39 int *begin(); // expected-note 3{{selected 'begin' function with iterator type 'int *'}} expected-note {{'begin' declared here}}
60 operator int &() { return val; }
66 constexpr int operator*(const C::It &) { return 0; }
77 for (char *a : A()) { // expected-error {{cannot initialize a variable of type 'char *' with an lvalue of type 'int'}}
79 for (char *a : X::B()) { // expected-error {{cannot initialize a variable of type 'char *' with an lvalue of type 'int'}}
81 // FIXME: Terrible diagnostic here. auto deduction should fail, but does not!
82 for (double a : f) { // expected-error {{cannot use type '<overloaded function type>' as a range}}
86 for (auto a : X::B()) {
88 for (auto *a : A()) { // expected-error {{variable 'a' with type 'auto *' has incompatible initializer of type 'int'}}
90 // : is not a typo for :: here.
91 for (A NS:A()) { // expected-error {{no viable conversion from 'int' to 'X::A'}}
93 for (auto not_in_scope : not_in_scope) { // expected-error {{use of undeclared identifier 'not_in_scope'}}
98 __range.begin(); // expected-error {{use of undeclared identifier '__range'}}
99 ++__begin; // expected-error {{use of undeclared identifier '__begin'}}
100 --__end; // expected-error {{use of undeclared identifier '__end'}}
103 for (char c : "test")
105 for (auto a : f()) // expected-error {{cannot use type 'void' as a range}}
108 extern int incomplete[];
109 for (auto a : incomplete) // expected-error {{cannot use incomplete type 'int []' as a range}}
111 extern struct Incomplete also_incomplete[2]; // expected-note 2{{forward declaration}}
112 for (auto &a : also_incomplete) // expected-error {{cannot use incomplete type 'struct Incomplete [2]' as a range}}
116 void begin(); // expected-note {{selected 'begin' function with iterator type 'void'}}
119 for (auto a : VoidBegin()) // expected-error {{cannot use type 'void' as an iterator}}
126 for (auto a : Differ())
127 #if __cplusplus <= 201402L
128 // expected-warning@-2 {{'begin' and 'end' returning different types ('int *' and 'null_t') is a C++17 extension}}
129 // expected-note@-6 {{selected 'begin' function with iterator type 'int *'}}
130 // expected-note@-6 {{selected 'end' function with iterator type 'null_t'}}
134 for (void f() : "error") // expected-error {{for range declaration must declare a variable}}
137 for (extern int a : A()) {} // expected-error {{loop variable 'a' may not be declared 'extern'}}
138 for (static int a : A()) {} // expected-error {{loop variable 'a' may not be declared 'static'}}
139 for (register int a : A()) {} // expected-error {{loop variable 'a' may not be declared 'register'}} expected-warning 0-1{{register}} expected-error 0-1{{register}}
140 for (constexpr int a : X::C()) {} // OK per CWG issue #1204.
142 for (auto u : X::NoBeginADL()) { // expected-error {{invalid range expression of type 'X::NoBeginADL'; no viable 'begin' function available}}
144 for (auto u : X::NoEndADL()) { // expected-error {{invalid range expression of type 'X::NoEndADL'; no viable 'end' function available}}
153 for (auto u : NoBegin()) { // expected-error {{no viable 'begin' function available}}
155 for (auto u : NoEnd()) { // expected-error {{no viable 'end' function available}}
159 void *begin(); // expected-note {{selected 'begin' function with iterator type 'void *'}}
162 for (auto u : NoIncr()) { // expected-error {{arithmetic on a pointer to void}}\
163 expected-note {{in implicit call to 'operator++' for iterator of type 'NoIncr'}}
167 NoNotEq begin(); // expected-note {{selected 'begin' function with iterator type 'NoNotEq'}}
171 for (auto u : NoNotEq()) { // expected-error {{invalid operands to binary expression}}\
172 expected-note {{in implicit call to 'operator!=' for iterator of type 'NoNotEq'}}
176 NoDeref begin(); // expected-note {{selected 'begin' function}}
179 bool operator!=(NoDeref &);
182 for (auto u : NoDeref()) { // expected-error {{indirection requires pointer operand}} \
183 expected-note {{in implicit call to 'operator*' for iterator of type 'NoDeref'}}
188 NoCopy(const NoCopy &) = delete;
192 for (int n : NoCopy()) { // ok
195 for (int n : 42) { // expected-error {{invalid range expression of type 'int'; no viable 'begin' function available}}
198 for (auto a : *also_incomplete) { // expected-error {{cannot use incomplete type 'struct Incomplete' as a range}}
202 template<typename T, typename U>
204 for (U u : t) { // expected-error {{no viable conversion from 'X::A' to 'int'}}
210 template void h<A, int>(A);
211 template void h<A(&)[4], A &>(A(&)[4]);
212 template void h<A(&)[13], A>(A(&)[13]);
213 template void h<A(&)[13], int>(A(&)[13]); // expected-note {{requested here}}
217 for (auto u : t) { // expected-error {{invalid range expression of type 'X::A *'; no viable 'begin' function available}} \
218 expected-error {{'this' argument to member function 'begin' has type 'const X::A', but function is not marked const}} \
219 expected-note {{when looking up 'begin' function}}
223 template void i<A[13]>(A*); // expected-note {{requested here}}
224 template void i<const A>(const A); // expected-note {{requested here}}
226 struct StdBeginEnd {};
228 int *begin(StdBeginEnd);
229 int *end(StdBeginEnd);
232 for (auto a : StdBeginEnd()) {} // expected-error {{invalid range expression of type 'StdBeginEnd'; no viable 'begin'}}
237 int *begin(ADL); // expected-note {{no known conversion from 'NS::NoADL' to 'NS::ADL'}}
242 int *begin(NS::NoADL);
245 struct VoidBeginADL {};
246 void begin(VoidBeginADL); // expected-note {{selected 'begin' function with iterator type 'void'}}
247 void end(VoidBeginADL);
250 for (auto u : NS::ADL()) {
252 for (auto u : NS::NoADL()) { // expected-error {{invalid range expression of type 'NS::NoADL'; no viable 'begin' function available}}
254 for (auto a : VoidBeginADL()) { // expected-error {{cannot use type 'void' as an iterator}}
260 int array[5] = { 1, 2, 3, 4, 5 };
265 namespace rdar13712739 {
267 void foo(const T& t) {
268 auto &x = t.get(); // expected-error{{member reference base type 'const int' is not a structure or union}}
269 for (auto &blah : x) { }
272 template void foo(const int&); // expected-note{{in instantiation of function template specialization}}
308 void begin(); // expected-note {{member is not a candidate because range type 'p0962r1::NE::E' has no 'end' member}}
315 void end(); // expected-note {{member is not a candidate because range type 'p0962r1::NF::F' has no 'begin' member}}
320 void use(NA::A a, NB::B b, NC::C c, ND::D d, NE::E e, NF::F f) {
323 for (auto x : c) {} // expected-error {{no viable 'end' function}}
324 for (auto x : d) {} // expected-error {{no viable 'begin' function}}
325 for (auto x : e) {} // expected-error {{no viable 'begin' function}}
326 for (auto x : f) {} // expected-error {{no viable 'end' function}}