https://github.com/mohitmishra786 created 
https://github.com/llvm/llvm-project/pull/244494

In C23, conversion to an enum with a fixed underlying type has the semantics of 
conversion to the underlying type (6.7.3.3p16). For `enum E : bool` clang 
truncated instead of comparing against zero, so `(enum E : bool)2` was 0, and 
converting a double like 2.5 emitted `fptoui double to i1` (poison), giving 
different results at -O0 and -O3.

`PrepareScalarCast` now treats such an enum like `bool`, so it gets the 
`*ToBoolean` cast kinds. C++ `static_cast` already does this. Complex sources 
needed one more change in CodeGen: `EmitComplexToScalarConversion` checked 
`DstTy->isBooleanType()`, which is false for the enum, so the 
`*ComplexToBoolean` case now converts to `bool`.

Tests: new CodeGen/enum-bool.c (explicit cast, implicit return, init, 
assignment, int/double/long long/complex sources) and static_asserts in 
C/C23/n3030.c for the constant evaluator. Sema, SemaCXX, CodeGen, C and AST 
tests show no new failures.

Fixes #241289

>From 572bbf605e301f0c4880b12ea37d461a1d0f24d2 Mon Sep 17 00:00:00 2001
From: Mohit Mishra <[email protected]>
Date: Fri, 20 Nov 2026 08:40:00 +0530
Subject: [PATCH] [clang] Convert to a C23 enum with bool underlying type like
 bool

C23 6.7.3.3p16 says conversion to an enumerated type with a fixed
underlying type has the same semantics as conversion to the underlying
type. For `enum E : bool`, any nonzero value has to become true, but
PrepareScalarCast classified the enum as STK_Integral and used
CK_IntegralCast / CK_FloatingToIntegral, which truncate. So
`(enum E : bool)2` was 0, and a double 2.5 gave a poison fptoui.

Treat a destination enum with a fixed bool underlying type as STK_Bool
in PrepareScalarCast, which gives CK_IntegralToBoolean,
CK_FloatingToBoolean and the complex-to-boolean kinds. This matches what
C++ static_cast already does for such enums ([expr.static.cast]p8).

For the complex-to-boolean casts, CodeGen decided whether to compare
against zero by looking at the destination type, so it still took the
real part only for the enum. Convert to bool there instead, since the
cast kind already says the result is boolean.

Fixes #241289
---
 clang/docs/ReleaseNotes.md         |  1 +
 clang/lib/CodeGen/CGExprScalar.cpp |  6 ++--
 clang/lib/Sema/SemaExpr.cpp        | 21 ++++++++++----
 clang/test/C/C23/n3030.c           |  8 ++++++
 clang/test/CodeGen/enum-bool.c     | 44 ++++++++++++++++++++++++++++++
 5 files changed, 72 insertions(+), 8 deletions(-)
 create mode 100644 clang/test/CodeGen/enum-bool.c

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 5c6ba2d846013d..0c313db94308f0 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -952,6 +952,7 @@ documentation](CIR/index.md) for more information regarding 
ClangIR's status and
 - Fixed conversion from `float` to `bool` in `__builtin_convertvector`. 
(#GH228649)
 - Fixed `-print-dependency-directives-minimized-source` output for object-like 
macros starting with '(' so that it is not incorrectly turned into a 
function-like macro.
 - Fixed an exponential time-increase bug when collecting argument dependent 
lookup candidates when the same type is used several times at each level.
+- Fixed conversion to a C23 enumeration with an underlying type of `bool`, 
which truncated the value instead of converting it like `bool` (e.g. `(enum E : 
bool)2` was 0 instead of 1). (#GH241289)
 
 #### Bug Fixes to Compiler Builtins
 
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp 
b/clang/lib/CodeGen/CGExprScalar.cpp
index 7b698208cacfdb..b2a37e133ba15d 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -3148,9 +3148,11 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
   case CK_IntegralComplexToBoolean: {
     CodeGenFunction::ComplexPairTy V = CGF.EmitComplexExpr(E);
 
+    // The destination may be an enumeration whose underlying type is bool
+    // (C23 6.7.3.3p16), so convert to bool rather than to DestTy.
     // TODO: kill this function off, inline appropriate case here
-    return EmitComplexToScalarConversion(V, E->getType(), DestTy,
-                                         CE->getExprLoc());
+    return EmitComplexToScalarConversion(
+        V, E->getType(), CGF.getContext().BoolTy, CE->getExprLoc());
   }
 
   case CK_ZeroToOCLOpaqueType: {
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 571ef6ea6b19eb..2779bec26c2c16 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7983,6 +7983,15 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
   if (Context.hasSameUnqualifiedType(SrcTy, DestTy))
     return CK_NoOp;
 
+  // C23 6.7.3.3p16: conversion to an enumerated type with a fixed underlying
+  // type has the same semantics as conversion to the underlying type, so an
+  // enumeration with an underlying type of bool converts like bool.
+  Type::ScalarTypeKind DestKind = DestTy->getScalarTypeKind();
+  if (DestKind == Type::STK_Integral)
+    if (const EnumDecl *ED = DestTy->getAsEnumDecl();
+        ED && ED->isFixed() && ED->getIntegerType()->isBooleanType())
+      DestKind = Type::STK_Bool;
+
   switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) {
   case Type::STK_MemberPointer:
     llvm_unreachable("member pointer type in C");
@@ -7990,7 +7999,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
   case Type::STK_CPointer:
   case Type::STK_BlockPointer:
   case Type::STK_ObjCObjectPointer:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_CPointer: {
       LangAS SrcAS = SrcTy->getPointeeType().getAddressSpace();
       LangAS DestAS = DestTy->getPointeeType().getAddressSpace();
@@ -8024,7 +8033,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_FixedPoint:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_FixedPoint:
       return CK_FixedPointCast;
     case Type::STK_Bool:
@@ -8049,7 +8058,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
 
   case Type::STK_Bool: // casting from bool is like casting from an integer
   case Type::STK_Integral:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_CPointer:
     case Type::STK_ObjCObjectPointer:
     case Type::STK_BlockPointer:
@@ -8081,7 +8090,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_Floating:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_Floating:
       return CK_FloatingCast;
     case Type::STK_Bool:
@@ -8110,7 +8119,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_FloatingComplex:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_FloatingComplex:
       return CK_FloatingComplexCast;
     case Type::STK_IntegralComplex:
@@ -8144,7 +8153,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_IntegralComplex:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_FloatingComplex:
       return CK_IntegralComplexToFloatingComplex;
     case Type::STK_IntegralComplex:
diff --git a/clang/test/C/C23/n3030.c b/clang/test/C/C23/n3030.c
index 94ea7037edd117..d38184b50aee6c 100644
--- a/clang/test/C/C23/n3030.c
+++ b/clang/test/C/C23/n3030.c
@@ -59,6 +59,14 @@ constexpr int b = _Generic((enum underlying)b0, int: 2, 
unsigned char: 1, defaul
 static_assert(a == 1);
 static_assert(b == 1);
 
+// Conversion to an enumeration with an underlying type of bool has the
+// semantics of conversion to bool (C23 6.7.3.3p16).
+enum eb : bool { eb_false, eb_true };
+static_assert((enum eb)2 == eb_true);
+static_assert((enum eb)-1 == eb_true);
+static_assert((enum eb)0.5 == eb_true);
+static_assert((enum eb)0 == eb_false);
+
 void f1(enum a : long b); // expected-error {{non-defining declaration of 
enumeration with a fixed underlying type is only permitted as a standalone 
declaration; missing list of enumerators?}}
                           // expected-warning@-1 {{declaration of 'enum a' 
will not be visible outside of this function}}
 void f2(enum c : long{x} d);
diff --git a/clang/test/CodeGen/enum-bool.c b/clang/test/CodeGen/enum-bool.c
new file mode 100644
index 00000000000000..434174404a7953
--- /dev/null
+++ b/clang/test/CodeGen/enum-bool.c
@@ -0,0 +1,44 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -std=c23 -emit-llvm -o - %s 
| FileCheck %s
+
+// C23 6.7.3.3p16: conversion to an enumerated type with a fixed underlying
+// type has the same semantics as conversion to the underlying type. For an
+// underlying type of bool, any nonzero value converts to true, so the value
+// has to be compared against zero rather than truncated.
+
+enum E : bool { Zero, One };
+
+enum E a(int x) { return (enum E)x; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @a(
+// CHECK-NOT: trunc
+// CHECK: %tobool = icmp ne i32 %{{.*}}, 0
+// CHECK: ret i1 %tobool
+
+enum E b(int x) { return x; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @b(
+// CHECK-NOT: trunc
+// CHECK: %tobool = icmp ne i32 %{{.*}}, 0
+// CHECK: ret i1 %tobool
+
+enum E c(double x) { return (enum E)x; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @c(
+// CHECK-NOT: fptoui
+// CHECK: %tobool = fcmp une double %{{.*}}, 0.000000e+00
+// CHECK: ret i1 %tobool
+
+enum E d(double x) { enum E e = x; return e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @d(
+// CHECK-NOT: fptoui
+// CHECK: %tobool = fcmp une double %{{.*}}, 0.000000e+00
+
+enum E f(long long x) { enum E e; e = x; return e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @f(
+// CHECK-NOT: trunc
+// CHECK: %tobool = icmp ne i64 %{{.*}}, 0
+
+enum E g(_Complex double x) { return (enum E)x; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @g(
+// CHECK-NOT: fptoui
+// CHECK: fcmp une double
+// CHECK: fcmp une double
+// CHECK: %[[OR:.*]] = or i1
+// CHECK: ret i1 %[[OR]]

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

Reply via email to