Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -4335,9 +4335,11 @@ typedef ProcessingContextState ParsingClassState; ParsingClassState PushParsingClass() { + ParsingClassDepth++; return DelayedDiagnostics.pushUndelayed(); } void PopParsingClass(ParsingClassState state) { + ParsingClassDepth--; DelayedDiagnostics.popUndelayed(state); } @@ -11544,6 +11546,8 @@ SmallVector DelayedDllExportMemberFunctions; private: + int ParsingClassDepth = 0; + class SavePendingParsedClassStateRAII { public: SavePendingParsedClassStateRAII(Sema &S) : S(S) { swapSavedState(); } @@ -11553,8 +11557,6 @@ "there shouldn't be any pending delayed exception spec checks"); assert(S.DelayedEquivalentExceptionSpecChecks.empty() && "there shouldn't be any pending delayed exception spec checks"); - assert(S.DelayedDllExportClasses.empty() && - "there shouldn't be any pending delayed DLL export classes"); swapSavedState(); } @@ -11564,14 +11566,12 @@ SavedOverridingExceptionSpecChecks; decltype(DelayedEquivalentExceptionSpecChecks) SavedEquivalentExceptionSpecChecks; - decltype(DelayedDllExportClasses) SavedDllExportClasses; void swapSavedState() { SavedOverridingExceptionSpecChecks.swap( S.DelayedOverridingExceptionSpecChecks); SavedEquivalentExceptionSpecChecks.swap( S.DelayedEquivalentExceptionSpecChecks); - SavedDllExportClasses.swap(S.DelayedDllExportClasses); } }; Index: clang/lib/Sema/SemaTemplateInstantiate.cpp =================================================================== --- clang/lib/Sema/SemaTemplateInstantiate.cpp +++ clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -2226,8 +2226,10 @@ CheckCompletedCXXClass(Instantiation); // Default arguments are parsed, if not instantiated. We can go instantiate - // default arg exprs for default constructors if necessary now. - ActOnFinishCXXNonNestedClass(Instantiation); + // default arg exprs for default constructors if necessary now. Unless we're + // parsing a class, in which case wait until that's finished. + if (ParsingClassDepth == 0) + ActOnFinishCXXNonNestedClass(Instantiation); // Instantiate late parsed attributes, and attach them to their decls. // See Sema::InstantiateAttrs Index: clang/test/CodeGenCXX/dllexport.cpp =================================================================== --- clang/test/CodeGenCXX/dllexport.cpp +++ clang/test/CodeGenCXX/dllexport.cpp @@ -860,6 +860,20 @@ }; // M32-DAG: define weak_odr dso_local dllexport x86_thiscallcc %"struct.InClassInits::PR40006"* @"??0PR40006@InClassInits@@QAE@XZ" +namespace pr40006 { +// Delay emitting the method also past the instantiation of Tmpl, i.e. +// until the top-level class Outer is completely finished. +template struct Tmpl {}; +struct Outer { + struct Inner { + __declspec(dllexport) Inner() = default; + unsigned int x = 0; + }; + Tmpl y; +}; +// M32-DAG: define weak_odr dso_local dllexport x86_thiscallcc %"struct.InClassInits::pr40006::Outer::Inner"* @"??0Inner@Outer@pr40006@InClassInits@@QAE@XZ" +} + // PR42857: Clang would try to emit the non-trivial explicitly defaulted // dllexport ctor twice when doing an explicit instantiation definition. struct Qux { Qux(); };