Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ Improvements to Clang's diagnostics
- Fixed fix-it hint for fold expressions. Clang now correctly places the suggested right
parenthesis when diagnosing malformed fold expressions. (#GH151787)

- Improved diagnostic location for templates declared inside local classes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reference the issue fixed by this patch like other entries.


Improvements to Clang's time-trace
----------------------------------

Expand Down
27 changes: 20 additions & 7 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8229,24 +8229,37 @@ Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
// C++ [temp.class.spec]p6: [P2096]
// A partial specialization may be declared in any scope in which the
// corresponding primary template may be defined.
auto FindTemplateParamsLoc = [](TemplateParameterList *TemplateParams,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the declaration so that the comment above and the corresponding code are not separated.

SourceLocation Fallback) {
SourceLocation DiagLoc = TemplateParams->getTemplateLoc();
if (DiagLoc.isValid())
return DiagLoc;

for (const NamedDecl *Param : *TemplateParams)
if (Param && Param->getLocation().isValid())
return Param->getLocation();

return Fallback;
};

if (Ctx) {
if (Ctx->isFileContext())
return false;
if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) {
// C++ [temp.mem]p2:
// A local class shall not have member templates.
if (RD->isLocalClass())
return Diag(TemplateParams->getTemplateLoc(),
if (RD->isLocalClass()) {
return Diag(FindTemplateParamsLoc(TemplateParams, RD->getLocation()),
diag::err_template_inside_local_class)
<< TemplateParams->getSourceRange();
else
return false;
<< TemplateParams->getSourceRange();
}
return false;
}
}

return Diag(TemplateParams->getTemplateLoc(),
return Diag(FindTemplateParamsLoc(TemplateParams, SourceLocation()),
diag::err_template_outside_namespace_or_class_scope)
<< TemplateParams->getSourceRange();
<< TemplateParams->getSourceRange();
}

/// Determine what kind of template specialization the given declaration
Expand Down
9 changes: 9 additions & 0 deletions clang/test/SemaCXX/template-local-class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s

void foo() {
struct Local {
template <typename T> // expected-error {{member templates are not allowed inside local classes}}
void bar();
};
}