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; }
76 #if __cplusplus >= 201703L
77 for (extern auto [x
, y
] : D()) {
78 } // expected-error@-1 {{decomposition declaration cannot be declared 'extern'}}
79 // expected-error@-2 {{loop variable '[x, y]' may not be declared 'extern'}}
92 for (char *a
: A()) { // expected-error {{cannot initialize a variable of type 'char *' with an lvalue of type 'int'}}
94 for (char *a
: X::B()) { // expected-error {{cannot initialize a variable of type 'char *' with an lvalue of type 'int'}}
96 // FIXME: Terrible diagnostic here. auto deduction should fail, but does not!
97 for (double a
: f
) { // expected-error {{cannot use type '<overloaded function type>' as a range}}
101 for (auto a
: X::B()) {
103 for (auto *a
: A()) { // expected-error {{variable 'a' with type 'auto *' has incompatible initializer of type 'int'}}
105 // : is not a typo for :: here.
106 for (A NS
:A()) { // expected-error {{no viable conversion from 'int' to 'A'}}
108 for (auto not_in_scope
: not_in_scope
) { // expected-error {{use of undeclared identifier 'not_in_scope'}}
113 __range
.begin(); // expected-error {{use of undeclared identifier '__range'}}
114 ++__begin
; // expected-error {{use of undeclared identifier '__begin'}}
115 --__end
; // expected-error {{use of undeclared identifier '__end'}}
118 for (char c
: "test")
120 for (auto a
: f()) // expected-error {{cannot use type 'void' as a range}}
123 extern int incomplete
[];
124 for (auto a
: incomplete
) // expected-error {{cannot use incomplete type 'int[]' as a range}}
126 extern struct Incomplete also_incomplete
[2]; // expected-note 2{{forward declaration}}
127 for (auto &a
: also_incomplete
) // expected-error {{cannot use incomplete type 'struct Incomplete[2]' as a range}}
131 void begin(); // expected-note {{selected 'begin' function with iterator type 'void'}}
134 for (auto a
: VoidBegin()) // expected-error {{cannot use type 'void' as an iterator}}
141 for (auto a
: Differ())
142 #if __cplusplus <= 201402L
143 // expected-warning@-2 {{'begin' and 'end' returning different types ('int *' and 'null_t') is a C++17 extension}}
144 // expected-note@-6 {{selected 'begin' function with iterator type 'int *'}}
145 // expected-note@-6 {{selected 'end' function with iterator type 'null_t'}}
149 for (void f() : "error") // expected-error {{for range declaration must declare a variable}}
152 for (extern int a
: A()) {} // expected-error {{loop variable 'a' may not be declared 'extern'}}
153 for (static int a
: A()) {} // expected-error {{loop variable 'a' may not be declared 'static'}}
154 for (thread_local
int a
: A()) {} // expected-error {{loop variable 'a' may not be declared 'thread_local'}}
155 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}}
156 for (constexpr int a
: X::C()) {} // OK per CWG issue #1204.
158 for (auto u
: X::NoBeginADL()) { // expected-error {{invalid range expression of type 'X::NoBeginADL'; no viable 'begin' function available}}
160 for (auto u
: X::NoEndADL()) { // expected-error {{invalid range expression of type 'X::NoEndADL'; no viable 'end' function available}}
169 for (auto u
: NoBegin()) { // expected-error {{no viable 'begin' function available}}
171 for (auto u
: NoEnd()) { // expected-error {{no viable 'end' function available}}
175 void *begin(); // expected-note {{selected 'begin' function with iterator type 'void *'}}
178 for (auto u
: NoIncr()) { // expected-error {{arithmetic on a pointer to void}}\
179 expected
-note
{{in implicit call to
'operator++' for iterator of type
'NoIncr'}}
183 NoNotEq
begin(); // expected-note {{selected 'begin' function with iterator type 'NoNotEq'}}
187 for (auto u
: NoNotEq()) { // expected-error {{invalid operands to binary expression}}\
188 expected
-note
{{in implicit call to
'operator!=' for iterator of type
'NoNotEq'}}
192 NoDeref
begin(); // expected-note {{selected 'begin' function}}
195 bool operator!=(NoDeref
&);
198 for (auto u
: NoDeref()) { // expected-error {{indirection requires pointer operand}} \
199 expected
-note
{{in implicit call to
'operator*' for iterator of type
'NoDeref'}}
204 NoCopy(const NoCopy
&) = delete;
208 for (int n
: NoCopy()) { // ok
211 for (int n
: 42) { // expected-error {{invalid range expression of type 'int'; no viable 'begin' function available}}
214 for (auto a
: *also_incomplete
) { // expected-error {{cannot use incomplete type 'struct Incomplete' as a range}}
218 template<typename T
, typename U
>
220 for (U u
: t
) { // expected-error {{no viable conversion from 'X::A' to 'int'}}
226 template void h
<A
, int>(A
);
227 template void h
<A(&)[4], A
&>(A(&)[4]);
228 template void h
<A(&)[13], A
>(A(&)[13]);
229 template void h
<A(&)[13], int>(A(&)[13]); // expected-note {{requested here}}
233 for (auto u
: t
) { // expected-error {{invalid range expression of type 'X::A *'; no viable 'begin' function available}} \
234 expected
-error
{{'this' argument to member function
'begin' has type
'const X::A', but function is
not marked
const}} \
235 expected
-note
{{when looking up
'begin' function
}}
239 template void i
<A
[13]>(A
*); // expected-note {{requested here}}
240 template void i
<const A
>(const A
); // expected-note {{requested here}}
242 struct StdBeginEnd
{};
244 int *begin(StdBeginEnd
);
245 int *end(StdBeginEnd
);
248 for (auto a
: StdBeginEnd()) {} // expected-error {{invalid range expression of type 'StdBeginEnd'; no viable 'begin'}}
253 int *begin(ADL
); // expected-note {{no known conversion from 'NS::NoADL' to 'ADL'}}
258 int *begin(NS::NoADL
);
261 struct VoidBeginADL
{};
262 void begin(VoidBeginADL
); // expected-note {{selected 'begin' function with iterator type 'void'}}
263 void end(VoidBeginADL
);
266 for (auto u
: NS::ADL()) {
268 for (auto u
: NS::NoADL()) { // expected-error {{invalid range expression of type 'NS::NoADL'; no viable 'begin' function available}}
270 for (auto a
: VoidBeginADL()) { // expected-error {{cannot use type 'void' as an iterator}}
276 int array
[5] = { 1, 2, 3, 4, 5 };
281 namespace rdar13712739
{
283 void foo(const T
& t
) {
284 auto &x
= t
.get(); // expected-error{{member reference base type 'const int' is not a structure or union}}
285 for (auto &blah
: x
) { }
288 template void foo(const int&); // expected-note{{in instantiation of function template specialization}}
324 void begin(); // expected-note {{member is not a candidate because range type 'NE::E' has no 'end' member}}
331 void end(); // expected-note {{member is not a candidate because range type 'NF::F' has no 'begin' member}}
336 void use(NA::A a
, NB::B b
, NC::C c
, ND::D d
, NE::E e
, NF::F f
) {
339 for (auto x
: c
) {} // expected-error {{invalid range expression of type 'NC::C'; no viable 'end' function available}}
340 for (auto x
: d
) {} // expected-error {{invalid range expression of type 'ND::D'; no viable 'begin' function available}}
341 for (auto x
: e
) {} // expected-error {{invalid range expression of type 'NE::E'; no viable 'begin' function available}}
342 for (auto x
: f
) {} // expected-error {{invalid range expression of type 'NF::F'; no viable 'end' function available}}