diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index b0afadfa8d3247..4efdb076ba7686 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -857,6 +857,9 @@ Bug Fixes in This Version - ``typeof_unqual`` now properly removes type qualifiers from arrays and their element types. (#GH92667) +- Fixed an assertion failure when a template non-type parameter contains + an invalid expression. + Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 01432301633ed2..97161febc15f7f 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -6229,7 +6229,12 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc)); } QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args); - if (T.isNull()) + // We may get a non-null type with errors, in which case + // `getAsCXXRecordDecl` will return `nullptr`. For instance, this + // happens when one of the template arguments is an invalid + // expression. We return early to avoid triggering the assertion + // about the `CodeSynthesisContext`. + if (T.isNull() || T->containsErrors()) return nullptr; CXXRecordDecl *SubstRecord = T->getAsCXXRecordDecl(); diff --git a/clang/test/SemaCXX/instantiate-template-broken-nontype-default.cpp b/clang/test/SemaCXX/instantiate-template-broken-nontype-default.cpp new file mode 100644 index 00000000000000..eb798e5887bda1 --- /dev/null +++ b/clang/test/SemaCXX/instantiate-template-broken-nontype-default.cpp @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s + +constexpr Missing a = 0; // expected-error {{unknown type name 'Missing'}} + +template < typename T, Missing b = a> // expected-error {{unknown type name 'Missing'}} +class Klass { // expected-note {{candidate template ignored: could not match 'Klass' against 'int'}} \ + expected-note {{implicit deduction guide declared as 'template ()> Klass(Klass) -> Klass'}} + Klass(T); // expected-note {{candidate template ignored: substitution failure [with T = int, b = ()]}} \ + expected-note {{implicit deduction guide declared as 'template ()> Klass(T) -> Klass'}} +}; + +Klass foo{5}; // no-crash \ + expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'Klass'}} +