[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
llvm-ci wrote: LLVM Buildbot has detected a new failure on builder `openmp-offload-libc-amdgpu-runtime` running on `omp-vega20-1` while building `clang` at step 7 "Add check check-offload". Full details are available at: https://lab.llvm.org/buildbot/#/builders/73/builds/12403 Here is the relevant piece of the build log for the reference ``` Step 7 (Add check check-offload) failure: test (failure) TEST 'libomptarget :: amdgcn-amd-amdhsa :: mapping/data_member_ref.cpp' FAILED Exit Code: 2 Command Output (stdout): -- # RUN: at line 1 /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/clang++ -fopenmp-I /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test -I /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src -L /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload -L /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib -L /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src -nogpulib -Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload -Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src -Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib -fopenmp-targets=amdgcn-amd-amdhsa /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test/mapping/data_member_ref.cpp -o /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload/test/amdgcn-amd-amdhsa/mapping/Output/data_member_ref.cpp.tmp -Xoffload-linker -lc -Xoffload-linker -lm /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib/libomptarget.devicertl.a && /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload/test/amdgcn-amd-amdhsa/mapping/Output/data_member_ref.cpp.tmp | /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/FileCheck /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test/mapping/data_member_ref.cpp # executed command: /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/clang++ -fopenmp -I /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test -I /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src -L /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload -L /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib -L /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src -nogpulib -Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload -Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src -Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib -fopenmp-targets=amdgcn-amd-amdhsa /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test/mapping/data_member_ref.cpp -o /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload/test/amdgcn-amd-amdhsa/mapping/Output/data_member_ref.cpp.tmp -Xoffload-linker -lc -Xoffload-linker -lm /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib/libomptarget.devicertl.a # executed command: /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload/test/amdgcn-amd-amdhsa/mapping/Output/data_member_ref.cpp.tmp # note: command had no output on stdout or stderr # error: command failed with exit status: -11 # executed command: /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/FileCheck /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test/mapping/data_member_ref.cpp # .---command stderr # | FileCheck error: '' is empty. # | FileCheck command line: /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/FileCheck /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test/mapping/data_member_ref.cpp # `- # error: command failed with exit status: 2 -- ``` https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
github-actions[bot] wrote: @BaLiKfromUA Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our [build bots](https://lab.llvm.org/buildbot/). If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail [here](https://llvm.org/docs/MyFirstTypoFix.html#myfirsttypofix-issues-after-landing-your-pr). If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of [LLVM development](https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy). You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/zyn0217 closed https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/zyn0217 approved this pull request. Thanks! https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/BaLiKfromUA updated https://github.com/llvm/llvm-project/pull/123533 >From e451a8869420d9240f9006eb2adb599a3e6fd9f8 Mon Sep 17 00:00:00 2001 From: Valentyn Yukhymenko Date: Sun, 19 Jan 2025 23:13:46 + Subject: [PATCH 1/6] [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 .../CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp | 4 ++-- clang/test/SemaCXX/alias-template.cpp | 5 +++-- 4 files changed, 14 insertions(+), 4 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/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp b/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp index a990c82564aa40..ab4c663d24c7d5 100644 --- a/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp +++ b/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp @@ -121,8 +121,8 @@ namespace PartialSpecialization { namespace FixedAliasTemplate { template struct S {}; - template using U = S; // expected-note 2{{template parameter is declared here}} - template U &f(U, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}} + template using Z = S; // expected-note 2{{template parameter is declared here}} + template Z &f(Z, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}} S &s1 = f({}, 0, 0.0); // expected-error {{no matching function}} } 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 struct S3 { // expected-note {{template parameter is declared here}} template using T = int; // expected-error {{declaration of 'T' shadows template parameter}} }; - template using Z = Z; + template // 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(); // expected-error{{no matching function for call to 's namespace NullExceptionDecl { template auto get = []() { try { } catch(...) {}; return I; }; // expected-error{{initializer contains unexpanded parameter pack 'I'}} -} +} \ No newline at end of file >From 5d2d7dca93adac1ef2d2348713f626d2f2d95f6a Mon Sep 17 00:00:00 2001 From: Valentyn Yukhymenko Date: Tue, 21 Jan 2025 22:38:43 + Subject: [PATCH 2/6] code review remark and removal of the loop --- clang/lib/Sema/SemaDeclCXX.cpp| 10 -- clang/test/SemaCXX/alias-template.cpp | 2 +- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index e2e6326c5094dc..068442e66c3049 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -13406,8 +13406,6 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS, SourceLocation UsingLoc, UnqualifiedId &Name, const ParsedAttributesView &AttrList, TypeResult Type, Decl *DeclFromDeclSpec) { - // Get the innermost enclosing declaration scope. - S = S->getDeclParent(); if (Type.isInvalid()) return nullptr; @@ -134
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/BaLiKfromUA updated https://github.com/llvm/llvm-project/pull/123533 >From e451a8869420d9240f9006eb2adb599a3e6fd9f8 Mon Sep 17 00:00:00 2001 From: Valentyn Yukhymenko Date: Sun, 19 Jan 2025 23:13:46 + Subject: [PATCH 1/5] [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 .../CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp | 4 ++-- clang/test/SemaCXX/alias-template.cpp | 5 +++-- 4 files changed, 14 insertions(+), 4 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/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp b/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp index a990c82564aa40..ab4c663d24c7d5 100644 --- a/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp +++ b/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp @@ -121,8 +121,8 @@ namespace PartialSpecialization { namespace FixedAliasTemplate { template struct S {}; - template using U = S; // expected-note 2{{template parameter is declared here}} - template U &f(U, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}} + template using Z = S; // expected-note 2{{template parameter is declared here}} + template Z &f(Z, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}} S &s1 = f({}, 0, 0.0); // expected-error {{no matching function}} } 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 struct S3 { // expected-note {{template parameter is declared here}} template using T = int; // expected-error {{declaration of 'T' shadows template parameter}} }; - template using Z = Z; + template // 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(); // expected-error{{no matching function for call to 's namespace NullExceptionDecl { template auto get = []() { try { } catch(...) {}; return I; }; // expected-error{{initializer contains unexpanded parameter pack 'I'}} -} +} \ No newline at end of file >From 5d2d7dca93adac1ef2d2348713f626d2f2d95f6a Mon Sep 17 00:00:00 2001 From: Valentyn Yukhymenko Date: Tue, 21 Jan 2025 22:38:43 + Subject: [PATCH 2/5] code review remark and removal of the loop --- clang/lib/Sema/SemaDeclCXX.cpp| 10 -- clang/test/SemaCXX/alias-template.cpp | 2 +- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index e2e6326c5094dc..068442e66c3049 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -13406,8 +13406,6 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS, SourceLocation UsingLoc, UnqualifiedId &Name, const ParsedAttributesView &AttrList, TypeResult Type, Decl *DeclFromDeclSpec) { - // Get the innermost enclosing declaration scope. - S = S->getDeclParent(); if (Type.isInvalid()) return nullptr; @@ -134
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/zygoloid edited https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/zygoloid commented: This change looks good to me. https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
@@ -65,7 +65,8 @@ namespace InFunctions { template struct S3 { // expected-note {{template parameter is declared here}} template using T = int; // expected-error {{declaration of 'T' shadows template parameter}} }; - template using Z = Z; + template // expected-note {{template parameter is declared here}} + using Z = Z; // expected-error {{declaration of 'Z' shadows template parameter}} zygoloid wrote: That namespace name is weird -- there are no functions in it at all. There doesn't even seem to be any common theme for these tests. It'd be nice to split this namespace up into distinct namespaces for the different things being tested -- maybe `UnexpandedPack` for `S0`, `InvalidType` for `T1` and `T2`, and `ShadowTemplateParam` for `S3` and `Z`. https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/BaLiKfromUA edited https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
@@ -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; + } +} + BaLiKfromUA wrote: @zygoloid thank you for the review! I pushed your suggestion and updated PR summary https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/BaLiKfromUA edited https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/BaLiKfromUA updated https://github.com/llvm/llvm-project/pull/123533 >From e451a8869420d9240f9006eb2adb599a3e6fd9f8 Mon Sep 17 00:00:00 2001 From: Valentyn Yukhymenko Date: Sun, 19 Jan 2025 23:13:46 + Subject: [PATCH 1/4] [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 .../CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp | 4 ++-- clang/test/SemaCXX/alias-template.cpp | 5 +++-- 4 files changed, 14 insertions(+), 4 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/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp b/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp index a990c82564aa40..ab4c663d24c7d5 100644 --- a/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp +++ b/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp @@ -121,8 +121,8 @@ namespace PartialSpecialization { namespace FixedAliasTemplate { template struct S {}; - template using U = S; // expected-note 2{{template parameter is declared here}} - template U &f(U, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}} + template using Z = S; // expected-note 2{{template parameter is declared here}} + template Z &f(Z, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}} S &s1 = f({}, 0, 0.0); // expected-error {{no matching function}} } 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 struct S3 { // expected-note {{template parameter is declared here}} template using T = int; // expected-error {{declaration of 'T' shadows template parameter}} }; - template using Z = Z; + template // 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(); // expected-error{{no matching function for call to 's namespace NullExceptionDecl { template auto get = []() { try { } catch(...) {}; return I; }; // expected-error{{initializer contains unexpanded parameter pack 'I'}} -} +} \ No newline at end of file >From 5d2d7dca93adac1ef2d2348713f626d2f2d95f6a Mon Sep 17 00:00:00 2001 From: Valentyn Yukhymenko Date: Tue, 21 Jan 2025 22:38:43 + Subject: [PATCH 2/4] code review remark and removal of the loop --- clang/lib/Sema/SemaDeclCXX.cpp| 10 -- clang/test/SemaCXX/alias-template.cpp | 2 +- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index e2e6326c5094dc..068442e66c3049 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -13406,8 +13406,6 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS, SourceLocation UsingLoc, UnqualifiedId &Name, const ParsedAttributesView &AttrList, TypeResult Type, Decl *DeclFromDeclSpec) { - // Get the innermost enclosing declaration scope. - S = S->getDeclParent(); if (Type.isInvalid()) return nullptr; @@ -134
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/BaLiKfromUA updated https://github.com/llvm/llvm-project/pull/123533 >From e451a8869420d9240f9006eb2adb599a3e6fd9f8 Mon Sep 17 00:00:00 2001 From: Valentyn Yukhymenko Date: Sun, 19 Jan 2025 23:13:46 + Subject: [PATCH 1/4] [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 .../CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp | 4 ++-- clang/test/SemaCXX/alias-template.cpp | 5 +++-- 4 files changed, 14 insertions(+), 4 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/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp b/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp index a990c82564aa40..ab4c663d24c7d5 100644 --- a/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp +++ b/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp @@ -121,8 +121,8 @@ namespace PartialSpecialization { namespace FixedAliasTemplate { template struct S {}; - template using U = S; // expected-note 2{{template parameter is declared here}} - template U &f(U, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}} + template using Z = S; // expected-note 2{{template parameter is declared here}} + template Z &f(Z, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}} S &s1 = f({}, 0, 0.0); // expected-error {{no matching function}} } 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 struct S3 { // expected-note {{template parameter is declared here}} template using T = int; // expected-error {{declaration of 'T' shadows template parameter}} }; - template using Z = Z; + template // 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(); // expected-error{{no matching function for call to 's namespace NullExceptionDecl { template auto get = []() { try { } catch(...) {}; return I; }; // expected-error{{initializer contains unexpanded parameter pack 'I'}} -} +} \ No newline at end of file >From 5d2d7dca93adac1ef2d2348713f626d2f2d95f6a Mon Sep 17 00:00:00 2001 From: Valentyn Yukhymenko Date: Tue, 21 Jan 2025 22:38:43 + Subject: [PATCH 2/4] code review remark and removal of the loop --- clang/lib/Sema/SemaDeclCXX.cpp| 10 -- clang/test/SemaCXX/alias-template.cpp | 2 +- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index e2e6326c5094dc..068442e66c3049 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -13406,8 +13406,6 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS, SourceLocation UsingLoc, UnqualifiedId &Name, const ParsedAttributesView &AttrList, TypeResult Type, Decl *DeclFromDeclSpec) { - // Get the innermost enclosing declaration scope. - S = S->getDeclParent(); if (Type.isInvalid()) return nullptr; @@ -134
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
@@ -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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/shafik commented: when the discussion is eventually resolved please add a more detailed summary explaining how the fix is implemented and why. These go in the git log and it is important for relevant details to show up there w/o the need for git show etc https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
@@ -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; + } +} + shafik wrote: @zygoloid since his patch added the first behavior changed later on. https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
@@ -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; + } +} + MagentaTreehouse wrote: Code to skip up scope was originally added here: https://github.com/llvm/llvm-project/commit/3f1b5d077b7e68c0c801a56f17b05dab96f7f992#diff-9ced4fa47ee2b9c03b6996ce89a1d131c0f5b71013993bc582209f50d5e934daR4713-R4715 https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
@@ -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; + } +} + erichkeane wrote: That doesn't seem right... I would expect it to? But someone probably needs to change it and see what happens, or dig through git history to see if there is justification for this. https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
https://github.com/BaLiKfromUA edited https://github.com/llvm/llvm-project/pull/123533 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits