diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -2508,6 +2508,14 @@ } } + // Make sure that we don't make an ill-formed AST where the type of the + // Class is non-dependent and its attached base class specifier is an + // dependent type, which violates invariants in many clang code paths (e.g. + // constexpr evaluator). If this case happens (in errory-recovery mode), we + // explicitly mark the Class decl invalid. The diagnostic was already + // emitted. + if (!Class->getTypeForDecl()->isDependentType()) + Class->setInvalidDecl(); return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, Class->getTagKind() == TTK_Class, Access, TInfo, EllipsisLoc); diff --git a/clang/test/SemaTemplate/temp_class_spec.cpp b/clang/test/SemaTemplate/temp_class_spec.cpp --- a/clang/test/SemaTemplate/temp_class_spec.cpp +++ b/clang/test/SemaTemplate/temp_class_spec.cpp @@ -375,3 +375,16 @@ Bar() : Foo() {} }; } // namespace + +namespace Crash { +template +class Base {}; + +template class Foo; + +template +class Foo : public Base {}; // expected-error{{partial specialization of 'Foo' does not use any of its template parameters}} + +// verify that getASTRecordLayout doesn't crash on the ClassTemplateSpecializationDecl. +constexpr int s = sizeof(Foo); +}