Skip to content

Commit 7ef636e

Browse files
authored
[clang][bytecode] Mark IndirectFieldDecl chain links as initialized (llvm#125869)
We only initialize the final field above, so make sure we're marking the links in the chain on the way there as initialized as well.
1 parent cf9806e commit 7ef636e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5604,6 +5604,22 @@ bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
56045604

56055605
if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr))
56065606
return false;
5607+
5608+
// Mark all chain links as initialized.
5609+
unsigned InitFieldOffset = 0;
5610+
for (const NamedDecl *ND : IFD->chain().drop_back()) {
5611+
const auto *FD = cast<FieldDecl>(ND);
5612+
const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
5613+
assert(FieldRecord);
5614+
NestedField = FieldRecord->getField(FD);
5615+
InitFieldOffset += NestedField->Offset;
5616+
assert(NestedField);
5617+
if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr))
5618+
return false;
5619+
if (!this->emitFinishInitPop(InitExpr))
5620+
return false;
5621+
}
5622+
56075623
} else {
56085624
assert(Init->isDelegatingInitializer());
56095625
if (!this->emitThis(InitExpr))

clang/test/AST/ByteCode/unions.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,26 @@ namespace MoveAssign {
463463
}
464464
static_assert(f()== 12);
465465
}
466+
467+
namespace IFD {
468+
template <class T>
469+
struct Optional {
470+
struct {
471+
union {
472+
char null_state;
473+
T val;
474+
};
475+
};
476+
constexpr Optional() : null_state(){}
477+
};
478+
479+
constexpr bool test()
480+
{
481+
Optional<int> opt{};
482+
Optional<int> opt2{};
483+
opt = opt2;
484+
return true;
485+
}
486+
static_assert(test());
487+
}
466488
#endif

0 commit comments

Comments
 (0)