Skip to content

Conversation

@jdoe3945
Copy link

@jdoe3945 jdoe3945 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
@github-actions
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;
     }
   }
 

@jdoe3945
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.

Copy link
Contributor

@ojhunt ojhunt left a comment

Choose a reason for hiding this comment

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

minor style nits, but needs some more tests added

Copy link
Contributor

@zwuis zwuis left a comment

Choose a reason for hiding this comment

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

LGTM modulo comments and tests. I'm not sure if tests are enough.

- 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.

// 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.

@zyn0217
Copy link
Contributor

zyn0217 commented Nov 9, 2025

Ping @emily-dror are you still working on this issue?

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)

7 participants