https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119497
Bug ID: 119497
Summary: Improve diagnostic for failed assert(expr) in
constexpr context
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: enhancement
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
Target Milestone: ---
When an assert fails in a constexpr function we get:
a.cc:5:3: error: call to non-'constexpr' function 'void __assert_fail(const
char*, const char*, unsigned int, const char*)'
5 | assert(i);
| ^~~~~~
/usr/include/assert.h:67:13: note: 'void __assert_fail(const char*, const
char*, unsigned int, const char*)' declared here
67 | extern void __assert_fail (const char *__assertion, const char *__file,
| ^~~~~~~~~~~~~
As suggested in https://github.com/llvm/llvm-project/pull/130458 it would be
more user-friendly to say that an assertion failed, rather than say why the
function wasn't constexpr. The proposal there is:
test.cpp:5:3: note: assertion failed in consteval context: 'result == 42'
which is done by recognising when the failure is in the expansion of a macro
called 'assert' and when the non-constexpr function that causes the error has a
name like __assert_fail or _wassert
Solaris: __assert_c99
AIX: __assert_c99
macOS: __assert_rtn ?
OpenBSD: __assert and __assert2
FreeBSD: __assert
Another way to word it could be:
note: failed assertion is not a constant expression: '...'