Skip to content

Conversation

tbaederr
Copy link
Contributor

Don't create CK_BitCast casts from nullptr_t to bool.

Fixes #155126

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 26, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Don't create CK_BitCast casts from nullptr_t to bool.

Fixes #155126


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

3 Files Affected:

  • (modified) clang/lib/Sema/SemaCast.cpp (+6-1)
  • (added) clang/test/CodeGen/issue155126.c (+11)
  • (modified) clang/test/Sema/constexpr.c (+2)
diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp
index 933a6c558e7ac..72c5e37c8e2ea 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -3171,7 +3171,12 @@ void CastOperation::CheckCStyleCast() {
       SrcExpr = ExprError();
       return;
     }
-    if (!DestType->isNullPtrType()) {
+    if (DestType->isBooleanType()) {
+      SrcExpr = ImplicitCastExpr::Create(
+          Self.Context, DestType, CK_PointerToBoolean, SrcExpr.get(), nullptr,
+          VK_PRValue, Self.CurFPFeatureOverrides());
+
+    } else if (!DestType->isNullPtrType()) {
       // Implicitly cast from the null pointer type to the type of the
       // destination.
       CastKind CK = DestType->isPointerType() ? CK_NullToPointer : CK_BitCast;
diff --git a/clang/test/CodeGen/issue155126.c b/clang/test/CodeGen/issue155126.c
new file mode 100644
index 0000000000000..ec15355d8d7df
--- /dev/null
+++ b/clang/test/CodeGen/issue155126.c
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c23 -verify %s
+// RUN: %clang_cc1 -std=c23 -verify -fexperimental-new-constant-interpreter %s
+
+// expected-no-diagnostics
+
+enum e : bool { b = true };
+void foo ()
+{
+  enum e e1;
+  e1 = (bool) nullptr;
+}
diff --git a/clang/test/Sema/constexpr.c b/clang/test/Sema/constexpr.c
index 3dcb0b3a7d95f..e8f0700c5894f 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -391,3 +391,5 @@ void ghissue109095() {
   _Static_assert(i == c[0]); // expected-error {{static assertion expression is not an integral constant expression}}\
                              // expected-note {{initializer of 'i' is not a constant expression}}
 }
+constexpr bool b2 = (bool)nullptr;
+_Static_assert(!b2);

Copy link
Contributor

@Fznamznon Fznamznon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A release note probably makes sense.

@tbaederr tbaederr merged commit 5a9f103 into llvm:main Aug 28, 2025
9 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 28, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-aarch64-darwin running on doug-worker-5 while building clang at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/190/builds/26289

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'Clang :: CodeGen/issue155126.c' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/lib/clang/22/include -nostdsysteminc -std=c23 /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/clang/test/CodeGen/issue155126.c -emit-llvm -o - | /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/clang/test/CodeGen/issue155126.c # RUN: at line 2
+ /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/lib/clang/22/include -nostdsysteminc -std=c23 /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/clang/test/CodeGen/issue155126.c -emit-llvm -o -
+ /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/clang/test/CodeGen/issue155126.c
�[1m/Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/clang/test/CodeGen/issue155126.c:6:17: �[0m�[0;1;31merror: �[0m�[1mCHECK-LABEL: expected string not found in input
�[0m// CHECK-LABEL: define dso_local void @foo(
�[0;1;32m                ^
�[0m�[1m<stdin>:1:1: �[0m�[0;1;30mnote: �[0m�[1mscanning from here
�[0m; ModuleID = '/Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/clang/test/CodeGen/issue155126.c'
�[0;1;32m^
�[0m�[1m<stdin>:7:1: �[0m�[0;1;30mnote: �[0m�[1mpossible intended match here
�[0mdefine void @foo() #0 {
�[0;1;32m^
�[0m
Input file: <stdin>
Check file: /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/clang/test/CodeGen/issue155126.c

-dump-input=help explains the following input dump.

Input was:
<<<<<<
�[1m�[0m�[0;1;30m           1: �[0m�[1m�[0;1;46m; ModuleID = '/Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/clang/test/CodeGen/issue155126.c' �[0m
�[0;1;31mlabel:6'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
�[0m�[0;1;30m           2: �[0m�[1m�[0;1;46msource_filename = "/Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/clang/test/CodeGen/issue155126.c" �[0m
�[0;1;31mlabel:6'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
�[0m�[0;1;30m           3: �[0m�[1m�[0;1;46mtarget datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32" �[0m
�[0;1;31mlabel:6'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
�[0m�[0;1;30m           4: �[0m�[1m�[0;1;46mtarget triple = "arm64-apple-darwin23.6.0" �[0m
�[0;1;31mlabel:6'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
�[0m�[0;1;30m           5: �[0m�[1m�[0;1;46m �[0m
�[0;1;31mlabel:6'0     ~
�[0m�[0;1;30m           6: �[0m�[1m�[0;1;46m; Function Attrs: noinline nounwind optnone �[0m
�[0;1;31mlabel:6'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
�[0m�[0;1;30m           7: �[0m�[1m�[0;1;46mdefine void @foo() #0 { �[0m
�[0;1;31mlabel:6'0     ~~~~~~~~~~~~~~~~~~~~~~~~
�[0m�[0;1;35mlabel:6'1     ?                        possible intended match
�[0m�[0;1;30m           8: �[0m�[1m�[0;1;46mentry: �[0m
�[0;1;31mlabel:6'0     ~~~~~~~
�[0m�[0;1;30m           9: �[0m�[1m�[0;1;46m %e1 = alloca i8, align 1 �[0m
�[0;1;31mlabel:6'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~
�[0m�[0;1;30m          10: �[0m�[1m�[0;1;46m store i8 0, ptr %e1, align 1 �[0m
�[0;1;31mlabel:6'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
�[0m�[0;1;30m          11: �[0m�[1m�[0;1;46m ret void �[0m
�[0;1;31mlabel:6'0     ~~~~~~~~~~
�[0m�[0;1;30m          12: �[0m�[1m�[0;1;46m} �[0m
�[0;1;31mlabel:6'0     ~~
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang][bytecode] Assertion `isPtrType(*ToT)' failed.
5 participants