419 lines · cpp
1// RUN: %clang_cc1 -std=c++2c -triple=x86_64-linux -fsyntax-only %s -verify2// RUN: %clang_cc1 -std=c++2c -triple=x86_64-linux -fsyntax-only %s -verify -fexperimental-new-constant-interpreter3 4static_assert(true, "");5static_assert(true, 0); // expected-error {{the message in a static assertion must be a string literal or an object with 'data()' and 'size()' member functions}}6struct Empty{};7static_assert(true, Empty{}); // expected-error {{the message object in this static assertion is missing 'data()' and 'size()' member functions}}8struct NoData {9 unsigned long size() const;10};11struct NoSize {12 const char* data() const;13};14static_assert(true, NoData{}); // expected-error {{the message object in this static assertion is missing a 'data()' member function}}15static_assert(true, NoSize{}); // expected-error {{the message object in this static assertion is missing a 'size()' member function}}16 17struct InvalidSize {18 const char* size() const;19 const char* data() const;20};21static_assert(true, InvalidSize{}); // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \22 // expected-error {{value of type 'const char *' is not implicitly convertible to '__size_t' (aka 'unsigned long')}}23struct InvalidData {24 unsigned long size() const;25 unsigned long data() const;26};27static_assert(true, InvalidData{}); // expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}} \28 // expected-error {{value of type 'unsigned long' is not implicitly convertible to 'const char *'}}29 30struct NonConstexprSize {31 unsigned long size() const; // expected-note 2{{declared here}}32 constexpr const char* data() const;33};34 35static_assert(true, NonConstexprSize{}); // expected-error {{the message in this static assertion is not a constant expression}} \36 // expected-note {{non-constexpr function 'size' cannot be used in a constant expression}}37 38static_assert(false, NonConstexprSize{}); // expected-error {{the message in a static assertion must be produced by a constant expression}} \39 // expected-error {{static assertion failed}} \40 // expected-note {{non-constexpr function 'size' cannot be used in a constant expression}}41 42struct NonConstexprData {43 constexpr unsigned long size() const {44 return 32;45 }46 const char* data() const; // expected-note 2{{declared here}}47};48 49static_assert(true, NonConstexprData{}); // expected-error {{the message in this static assertion is not a constant expression}} \50 // expected-note {{non-constexpr function 'data' cannot be used in a constant expression}}51 52static_assert(false, NonConstexprData{}); // expected-error {{the message in a static assertion must be produced by a constant expression}} \53 // expected-error {{static assertion failed}} \54 // expected-note {{non-constexpr function 'data' cannot be used in a constant expression}}55 56struct string_view {57 int S;58 const char* D;59 constexpr string_view(const char* Str) : S(__builtin_strlen(Str)), D(Str) {}60 constexpr string_view(int Size, const char* Str) : S(Size), D(Str) {}61 constexpr int size() const {62 return S;63 }64 constexpr const char* data() const {65 return D;66 }67};68 69constexpr string_view operator+(auto, string_view S) {70 return S;71}72 73constexpr const char g_[] = "long string";74 75template <typename T, int S>76struct array {77 constexpr unsigned long size() const {78 return S;79 }80 constexpr const char* data() const {81 return d_;82 }83 const char d_[S];84};85 86static_assert(false, string_view("test")); // expected-error {{static assertion failed: test}}87static_assert(false, "Literal" + string_view("test")); // expected-error {{static assertion failed: test}}88static_assert(false, L"Wide Literal" + string_view("test")); // expected-error {{static assertion failed: test}}89static_assert(false, "Wild" "Literal" "Concatenation" + string_view("test")); // expected-error {{static assertion failed: test}}90static_assert(false, "Wild" "Literal" L"Concatenation" + string_view("test")); // expected-error {{static assertion failed: test}}91static_assert(false, "Wild" u"Literal" L"Concatenation" + string_view("test")); // expected-error {{unsupported non-standard concatenation of string literals}}92static_assert(false, string_view("😀")); // expected-error {{static assertion failed: 😀}}93static_assert(false, string_view(0, nullptr)); // expected-error {{static assertion failed:}}94static_assert(false, string_view(1, "ABC")); // expected-error {{static assertion failed: A}}95static_assert(false, string_view(42, "ABC")); // expected-error {{static assertion failed: ABC}} \96 // expected-error {{the message in a static assertion must be produced by a constant expression}} \97 // expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}98static_assert(false, array<char, 2>{'a', 'b'}); // expected-error {{static assertion failed: ab}}99 100 101 102struct ConvertibleToInt {103 constexpr operator int() {104 return 4;105 }106};107struct ConvertibleToCharPtr {108 constexpr operator const char*() {109 return "test";110 }111};112struct MessageFromConvertible {113 constexpr ConvertibleToInt size() const {114 return {};115 }116 constexpr ConvertibleToCharPtr data() const {117 return {};118 }119};120 121static_assert(true, MessageFromConvertible{});122static_assert(false, MessageFromConvertible{}); // expected-error{{static assertion failed: test}}123 124 125 126struct Leaks {127 constexpr unsigned long size() const {128 return 2;129 }130 constexpr const char* data() const {131 return new char[2]{'u', 'b'}; // expected-note {{allocation performed here was not deallocated}}132 }133};134 135static_assert(false, Leaks{}); //expected-error {{the message in a static assertion must be produced by a constant expression}} \136 // expected-error {{static assertion failed: ub}}137 138struct RAII {139 const char* d = new char[2]{'o', 'k'};140 constexpr unsigned long size() const {141 return 2;142 }143 144 constexpr const char* data() const {145 return d;146 }147 148 constexpr ~RAII() {149 delete[] d;150 }151};152static_assert(false, RAII{}); // expected-error {{static assertion failed: ok}}153 154namespace MoreTemporary {155 156struct Data{157constexpr operator const char*() const {158 return d;159}160char d[6] = { "Hello" };161};162 163struct Size {164 constexpr operator int() const {165 return 5;166 }167};168 169struct Message {170 constexpr auto size() const {171 return Size{};172 }173 constexpr auto data() const {174 return Data{};175 }176};177 178static_assert(false, Message{}); // expected-error {{static assertion failed: Hello}}179 180}181 182struct MessageInvalidSize {183 constexpr unsigned long size(int) const; // expected-note {{'size' declared here}}184 constexpr const char* data() const;185};186struct MessageInvalidData {187 constexpr unsigned long size() const;188 constexpr const char* data(int) const; // expected-note {{'data' declared here}}189};190 191static_assert(false, MessageInvalidSize{}); // expected-error {{static assertion failed}} \192 // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \193 // expected-error {{too few arguments to function call, expected 1, have 0}}194static_assert(false, MessageInvalidData{}); // expected-error {{static assertion failed}} \195 // expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}} \196 // expected-error {{too few arguments to function call, expected 1, have 0}}197 198struct NonConstMembers {199 constexpr int size() {200 return 1;201 }202 constexpr const char* data() {203 return "A";204 }205};206 207static_assert(false, NonConstMembers{}); // expected-error {{static assertion failed: A}}208 209struct DefaultArgs {210 constexpr int size(int i = 0) {211 return 2;212 }213 constexpr const char* data(int i =0, int j = 42) {214 return "OK";215 }216};217 218static_assert(false, DefaultArgs{}); // expected-error {{static assertion failed: OK}}219 220struct Variadic {221 constexpr int size(auto...) {222 return 2;223 }224 constexpr const char* data(auto...) {225 return "OK";226 }227};228 229static_assert(false, Variadic{}); // expected-error {{static assertion failed: OK}}230 231template <typename T>232struct DeleteAndRequires {233 constexpr int size() = delete; // expected-note {{'size' has been explicitly marked deleted here}}234 constexpr const char* data() requires false; // expected-note {{because 'false' evaluated to false}}235};236static_assert(false, DeleteAndRequires<void>{});237// expected-error@-1 {{static assertion failed}} \238// expected-error@-1 {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}}\239// expected-error@-1 {{invalid reference to function 'data': constraints not satisfied}} \240// expected-error@-1 {{attempt to use a deleted function}}241 242class Private {243 constexpr int size(int i = 0) { // expected-note {{implicitly declared private here}}244 return 2;245 }246 constexpr const char* data(int i =0, int j = 42) { // expected-note {{implicitly declared private here}}247 return "OK";248 }249};250 251static_assert(false, Private{}); // expected-error {{'data' is a private member of 'Private'}}\252 // expected-error {{'size' is a private member of 'Private'}}\253 // expected-error {{static assertion failed: OK}}254 255struct MessageOverload {256 constexpr int size() {257 return 1;258 }259 constexpr int size() const;260 261 constexpr const char* data() {262 return "A";263 }264 constexpr const char* data() const;265};266 267static_assert(false, MessageOverload{}); // expected-error {{static assertion failed: A}}268 269struct InvalidPtr {270 consteval auto size() {271 return 42;272 }273 consteval const char *data() {274 const char *ptr; // Garbage275 return ptr; // expected-note {{read of uninitialized object is not allowed in a constant expression}}276 }277};278 279static_assert(false, InvalidPtr{}); // expected-error{{the message in a static assertion must be produced by a constant expression}} \280 //expected-error {{static assertion failed}} \281 // expected-note {{in call to 'InvalidPtr{}.data()'}}282 283namespace DependentMessage {284template <typename Ty>285struct Good {286 static_assert(false, Ty{}); // expected-error {{static assertion failed: hello}}287};288 289template <typename Ty>290struct Bad {291 static_assert(false, Ty{}); // expected-error {{the message in a static assertion must be a string literal or an object with 'data()' and 'size()' member functions}} \292 // expected-error {{static assertion failed}}293};294 295struct Frobble {296 constexpr int size() const { return 5; }297 constexpr const char *data() const { return "hello"; }298};299 300constexpr Frobble operator ""_myd (const char *, unsigned long) { return Frobble{}; }301static_assert (false, "foo"_myd); // expected-error {{static assertion failed: hello}}302 303Good<Frobble> a; // expected-note {{in instantiation}}304Bad<int> b; // expected-note {{in instantiation}}305 306}307 308namespace EscapeInDiagnostic {309static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \310 // expected-note {{evaluates to ''\t' (0x09, 9) == '<U+0001>' (0x01, 1)'}}311static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \312 // expected-note {{evaluates to 'u8'<80>' (0x80, 128) == u8'<85>' (0x85, 133)'}}313static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \314 // expected-note {{evaluates to 'u'' (0xFEFF, 65279) == u'\xDB93' (0xDB93, 56211)'}}315}316 317struct Static {318 static constexpr int size() { return 5; }319 static constexpr const char *data() { return "hello"; }320};321static_assert(false, Static{}); // expected-error {{static assertion failed: hello}}322 323struct Data {324 unsigned long size = 0;325 const char* data = "hello";326};327static_assert(false, Data{}); // expected-error {{called object type 'unsigned long' is not a function or function pointer}} \328 // expected-error {{called object type 'const char *' is not a function or function pointer}} \329 // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \330 // expected-error {{static assertion failed}}331 332struct Callable {333 struct {334 constexpr auto operator()() const {335 return 5;336 };337 } size;338 struct {339 constexpr auto operator()() const {340 return "hello";341 };342 } data;343};344static_assert(false, Callable{}); // expected-error {{static assertion failed: hello}}345 346namespace GH89407 {347struct A {348 constexpr __SIZE_TYPE__ size() const { return -1; }349 constexpr const char* data() const { return ""; }350};351 352struct B {353 constexpr long long size() const { return 18446744073709551615U; }354 constexpr const char* data() const { return ""; }355};356 357struct C {358 constexpr __int128 size() const { return -1; }359 constexpr const char* data() const { return ""; }360};361 362struct D {363 constexpr unsigned __int128 size() const { return -1; }364 constexpr const char* data() const { return ""; }365};366 367struct E {368 constexpr __SIZE_TYPE__ size() const { return 18446744073709551615U; }369 constexpr const char* data() const { return ""; }370};371 372static_assert(true, A{}); // expected-error {{the message in this static assertion is not a constant expression}}373 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}374static_assert(true, B{}); // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}375 // expected-error@-1 {{the message in this static assertion is not a constant expression}}376 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}377static_assert(true, C{}); // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}378 // expected-error@-1 {{the message in this static assertion is not a constant expression}}379 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}380static_assert(true, D{}); // expected-error {{call to 'size()' evaluates to 340282366920938463463374607431768211455, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}381 // expected-error@-1 {{the message in this static assertion is not a constant expression}}382 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}383static_assert(true, E{}); // expected-error {{the message in this static assertion is not a constant expression}}384 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}385 386static_assert(387 false, // expected-error {{static assertion failed}}388 A{} // expected-error {{the message in a static assertion must be produced by a constant expression}}389 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}390);391 392static_assert(393 false, // expected-error {{static assertion failed}}394 B{} // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}395 // expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}396 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}397);398 399static_assert(400 false, // expected-error {{static assertion failed}}401 C{} // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}402 // expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}403 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}404);405 406static_assert(407 false, // expected-error {{static assertion failed}}408 D{} // expected-error {{call to 'size()' evaluates to 340282366920938463463374607431768211455, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}409 // expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}410 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}411);412 413static_assert(414 false, // expected-error {{static assertion failed}}415 E{} // expected-error {{the message in a static assertion must be produced by a constant expression}}416 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}417);418}419