[clang] Better diagnostics when assertion fails in `consteval` (PR #130458)

JJ Marr via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 10 10:43:44 PDT 2025


================
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20_plus %s
+
+#ifdef __ASSERT_FUNCTION
+#undef __ASSERT_FUNCTION
+#endif
+extern "C" void __assert_fail(const char*, const char*, unsigned, const char*);
+
+#define assert(cond) \
+  ((cond) ? (void)0 : __assert_fail(#cond, __FILE__, __LINE__, __func__))
+
+consteval int square(int x) {
+  int result = x * x;
+  assert(result == 42); // expected-note {{assertion failed in consteval context: 'result == 42'}}
----------------
jj-marr wrote:

Perhaps it should say "during constant evaluation". Both `constexpr` and `consteval` functions can be constant evaluated (i.e. evaluated at compile-time), but it's also possible for a `constexpr` function to be used at runtime, in which case the assertion would _not_ be triggered.

https://github.com/llvm/llvm-project/pull/130458


More information about the cfe-commits mailing list