[clang] [Clang] prevent an assertion failure caused by C++ constant expression checks in C23 floating conversions (PR #174113)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 7 03:13:11 PST 2026
https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/174113
>From e92e6c74f7134d02bb86b8048998cd135aa38f63 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <oleksandr.tarasiuk at outlook.com>
Date: Wed, 31 Dec 2025 22:57:20 +0200
Subject: [PATCH] [Clang] prevent an assertion failure caused by C++ constant
expression checks in C23 floating conversions
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaOverload.cpp | 3 ++-
clang/test/Sema/constexpr.c | 8 ++++++++
3 files changed, 11 insertions(+), 1 deletion(-)
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}}
+}
More information about the cfe-commits
mailing list