Skip to content

[clang][Diagnostic] Clarify error message for auto #149781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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 @@ -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.

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,
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();
};
}