Skip to content

Commit 22ba468

Browse files
authored
[clang][bytecode] Fix non-defaulted union copy/move ctors (#199394)
They are like regular record ctors.
1 parent 5a616ce commit 22ba468

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6856,8 +6856,8 @@ bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
68566856
return false;
68576857
bool IsUnion = R->isUnion();
68586858

6859-
// Union copy and move ctors are special.
6860-
if (IsUnion && Ctor->isCopyOrMoveConstructor()) {
6859+
// Default union copy and move ctors are special.
6860+
if (IsUnion && Ctor->isCopyOrMoveConstructor() && Ctor->isDefaulted()) {
68616861
LocOverrideScope<Emitter> LOS(this, SourceInfo{});
68626862

68636863
// No special case for NumFields == 0 here, so the Memcpy op

clang/test/AST/ByteCode/unions.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,18 @@ namespace CopyCtor {
386386
static_assert(y.a == 42, "");
387387
static_assert(y.b == 42, ""); // both-error {{constant expression}} \
388388
// both-note {{'b' of union with active member 'a'}}
389+
390+
/// Non-defaulted copy ctor.
391+
union U2 {
392+
int a;
393+
constexpr U2() : a(100) {}
394+
constexpr U2(const U2 &u) {
395+
a = 20;
396+
};
397+
};
398+
constexpr U2 u2;
399+
constexpr U2 u22(u2);
400+
static_assert(u22.a == 20, "");
389401
}
390402

391403
namespace UnionInBase {

0 commit comments

Comments
 (0)