Index: clang/lib/AST/Interp/Descriptor.cpp =================================================================== --- clang/lib/AST/Interp/Descriptor.cpp +++ clang/lib/AST/Interp/Descriptor.cpp @@ -126,7 +126,7 @@ auto *Desc = reinterpret_cast(Ptr + SubOff) - 1; Desc->Offset = SubOff; Desc->Desc = F; - Desc->IsInitialized = (B->isStatic() || F->IsArray) && !IsBase; + Desc->IsInitialized = F->IsArray && !IsBase; Desc->IsBase = IsBase; Desc->IsActive = IsActive && !IsUnion; Desc->IsConst = IsConst || F->IsConst; Index: clang/lib/AST/Interp/Interp.h =================================================================== --- clang/lib/AST/Interp/Interp.h +++ clang/lib/AST/Interp/Interp.h @@ -91,6 +91,9 @@ /// Checks if a method is pure virtual. bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD); +/// Checks that all fields are initialized after a constructor call. +bool CheckCtorCall(InterpState &S, CodePtr OpPC, const Pointer &This); + /// Checks if Div/Rem operation on LHS and RHS is valid. template bool CheckDivRem(InterpState &S, CodePtr OpPC, const T &LHS, const T &RHS) { @@ -1272,8 +1275,10 @@ inline bool Call(InterpState &S, CodePtr &PC, const Function *Func) { auto NewFrame = std::make_unique(S, Func, PC); + Pointer ThisPtr; if (Func->hasThisPointer()) { - if (!CheckInvoke(S, PC, NewFrame->getThis())) { + ThisPtr = NewFrame->getThis(); + if (!CheckInvoke(S, PC, ThisPtr)) { return false; } } @@ -1291,6 +1296,11 @@ if (Interpret(S, CallResult)) { NewFrame.release(); // Frame was delete'd already. assert(S.Current == FrameBefore); + + // For constructors, check that all fields have been initialized. + if (Func->isConstructor() && !CheckCtorCall(S, PC, ThisPtr)) + return false; + return true; } Index: clang/lib/AST/Interp/Interp.cpp =================================================================== --- clang/lib/AST/Interp/Interp.cpp +++ clang/lib/AST/Interp/Interp.cpp @@ -401,6 +401,38 @@ return false; } +static bool CheckFieldsInitialized(InterpState &S, CodePtr OpPC, + const Pointer &BasePtr, const Record *R) { + assert(R); + bool Result = true; + // Check all fields of this record are initialized. + for (const Record::Field &F : R->fields()) { + Pointer FieldPtr = BasePtr.atField(F.Offset); + QualType FieldType = FieldPtr.getType(); + + if (FieldType->isRecordType()) { + Result &= CheckFieldsInitialized(S, OpPC, FieldPtr, FieldPtr.getRecord()); + } else if (FieldType->isArrayType()) { + // FIXME: Arrays need to be handled here as well I think. + } else if (!FieldPtr.isInitialized()) { + const SourceInfo &SI = S.Current->getSource(OpPC); + S.FFDiag(SI, diag::note_constexpr_uninitialized) + << true << F.Decl->getType(); + SourceLocation SubobjectLoc = F.Decl->getLocation(); + if (SubobjectLoc.isValid()) + S.Note(SubobjectLoc, diag::note_constexpr_subobject_declared_here); + Result = false; + } + } + return Result; +} + +bool CheckCtorCall(InterpState &S, CodePtr OpPC, const Pointer &This) { + assert(!This.isZero()); + const Record *R = This.getRecord(); + return CheckFieldsInitialized(S, OpPC, This, R); +} + bool Interpret(InterpState &S, APValue &Result) { // The current stack frame when we started Interpret(). // This is being used by the ops to determine wheter Index: clang/lib/AST/Interp/InterpFrame.cpp =================================================================== --- clang/lib/AST/Interp/InterpFrame.cpp +++ clang/lib/AST/Interp/InterpFrame.cpp @@ -64,8 +64,6 @@ } InterpFrame::~InterpFrame() { - if (Func && Func->isConstructor() && This.isBaseClass()) - This.initialize(); for (auto &Param : Params) S.deallocate(reinterpret_cast(Param.second.get())); } Index: clang/test/AST/Interp/cxx20.cpp =================================================================== --- clang/test/AST/Interp/cxx20.cpp +++ clang/test/AST/Interp/cxx20.cpp @@ -108,3 +108,35 @@ // ref-note {{declared here}} static_assert(!b4); // ref-error {{not an integral constant expression}} \ // ref-note {{not a constant expression}} + +namespace UninitializedFields { + class A { + public: + int a; // expected-note {{subobject declared here}} \ + // ref-note {{subobject declared here}} + constexpr A() {} + }; + constexpr A a; // expected-error {{must be initialized by a constant expression}} \ + // expected-note {{subobject of type 'int' is not initialized}} \ + // ref-error {{must be initialized by a constant expression}} \ + // ref-note {{subobject of type 'int' is not initialized}} + + + class Base { + public: + bool b; + int a; // expected-note {{subobject declared here}} \ + // ref-note {{subobject declared here}} + constexpr Base() : b(true) {} + }; + + class Derived : public Base { + public: + constexpr Derived() : Base() {} // expected-note {{subobject of type 'int' is not initialized}} + }; + +constexpr Derived D; // expected-error {{must be initialized by a constant expression}} \\ + // expected-note {{in call to 'Derived()'}} \ + // ref-error {{must be initialized by a constant expression}} \ + // ref-note {{subobject of type 'int' is not initialized}} +}; Index: clang/test/AST/Interp/records.cpp =================================================================== --- clang/test/AST/Interp/records.cpp +++ clang/test/AST/Interp/records.cpp @@ -295,13 +295,14 @@ // ref-note 2{{non-constexpr constructor 'Base' cannot be used in a constant expression}} }; - // FIXME: This is currently not being diagnosed with the new constant interpreter. constexpr Derived D(12); // ref-error {{must be initialized by a constant expression}} \ // ref-note {{in call to 'Derived(12)'}} \ // ref-note {{declared here}} \ // expected-error {{must be initialized by a constant expression}} static_assert(D.Val == 0, ""); // ref-error {{not an integral constant expression}} \ - // ref-note {{initializer of 'D' is not a constant expression}} + // ref-note {{initializer of 'D' is not a constant expression}} \ + // expected-error {{not an integral constant expression}} \ + // expected-note {{read of object outside its lifetime}} struct AnotherBase { int Val;