[clang] 09f6992 - [clang][bytecode] Don't implicitly begin union member lifetime... (#192212)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 16 05:39:16 PDT 2026
Author: Timm Baeder
Date: 2026-04-16T14:39:11+02:00
New Revision: 09f6992a4bdd7f8e06c8640d85b60d673a4b7297
URL: https://github.com/llvm/llvm-project/commit/09f6992a4bdd7f8e06c8640d85b60d673a4b7297
DIFF: https://github.com/llvm/llvm-project/commit/09f6992a4bdd7f8e06c8640d85b60d673a4b7297.diff
LOG: [clang][bytecode] Don't implicitly begin union member lifetime... (#192212)
... on assignment operator calls if the LHS type does not have a
non-deleted trivial default constructor.
Added:
Modified:
clang/lib/AST/ByteCode/Compiler.cpp
clang/test/AST/ByteCode/unions.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 59510612d9617..03bd1f48addc7 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -5601,12 +5601,15 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
SmallVector<const Expr *, 8> Args(ArrayRef(E->getArgs(), E->getNumArgs()));
bool IsAssignmentOperatorCall = false;
+ bool ActivateLHS = false;
if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
OCE && OCE->isAssignmentOp()) {
// Just like with regular assignments, we need to special-case assignment
// operators here and evaluate the RHS (the second arg) before the LHS (the
// first arg). We fix this by using a Flip op later.
assert(Args.size() == 2);
+ const CXXRecordDecl *LHSRecord = Args[0]->getType()->getAsCXXRecordDecl();
+ ActivateLHS = LHSRecord && LHSRecord->hasTrivialDefaultConstructor();
IsAssignmentOperatorCall = true;
std::reverse(Args.begin(), Args.end());
}
@@ -5684,7 +5687,7 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
return false;
}
- if (!this->visitCallArgs(Args, FuncDecl, IsAssignmentOperatorCall,
+ if (!this->visitCallArgs(Args, FuncDecl, ActivateLHS,
isa<CXXOperatorCallExpr>(E)))
return false;
diff --git a/clang/test/AST/ByteCode/unions.cpp b/clang/test/AST/ByteCode/unions.cpp
index 8cf1d414b70b1..8c6f52c837374 100644
--- a/clang/test/AST/ByteCode/unions.cpp
+++ b/clang/test/AST/ByteCode/unions.cpp
@@ -1007,4 +1007,39 @@ namespace NonTrivialUnionCtor {
static_assert(j()); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
}
+
+/// u.a should not implicitly get activated when assigning to it, since A
+/// does not have a non-deleted trivial default constructor.
+namespace NoTrivialCtor {
+ struct A {
+ int i;
+ constexpr A() : i(0) {}
+ constexpr A(int i) : i(i) {}
+ };
+
+ constexpr auto test() {
+ union U {
+ int i;
+ A a;
+ } u{.i = 0};
+
+ u.a = {123}; // both-note {{member call on member 'a' of union with active member 'i'}}
+ return u.a.i;
+ }
+
+ constexpr auto r = test(); // both-error {{must be initialized by a constant expression}} \
+ // both-note {{in call to}}
+
+ /// Should still work if the LHS is not a record type.
+ enum byte {};
+ constexpr byte operator|=(byte a, byte b) {
+ return byte{};
+ }
+ constexpr int foo() {
+ byte b{};
+ b |= byte{};
+ return 10;
+ }
+ static_assert(foo() == 10);
+}
#endif
More information about the cfe-commits
mailing list