Skip to content

Commit b77109f

Browse files
authored
Better diagnostics when assertion fails in consteval (#130458)
Take this piece of code: ```cpp #include <cassert> consteval int square(int x) { int result = x * x; assert(result == 42); return result; } void test() { auto val = square(2); } ``` The assertion will fail, and `clang++` will output (https://godbolt.org/z/hjz3KbTTv): ```cpp <source>:10:14: error: call to consteval function 'square' is not a constant expression 10 | auto val = square(2); | ^ <source>:5:3: note: non-constexpr function '__assert_fail' cannot be used in a constant expression 5 | assert(result == 42); | ^ /usr/include/assert.h:95:9: note: expanded from macro 'assert' 95 | : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION)) | ^ <source>:10:14: note: in call to 'square(2)' 10 | auto val = square(2); | ^~~~~~~~~ /usr/include/assert.h:69:13: note: declared here 69 | extern void __assert_fail (const char *__assertion, const char *__file, | ^ 1 error generated. Compiler returned: 1 ``` This is confusing because it implies that the issue was using an assertion in a constant-evaluted context, and not that the assertion failed (`assert()` is OK in constant evaluation). This PR changes the error message to: ```cpp test.cpp:10:14: error: call to consteval function 'square' is not a constant expression 10 | auto val = square(2); | ^ test.cpp:5:3: note: assertion failed in consteval context: 'result == 42' 5 | assert(result == 42); | ^ /nix/store/lw21wr626v5sdcaxxkv2k4zf1121hfc9-glibc-2.40-36-dev/include/assert.h:102:9: note: expanded from macro 'assert' 102 | : __assert_fail (#expr, __ASSERT_FILE, __ASSERT_LINE, \ | ^ test.cpp:10:14: note: in call to 'square(2)' 10 | auto val = square(2); | ^~~~~~~~~ 1 error generated.```
1 parent 6c1bb48 commit b77109f

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,10 @@ Improvements to Clang's diagnostics
525525

526526
- An error is now emitted when OpenMP ``collapse`` and ``ordered`` clauses have an
527527
argument larger than what can fit within a 64-bit integer.
528+
529+
- Explanatory note is printed when ``assert`` fails during evaluation of a
530+
constant expression. Prior to this, the error inaccurately implied that assert
531+
could not be used at all in a constant expression (#GH130458)
528532

529533
- A new off-by-default warning ``-Wms-bitfield-padding`` has been added to alert to cases where bit-field
530534
packing may differ under the MS struct ABI (#GH117428).

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ def err_ice_too_large : Error<
164164
"integer constant expression evaluates to value %0 that cannot be "
165165
"represented in a %1-bit %select{signed|unsigned}2 integer type">;
166166
def err_expr_not_string_literal : Error<"expression is not a string literal">;
167+
def note_constexpr_assert_failed : Note<
168+
"assertion failed during evaluation of constant expression">;
167169

168170
// Semantic analysis of constant literals.
169171
def ext_predef_outside_function : Warning<

clang/lib/AST/ExprConstant.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5975,9 +5975,22 @@ static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
59755975
Definition->hasAttr<MSConstexprAttr>())))
59765976
return true;
59775977

5978-
if (Info.getLangOpts().CPlusPlus11) {
5979-
const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
5978+
const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
5979+
// Special note for the assert() macro, as the normal error message falsely
5980+
// implies we cannot use an assertion during constant evaluation.
5981+
if (CallLoc.isMacroID() && DiagDecl->getIdentifier()) {
5982+
// FIXME: Instead of checking for an implementation-defined function,
5983+
// check and evaluate the assert() macro.
5984+
StringRef Name = DiagDecl->getName();
5985+
bool AssertFailed =
5986+
Name == "__assert_rtn" || Name == "__assert_fail" || Name == "_wassert";
5987+
if (AssertFailed) {
5988+
Info.FFDiag(CallLoc, diag::note_constexpr_assert_failed);
5989+
return false;
5990+
}
5991+
}
59805992

5993+
if (Info.getLangOpts().CPlusPlus11) {
59815994
// If this function is not constexpr because it is an inherited
59825995
// non-constexpr constructor, diagnose that directly.
59835996
auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20_plus -DTEST_LINUX %s
2+
// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20_plus -DTEST_WINDOWS %s
3+
// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20_plus -DTEST_DARWIN %s
4+
5+
#ifdef __ASSERT_FUNCTION
6+
#undef __ASSERT_FUNCTION
7+
#endif
8+
9+
#if defined(TEST_LINUX)
10+
extern "C" void __assert_fail(const char*, const char*, unsigned, const char*);
11+
#define assert(cond) \
12+
((cond) ? (void)0 : __assert_fail(#cond, __FILE__, __LINE__, __func__))
13+
#elif defined(TEST_DARWIN)
14+
void __assert_rtn(const char *, const char *, int, const char *);
15+
#define assert(cond) \
16+
(__builtin_expect(!(cond), 0) ? __assert_rtn(__func__, __FILE__, __LINE__, #cond) : (void)0)
17+
#elif defined(TEST_WINDOWS)
18+
void /*__cdecl*/ _wassert(const wchar_t*, const wchar_t*, unsigned);
19+
#define _CRT_WIDE_(s) L ## s
20+
#define _CRT_WIDE(s) _CRT_WIDE_(s)
21+
#define assert(cond) \
22+
(void)((!!(cond)) || (_wassert(_CRT_WIDE(#cond), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0))
23+
#endif
24+
25+
consteval int square(int x) {
26+
int result = x * x;
27+
assert(result == 42); // expected-note {{assertion failed during evaluation of constant expression}}
28+
return result;
29+
}
30+
31+
void test() {
32+
auto val = square(2); // expected-note {{in call to 'square(2)'}} \
33+
// expected-error {{call to consteval function 'square' is not a constant expression}}
34+
}

0 commit comments

Comments
 (0)