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

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 23 12:48:18 PST 2025


================
@@ -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;
+      }
+    }
+
----------------
zygoloid wrote:

I would think that we still need to do this `S = S->getDeclParent();` step, like we do in `HandleDeclarator` and elsewhere, but we're just doing it at the wrong moment -- it should be done after we do lookup and before we pass `S` to `FilterLookupForScope` and `PushOnScopeChains`.

I think removing it entirely happens to work because alias templates can only appear in scopes that correspond to a `DeclContext`, so the change to push the name into the "wrong" `Scope` turns out to not matter -- lookup in the `DeclContext` finds it anyway. So I'm not sure if the difference is observable, but it would be more correct to move the `S = S->getDeclParent();` step to after the lookup instead of removing it entirely. (This is what `HandleDeclarator` does, so it's what we do for `typedef`.)

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


More information about the cfe-commits mailing list