-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[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
base: main
Are you sure you want to change the base?
[clang][Diagnostic] Clarify error message for auto #149781
Conversation
Show line and file for auto template error in -std=c++20 or -std=c++23
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: Emily Dror (emily-dror) ChangesShow line and file for auto template error in -std=c++20 or -std=c++23 Full diff: https://github.com/llvm/llvm-project/pull/149781.diff 1 Files Affected:
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index b76619fc50268..4505d5f7f2226 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8238,12 +8238,24 @@ Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
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(),
- diag::err_template_inside_local_class)
- << TemplateParams->getSourceRange();
- else
- return false;
+ if (RD->isLocalClass()) {
+ SourceLocation DiagLoc = TemplateParams->getTemplateLoc();
+ if (DiagLoc.isInvalid()) {
+ for (const NamedDecl *Param : *TemplateParams) {
+ if (Param && Param->getLocation().isValid()) {
+ DiagLoc = Param->getLocation();
+ break;
+ }
+ }
+ }
+ if (DiagLoc.isInvalid()) {
+ // Still no good location? Fall back to the class declaration itself
+ DiagLoc = RD->getLocation();
+ }
+ return Diag(DiagLoc, diag::err_template_inside_local_class)
+ << TemplateParams->getSourceRange();
+ }
+ return false;
}
}
|
Fixes #147324 |
Hi, thank you for your contribution! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'd want to extract the determination of a dialog location into a separate function (lambda?) to help calculate it, in both of the diag cases, including hte err_template_outside_namespace_or_class_scope
diag.
Also: we need a test for this. A simple reproducer that uses //expected-error
lines that point to a line instead of 'in general' i think will work.
else | ||
return false; | ||
if (RD->isLocalClass()) { | ||
SourceLocation DiagLoc = TemplateParams->getTemplateLoc(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to cover 8261 as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks for the review. Would you direct me to the documentation for writing test cases?
Also: we need a test for this. A simple reproducer that uses //expected-error lines that point to a line instead of 'in general' i think will work.
Show line and file for auto template error in -std=c++20 or -std=c++23
Fixes #147324