[clang] 8bda565 - [Clang][Sema] Allow access to a public template alias declaration that refers to friend's private nested type (#83847)

2024-03-12 Thread via cfe-commits

Author: Qizhi Hu
Date: 2024-03-13T08:42:22+08:00
New Revision: 8bda5657332c7a94900d3eb2891d2b86e60b0e68

URL: 
https://github.com/llvm/llvm-project/commit/8bda5657332c7a94900d3eb2891d2b86e60b0e68
DIFF: 
https://github.com/llvm/llvm-project/commit/8bda5657332c7a94900d3eb2891d2b86e60b0e68.diff

LOG: [Clang][Sema] Allow access to a public template alias declaration that 
refers to friend's private nested type (#83847)

This patch attempts to fix
https://github.com/llvm/llvm-project/issues/25708
Current access check missed qualifier(`NestedNameSpecifier`) in friend
class checking. Add it to `Records` of `EffectiveContext` by changing
the `DeclContext` makes `MatchesFriend` work.

Co-authored-by: huqizhi <836744...@qq.com>

Added: 
clang/test/SemaTemplate/PR25708.cpp

Modified: 
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaTemplate.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e14c92eae0afe1..64a9fe0d8bcc48 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -360,6 +360,8 @@ Bug Fixes to C++ Support
   when one of the function had more specialized templates.
   Fixes (`#82509 `_)
   and (`#74494 `_)
+- Allow access to a public template alias declaration that refers to friend's
+  private nested type. (#GH25708).
 
 Bug Fixes to AST Handling
 ^

diff  --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index d62095558d0ffb..d8c9a5c09944c4 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4343,9 +4343,13 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
 if (Inst.isInvalid())
   return QualType();
 
-CanonType = SubstType(Pattern->getUnderlyingType(),
-  TemplateArgLists, AliasTemplate->getLocation(),
-  AliasTemplate->getDeclName());
+std::optional SavedContext;
+if (!AliasTemplate->getDeclContext()->isFileContext())
+  SavedContext.emplace(*this, AliasTemplate->getDeclContext());
+
+CanonType =
+SubstType(Pattern->getUnderlyingType(), TemplateArgLists,
+  AliasTemplate->getLocation(), AliasTemplate->getDeclName());
 if (CanonType.isNull()) {
   // If this was enable_if and we failed to find the nested type
   // within enable_if in a SFINAE context, dig out the specific

diff  --git a/clang/test/SemaTemplate/PR25708.cpp 
b/clang/test/SemaTemplate/PR25708.cpp
new file mode 100644
index 00..6a214fc6b43bc1
--- /dev/null
+++ b/clang/test/SemaTemplate/PR25708.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -std=c++11 -verify %s
+// expected-no-diagnostics
+
+struct FooAccessor
+{
+template 
+using Foo = typename T::Foo;
+};
+
+class Type
+{
+friend struct FooAccessor;
+
+using Foo = int;
+};
+
+int main()
+{
+FooAccessor::Foo t;
+}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 8bda565 - [Clang][Sema] Allow access to a public template alias declaration that refers to friend's private nested type (#83847)

2024-03-12 Thread via cfe-commits

Author: Qizhi Hu
Date: 2024-03-13T08:42:22+08:00
New Revision: 8bda5657332c7a94900d3eb2891d2b86e60b0e68

URL: 
https://github.com/llvm/llvm-project/commit/8bda5657332c7a94900d3eb2891d2b86e60b0e68
DIFF: 
https://github.com/llvm/llvm-project/commit/8bda5657332c7a94900d3eb2891d2b86e60b0e68.diff

LOG: [Clang][Sema] Allow access to a public template alias declaration that 
refers to friend's private nested type (#83847)

This patch attempts to fix
https://github.com/llvm/llvm-project/issues/25708
Current access check missed qualifier(`NestedNameSpecifier`) in friend
class checking. Add it to `Records` of `EffectiveContext` by changing
the `DeclContext` makes `MatchesFriend` work.

Co-authored-by: huqizhi <836744...@qq.com>

Added: 
clang/test/SemaTemplate/PR25708.cpp

Modified: 
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaTemplate.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e14c92eae0afe1..64a9fe0d8bcc48 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -360,6 +360,8 @@ Bug Fixes to C++ Support
   when one of the function had more specialized templates.
   Fixes (`#82509 `_)
   and (`#74494 `_)
+- Allow access to a public template alias declaration that refers to friend's
+  private nested type. (#GH25708).
 
 Bug Fixes to AST Handling
 ^

diff  --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index d62095558d0ffb..d8c9a5c09944c4 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4343,9 +4343,13 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
 if (Inst.isInvalid())
   return QualType();
 
-CanonType = SubstType(Pattern->getUnderlyingType(),
-  TemplateArgLists, AliasTemplate->getLocation(),
-  AliasTemplate->getDeclName());
+std::optional SavedContext;
+if (!AliasTemplate->getDeclContext()->isFileContext())
+  SavedContext.emplace(*this, AliasTemplate->getDeclContext());
+
+CanonType =
+SubstType(Pattern->getUnderlyingType(), TemplateArgLists,
+  AliasTemplate->getLocation(), AliasTemplate->getDeclName());
 if (CanonType.isNull()) {
   // If this was enable_if and we failed to find the nested type
   // within enable_if in a SFINAE context, dig out the specific

diff  --git a/clang/test/SemaTemplate/PR25708.cpp 
b/clang/test/SemaTemplate/PR25708.cpp
new file mode 100644
index 00..6a214fc6b43bc1
--- /dev/null
+++ b/clang/test/SemaTemplate/PR25708.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -std=c++11 -verify %s
+// expected-no-diagnostics
+
+struct FooAccessor
+{
+template 
+using Foo = typename T::Foo;
+};
+
+class Type
+{
+friend struct FooAccessor;
+
+using Foo = int;
+};
+
+int main()
+{
+FooAccessor::Foo t;
+}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits