diff --git a/clang/lib/AST/Interp/Descriptor.cpp b/clang/lib/AST/Interp/Descriptor.cpp --- a/clang/lib/AST/Interp/Descriptor.cpp +++ b/clang/lib/AST/Interp/Descriptor.cpp @@ -170,9 +170,8 @@ const Descriptor *D) { for (const auto &F : D->ElemRecord->fields()) { auto FieldOff = F.Offset; - auto FieldDesc = F.Desc; + auto *FieldDesc = F.Desc; - *(reinterpret_cast(Dst + FieldOff) - 1) = FieldDesc; if (auto Fn = FieldDesc->MoveFn) Fn(B, Src + FieldOff, Dst + FieldOff, FieldDesc); } diff --git a/clang/lib/AST/Interp/InterpBlock.h b/clang/lib/AST/Interp/InterpBlock.h --- a/clang/lib/AST/Interp/InterpBlock.h +++ b/clang/lib/AST/Interp/InterpBlock.h @@ -156,6 +156,7 @@ /// Returns a pointer to the stored data. std::byte *data() { return B.data(); } + std::byte *rawData() { return B.rawData(); } private: friend class Block; diff --git a/clang/lib/AST/Interp/InterpState.cpp b/clang/lib/AST/Interp/InterpState.cpp --- a/clang/lib/AST/Interp/InterpState.cpp +++ b/clang/lib/AST/Interp/InterpState.cpp @@ -58,9 +58,13 @@ reinterpret_cast(std::malloc(sizeof(DeadBlock) + Size)); auto *D = new (Memory) DeadBlock(DeadBlocks, B); - // Move data from one block to another. - if (Desc->MoveFn) + // Move data and metadata from the old block to the new (dead)block. + if (Desc->MoveFn) { Desc->MoveFn(B, B->data(), D->data(), Desc); + if (Desc->getMetadataSize() > 0) + std::memcpy(D->rawData(), B->rawData(), Desc->getMetadataSize()); + } + } else { // Free storage, if necessary. if (Desc->DtorFn) diff --git a/clang/test/AST/Interp/lifetimes.cpp b/clang/test/AST/Interp/lifetimes.cpp new file mode 100644 --- /dev/null +++ b/clang/test/AST/Interp/lifetimes.cpp @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s +// RUN: %clang_cc1 -verify=ref %s + +struct Foo { + int a; +}; + +constexpr int dead1() { // expected-error {{never produces a constant expression}} + + Foo *F2 = nullptr; + { + Foo F{12}; // expected-note 2{{declared here}} + F2 = &F; + } // Ends lifetime of F. + + return F2->a; // expected-note 2{{read of variable whose lifetime has ended}} \ + // ref-note {{read of object outside its lifetime is not allowed in a constant expression}} +} +static_assert(dead1() == 1, ""); // expected-error {{not an integral constant expression}} \ + // expected-note {{in call to}} \ + // ref-error {{not an integral constant expression}} \ + // ref-note {{in call to}} \ + +