304 lines · cpp
1// RUN: %clang_cc1 -std=c++2c -fsyntax-only -verify %s2 3struct S {4 void f() = delete("deleted (1)"); // expected-note {{explicitly marked deleted}}5 6 template <typename T>7 T g() = delete("deleted (2)"); // expected-note {{explicitly deleted}}8};9 10template <typename T>11struct TS {12 T f() = delete("deleted (3)"); // expected-note {{explicitly marked deleted}}13 14 template <typename U>15 T g(U) = delete("deleted (4)"); // expected-note {{explicitly deleted}}16};17 18void f() = delete("deleted (5)"); // expected-note {{explicitly deleted}}19 20template <typename T>21T g() = delete("deleted (6)"); // expected-note {{explicitly deleted}}22 23template <typename T, typename U>24struct TSp {25 T f() = delete("deleted (7)"); // expected-note 2 {{explicitly marked deleted}}26};27 28template <typename T>29struct TSp<T, int> {30 T f() = delete("deleted (8)"); // expected-note {{explicitly marked deleted}}31};32 33template <>34struct TSp<int, int> {35 int f() = delete("deleted (9)"); // expected-note {{explicitly marked deleted}}36};37 38void u1() = delete(L"\xFFFFFFFF"); // expected-error {{an unevaluated string literal cannot have an encoding prefix}} \39 // expected-error {{invalid escape sequence '\xFFFFFFFF' in an unevaluated string literal}}40void u2() = delete(u"\U000317FF"); // expected-error {{an unevaluated string literal cannot have an encoding prefix}}41 42void u3() = delete("Ω"); // expected-note {{explicitly deleted}}43void u4() = delete("\u1234"); // expected-note {{explicitly deleted}}44 45void u5() = delete("\x1ff" // expected-error {{hex escape sequence out of range}} \46 // expected-error {{invalid escape sequence '\x1ff' in an unevaluated string literal}}47 "0\x123" // expected-error {{invalid escape sequence '\x123' in an unevaluated string literal}}48 "fx\xfffff" // expected-error {{invalid escape sequence '\xfffff' in an unevaluated string literal}}49 "goop");50 51void u6() = delete("\'\"\?\\\a\b\f\n\r\t\v"); // expected-note {{explicitly deleted}}52void u7() = delete("\xFF"); // expected-error {{invalid escape sequence '\xFF' in an unevaluated string literal}}53void u8() = delete("\123"); // expected-error {{invalid escape sequence '\123' in an unevaluated string literal}}54void u9() = delete("\pOh no, a Pascal string!"); // expected-warning {{unknown escape sequence '\p'}} \55 // expected-error {{invalid escape sequence '\p' in an unevaluated string literal}}56// expected-note@+1 {{explicitly deleted}}57void u10() = delete(R"(a58\tb59c60)");61 62void u11() = delete("\u0080\u0081\u0082\u0083\u0099\u009A\u009B\u009C\u009D\u009E\u009F"); // expected-note {{explicitly deleted}}63 64 65//! Contains RTL/LTR marks66void u12() = delete("\u200Eabc\u200Fdef\u200Fgh"); // expected-note {{explicitly deleted}}67 68//! Contains ZWJ/regional indicators69void u13() = delete("🏳️🌈 🏴 🇪🇺"); // expected-note {{explicitly deleted}}70 71void h() {72 S{}.f(); // expected-error {{attempt to use a deleted function: deleted (1)}}73 S{}.g<int>(); // expected-error {{call to deleted member function 'g': deleted (2)}}74 TS<int>{}.f(); // expected-error {{attempt to use a deleted function: deleted (3)}}75 TS<int>{}.g<int>(0); // expected-error {{call to deleted member function 'g': deleted (4)}}76 f(); // expected-error {{call to deleted function 'f': deleted (5)}}77 g<int>(); // expected-error {{call to deleted function 'g': deleted (6)}}78 TSp<double, double>{}.f(); // expected-error {{attempt to use a deleted function: deleted (7)}}79 TSp<int, double>{}.f(); // expected-error {{attempt to use a deleted function: deleted (7)}}80 TSp<double, int>{}.f(); // expected-error {{attempt to use a deleted function: deleted (8)}}81 TSp<int, int>{}.f(); // expected-error {{attempt to use a deleted function: deleted (9)}}82 u3(); // expected-error {{call to deleted function 'u3': Ω}}83 u4(); // expected-error {{call to deleted function 'u4': ሴ}}84 u6(); // expected-error {{call to deleted function 'u6': '"?\<U+0007><U+0008>}}85 u10(); // expected-error {{call to deleted function 'u10': a\n\tb\nc\n}}86 u11(); // expected-error {{call to deleted function 'u11': <U+0080><U+0081><U+0082><U+0083><U+0099><U+009A><U+009B><U+009C><U+009D><U+009E><U+009F>}}87 u12(); // expected-error {{call to deleted function 'u12': abcdefgh}}88 u13(); // expected-error {{call to deleted function 'u13': 🏳️🌈 🏴 🇪🇺}}89}90 91struct C {92 C() = delete("deleted (C, Constructor)"); // expected-note {{explicitly marked deleted}}93 C(int) = delete("deleted (C, C(int))"); // expected-note {{explicitly marked deleted}}94 C(const C&) = delete("deleted (C, Copy Constructor)"); // expected-note {{explicitly marked deleted}}95 C(C&&) = delete("deleted (C, Move Constructor)"); // expected-note {{explicitly marked deleted}}96 C& operator=(const C&) = delete("deleted (C, Copy Assignment)"); // expected-note 2 {{explicitly deleted}}97 C& operator=(C&&) = delete("deleted (C, Move Assignment)"); // expected-note {{explicitly deleted}} expected-note {{not viable}}98 ~C() = delete("deleted (C, Destructor)"); // expected-note {{explicitly marked deleted}}99 void* operator new(__SIZE_TYPE__) = delete("deleted (C, New)"); // expected-note {{explicitly deleted}}100 void operator delete(void*) = delete("deleted (C, Delete)"); // expected-note {{explicitly marked deleted}}101};102 103template <typename T, typename U>104struct TC {105 TC() = delete("deleted (TC, Constructor)"); // expected-note {{explicitly marked deleted}}106 TC(int) = delete("deleted (TC, TC(int))"); // expected-note {{explicitly marked deleted}}107 TC(const TC&) = delete("deleted (TC, Copy Constructor)"); // expected-note {{explicitly marked deleted}}108 TC(TC&&) = delete("deleted (TC, Move Constructor)"); // expected-note {{explicitly marked deleted}}109 TC& operator=(const TC&) = delete("deleted (TC, Copy Assignment)"); // expected-note 2 {{explicitly deleted}}110 TC& operator=(TC&&) = delete("deleted (TC, Move Assignment)"); // expected-note {{explicitly deleted}} expected-note {{not viable}}111 ~TC() = delete("deleted (TC, Destructor)"); // expected-note {{explicitly marked deleted}}112 void* operator new(__SIZE_TYPE__) = delete("deleted (TC, New)"); // expected-note {{explicitly deleted}}113 void operator delete(void*) = delete("deleted (TC, Delete)"); // expected-note {{explicitly marked deleted}}114};115 116template <typename T>117struct TC<T, int> {118 TC() = delete("deleted (TC<T, int>, Constructor)"); // expected-note {{explicitly marked deleted}}119 TC(int) = delete("deleted (TC<T, int>, TC(int))"); // expected-note {{explicitly marked deleted}}120 TC(const TC&) = delete("deleted (TC<T, int>, Copy Constructor)"); // expected-note {{explicitly marked deleted}}121 TC(TC&&) = delete("deleted (TC<T, int>, Move Constructor)"); // expected-note {{explicitly marked deleted}}122 TC& operator=(const TC&) = delete("deleted (TC<T, int>, Copy Assignment)"); // expected-note 2 {{explicitly deleted}}123 TC& operator=(TC&&) = delete("deleted (TC<T, int>, Move Assignment)"); // expected-note {{explicitly deleted}} expected-note {{not viable}}124 ~TC() = delete("deleted (TC<T, int>, Destructor)"); // expected-note {{explicitly marked deleted}}125 void* operator new(__SIZE_TYPE__) = delete("deleted (TC<T, int>, New)"); // expected-note {{explicitly deleted}}126 void operator delete(void*) = delete("deleted (TC<T, int>, Delete)"); // expected-note {{explicitly marked deleted}}127};128 129template <>130struct TC<int, int> {131 TC() = delete("deleted (TC<int, int>, Constructor)"); // expected-note {{explicitly marked deleted}}132 TC(int) = delete("deleted (TC<int, int>, TC(int))"); // expected-note {{explicitly marked deleted}}133 TC(const TC&) = delete("deleted (TC<int, int>, Copy Constructor)"); // expected-note {{explicitly marked deleted}}134 TC(TC&&) = delete("deleted (TC<int, int>, Move Constructor)"); // expected-note {{explicitly marked deleted}}135 TC& operator=(const TC&) = delete("deleted (TC<int, int>, Copy Assignment)"); // expected-note 2 {{explicitly deleted}}136 TC& operator=(TC&&) = delete("deleted (TC<int, int>, Move Assignment)"); // expected-note {{explicitly deleted}} expected-note {{not viable}}137 ~TC() = delete("deleted (TC<int, int>, Destructor)"); // expected-note {{explicitly marked deleted}}138 void* operator new(__SIZE_TYPE__) = delete("deleted (TC<int, int>, New)"); // expected-note {{explicitly deleted}}139 void operator delete(void*) = delete("deleted (TC<int, int>, Delete)"); // expected-note {{explicitly marked deleted}}140};141 142void special_members(143 C& c1,144 C& c2,145 TC<double, double>& tc1,146 TC<double, double>& tc2,147 TC<double, int>& tc_int1,148 TC<double, int>& tc_int2,149 TC<int, int>& tc_int_int1,150 TC<int, int>& tc_int_int2151) {152 C{}; // expected-error {{call to deleted constructor of 'C': deleted (C, Constructor)}}153 C{c1}; // expected-error {{call to deleted constructor of 'C': deleted (C, Copy Constructor)}}154 C{static_cast<C&&>(c1)}; // expected-error {{call to deleted constructor of 'C': deleted (C, Move Constructor)}}155 c1 = c2; // expected-error {{overload resolution selected deleted operator '=': deleted (C, Copy Assignment)}}156 c1 = static_cast<C&&>(c2); // expected-error {{overload resolution selected deleted operator '=': deleted (C, Move Assignment)}}157 c1.~C(); // expected-error {{attempt to use a deleted function: deleted (C, Destructor)}}158 new C{}; // expected-error {{call to deleted function 'operator new': deleted (C, New)}}159 delete &c2; // expected-error {{attempt to use a deleted function: deleted (C, Delete)}}160 161 TC<double, double>{}; // expected-error {{call to deleted constructor of 'TC<double, double>': deleted (TC, Constructor)}}162 TC<double, double>{tc1}; // expected-error {{call to deleted constructor of 'TC<double, double>': deleted (TC, Copy Constructor)}}163 TC<double, double>{static_cast<TC<double, double>&&>(tc1)}; // expected-error {{call to deleted constructor of 'TC<double, double>': deleted (TC, Move Constructor)}}164 tc1 = tc2; // expected-error {{overload resolution selected deleted operator '=': deleted (TC, Copy Assignment)}}165 tc1 = static_cast<TC<double, double>&&>(tc2); // expected-error {{overload resolution selected deleted operator '=': deleted (TC, Move Assignment)}}166 tc1.~TC(); // expected-error {{attempt to use a deleted function: deleted (TC, Destructor)}}167 new TC<double, double>{}; // expected-error {{call to deleted function 'operator new': deleted (TC, New)}}168 delete &tc2; // expected-error {{attempt to use a deleted function: deleted (TC, Delete)}}169 170 TC<double, int>{}; // expected-error {{call to deleted constructor of 'TC<double, int>': deleted (TC<T, int>, Constructor)}}171 TC<double, int>{tc_int1}; // expected-error {{call to deleted constructor of 'TC<double, int>': deleted (TC<T, int>, Copy Constructor)}}172 TC<double, int>{static_cast<TC<double, int>&&>(tc_int1)}; // expected-error {{call to deleted constructor of 'TC<double, int>': deleted (TC<T, int>, Move Constructor)}}173 tc_int1 = tc_int2; // expected-error {{overload resolution selected deleted operator '=': deleted (TC<T, int>, Copy Assignment)}}174 tc_int1 = static_cast<TC<double, int>&&>(tc_int2); // expected-error {{overload resolution selected deleted operator '=': deleted (TC<T, int>, Move Assignment)}}175 tc_int1.~TC(); // expected-error {{attempt to use a deleted function: deleted (TC<T, int>, Destructor)}}176 new TC<double, int>{}; // expected-error {{call to deleted function 'operator new': deleted (TC<T, int>, New)}}177 delete &tc_int2; // expected-error {{attempt to use a deleted function: deleted (TC<T, int>, Delete)}}178 179 TC<int, int>{}; // expected-error {{call to deleted constructor of 'TC<int, int>': deleted (TC<int, int>, Constructor)}}180 TC<int, int>{tc_int_int1}; // expected-error {{call to deleted constructor of 'TC<int, int>': deleted (TC<int, int>, Copy Constructor)}}181 TC<int, int>{static_cast<TC<int, int>&&>(tc_int_int1)}; // expected-error {{call to deleted constructor of 'TC<int, int>': deleted (TC<int, int>, Move Constructor)}}182 tc_int_int1 = tc_int_int2; // expected-error {{overload resolution selected deleted operator '=': deleted (TC<int, int>, Copy Assignment)}}183 tc_int_int1 = static_cast<TC<int, int>&&>(tc_int_int2); // expected-error {{overload resolution selected deleted operator '=': deleted (TC<int, int>, Move Assignment)}}184 tc_int_int1.~TC(); // expected-error {{attempt to use a deleted function: deleted (TC<int, int>, Destructor)}}185 new TC<int, int>{}; // expected-error {{call to deleted function 'operator new': deleted (TC<int, int>, New)}}186 delete &tc_int_int2; // expected-error {{attempt to use a deleted function: deleted (TC<int, int>, Delete)}}187}188 189C conv1() { return 1; } // expected-error {{conversion function from 'int' to 'C' invokes a deleted function: deleted (C, C(int))}}190TC<double, double> conv2() { return 1; } // expected-error {{conversion function from 'int' to 'TC<double, double>' invokes a deleted function: deleted (TC, TC(int))}}191TC<double, int> conv3() { return 1; } // expected-error {{conversion function from 'int' to 'TC<double, int>' invokes a deleted function: deleted (TC<T, int>, TC(int))}}192TC<int, int> conv4() { return 1; } // expected-error {{conversion function from 'int' to 'TC<int, int>' invokes a deleted function: deleted (TC<int, int>, TC(int))}}193 194struct O {195 int x;196 int operator+() = delete("deleted (O, +)"); // expected-note {{explicitly deleted}}197 O* operator->() = delete("deleted (O, ->)"); // expected-note {{explicitly deleted}}198 int operator-(O) = delete("deleted (O, -)"); // expected-note {{explicitly deleted}}199 int operator[](O) = delete("deleted (O, [])"); // expected-note {{explicitly deleted}}200 int operator()(O) = delete("deleted (O, ())"); // expected-note {{explicitly marked deleted}} expected-note {{explicitly deleted}}201 explicit operator bool() = delete("deleted (O, operator bool)"); // expected-note {{explicitly marked deleted}} expected-note {{explicitly deleted}}202};203 204template <typename T, typename U>205struct TO {206 T x;207 T operator+() = delete("deleted (TO, +)"); // expected-note {{explicitly deleted}}208 T* operator->() = delete("deleted (TO, ->)"); // expected-note {{explicitly deleted}}209 T operator-(TO) = delete("deleted (TO, -)"); // expected-note {{explicitly deleted}}210 T operator[](TO) = delete("deleted (TO, [])"); // expected-note {{explicitly deleted}}211 T operator()(TO) = delete("deleted (TO, ())"); // expected-note {{explicitly marked deleted}} expected-note {{explicitly deleted}}212 explicit operator bool() = delete("deleted (TO, operator bool)"); // expected-note {{explicitly marked deleted}} expected-note {{explicitly deleted}}213};214 215template <typename T>216struct TO<T, int> {217 T x;218 T operator+() = delete("deleted (TO<T, int>, +)"); // expected-note {{explicitly deleted}}219 T* operator->() = delete("deleted (TO<T, int>, ->)"); // expected-note {{explicitly deleted}}220 T operator-(TO) = delete("deleted (TO<T, int>, -)"); // expected-note {{explicitly deleted}}221 T operator[](TO) = delete("deleted (TO<T, int>, [])"); // expected-note {{explicitly deleted}}222 T operator()(TO) = delete("deleted (TO<T, int>, ())"); // expected-note {{explicitly marked deleted}} expected-note {{explicitly deleted}}223 explicit operator bool() = delete("deleted (TO<T, int>, operator bool)"); // expected-note {{explicitly marked deleted}} expected-note {{explicitly deleted}}224};225 226template <>227struct TO<int, int> {228 int x;229 int operator+() = delete("deleted (TO<int, int>, +)"); // expected-note {{explicitly deleted}}230 int* operator->() = delete("deleted (TO<int, int>, ->)"); // expected-note {{explicitly deleted}}231 int operator-(TO) = delete("deleted (TO<int, int>, -)"); // expected-note {{explicitly deleted}}232 int operator[](TO) = delete("deleted (TO<int, int>, [])"); // expected-note {{explicitly deleted}}233 int operator()(TO) = delete("deleted (TO<int, int>, ())"); // expected-note {{explicitly marked deleted}} expected-note {{explicitly deleted}}234 explicit operator bool() = delete("deleted (TO<int, int>, operator bool)"); // expected-note {{explicitly marked deleted}} expected-note {{explicitly deleted}}235};236 237void operators() {238 O o;239 +o; // expected-error {{overload resolution selected deleted operator '+': deleted (O, +)}}240 o->x; // expected-error {{overload resolution selected deleted operator '->': deleted (O, ->)}}241 o - o; // expected-error {{overload resolution selected deleted operator '-': deleted (O, -)}}242 o[o]; // expected-error {{overload resolution selected deleted operator '[]': deleted (O, [])}}243 o(o); // expected-error {{call to deleted function call operator in type 'O': deleted (O, ())}} expected-error {{attempt to use a deleted function: deleted (O, ())}}244 if (o) {} // expected-error {{attempt to use a deleted function: deleted (O, operator bool)}}245 static_cast<bool>(o); // expected-error {{static_cast from 'O' to 'bool' uses deleted function: deleted (O, operator bool)}}246 247 TO<double, double> to;248 +to; // expected-error {{overload resolution selected deleted operator '+': deleted (TO, +)}}249 to->x; // expected-error {{overload resolution selected deleted operator '->': deleted (TO, ->)}}250 to - to; // expected-error {{overload resolution selected deleted operator '-': deleted (TO, -)}}251 to[to]; // expected-error {{overload resolution selected deleted operator '[]': deleted (TO, [])}}252 to(to); // expected-error {{call to deleted function call operator in type 'TO<double, double>': deleted (TO, ())}} expected-error {{attempt to use a deleted function: deleted (TO, ())}}253 if (to) {} // expected-error {{attempt to use a deleted function: deleted (TO, operator bool)}}254 static_cast<bool>(to); // expected-error {{static_cast from 'TO<double, double>' to 'bool' uses deleted function: deleted (TO, operator bool)}}255 256 TO<double, int> to_int;257 +to_int; // expected-error {{overload resolution selected deleted operator '+': deleted (TO<T, int>, +)}}258 to_int->x; // expected-error {{overload resolution selected deleted operator '->': deleted (TO<T, int>, ->)}}259 to_int - to_int; // expected-error {{overload resolution selected deleted operator '-': deleted (TO<T, int>, -)}}260 to_int[to_int]; // expected-error {{overload resolution selected deleted operator '[]': deleted (TO<T, int>, [])}}261 to_int(to_int); // expected-error {{call to deleted function call operator in type 'TO<double, int>': deleted (TO<T, int>, ())}} expected-error {{attempt to use a deleted function: deleted (TO<T, int>, ())}}262 if (to_int) {} // expected-error {{attempt to use a deleted function: deleted (TO<T, int>, operator bool)}}263 static_cast<bool>(to_int); // expected-error {{static_cast from 'TO<double, int>' to 'bool' uses deleted function: deleted (TO<T, int>, operator bool)}}264 265 TO<int, int> to_int_int;266 +to_int_int; // expected-error {{overload resolution selected deleted operator '+': deleted (TO<int, int>, +)}}267 to_int_int->x; // expected-error {{overload resolution selected deleted operator '->': deleted (TO<int, int>, ->)}}268 to_int_int - to_int_int; // expected-error {{overload resolution selected deleted operator '-': deleted (TO<int, int>, -)}}269 to_int_int[to_int_int]; // expected-error {{overload resolution selected deleted operator '[]': deleted (TO<int, int>, [])}}270 to_int_int(to_int_int); // expected-error {{call to deleted function call operator in type 'TO<int, int>': deleted (TO<int, int>, ())}} expected-error {{attempt to use a deleted function: deleted (TO<int, int>, ())}}271 if (to_int_int) {} // expected-error {{attempt to use a deleted function: deleted (TO<int, int>, operator bool)}}272 static_cast<bool>(to_int_int); // expected-error {{static_cast from 'TO<int, int>' to 'bool' uses deleted function: deleted (TO<int, int>, operator bool)}}273};274 275namespace gh135506 {276struct a {277 // FIXME: We currently don't diagnose these invalid redeclarations if the278 // second declaration is defaulted or deleted. This probably needs to be279 // handled in ParseCXXInlineMethodDef() after parsing the defaulted/deleted280 // body.281 friend consteval int f() { return 3; }282 friend consteval int f() = delete("foo");283 284 friend consteval int g() { return 3; }285 friend consteval int g() = delete;286 287 friend int h() { return 3; }288 friend int h() = delete;289 290 friend consteval int i() = delete; // expected-note {{previous definition is here}}291 friend consteval int i() { return 3; } // expected-error {{redefinition of 'i'}}292};293 294struct b {295 friend consteval bool operator==(b, b) { return true; } // expected-note {{previous declaration is here}}296 friend consteval bool operator==(b, b) = default; // expected-error {{defaulting this equality comparison operator is not allowed because it was already declared outside the class}}297};298 299struct c {300 friend consteval bool operator==(c, c) = default; // expected-note {{previous definition is here}}301 friend consteval bool operator==(c, c) { return true; } // expected-error {{redefinition of 'operator=='}}302};303}304