[clang] [clang] Allow fixed-point to bool enumeration conversions (PR #245853)

Sendao Yan via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 18:53:46 PST 2026


https://github.com/sendaoYan created https://github.com/llvm/llvm-project/pull/245853

Fixes #245850.

Since #244494, `Type::getScalarTypeKind()` returns `STK_Bool` for an enumeration
with a fixed bool underlying type, so converting a fixed-point value to such an
enumeration selects `CK_FixedPointToBoolean`. `VisitCastExpr()` still asserts
that the destination of that cast kind is `bool`, which crashes an assertions
build:

```c
    enum E : bool { F, T };
    enum E to_enum(_Accum x) { return (enum E)x; }   /* clang -std=c23 -ffixed-point */
```

`EmitScalarConversion()` already treats such enumerations as bool, so only the
assertion needs relaxing.

Tests: new `_Accum` case in `clang/test/CodeGen/enum-bool.c`.

Note: found while testing #244494, see
https://github.com/llvm/llvm-project/pull/244256#issuecomment-7138866498

>From e437dbaf59acc5170f7127a80d676721744f1af1 Mon Sep 17 00:00:00 2001
From: Sendao Yan <yansendao at 126.com>
Date: Wed, 25 Nov 2026 10:24:15 +0800
Subject: [PATCH] [clang] Allow fixed-point to bool enumeration conversions

Since #244494, Type::getScalarTypeKind() returns STK_Bool for an
enumeration with a fixed bool underlying type, so converting a
fixed-point value to such an enumeration selects CK_FixedPointToBoolean.
VisitCastExpr() still asserted that the destination of that cast kind is
bool, which crashed an assertions build:

  enum E : bool { F, T };
  enum E to_enum(_Accum x) { return (enum E)x; }

EmitScalarConversion() already treats such enumerations as bool, so only
the assertion needs relaxing.

Fixes https://github.com/llvm/llvm-project/issues/245850
---
 clang/lib/CodeGen/CGExprScalar.cpp | 3 ++-
 clang/test/CodeGen/enum-bool.c     | 5 +++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 3d00ddff57ff79..b8d61b8887fc44 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -3051,7 +3051,8 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
   case CK_FixedPointToBoolean:
     assert(E->getType()->isFixedPointType() &&
            "Expected src type to be fixed point type");
-    assert(DestTy->isBooleanType() && "Expected dest type to be boolean type");
+    assert((DestTy->isBooleanType() || hasBoolConversionSemantics(DestTy)) &&
+           "Expected dest type to be a boolean type");
     return EmitScalarConversion(Visit(E), E->getType(), DestTy,
                                 CE->getExprLoc());
 
diff --git a/clang/test/CodeGen/enum-bool.c b/clang/test/CodeGen/enum-bool.c
index e98ecce22ee8df..872834d573f1bb 100644
--- a/clang/test/CodeGen/enum-bool.c
+++ b/clang/test/CodeGen/enum-bool.c
@@ -90,3 +90,8 @@ void o(_Atomic(enum E) *p) { ++*p; }
 // CHECK-LABEL: define{{.*}} void @o(
 // CHECK-NOT: atomicrmw add
 // CHECK: store atomic i8 1, ptr %{{.*}} seq_cst
+
+enum E r(_Accum x) { return (enum E)x; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @r(
+// CHECK: %tobool = icmp ne i32 %{{.*}}, 0
+// CHECK: ret i1 %tobool



More information about the cfe-commits mailing list