Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -398,6 +398,8 @@ Improvements to Clang's diagnostics
- Clang now emits a diagnostic in case `vector_size` or `ext_vector_type`
attributes are used with a negative size (#GH165463).

- 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
25 changes: 19 additions & 6 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8454,24 +8454,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(),
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
14 changes: 14 additions & 0 deletions clang/test/SemaCXX/auto-local-class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clang_cc1 %std_cxx98-14 -fsyntax-only -verify=expected,precxx17 %s
// RUN: %clang_cc1 %std_cxx17- -fsyntax-only -verify=expected,cxx17 %s

// Check that 'auto' in a local class function parameter
// produces the correct diagnostic depending on the language standard:
// * Before C++17 → "'auto' not allowed in function prototype"
// * C++17 and later → "templates cannot be declared inside of a local class"

int main() {
struct A {
void foo(auto x) {} // precxx17-error {{'auto' not allowed in function prototype}} \
// cxx17-error {{templates cannot be declared inside of a local class}}
};
}
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();
};
}

Loading