https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/73234
>From c274d62d03e1ab390284b26d6e23a23d099a98f6 Mon Sep 17 00:00:00 2001 From: Corentin Jabot <corentinja...@gmail.com> Date: Thu, 23 Nov 2023 12:51:46 +0100 Subject: [PATCH] [Clang] Improve support for expression messages in `static_assert` - Support non-member functions and callable objects for size and data(). We previously tried to (badly) pick the best overload ourselves, in a way that would only support member functions. We now leave clamg construct an unresolved member expression and call that, properly performing overload resolution with callable objects and static functions, cojnsistent with the logic for `get` calls for structured bindings. - Support UDLs as message expression. - Add tests and mark CWG2798 as resolved --- clang/docs/ReleaseNotes.rst | 3 ++ clang/lib/Parse/ParseDeclCXX.cpp | 2 +- clang/lib/Sema/SemaDeclCXX.cpp | 24 ++-------- clang/test/CXX/drs/dr27xx.cpp | 30 ++++++++++++ clang/test/SemaCXX/static-assert-cxx26.cpp | 54 ++++++++++++++++++---- clang/www/cxx_dr_status.html | 2 +- 6 files changed, 82 insertions(+), 33 deletions(-) create mode 100644 clang/test/CXX/drs/dr27xx.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 7e2c990d03e7f27..5a8e63be1258a28 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -617,6 +617,9 @@ Bug Fixes in This Version - Fix crash during code generation of C++ coroutine initial suspend when the return type of await_resume is not trivially destructible. Fixes (`#63803 <https://github.com/llvm/llvm-project/issues/63803>`_) +- Fix crash when the object used as a ``static_asssert`` message has ``size`` or ``data`` members + which are not member functions. +- Support UDLs in ``static_asssert`` message. Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index d9125955fda2783..910112ecae964cc 100644 --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -1023,7 +1023,7 @@ Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd) { const Token &T = GetLookAheadToken(I); if (T.is(tok::r_paren)) break; - if (!tokenIsLikeStringLiteral(T, getLangOpts())) { + if (!tokenIsLikeStringLiteral(T, getLangOpts()) || T.hasUDSuffix()) { ParseAsExpression = true; break; } diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 3688192e6cbe5c5..1d9e4a0ac46fffe 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -17291,33 +17291,15 @@ bool Sema::EvaluateStaticAssertMessageAsString(Expr *Message, auto FindMember = [&](StringRef Member, bool &Empty, bool Diag = false) -> std::optional<LookupResult> { - QualType ObjectType = Message->getType(); - Expr::Classification ObjectClassification = - Message->Classify(getASTContext()); - DeclarationName DN = PP.getIdentifierInfo(Member); LookupResult MemberLookup(*this, DN, Loc, Sema::LookupMemberName); LookupQualifiedName(MemberLookup, RD); Empty = MemberLookup.empty(); OverloadCandidateSet Candidates(MemberLookup.getNameLoc(), OverloadCandidateSet::CSK_Normal); - for (NamedDecl *D : MemberLookup) { - AddMethodCandidate(DeclAccessPair::make(D, D->getAccess()), ObjectType, - ObjectClassification, /*Args=*/{}, Candidates); - } - OverloadCandidateSet::iterator Best; - switch (Candidates.BestViableFunction(*this, Loc, Best)) { - case OR_Success: - return std::move(MemberLookup); - default: - if (Diag) - Candidates.NoteCandidates( - PartialDiagnosticAt( - Loc, PDiag(diag::err_static_assert_invalid_mem_fn_ret_ty) - << (Member == "data")), - *this, OCD_AllCandidates, /*Args=*/{}); - } - return std::nullopt; + if (MemberLookup.empty()) + return std::nullopt; + return MemberLookup; }; bool SizeNotFound, DataNotFound; diff --git a/clang/test/CXX/drs/dr27xx.cpp b/clang/test/CXX/drs/dr27xx.cpp new file mode 100644 index 000000000000000..f17726eb045f933 --- /dev/null +++ b/clang/test/CXX/drs/dr27xx.cpp @@ -0,0 +1,30 @@ +// RUN: %clang_cc1 -std=c++2c -verify %s + +namespace dr2798 { // dr2798: 17 drafting +#if __cpp_static_assert >= 202306 +struct string { + constexpr string() { + data_ = new char[6](); + __builtin_memcpy(data_, "Hello", 5); + data_[5] = 0; + } + constexpr ~string() { + delete[] data_; + } + constexpr unsigned long size() const { + return 5; + }; + constexpr const char* data() const { + return data_; + } + + char* data_; +}; +struct X { + string s; +}; +consteval X f() { return {}; } + +static_assert(false, f().s); // expected-error {{static assertion failed: Hello}} +#endif +} diff --git a/clang/test/SemaCXX/static-assert-cxx26.cpp b/clang/test/SemaCXX/static-assert-cxx26.cpp index b19aac6cabd1324..f4ede74f9214a44 100644 --- a/clang/test/SemaCXX/static-assert-cxx26.cpp +++ b/clang/test/SemaCXX/static-assert-cxx26.cpp @@ -179,18 +179,20 @@ static_assert(false, Message{}); // expected-error {{static assertion failed: He } struct MessageInvalidSize { - constexpr auto size(int) const; // expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}} - constexpr auto data() const; + constexpr unsigned long size(int) const; // expected-note {{'size' declared here}} + constexpr const char* data() const; }; struct MessageInvalidData { - constexpr auto size() const; - constexpr auto data(int) const; // expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}} + constexpr unsigned long size() const; + constexpr const char* data(int) const; // expected-note {{'data' declared here}} }; static_assert(false, MessageInvalidSize{}); // expected-error {{static assertion failed}} \ - // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} + // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \ + // expected-error {{too few arguments to function call, expected 1, have 0}} static_assert(false, MessageInvalidData{}); // expected-error {{static assertion failed}} \ - // expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}} + // expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}} \ + // expected-error {{too few arguments to function call, expected 1, have 0}} struct NonConstMembers { constexpr int size() { @@ -227,14 +229,14 @@ static_assert(false, Variadic{}); // expected-error {{static assertion failed: O template <typename T> struct DeleteAndRequires { - constexpr int size() = delete; // expected-note {{candidate function has been explicitly deleted}} - constexpr const char* data() requires false; // expected-note {{candidate function not viable: constraints not satisfied}} \ - // expected-note {{because 'false' evaluated to false}} + constexpr int size() = delete; // expected-note {{'size' has been explicitly marked deleted here}} + constexpr const char* data() requires false; // expected-note {{because 'false' evaluated to false}} }; static_assert(false, DeleteAndRequires<void>{}); // expected-error@-1 {{static assertion failed}} \ // expected-error@-1 {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}}\ -// expected-error@-1 {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}} +// expected-error@-1 {{invalid reference to function 'data': constraints not satisfied}} \ +// expected-error@-1 {{attempt to use a deleted function}} class Private { constexpr int size(int i = 0) { // expected-note {{implicitly declared private here}} @@ -294,6 +296,9 @@ struct Frobble { constexpr const char *data() const { return "hello"; } }; +constexpr Frobble operator ""_myd (const char *, unsigned long) { return Frobble{}; } +static_assert (false, "foo"_myd); // expected-error {{static assertion failed: hello}} + Good<Frobble> a; // expected-note {{in instantiation}} Bad<int> b; // expected-note {{in instantiation}} @@ -307,3 +312,32 @@ static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \ // expected-note {{evaluates to 'u'' (0xFEFF, 65279) == u'\xDB93' (0xDB93, 56211)'}} } + +struct Static { + static constexpr int size() { return 5; } + static constexpr const char *data() { return "hello"; } +}; +static_assert(false, Static{}); // expected-error {{static assertion failed: hello}} + +struct Data { + unsigned long size = 0; + const char* data = "hello"; +}; +static_assert(false, Data{}); // expected-error {{called object type 'unsigned long' is not a function or function pointer}} \ + // expected-error {{called object type 'const char *' is not a function or function pointer}} \ + // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \ + // expected-error {{static assertion failed}} + +struct Callable { + struct { + constexpr auto operator()() const { + return 5; + }; + } size; + struct { + constexpr auto operator()() const { + return "hello"; + }; + } data; +}; +static_assert(false, Callable{}); // expected-error {{static assertion failed: hello}} diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html index e711b2173d191c1..266891a2e373090 100755 --- a/clang/www/cxx_dr_status.html +++ b/clang/www/cxx_dr_status.html @@ -7145,7 +7145,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2> <td><a href="https://cplusplus.github.io/CWG/issues/1223.html">1223</a></td> <td>drafting</td> <td>Syntactic disambiguation and <I>trailing-return-type</I>s</td> - <td class="unreleased" align="center">Clang 17</td> + <td class="full" align="center">Clang 17</td> </tr> <tr id="1224"> <td><a href="https://cplusplus.github.io/CWG/issues/1224.html">1224</a></td> _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits