[clang] [Clang] Reject declaring an alias template with the same name as its template parameter. (PR #123533)

Valentyn Yukhymenko via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 19 15:30:36 PST 2025


https://github.com/BaLiKfromUA created https://github.com/llvm/llvm-project/pull/123533

Fixes llvm#123423

**How did I test it?**

Modified existing test and checked an example from the issue manually.


>From 0852c8ca587e772d5f851ac0983f43bdeec2ebd5 Mon Sep 17 00:00:00 2001
From: Valentyn Yukhymenko <valentin.yukhymenko at gmail.com>
Date: Sun, 19 Jan 2025 23:13:46 +0000
Subject: [PATCH] [Clang] Reject declaring an alias template with the same name
 as its template parameter.

Fixes llvm#123423
---
 clang/docs/ReleaseNotes.rst           | 1 +
 clang/lib/Sema/SemaDeclCXX.cpp        | 8 ++++++++
 clang/test/SemaCXX/alias-template.cpp | 5 +++--
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index aa1c02d04f7caa..29e40b4ecab412 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -956,6 +956,7 @@ Bug Fixes to C++ Support
 - Fixed a crash caused by the incorrect construction of template arguments for CTAD alias guides when type
   constraints are applied. (#GH122134)
 - Fixed canonicalization of pack indexing types - Clang did not always recognized identical pack indexing. (#GH123033)
+- Clang now rejects declaring an alias template with the same name as its template parameter. (#GH123423)
 
 
 Bug Fixes to AST Handling
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index a867ed73bd4033..4e43a8397cec4e 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -13464,6 +13464,14 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS,
     }
     TemplateParameterList *TemplateParams = TemplateParamLists[0];
 
+    // Check shadowing of a template parameter name
+    for (NamedDecl *TP : TemplateParams->asArray()) {
+      if (NameInfo.getName() == TP->getDeclName()) {
+        DiagnoseTemplateParameterShadow(Name.StartLocation, TP);
+        return nullptr;
+      }
+    }
+
     // Check that we can declare a template here.
     if (CheckTemplateDeclScope(S, TemplateParams))
       return nullptr;
diff --git a/clang/test/SemaCXX/alias-template.cpp b/clang/test/SemaCXX/alias-template.cpp
index 5189405e23db56..97134d2f3a96ad 100644
--- a/clang/test/SemaCXX/alias-template.cpp
+++ b/clang/test/SemaCXX/alias-template.cpp
@@ -65,7 +65,8 @@ namespace InFunctions {
   template<typename...T> struct S3 { // expected-note {{template parameter is declared here}}
     template<typename Z> using T = int; // expected-error {{declaration of 'T' shadows template parameter}}
   };
-  template<typename Z> using Z = Z;
+  template<typename Z> // expected-note {{template parameter is declared here}}
+  using Z = Z; // expected-error {{declaration of 'Z' shadows template parameter}}
 }
 
 namespace ClassNameRedecl {
@@ -191,4 +192,4 @@ int g = sfinae_me<int>(); // expected-error{{no matching function for call to 's
 
 namespace NullExceptionDecl {
 template<int... I> auto get = []() { try { } catch(...) {}; return I; }; // expected-error{{initializer contains unexpanded parameter pack 'I'}}
-}
+}
\ No newline at end of file



More information about the cfe-commits mailing list