1762bb9d0ad20320b9f97a841dce57ba5e8e48b07Richard Smith// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
3ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithnamespace std {
4ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  template<typename T>
5ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    auto begin(T &&t) -> decltype(t.begin()) { return t.begin(); } // expected-note 4{{ignored: substitution failure}}
6ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  template<typename T>
7ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    auto end(T &&t) -> decltype(t.end()) { return t.end(); } // expected-note {{candidate template ignored: substitution failure [with T = }}
8ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
9ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  template<typename T>
10ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    auto begin(T &&t) -> decltype(t.alt_begin()) { return t.alt_begin(); } // expected-note {{selected 'begin' template [with T = }} \
11ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith                                                                              expected-note 4{{candidate template ignored: substitution failure [with T = }}
12ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  template<typename T>
13ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    auto end(T &&t) -> decltype(t.alt_end()) { return t.alt_end(); } // expected-note {{candidate template ignored: substitution failure [with T = }}
14ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
15ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  namespace inner {
16ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    // These should never be considered.
17ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    int begin(int);
18ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    int end(int);
19ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
20ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
21ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  using namespace inner;
22ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
23ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
2485ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redlstruct A { // expected-note 2 {{candidate constructor}}
25ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  A();
26ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  int *begin(); // expected-note 3{{selected 'begin' function with iterator type 'int *'}} expected-note {{'begin' declared here}}
27ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  int *end();
28ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith};
29ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
30ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstruct B {
31ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  B();
32ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  int *alt_begin();
33ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  int *alt_end();
34ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith};
35ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
369625e44c0252485277a340746ed8ac950686156fDouglas Gregorvoid f();
379625e44c0252485277a340746ed8ac950686156fDouglas Gregorvoid f(int);
38ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
39ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid g() {
40ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (int a : A())
41ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    A __begin;
42ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (char *a : A()) { // expected-error {{cannot initialize a variable of type 'char *' with an lvalue of type 'int'}}
43ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
44ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (char *a : B()) { // expected-error {{cannot initialize a variable of type 'char *' with an lvalue of type 'int'}}
45ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
46ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // FIXME: Terrible diagnostic here. auto deduction should fail, but does not!
479625e44c0252485277a340746ed8ac950686156fDouglas Gregor  for (double a : f) { // expected-error {{cannot use type '<overloaded function type>' as a range}}
48ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
49ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto a : A()) {
50ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
51ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto a : B()) {
52ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
53ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto *a : A()) { // expected-error {{variable 'a' with type 'auto *' has incompatible initializer of type 'int'}}
54ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
55ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // : is not a typo for :: here.
56ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (A NS:A()) { // expected-error {{no viable conversion from 'int' to 'A'}}
57ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
58ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto not_in_scope : not_in_scope) { // expected-error {{use of undeclared identifier 'not_in_scope'}}
59ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
60ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
61ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto a : A())
62ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    for (auto b : A()) {
63ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith      __range.begin(); // expected-error {{use of undeclared identifier '__range'}}
64ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith      ++__begin; // expected-error {{use of undeclared identifier '__begin'}}
65ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith      --__end; // expected-error {{use of undeclared identifier '__end'}}
66ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    }
67ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
68ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (char c : "test")
69ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    ;
70ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto a : f()) // expected-error {{cannot use type 'void' as a range}}
71ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    ;
72ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
73ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  extern int incomplete[];
74ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto a : incomplete) // expected-error {{cannot use incomplete type 'int []' as a range}}
75ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    ;
76ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  extern struct Incomplete also_incomplete[2]; // expected-note {{forward declaration}}
77ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto &a : also_incomplete) // expected-error {{cannot use incomplete type 'struct Incomplete [2]' as a range}}
78ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    ;
79ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
80ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  struct VoidBegin {
81ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    void begin(); // expected-note {{selected 'begin' function with iterator type 'void'}}
82ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    void end();
83ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  };
84ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto a : VoidBegin()) // expected-error {{cannot use type 'void' as an iterator}}
85ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    ;
86ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
87ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  struct null_t {
88ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    operator int*();
89ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  };
90ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  struct Differ {
91ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    int *begin(); // expected-note {{selected 'begin' function with iterator type 'int *'}}
92ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    null_t end(); // expected-note {{selected 'end' function with iterator type 'null_t'}}
93ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  };
94ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto a : Differ()) // expected-error {{'begin' and 'end' must return the same type (got 'int *' and 'null_t')}}
95ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    ;
96ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
97ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (void f() : "error") // expected-error {{for range declaration must declare a variable}}
98ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    ;
99ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
100ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (extern int a : A()) {} // expected-error {{loop variable 'a' may not be declared 'extern'}}
101ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (static int a : A()) {} // expected-error {{loop variable 'a' may not be declared 'static'}}
102ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (register int a : A()) {} // expected-error {{loop variable 'a' may not be declared 'register'}}
103c6d990a767150b02337de1136fdb55ccf349f4d1Richard Smith  for (constexpr int a : A()) {} // expected-error {{loop variable 'a' may not be declared 'constexpr'}}
104ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
105ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  struct NoBeginADL {
106ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    null_t alt_end();
107ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  };
108ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  struct NoEndADL {
109ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    null_t alt_begin();
110ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  };
111ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto u : NoBeginADL()) { // expected-error {{no matching function for call to 'begin'}} expected-note {{range has type 'NoBeginADL'}}
112ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
113ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto u : NoEndADL()) { // expected-error {{no matching function for call to 'end'}} expected-note {{range has type 'NoEndADL'}}
114ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
115ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
116ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  struct NoBegin {
117ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    null_t end();
118ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  };
119ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  struct NoEnd {
120ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    null_t begin();
121ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  };
122ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto u : NoBegin()) { // expected-error {{range type 'NoBegin' has 'end' member but no 'begin' member}}
123ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
124ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto u : NoEnd()) { // expected-error {{range type 'NoEnd' has 'begin' member but no 'end' member}}
125ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
126ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
127ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  struct NoIncr {
128ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    void *begin(); // expected-note {{selected 'begin' function with iterator type 'void *'}}
129ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    void *end();
130ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  };
13113b21be065e9feb0759303bd51b8e8653130f0fbChandler Carruth  for (auto u : NoIncr()) { // expected-error {{arithmetic on a pointer to void type}}
132ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
133ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
134ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  struct NoNotEq {
135ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    NoNotEq begin(); // expected-note {{selected 'begin' function with iterator type 'NoNotEq'}}
136ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    NoNotEq end();
137ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    void operator++();
138ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  };
139ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto u : NoNotEq()) { // expected-error {{invalid operands to binary expression}}
140ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
141ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
142ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  struct NoCopy {
143ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    NoCopy();
144ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    NoCopy(const NoCopy &) = delete;
145ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    int *begin();
146ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    int *end();
147ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  };
148ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (int n : NoCopy()) { // ok
149ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
150ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
151ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (int n : 42) { // expected-error {{no matching function for call to 'begin'}} \
152ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith                        expected-note {{range has type 'int'}}
153ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
154ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
155ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto a : *also_incomplete) { // expected-error {{cannot use incomplete type 'struct Incomplete' as a range}}
156ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
157ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
158ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
159ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithtemplate<typename T, typename U>
160ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid h(T t) {
161ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (U u : t) { // expected-error {{no viable conversion from 'A' to 'int'}}
162ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
163ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto u : t) {
164ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
165ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
166ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
167ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithtemplate void h<A, int>(A);
168ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithtemplate void h<A(&)[4], A &>(A(&)[4]);
169ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithtemplate void h<A(&)[13], A>(A(&)[13]);
170ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithtemplate void h<A(&)[13], int>(A(&)[13]); // expected-note {{requested here}}
171ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
172ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithtemplate<typename T>
173ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid i(T t) {
174ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto u : t) { // expected-error {{no matching function for call to 'begin'}} \
175ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith                        expected-error {{member function 'begin' not viable}} \
176ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith                        expected-note {{range has type}}
177ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
178ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
179ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithtemplate void i<A[13]>(A*); // expected-note {{requested here}}
180ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithtemplate void i<const A>(const A); // expected-note {{requested here}}
181ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
182ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithnamespace NS {
183ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  class ADL {};
184ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  int *begin(ADL); // expected-note {{no known conversion from 'NS::NoADL' to 'NS::ADL'}}
185ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  int *end(ADL);
186ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
187ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  class NoADL {};
188ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
189ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithint *begin(NS::NoADL);
190ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithint *end(NS::NoADL);
191ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
192ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstruct VoidBeginADL {};
193ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid begin(VoidBeginADL); // expected-note {{selected 'begin' function with iterator type 'void'}}
194ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid end(VoidBeginADL);
195ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
196ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid j() {
197ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto u : NS::ADL()) {
198ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
199ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto u : NS::NoADL()) { // expected-error {{no matching function for call to 'begin'}} expected-note {{range has type}}
200ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
201ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (auto a : VoidBeginADL()) { // expected-error {{cannot use type 'void' as an iterator}}
202ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
203ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
204ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
205ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid example() {
206ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  int array[5] = { 1, 2, 3, 4, 5 };
207ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  for (int &x : array)
208ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    x *= 2;
209ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
210