Skip to content

Commit fdefe88

Browse files
authored
[Clang] Improve support for expression messages in static_assert (#73234)
- 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 clang construct an unresolved member expression and call that, properly performing overload resolution with callable objects and static functions, consistent with the logic for `get` calls for structured bindings. - Support UDLs as message expression. - Add tests and mark CWG2798 as resolved
1 parent 4537985 commit fdefe88

File tree

6 files changed

+76
-33
lines changed

6 files changed

+76
-33
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,9 @@ Bug Fixes in This Version
635635
- Fix crash during code generation of C++ coroutine initial suspend when the return
636636
type of await_resume is not trivially destructible.
637637
Fixes (`#63803 <https://github.com/llvm/llvm-project/issues/63803>`_)
638+
- Fix crash when the object used as a ``static_assert`` message has ``size`` or ``data`` members
639+
which are not member functions.
640+
- Support UDLs in ``static_assert`` message.
638641

639642
Bug Fixes to Compiler Builtins
640643
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd) {
10231023
const Token &T = GetLookAheadToken(I);
10241024
if (T.is(tok::r_paren))
10251025
break;
1026-
if (!tokenIsLikeStringLiteral(T, getLangOpts())) {
1026+
if (!tokenIsLikeStringLiteral(T, getLangOpts()) || T.hasUDSuffix()) {
10271027
ParseAsExpression = true;
10281028
break;
10291029
}

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17291,33 +17291,15 @@ bool Sema::EvaluateStaticAssertMessageAsString(Expr *Message,
1729117291

1729217292
auto FindMember = [&](StringRef Member, bool &Empty,
1729317293
bool Diag = false) -> std::optional<LookupResult> {
17294-
QualType ObjectType = Message->getType();
17295-
Expr::Classification ObjectClassification =
17296-
Message->Classify(getASTContext());
17297-
1729817294
DeclarationName DN = PP.getIdentifierInfo(Member);
1729917295
LookupResult MemberLookup(*this, DN, Loc, Sema::LookupMemberName);
1730017296
LookupQualifiedName(MemberLookup, RD);
1730117297
Empty = MemberLookup.empty();
1730217298
OverloadCandidateSet Candidates(MemberLookup.getNameLoc(),
1730317299
OverloadCandidateSet::CSK_Normal);
17304-
for (NamedDecl *D : MemberLookup) {
17305-
AddMethodCandidate(DeclAccessPair::make(D, D->getAccess()), ObjectType,
17306-
ObjectClassification, /*Args=*/{}, Candidates);
17307-
}
17308-
OverloadCandidateSet::iterator Best;
17309-
switch (Candidates.BestViableFunction(*this, Loc, Best)) {
17310-
case OR_Success:
17311-
return std::move(MemberLookup);
17312-
default:
17313-
if (Diag)
17314-
Candidates.NoteCandidates(
17315-
PartialDiagnosticAt(
17316-
Loc, PDiag(diag::err_static_assert_invalid_mem_fn_ret_ty)
17317-
<< (Member == "data")),
17318-
*this, OCD_AllCandidates, /*Args=*/{});
17319-
}
17320-
return std::nullopt;
17300+
if (MemberLookup.empty())
17301+
return std::nullopt;
17302+
return MemberLookup;
1732117303
};
1732217304

1732317305
bool SizeNotFound, DataNotFound;

clang/test/CXX/drs/dr27xx.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clang_cc1 -std=c++2c -verify %s
2+
3+
namespace dr2798 { // dr2798: 17 drafting
4+
#if __cpp_static_assert >= 202306
5+
struct string {
6+
constexpr string() {
7+
data_ = new char[6]();
8+
__builtin_memcpy(data_, "Hello", 5);
9+
data_[5] = 0;
10+
}
11+
constexpr ~string() { delete[] data_; }
12+
constexpr unsigned long size() const { return 5; };
13+
constexpr const char *data() const { return data_; }
14+
15+
char *data_;
16+
};
17+
struct X {
18+
string s;
19+
};
20+
consteval X f() { return {}; }
21+
22+
static_assert(false, f().s); // expected-error {{static assertion failed: Hello}}
23+
#endif
24+
} // namespace dr2798

clang/test/SemaCXX/static-assert-cxx26.cpp

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,20 @@ static_assert(false, Message{}); // expected-error {{static assertion failed: He
179179
}
180180

181181
struct MessageInvalidSize {
182-
constexpr auto size(int) const; // expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}}
183-
constexpr auto data() const;
182+
constexpr unsigned long size(int) const; // expected-note {{'size' declared here}}
183+
constexpr const char* data() const;
184184
};
185185
struct MessageInvalidData {
186-
constexpr auto size() const;
187-
constexpr auto data(int) const; // expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}}
186+
constexpr unsigned long size() const;
187+
constexpr const char* data(int) const; // expected-note {{'data' declared here}}
188188
};
189189

190190
static_assert(false, MessageInvalidSize{}); // expected-error {{static assertion failed}} \
191-
// expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}}
191+
// expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \
192+
// expected-error {{too few arguments to function call, expected 1, have 0}}
192193
static_assert(false, MessageInvalidData{}); // expected-error {{static assertion failed}} \
193-
// expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}}
194+
// expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}} \
195+
// expected-error {{too few arguments to function call, expected 1, have 0}}
194196

195197
struct NonConstMembers {
196198
constexpr int size() {
@@ -227,14 +229,14 @@ static_assert(false, Variadic{}); // expected-error {{static assertion failed: O
227229

228230
template <typename T>
229231
struct DeleteAndRequires {
230-
constexpr int size() = delete; // expected-note {{candidate function has been explicitly deleted}}
231-
constexpr const char* data() requires false; // expected-note {{candidate function not viable: constraints not satisfied}} \
232-
// expected-note {{because 'false' evaluated to false}}
232+
constexpr int size() = delete; // expected-note {{'size' has been explicitly marked deleted here}}
233+
constexpr const char* data() requires false; // expected-note {{because 'false' evaluated to false}}
233234
};
234235
static_assert(false, DeleteAndRequires<void>{});
235236
// expected-error@-1 {{static assertion failed}} \
236237
// expected-error@-1 {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}}\
237-
// expected-error@-1 {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}}
238+
// expected-error@-1 {{invalid reference to function 'data': constraints not satisfied}} \
239+
// expected-error@-1 {{attempt to use a deleted function}}
238240

239241
class Private {
240242
constexpr int size(int i = 0) { // expected-note {{implicitly declared private here}}
@@ -294,6 +296,9 @@ struct Frobble {
294296
constexpr const char *data() const { return "hello"; }
295297
};
296298

299+
constexpr Frobble operator ""_myd (const char *, unsigned long) { return Frobble{}; }
300+
static_assert (false, "foo"_myd); // expected-error {{static assertion failed: hello}}
301+
297302
Good<Frobble> a; // expected-note {{in instantiation}}
298303
Bad<int> b; // expected-note {{in instantiation}}
299304

@@ -307,3 +312,32 @@ static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}}
307312
static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
308313
// expected-note {{evaluates to 'u'' (0xFEFF, 65279) == u'\xDB93' (0xDB93, 56211)'}}
309314
}
315+
316+
struct Static {
317+
static constexpr int size() { return 5; }
318+
static constexpr const char *data() { return "hello"; }
319+
};
320+
static_assert(false, Static{}); // expected-error {{static assertion failed: hello}}
321+
322+
struct Data {
323+
unsigned long size = 0;
324+
const char* data = "hello";
325+
};
326+
static_assert(false, Data{}); // expected-error {{called object type 'unsigned long' is not a function or function pointer}} \
327+
// expected-error {{called object type 'const char *' is not a function or function pointer}} \
328+
// expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \
329+
// expected-error {{static assertion failed}}
330+
331+
struct Callable {
332+
struct {
333+
constexpr auto operator()() const {
334+
return 5;
335+
};
336+
} size;
337+
struct {
338+
constexpr auto operator()() const {
339+
return "hello";
340+
};
341+
} data;
342+
};
343+
static_assert(false, Callable{}); // expected-error {{static assertion failed: hello}}

clang/www/cxx_dr_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7145,7 +7145,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
71457145
<td><a href="https://cplusplus.github.io/CWG/issues/1223.html">1223</a></td>
71467146
<td>drafting</td>
71477147
<td>Syntactic disambiguation and <I>trailing-return-type</I>s</td>
7148-
<td class="unreleased" align="center">Clang 17</td>
7148+
<td class="full" align="center">Clang 17</td>
71497149
</tr>
71507150
<tr id="1224">
71517151
<td><a href="https://cplusplus.github.io/CWG/issues/1224.html">1224</a></td>

0 commit comments

Comments
 (0)