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

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 18:54:37 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Sendao Yan (sendaoYan)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/245853.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/CGExprScalar.cpp (+2-1) 
- (modified) clang/test/CodeGen/enum-bool.c (+5) 


``````````diff
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 3d00ddff57ff7..b8d61b8887fc4 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 e98ecce22ee8d..872834d573f1b 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

``````````

</details>


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


More information about the cfe-commits mailing list