[clang] [clang][bytecode] Fix non-defaulted union copy/move ctors (PR #199394)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sat May 23 21:44:21 PDT 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/199394
They are like regular record ctors.
>From 0f002a63c4d46535cec3b723ad88c7fab5c94666 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 24 May 2026 06:42:58 +0200
Subject: [PATCH] [clang][bytecode] Fix non-defaulted union copy/move ctors
They are like regular record ctors.
---
clang/lib/AST/ByteCode/Compiler.cpp | 4 ++--
clang/test/AST/ByteCode/unions.cpp | 12 ++++++++++++
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 4d13cd7139f83..1e58d91861ad5 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -6856,8 +6856,8 @@ bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
return false;
bool IsUnion = R->isUnion();
- // Union copy and move ctors are special.
- if (IsUnion && Ctor->isCopyOrMoveConstructor()) {
+ // Default union copy and move ctors are special.
+ if (IsUnion && Ctor->isCopyOrMoveConstructor() && Ctor->isDefaulted()) {
LocOverrideScope<Emitter> LOS(this, SourceInfo{});
// No special case for NumFields == 0 here, so the Memcpy op
diff --git a/clang/test/AST/ByteCode/unions.cpp b/clang/test/AST/ByteCode/unions.cpp
index 399c4c891be00..17d868325f1a7 100644
--- a/clang/test/AST/ByteCode/unions.cpp
+++ b/clang/test/AST/ByteCode/unions.cpp
@@ -386,6 +386,18 @@ namespace CopyCtor {
static_assert(y.a == 42, "");
static_assert(y.b == 42, ""); // both-error {{constant expression}} \
// both-note {{'b' of union with active member 'a'}}
+
+ /// Non-defaulted copy ctor.
+ union U2 {
+ int a;
+ constexpr U() : a(100) {}
+ constexpr U(const U &u) {
+ a = 20;
+ };
+ };
+ constexpr U u2;
+ constexpr U u22(u2);
+ static_assert(u22.a == 20);
}
namespace UnionInBase {
More information about the cfe-commits
mailing list