Skip to content

Commit

Permalink
[Clang][Sema] Allow access to a public template alias declaration tha…
Browse files Browse the repository at this point in the history
…t refers to friend's private nested type (#83847)

This patch attempts to fix
#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 <836744285@qq.com>
  • Loading branch information
jcsxky committed Mar 13, 2024
1 parent 97c0cad commit 8bda565
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ Bug Fixes to C++ Support
when one of the function had more specialized templates.
Fixes (`#82509 <https://github.com/llvm/llvm-project/issues/82509>`_)
and (`#74494 <https://github.com/llvm/llvm-project/issues/74494>`_)
- Allow access to a public template alias declaration that refers to friend's
private nested type. (#GH25708).

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
10 changes: 7 additions & 3 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContextRAII> 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
Expand Down
20 changes: 20 additions & 0 deletions clang/test/SemaTemplate/PR25708.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %clang_cc1 -std=c++11 -verify %s
// expected-no-diagnostics

struct FooAccessor
{
template <typename T>
using Foo = typename T::Foo;
};

class Type
{
friend struct FooAccessor;

using Foo = int;
};

int main()
{
FooAccessor::Foo<Type> t;
}

0 comments on commit 8bda565

Please sign in to comment.