PR46859: Fix crash if declaring a template in a DeclScope with no DeclContext.

This can happen during error recovery; it could also happen at block
scope if we ever parsed a template declaration at block scope.
This commit is contained in:
Richard Smith 2020-07-29 12:01:47 -07:00
parent 7aaa85627b
commit e69138dad5
2 changed files with 7 additions and 3 deletions

View File

@ -7774,15 +7774,14 @@ Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
// C++ [temp]p4:
// A template [...] shall not have C linkage.
DeclContext *Ctx = S->getEntity();
assert(Ctx && "Unknown context");
if (Ctx->isExternCContext()) {
if (Ctx && Ctx->isExternCContext()) {
Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
<< TemplateParams->getSourceRange();
if (const LinkageSpecDecl *LSD = Ctx->getExternCContext())
Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here);
return true;
}
Ctx = Ctx->getRedeclContext();
Ctx = Ctx ? Ctx->getRedeclContext() : nullptr;
// C++ [temp]p2:
// A template-declaration can appear only as a namespace scope or

View File

@ -259,3 +259,8 @@ namespace PR35697 {
}
}
}
namespace PR46859 {
extern "bogus" // expected-error {{unknown linkage language}}
template<int> struct X {}; // expected-error {{templates can only be declared in namespace or class scope}}
}