[clang] [clang][SemaCXX] Fix crash when using static_cast to _Atomic types in C++ (PR #207616)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 5 14:54:17 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: AZero13 (AZero13)
<details>
<summary>Changes</summary>
This PR fixes a compiler crash (assertion failure) in both the constant evaluator (`ExprConstant.cpp`) and LLVM CodeGen (`CGExprAgg.cpp`) when evaluating a static_cast to an _Atomic type in C++.
When evaluating casts, CastOperation::CastOperation in was unconditionally stripping the _Atomic qualifier from the destination type using getAtomicUnqualifiedType(). This behavior is correct for C23 (under C23 6.5.4p6, where cast expressions yield the non-atomic version of the type), but it is incorrect for C++ where _Atomic(T) is treated as a distinct type wrapper.
Modify CastOperation::CastOperation to only strip the _Atomic qualifier in C mode. In C++ mode, we preserve the _Atomic wrapper, ensuring that InitializationSequence properly evaluates the target type as _Atomic(T) and adds the implicit CK_NonAtomicToAtomic conversion step.
Fixes #<!-- -->207610.
---
Full diff: https://github.com/llvm/llvm-project/pull/207616.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaCast.cpp (+4-1)
- (modified) clang/test/SemaCXX/atomic-type.cpp (+9)
``````````diff
diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp
index 133837623a7a6..bb137a4bf19d7 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -78,7 +78,10 @@ namespace {
// the qualifier.
if (!S.Context.getLangOpts().ObjC && !DestType->isRecordType() &&
!DestType->isArrayType() && !DestType.getPointerAuth()) {
- DestType = DestType.getAtomicUnqualifiedType();
+ if (S.Context.getLangOpts().CPlusPlus)
+ DestType = DestType.getUnqualifiedType();
+ else
+ DestType = DestType.getAtomicUnqualifiedType();
}
if (const BuiltinType *placeholder =
diff --git a/clang/test/SemaCXX/atomic-type.cpp b/clang/test/SemaCXX/atomic-type.cpp
index 3200a59bc86a2..b8f8f184da643 100644
--- a/clang/test/SemaCXX/atomic-type.cpp
+++ b/clang/test/SemaCXX/atomic-type.cpp
@@ -111,3 +111,12 @@ namespace non_trivially_copyable {
_Atomic S s; // expected-error {{_Atomic cannot be applied to type 'S' which is not trivially copyable}} \
// expected-warning {{'_Atomic' is a C11 extension}}
}
+
+namespace static_cast_crash {
+ struct S { char a; };
+ void static_cast_non_atomic_to_atomic() {
+ _Atomic struct S a; // expected-warning {{'_Atomic' is a C11 extension}}
+ a = static_cast<_Atomic(struct S)>(S()); // expected-warning {{'_Atomic' is a C11 extension}}
+ }
+}
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/207616
More information about the cfe-commits
mailing list