xref: /llvm-project/clang/test/SemaTemplate/concepts-recovery-expr.cpp (revision 141de749597c7b59ebe2c4aa7ee573d124dc903c)
1684a7896SErich Keane // RUN: %clang_cc1 -std=c++20 -verify %s
2684a7896SErich Keane 
3684a7896SErich Keane // expected-error@+1{{use of undeclared identifier 'b'}}
4684a7896SErich Keane constexpr bool CausesRecoveryExpr = b;
5684a7896SErich Keane 
6684a7896SErich Keane template<typename T>
7684a7896SErich Keane concept ReferencesCRE = CausesRecoveryExpr;
8684a7896SErich Keane 
9684a7896SErich Keane template<typename T> requires CausesRecoveryExpr // #NVC1REQ
NoViableCands1()10684a7896SErich Keane void NoViableCands1(){} // #NVC1
11684a7896SErich Keane 
12684a7896SErich Keane template<typename T> requires ReferencesCRE<T> // #NVC2REQ
NoViableCands2()13684a7896SErich Keane void NoViableCands2(){} // #NVC2
14684a7896SErich Keane 
15684a7896SErich Keane template<ReferencesCRE T> // #NVC3REQ
NoViableCands3()16684a7896SErich Keane void NoViableCands3(){} // #NVC3
17684a7896SErich Keane 
NVCUse()18684a7896SErich Keane void NVCUse() {
19684a7896SErich Keane   NoViableCands1<int>();
20684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'NoViableCands1'}}
21684a7896SErich Keane   // expected-note@#NVC1{{candidate template ignored: constraints not satisfied}}
22684a7896SErich Keane   // expected-note@#NVC1REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
23684a7896SErich Keane 
24684a7896SErich Keane   NoViableCands2<int>();
25684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'NoViableCands2'}}
26684a7896SErich Keane   // expected-note@#NVC2{{candidate template ignored: constraints not satisfied}}
27684a7896SErich Keane   // expected-note@#NVC2REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
28684a7896SErich Keane   NoViableCands3<int>();
29684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'NoViableCands3'}}
30684a7896SErich Keane   // expected-note@#NVC3{{candidate template ignored: constraints not satisfied}}
31684a7896SErich Keane   // expected-note@#NVC3REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
32684a7896SErich Keane }
33684a7896SErich Keane 
34684a7896SErich Keane template<typename T> requires CausesRecoveryExpr // #OVC1REQ
OtherViableCands1()35684a7896SErich Keane void OtherViableCands1(){} // #OVC1
36684a7896SErich Keane 
37684a7896SErich Keane template<typename T>
OtherViableCands1()38684a7896SErich Keane void OtherViableCands1(){} // #OVC1_ALT
39684a7896SErich Keane 
40684a7896SErich Keane template<typename T> requires ReferencesCRE<T> // #OVC2REQ
OtherViableCands2()41684a7896SErich Keane void OtherViableCands2(){} // #OVC2
42684a7896SErich Keane 
43684a7896SErich Keane template<typename T>
OtherViableCands2()44684a7896SErich Keane void OtherViableCands2(){} // #OVC2_ALT
45684a7896SErich Keane 
46684a7896SErich Keane template<ReferencesCRE T> // #OVC3REQ
OtherViableCands3()47684a7896SErich Keane void OtherViableCands3(){} // #OVC3
48684a7896SErich Keane template<typename T>
OtherViableCands3()49684a7896SErich Keane void OtherViableCands3(){} // #OVC3_ALT
50684a7896SErich Keane 
OVCUse()51684a7896SErich Keane void OVCUse() {
52684a7896SErich Keane   OtherViableCands1<int>();
53684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'OtherViableCands1'}}
54684a7896SErich Keane   // expected-note@#OVC1_ALT {{candidate function}}
55684a7896SErich Keane   // expected-note@#OVC1 {{candidate template ignored: constraints not satisfied}}
56684a7896SErich Keane   // expected-note@#OVC1REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
57684a7896SErich Keane   OtherViableCands2<int>();
58684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'OtherViableCands2'}}
59684a7896SErich Keane   // expected-note@#OVC2_ALT {{candidate function}}
60684a7896SErich Keane   // expected-note@#OVC2 {{candidate template ignored: constraints not satisfied}}
61684a7896SErich Keane   // expected-note@#OVC2REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
62684a7896SErich Keane   OtherViableCands3<int>();
63684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'OtherViableCands3'}}
64684a7896SErich Keane   // expected-note@#OVC3_ALT {{candidate function}}
65684a7896SErich Keane   // expected-note@#OVC3 {{candidate template ignored: constraints not satisfied}}
66684a7896SErich Keane   // expected-note@#OVC3REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
67684a7896SErich Keane }
68684a7896SErich Keane 
69684a7896SErich Keane template<typename T> requires CausesRecoveryExpr // #OBNVC1REQ
OtherBadNoViableCands1()70684a7896SErich Keane void OtherBadNoViableCands1(){} // #OBNVC1
71684a7896SErich Keane 
72684a7896SErich Keane template<typename T> requires false // #OBNVC1REQ_ALT
OtherBadNoViableCands1()73684a7896SErich Keane void OtherBadNoViableCands1(){} // #OBNVC1_ALT
74684a7896SErich Keane 
75684a7896SErich Keane template<typename T> requires ReferencesCRE<T> // #OBNVC2REQ
OtherBadNoViableCands2()76684a7896SErich Keane void OtherBadNoViableCands2(){} // #OBNVC2
77684a7896SErich Keane 
78684a7896SErich Keane template<typename T> requires false// #OBNVC2REQ_ALT
OtherBadNoViableCands2()79684a7896SErich Keane void OtherBadNoViableCands2(){} // #OBNVC2_ALT
80684a7896SErich Keane 
81684a7896SErich Keane template<ReferencesCRE T> // #OBNVC3REQ
OtherBadNoViableCands3()82684a7896SErich Keane void OtherBadNoViableCands3(){} // #OBNVC3
83684a7896SErich Keane template<typename T> requires false // #OBNVC3REQ_ALT
OtherBadNoViableCands3()84684a7896SErich Keane void OtherBadNoViableCands3(){} // #OBNVC3_ALT
85684a7896SErich Keane 
OBNVCUse()86684a7896SErich Keane void OBNVCUse() {
87684a7896SErich Keane   OtherBadNoViableCands1<int>();
88684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'OtherBadNoViableCands1'}}
89684a7896SErich Keane   // expected-note@#OBNVC1_ALT {{candidate template ignored: constraints not satisfied}}
90684a7896SErich Keane   // expected-note@#OBNVC1REQ_ALT {{because 'false' evaluated to false}}
91684a7896SErich Keane   // expected-note@#OBNVC1 {{candidate template ignored: constraints not satisfied}}
92684a7896SErich Keane   // expected-note@#OBNVC1REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
93684a7896SErich Keane   OtherBadNoViableCands2<int>();
94684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'OtherBadNoViableCands2'}}
95684a7896SErich Keane   // expected-note@#OBNVC2_ALT {{candidate template ignored: constraints not satisfied}}
96684a7896SErich Keane   // expected-note@#OBNVC2REQ_ALT {{because 'false' evaluated to false}}
97684a7896SErich Keane   // expected-note@#OBNVC2 {{candidate template ignored: constraints not satisfied}}
98684a7896SErich Keane   // expected-note@#OBNVC2REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
99684a7896SErich Keane   OtherBadNoViableCands3<int>();
100684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'OtherBadNoViableCands3'}}
101684a7896SErich Keane   // expected-note@#OBNVC3_ALT {{candidate template ignored: constraints not satisfied}}
102684a7896SErich Keane   // expected-note@#OBNVC3REQ_ALT {{because 'false' evaluated to false}}
103684a7896SErich Keane   // expected-note@#OBNVC3 {{candidate template ignored: constraints not satisfied}}
104684a7896SErich Keane   // expected-note@#OBNVC3REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
105684a7896SErich Keane }
106684a7896SErich Keane 
107684a7896SErich Keane 
108684a7896SErich Keane // Same tests with member functions.
109684a7896SErich Keane struct OVC {
110684a7896SErich Keane template<typename T> requires CausesRecoveryExpr // #MEMOVC1REQ
OtherViableCands1OVC111684a7896SErich Keane void OtherViableCands1(){} // #MEMOVC1
112684a7896SErich Keane 
113684a7896SErich Keane template<typename T>
OtherViableCands1OVC114684a7896SErich Keane void OtherViableCands1(){} // #MEMOVC1_ALT
115684a7896SErich Keane 
116684a7896SErich Keane template<typename T> requires ReferencesCRE<T> // #MEMOVC2REQ
OtherViableCands2OVC117684a7896SErich Keane void OtherViableCands2(){} // #MEMOVC2
118684a7896SErich Keane 
119684a7896SErich Keane template<typename T>
OtherViableCands2OVC120684a7896SErich Keane void OtherViableCands2(){} // #MEMOVC2_ALT
121684a7896SErich Keane 
122684a7896SErich Keane template<ReferencesCRE T> // #MEMOVC3REQ
OtherViableCands3OVC123684a7896SErich Keane void OtherViableCands3(){} // #MEMOVC3
124684a7896SErich Keane template<typename T>
OtherViableCands3OVC125684a7896SErich Keane void OtherViableCands3(){} // #MEMOVC3_ALT
126684a7896SErich Keane };
127684a7896SErich Keane 
MemOVCUse()128684a7896SErich Keane void MemOVCUse() {
129684a7896SErich Keane   OVC S;
130684a7896SErich Keane   S.OtherViableCands1<int>();
131684a7896SErich Keane   // expected-error@-1 {{no matching member function for call to 'OtherViableCands1'}}
132684a7896SErich Keane   // expected-note@#MEMOVC1_ALT {{candidate function}}
133684a7896SErich Keane   // expected-note@#MEMOVC1 {{candidate template ignored: constraints not satisfied}}
134684a7896SErich Keane   // expected-note@#MEMOVC1REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
135684a7896SErich Keane   S.OtherViableCands2<int>();
136684a7896SErich Keane   // expected-error@-1 {{no matching member function for call to 'OtherViableCands2'}}
137684a7896SErich Keane   // expected-note@#MEMOVC2_ALT {{candidate function}}
138684a7896SErich Keane   // expected-note@#MEMOVC2 {{candidate template ignored: constraints not satisfied}}
139684a7896SErich Keane   // expected-note@#MEMOVC2REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
140684a7896SErich Keane   S.OtherViableCands3<int>();
141684a7896SErich Keane   // expected-error@-1 {{no matching member function for call to 'OtherViableCands3'}}
142684a7896SErich Keane   // expected-note@#MEMOVC3_ALT {{candidate function}}
143684a7896SErich Keane   // expected-note@#MEMOVC3 {{candidate template ignored: constraints not satisfied}}
144684a7896SErich Keane   // expected-note@#MEMOVC3REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
145684a7896SErich Keane }
146684a7896SErich Keane 
147684a7896SErich Keane struct StaticOVC {
148684a7896SErich Keane template<typename T> requires CausesRecoveryExpr // #SMEMOVC1REQ
OtherViableCands1StaticOVC149684a7896SErich Keane static void OtherViableCands1(){} // #SMEMOVC1
150684a7896SErich Keane 
151684a7896SErich Keane template<typename T>
OtherViableCands1StaticOVC152684a7896SErich Keane static void OtherViableCands1(){} // #SMEMOVC1_ALT
153684a7896SErich Keane 
154684a7896SErich Keane template<typename T> requires ReferencesCRE<T> // #SMEMOVC2REQ
OtherViableCands2StaticOVC155684a7896SErich Keane static void OtherViableCands2(){} // #SMEMOVC2
156684a7896SErich Keane 
157684a7896SErich Keane template<typename T>
OtherViableCands2StaticOVC158684a7896SErich Keane static void OtherViableCands2(){} // #SMEMOVC2_ALT
159684a7896SErich Keane 
160684a7896SErich Keane template<ReferencesCRE T> // #SMEMOVC3REQ
OtherViableCands3StaticOVC161684a7896SErich Keane static void OtherViableCands3(){} // #SMEMOVC3
162684a7896SErich Keane template<typename T>
OtherViableCands3StaticOVC163684a7896SErich Keane static void OtherViableCands3(){} // #SMEMOVC3_ALT
164684a7896SErich Keane };
165684a7896SErich Keane 
StaticMemOVCUse()166684a7896SErich Keane void StaticMemOVCUse() {
167684a7896SErich Keane   StaticOVC::OtherViableCands1<int>();
168684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'OtherViableCands1'}}
169684a7896SErich Keane   // expected-note@#SMEMOVC1_ALT {{candidate function}}
170684a7896SErich Keane   // expected-note@#SMEMOVC1 {{candidate template ignored: constraints not satisfied}}
171684a7896SErich Keane   // expected-note@#SMEMOVC1REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
172684a7896SErich Keane   StaticOVC::OtherViableCands2<int>();
173684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'OtherViableCands2'}}
174684a7896SErich Keane   // expected-note@#SMEMOVC2_ALT {{candidate function}}
175684a7896SErich Keane   // expected-note@#SMEMOVC2 {{candidate template ignored: constraints not satisfied}}
176684a7896SErich Keane   // expected-note@#SMEMOVC2REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
177684a7896SErich Keane   StaticOVC::OtherViableCands3<int>();
178684a7896SErich Keane   // expected-error@-1 {{no matching function for call to 'OtherViableCands3'}}
179684a7896SErich Keane   // expected-note@#SMEMOVC3_ALT {{candidate function}}
180684a7896SErich Keane   // expected-note@#SMEMOVC3 {{candidate template ignored: constraints not satisfied}}
181684a7896SErich Keane   // expected-note@#SMEMOVC3REQ{{because substituted constraint expression is ill-formed: constraint depends on a previously diagnosed expression}}
182684a7896SErich Keane }
183*141de749SYounan Zhang 
184*141de749SYounan Zhang namespace GH58548 {
185*141de749SYounan Zhang 
186*141de749SYounan Zhang template <class, class> struct formatter; // #primary-template
187*141de749SYounan Zhang template <class, class> struct basic_format_context {};
188*141de749SYounan Zhang 
189*141de749SYounan Zhang template <typename CharType>
190*141de749SYounan Zhang concept has_format_function =
191*141de749SYounan Zhang     format(basic_format_context<CharType, CharType>());
192*141de749SYounan Zhang 
193*141de749SYounan Zhang template <typename ValueType, typename CharType>
194*141de749SYounan Zhang   requires has_format_function<CharType>
195*141de749SYounan Zhang struct formatter<ValueType, CharType> {
196*141de749SYounan Zhang   template <typename OutputIt>
197*141de749SYounan Zhang   CharType format(basic_format_context<OutputIt, CharType>);
198*141de749SYounan Zhang };
199*141de749SYounan Zhang 
handle_replacement_field(Ctx arg)200*141de749SYounan Zhang template <class Ctx> int handle_replacement_field(Ctx arg) {
201*141de749SYounan Zhang   formatter<decltype(arg), int> ctx; // expected-error {{implicit instantiation of undefined template}}
202*141de749SYounan Zhang   return 0;
203*141de749SYounan Zhang }
204*141de749SYounan Zhang 
205*141de749SYounan Zhang int x = handle_replacement_field(0);
206*141de749SYounan Zhang // expected-note@-1 {{template specialization 'GH58548::handle_replacement_field<int>' requested here}}
207*141de749SYounan Zhang // expected-note@#primary-template {{is declared here}}
208*141de749SYounan Zhang 
209*141de749SYounan Zhang } // GH58548
210