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 2 commits into
base: main
Choose a base branch
from

Conversation

emily-dror
Copy link

@emily-dror emily-dror commented Jul 21, 2025

Show line and file for auto template error in -std=c++20 or -std=c++23
Fixes #147324

Show line and file for auto template error in -std=c++20 or -std=c++23
Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 21, 2025

@llvm/pr-subscribers-clang

Author: Emily Dror (emily-dror)

Changes

Show 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:

  • (modified) clang/lib/Sema/SemaTemplate.cpp (+18-6)
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;
     }
   }
 

@emily-dror
Copy link
Author

Fixes #147324

@vbvictor
Copy link
Contributor

vbvictor commented Jul 21, 2025

Hi, thank you for your contribution!
Please add comprehensive test-cases for your change to make sure everything work as expected and continue to do so in the future.
Please add an entry in clang/docs/ReleaseNotes.rst to let everyone know about your fix.
Please add "Fixes #147324" into PR description (not standalone comment) to automatically link issue to this PR.

Copy link
Collaborator

@erichkeane erichkeane left a 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();
Copy link
Collaborator

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.

Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang: Missing line and file in message error when ‘auto’ is used in a not permitted context (and bad diagnostic)
4 participants