[clang] [Clang] Fix crash when comparing fixed point type with BitInt (PR #199912)
via cfe-commits
cfe-commits at lists.llvm.org
Thu May 28 00:56:07 PDT 2026
https://github.com/superdusty updated https://github.com/llvm/llvm-project/pull/199912
>From 8b793c0202da13b640bb22a7b3fb3d0646690714 Mon Sep 17 00:00:00 2001
From: cry <2091136672 at foxmail.com>
Date: Thu, 28 May 2026 12:57:42 +0800
Subject: [PATCH 1/6] [Clang] Fix crash when comparing fixed point type with
BitInt
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaExpr.cpp | 5 ++++-
clang/test/Sema/fixed-point-bitint.c | 8 ++++++++
3 files changed, 13 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Sema/fixed-point-bitint.c
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6838cf3defcc1..f8c421824cb7b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -559,6 +559,7 @@ Improvements to Coverage Mapping
Bug Fixes in This Version
-------------------------
+- Fixed a crash when comparing a fixed point type with a ``_BitInt`` type. (#GH196948)
- Fixed atomic boolean compound assignment; the conversion back to atomic bool would be miscompiled. (#GH33210)
- Correctly handle default template argument when establishing subsumption. (#GH188640)
- Fixed a failed assertion in the preprocessor when ``__has_embed`` parameters are missing parentheses. (#GH175088)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 521a8516ac179..d88e34fcc43fc 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1287,6 +1287,10 @@ static QualType handleFloatConversion(Sema &S, ExprResult &LHS,
/// Helper function of UsualArithmeticConversions().
static bool unsupportedTypeConversion(const Sema &S, QualType LHSType,
QualType RHSType) {
+ if ((LHSType->isFixedPointType() && RHSType->isBitIntType()) ||
+ (LHSType->isBitIntType() && RHSType->isFixedPointType()))
+ return true;
+
// No issue if either is not a floating point type.
if (!LHSType->isFloatingType() || !RHSType->isFloatingType())
return false;
@@ -1533,7 +1537,6 @@ static QualType handleFixedPointConversion(Sema &S, QualType LHSTy,
RHSTy->isFixedPointOrIntegerType()) &&
"Special fixed point arithmetic operation conversions are only "
"applied to ints or other fixed point types");
-
// If one operand has signed fixed-point type and the other operand has
// unsigned fixed-point type, then the unsigned fixed-point operand is
// converted to its corresponding signed fixed-point type and the resulting
diff --git a/clang/test/Sema/fixed-point-bitint.c b/clang/test/Sema/fixed-point-bitint.c
new file mode 100644
index 0000000000000..d9ebf9fd61dbe
--- /dev/null
+++ b/clang/test/Sema/fixed-point-bitint.c
@@ -0,0 +1,8 @@
+// RUN: %%clang_cc1 -ffixed-point -fsyntax-only -verify %%s
+
+// Test that comparing fixed point type with BitInt doesn't crash
+// Fixes issue #196948
+
+constexpr _BitInt(128) i = 42;
+static_assert(i == 42.0k);
+// expected-error at -1 {{invalid operands to binary expression}}
>From 01d90659ecaf45aaad6758756c0a205135b3f4e3 Mon Sep 17 00:00:00 2001
From: superdusty <superdusty at foxmail.com>
Date: Thu, 28 May 2026 13:35:27 +0800
Subject: [PATCH 2/6] Update fixed-point-bitint.c
---
clang/test/Sema/fixed-point-bitint.c | 3 ---
1 file changed, 3 deletions(-)
diff --git a/clang/test/Sema/fixed-point-bitint.c b/clang/test/Sema/fixed-point-bitint.c
index d9ebf9fd61dbe..78f7cb787d571 100644
--- a/clang/test/Sema/fixed-point-bitint.c
+++ b/clang/test/Sema/fixed-point-bitint.c
@@ -1,8 +1,5 @@
// RUN: %%clang_cc1 -ffixed-point -fsyntax-only -verify %%s
-// Test that comparing fixed point type with BitInt doesn't crash
-// Fixes issue #196948
-
constexpr _BitInt(128) i = 42;
static_assert(i == 42.0k);
// expected-error at -1 {{invalid operands to binary expression}}
>From 6410f56d63a0aad05e46592e046ea848ae5c89d0 Mon Sep 17 00:00:00 2001
From: superdusty <superdusty at foxmail.com>
Date: Thu, 28 May 2026 13:38:16 +0800
Subject: [PATCH 3/6] Update ReleaseNotes.rst
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f8c421824cb7b..bf44a2a4e3d11 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -559,7 +559,7 @@ Improvements to Coverage Mapping
Bug Fixes in This Version
-------------------------
-- Fixed a crash when comparing a fixed point type with a ``_BitInt`` type. (#GH196948)
+- Fixed an assertion when comparing a fixed point type with a ``_BitInt`` type. (#GH196948)
- Fixed atomic boolean compound assignment; the conversion back to atomic bool would be miscompiled. (#GH33210)
- Correctly handle default template argument when establishing subsumption. (#GH188640)
- Fixed a failed assertion in the preprocessor when ``__has_embed`` parameters are missing parentheses. (#GH175088)
>From f1d0967f0b7a3f27749806ac1fadb3a20a26e534 Mon Sep 17 00:00:00 2001
From: superdusty <superdusty at foxmail.com>
Date: Thu, 28 May 2026 13:42:13 +0800
Subject: [PATCH 4/6] Update SemaExpr.cpp
---
clang/lib/Sema/SemaExpr.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d88e34fcc43fc..e6bad816d2d0a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1285,6 +1285,9 @@ static QualType handleFloatConversion(Sema &S, ExprResult &LHS,
/// Diagnose attempts to convert between __float128, __ibm128 and
/// long double if there is no support for such conversion.
/// Helper function of UsualArithmeticConversions().
+/// Returns true if the conversion between LHSType and RHSType is not supported.
+/// This includes conversions between fixed point types and BitInt types,
+/// which would otherwise cause an assertion failure later.
static bool unsupportedTypeConversion(const Sema &S, QualType LHSType,
QualType RHSType) {
if ((LHSType->isFixedPointType() && RHSType->isBitIntType()) ||
>From b160488c1f5431e070c061d3674068e720af0d21 Mon Sep 17 00:00:00 2001
From: superdusty <superdusty at foxmail.com>
Date: Thu, 28 May 2026 13:42:57 +0800
Subject: [PATCH 5/6] Update SemaExpr.cpp
---
clang/lib/Sema/SemaExpr.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e6bad816d2d0a..aebb3a8bdc657 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1540,6 +1540,7 @@ static QualType handleFixedPointConversion(Sema &S, QualType LHSTy,
RHSTy->isFixedPointOrIntegerType()) &&
"Special fixed point arithmetic operation conversions are only "
"applied to ints or other fixed point types");
+
// If one operand has signed fixed-point type and the other operand has
// unsigned fixed-point type, then the unsigned fixed-point operand is
// converted to its corresponding signed fixed-point type and the resulting
>From c398595fb509bc4631b62a446e1e67a1e9a7dc5c Mon Sep 17 00:00:00 2001
From: superdusty <superdusty at foxmail.com>
Date: Thu, 28 May 2026 15:55:54 +0800
Subject: [PATCH 6/6] Update SemaExpr.cpp
---
clang/lib/Sema/SemaExpr.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index aebb3a8bdc657..650976250ec61 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1540,7 +1540,7 @@ static QualType handleFixedPointConversion(Sema &S, QualType LHSTy,
RHSTy->isFixedPointOrIntegerType()) &&
"Special fixed point arithmetic operation conversions are only "
"applied to ints or other fixed point types");
-
+
// If one operand has signed fixed-point type and the other operand has
// unsigned fixed-point type, then the unsigned fixed-point operand is
// converted to its corresponding signed fixed-point type and the resulting
More information about the cfe-commits
mailing list