[clang] [clang][bytecode] Don't implicitly begin union member lifetime... (PR #192212)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 15 02:14:22 PDT 2026
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/192212
>From eb7e034c9e83e6646f916eacac0d3641524d0a5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 15 Apr 2026 10:52:42 +0200
Subject: [PATCH] [clang][bytecode] Don't implicitly begin union member
lifetime...
... on assignment operator calls if the LHS type does not have a
non-deleted trivial default constructor.
---
clang/lib/AST/ByteCode/Compiler.cpp | 5 ++++-
clang/test/AST/ByteCode/unions.cpp | 23 +++++++++++++++++++++++
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 59510612d9617..f131b9eab3cbf 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -5601,6 +5601,7 @@ 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
@@ -5609,6 +5610,8 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
assert(Args.size() == 2);
IsAssignmentOperatorCall = true;
std::reverse(Args.begin(), Args.end());
+ const CXXRecordDecl *LHSRecord = Args[0]->getType()->getAsCXXRecordDecl();
+ ActivateLHS = !LHSRecord || LHSRecord->hasTrivialDefaultConstructor();
}
// Calling a static operator will still
// pass the instance, but we don't need it.
@@ -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 2123a932fce10..2422b76a8bd6e 100644
--- a/clang/test/AST/ByteCode/unions.cpp
+++ b/clang/test/AST/ByteCode/unions.cpp
@@ -1007,4 +1007,27 @@ 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}}
+}
#endif
More information about the cfe-commits
mailing list