[clang] 8658d15 - Revert "[Clang][Sema] Diagnose function/variable templates that shadow their own template parameters (#78274)"

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 22 00:30:49 PST 2024


Author: Corentin Jabot
Date: 2024-01-22T09:29:01+01:00
New Revision: 8658d157654832fe24b4f3d2a9a62784a4d6a162

URL: https://github.com/llvm/llvm-project/commit/8658d157654832fe24b4f3d2a9a62784a4d6a162
DIFF: https://github.com/llvm/llvm-project/commit/8658d157654832fe24b4f3d2a9a62784a4d6a162.diff

LOG: Revert "[Clang][Sema] Diagnose function/variable templates that shadow their own template parameters (#78274)"

This reverts commit fc0253264445be7f88d4cf0f9129dcb10c2fb84b.

This errors is disruptive to downstream projects
and should be reintroduced as a separate on-by-default
warning.

https://github.com/llvm/llvm-project/pull/78274

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaDecl.cpp
    clang/test/CXX/temp/temp.res/temp.local/p6.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2c7c7b8a21b8e74..7c9f9ecca727a48 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -590,7 +590,6 @@ Improvements to Clang's diagnostics
 - Clang now diagnoses the requirement that non-template friend declarations with requires clauses
   and template friend declarations with a constraint that depends on a template parameter from an
   enclosing template must be a definition.
-- Clang now diagnoses function/variable templates that shadow their own template parameters, e.g. ``template<class T> void T();``.
 - Clang now diagnoses incorrect usage of ``const`` and ``pure`` attributes, so ``-Wignored-attributes`` diagnoses more cases.
 - Clang now emits more descriptive diagnostics for 'unusual' expressions (e.g. incomplete index
   expressions on matrix types or builtin functions without an argument list) as placement-args

diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 8dff2cdc063df32..13ca438e6a487fe 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -6377,6 +6377,12 @@ NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D,
   } else if (DiagnoseUnexpandedParameterPack(NameInfo, UPPC_DeclarationType))
     return nullptr;
 
+  // The scope passed in may not be a decl scope.  Zip up the scope tree until
+  // we find one that is.
+  while ((S->getFlags() & Scope::DeclScope) == 0 ||
+         (S->getFlags() & Scope::TemplateParamScope) != 0)
+    S = S->getParent();
+
   DeclContext *DC = CurContext;
   if (D.getCXXScopeSpec().isInvalid())
     D.setInvalidType();
@@ -6529,12 +6535,6 @@ NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D,
   if (getLangOpts().CPlusPlus)
     CheckExtraCXXDefaultArguments(D);
 
-  // The scope passed in may not be a decl scope.  Zip up the scope tree until
-  // we find one that is.
-  while ((S->getFlags() & Scope::DeclScope) == 0 ||
-         (S->getFlags() & Scope::TemplateParamScope) != 0)
-    S = S->getParent();
-
   NamedDecl *New;
 
   bool AddToScope = true;

diff  --git a/clang/test/CXX/temp/temp.res/temp.local/p6.cpp b/clang/test/CXX/temp/temp.res/temp.local/p6.cpp
index 0702966e5685480..e2aa0ff344291d2 100644
--- a/clang/test/CXX/temp/temp.res/temp.local/p6.cpp
+++ b/clang/test/CXX/temp/temp.res/temp.local/p6.cpp
@@ -127,30 +127,16 @@ template<int T> struct Z { // expected-note 16{{declared here}}
 template<typename T> // expected-note {{declared here}}
 void f(int T) {} // expected-error {{declaration of 'T' shadows template parameter}}
 
+// FIXME: These are ill-formed: a template-parameter shall not have the same name as the template name.
 namespace A {
   template<typename T> struct T {};  // expected-error{{declaration of 'T' shadows template parameter}}
                                      // expected-note at -1{{template parameter is declared here}}
-  template<typename T> struct U {
-    template<typename V> struct V {}; // expected-error{{declaration of 'V' shadows template parameter}}
-                                      // expected-note at -1{{template parameter is declared here}}
-  };
 }
 namespace B {
-  template<typename T> void T() {} // expected-error{{declaration of 'T' shadows template parameter}}
-                                   // expected-note at -1{{template parameter is declared here}}
-
-  template<typename T> struct U {
-    template<typename V> void V(); // expected-error{{declaration of 'V' shadows template parameter}}
-                                   // expected-note at -1{{template parameter is declared here}}
-  };
+  template<typename T> void T() {}
 }
 namespace C {
-  template<typename T> int T; // expected-error{{declaration of 'T' shadows template parameter}}
-                              // expected-note at -1{{template parameter is declared here}}
-  template<typename T> struct U {
-    template<typename V> static int V; // expected-error{{declaration of 'V' shadows template parameter}}
-                                       // expected-note at -1{{template parameter is declared here}}
-  };
+  template<typename T> int T;
 }
 
 namespace PR28023 {


        


More information about the cfe-commits mailing list