-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Template Diagnostic Improvements #99933
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
Conversation
@llvm/pr-subscribers-clang Author: Braden Helmer (bradenhelmer) ChangesThis is for #17959. It turns out Would like some feedback as I am unsure if this is the right fix. Thanks Full diff: https://github.com/llvm/llvm-project/pull/99933.diff 2 Files Affected:
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 22d38adc28ebe..bdb3fc051d0b3 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -299,6 +299,15 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
goto Retry;
}
+ case tok::kw_template: {
+ SourceLocation DeclEnd;
+ ParsedAttributes Attrs(AttrFactory);
+ ParseTemplateDeclarationOrSpecialization(DeclaratorContext::Block, DeclEnd,
+ Attrs,
+ getAccessSpecifierIfPresent());
+ return StmtError();
+ }
+
case tok::kw_case: // C99 6.8.1: labeled-statement
return ParseCaseStatement(StmtCtx);
case tok::kw_default: // C99 6.8.1: labeled-statement
diff --git a/clang/test/Parser/cxx-template-decl.cpp b/clang/test/Parser/cxx-template-decl.cpp
index 734438069b9ae..5a2d1cec9ad31 100644
--- a/clang/test/Parser/cxx-template-decl.cpp
+++ b/clang/test/Parser/cxx-template-decl.cpp
@@ -297,3 +297,7 @@ namespace PR46231 {
template<> int; // expected-error {{declaration does not declare anything}}
template<int> int; // expected-error {{declaration does not declare anything}}
}
+
+namespace NoTemplateInBlockScope {
+ void foo() { template <typename> int i; } // expected-error {{templates can only be declared in namespace or class scope}}
+}
|
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.
Hi.
The change looks good.
There are some other examples of bad diagnostics in that issue, and it would be ideal if they are preserved in case this PR closes that issue.
Please also add an entry to clang/docs/ReleaseNotes.rst
.
This is handling the other non-local case now, and the release notes have been updated. The local class case was already being handled. |
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.
LGTM, Thanks!
Please give it a couple of days before merging, in case anyone else wants to chime in.
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.
These updated test changes still LGTM.
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.
LGTM.
I don't have write access, if someone could merge that would be great! |
It turns out `SemaTemplate` handles this type of diagnostic already, however when template gets encountered, it never gets parsed as a possible statement or declaration, only as an expression. Fixes #17959.
It turns out
SemaTemplate
handles this type of diagnostic already, however when template gets encountered, it never gets parsed as a possible statement or declaration, only as an expression.Would like some feedback as I am unsure if this is the right fix. Thanks
Fixes #17959.