[clang] [Clang] prevent an assertion failure caused by C++ constant expression checks in C23 floating conversions (PR #174113)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 31 13:18:33 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Oleksandr T. (a-tarasyuk)
<details>
<summary>Changes</summary>
Fixes #<!-- -->173847
---
This patch addresses an assertion failure during compilation of C23 code involving floating-point conversions.
As part of the C23 constexpr support introduced in PR #<!-- -->73099, Clang began reusing parts of the C++ constant evaluation and narrowing logic. In C23 mode, a failed constant evaluation caused the condition to proceed to C++ constant-expression checks, resulting in an assertion failure.
This change evaluates constants using `EvaluateAsRValue` in C23 mode and restricts C++ constant-expression checks to C++ mode.
---
Full diff: https://github.com/llvm/llvm-project/pull/174113.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+1)
- (modified) clang/lib/Sema/SemaOverload.cpp (+2-1)
- (modified) clang/test/Sema/constexpr.c (+8)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ecdbbc05cdef4..4bc5a30440b3c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -623,6 +623,7 @@ Bug Fixes to C++ Support
- Fix the result of ``__is_pointer_interconvertible_base_of`` when arguments are qualified and passed via template parameters. (#GH135273)
- Fixed a crash when evaluating nested requirements in requires-expressions that reference invented parameters. (#GH166325)
- Fixed a crash when standard comparison categories (e.g. ``std::partial_ordering``) are defined with incorrect static member types. (#GH170015) (#GH56571)
+- Fixed an assertion failure in floating conversion narrowing caused by C++ constant expression checks in C23 mode. (#GH173847)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index bc3cfe7ef9a0c..aa563f772c89e 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -449,7 +449,8 @@ NarrowingKind StandardConversionSequence::getNarrowingKind(
Expr::EvalResult R;
if ((Ctx.getLangOpts().C23 && Initializer->EvaluateAsRValue(R, Ctx)) ||
- Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) {
+ ((Ctx.getLangOpts().CPlusPlus &&
+ Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)))) {
// Constant!
if (Ctx.getLangOpts().C23)
ConstantValue = R.Val;
diff --git a/clang/test/Sema/constexpr.c b/clang/test/Sema/constexpr.c
index e9b738ab4d190..16255c264423a 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -401,3 +401,11 @@ bool issue155507(v2int16_t a, v2int16_t b) {
constexpr bool b2 = (bool)nullptr;
_Static_assert(!b2);
+
+double ghissue173847(double a) {
+ double result = 3.0 / (a + 4.5 - 2.1 * 0.7);
+ return result;
+}
+void ghissue173847_test() {
+ constexpr float f_const = ghissue173847(2.0); // expected-error {{constexpr variable 'f_const' must be initialized by a constant expression}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/174113
More information about the cfe-commits
mailing list