diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index cf89cdc667acc..1e1e96a1c4782 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -5604,6 +5604,22 @@ bool Compiler::compileConstructor(const CXXConstructorDecl *Ctor) { if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr)) return false; + + // Mark all chain links as initialized. + unsigned InitFieldOffset = 0; + for (const NamedDecl *ND : IFD->chain().drop_back()) { + const auto *FD = cast(ND); + const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent()); + assert(FieldRecord); + NestedField = FieldRecord->getField(FD); + InitFieldOffset += NestedField->Offset; + assert(NestedField); + if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr)) + return false; + if (!this->emitFinishInitPop(InitExpr)) + return false; + } + } else { assert(Init->isDelegatingInitializer()); if (!this->emitThis(InitExpr)) diff --git a/clang/test/AST/ByteCode/unions.cpp b/clang/test/AST/ByteCode/unions.cpp index b1fbb0c4dfc06..c6b5e34810f05 100644 --- a/clang/test/AST/ByteCode/unions.cpp +++ b/clang/test/AST/ByteCode/unions.cpp @@ -463,4 +463,26 @@ namespace MoveAssign { } static_assert(f()== 12); } + +namespace IFD { + template + struct Optional { + struct { + union { + char null_state; + T val; + }; + }; + constexpr Optional() : null_state(){} + }; + + constexpr bool test() + { + Optional opt{}; + Optional opt2{}; + opt = opt2; + return true; + } + static_assert(test()); +} #endif