Author: Oleksandr T.
Date: 2026-01-12T15:50:05+02:00
New Revision: 8380b57b7ec4adc715c7e4458dedb07e586b9477

URL: 
https://github.com/llvm/llvm-project/commit/8380b57b7ec4adc715c7e4458dedb07e586b9477
DIFF: 
https://github.com/llvm/llvm-project/commit/8380b57b7ec4adc715c7e4458dedb07e586b9477.diff

LOG: [Clang] prevent an assertion failure caused by C++ constant expression 
checks in C23 floating conversions (#174113)

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.

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaOverload.cpp
    clang/test/Sema/constexpr.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 165e7f186acf3..c56d2015f7a3f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -654,6 +654,7 @@ Bug Fixes to C++ Support
 - Fixed a crash when parsing the ``enable_if`` attribute on C function 
declarations with identifier-list parameters. (#GH173826)
 - Fixed an assertion failure triggered by nested lambdas during capture 
handling. (#GH172814)
 - Fixed an assertion failure in vector conversions involving 
instantiation-dependent template expressions. (#GH173347)
+- 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 a7a3b7900ccc2..7115b8b7d446a 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}}
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to