Skip to content

Commit 6b1aa31

Browse files
authored
[clang] Substitute alias templates from correct context (#74335)
Current context set to where alias was met, not where it is declared caused incorrect access check in case alias referenced private members of the parent class. Fixes #41693
1 parent 6ed7a8e commit 6b1aa31

File tree

5 files changed

+85
-8
lines changed

5 files changed

+85
-8
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,9 @@ Bug Fixes in This Version
658658
Fixes (`#64467 <https://github.com/llvm/llvm-project/issues/64467>`_)
659659
- Clang's ``-Wchar-subscripts`` no longer warns on chars whose values are known non-negative constants.
660660
Fixes (`#18763 <https://github.com/llvm/llvm-project/issues/18763>`_)
661+
- Fixed false positive error emitted when templated alias inside a class
662+
used private members of the same class.
663+
Fixes (`#41693 <https://github.com/llvm/llvm-project/issues/41693>`_)
661664

662665
Bug Fixes to Compiler Builtins
663666
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaCXXScopeSpec.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,33 @@ static CXXRecordDecl *getCurrentInstantiationOf(QualType T,
3030
return nullptr;
3131

3232
const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
33+
if (isa<TemplateSpecializationType>(Ty)) {
34+
if (auto *Record = dyn_cast<CXXRecordDecl>(CurContext)) {
35+
if (isa<ClassTemplatePartialSpecializationDecl>(Record) ||
36+
Record->getDescribedClassTemplate()) {
37+
const Type *ICNT = Record->getTypeForDecl();
38+
QualType Injected =
39+
cast<InjectedClassNameType>(ICNT)->getInjectedSpecializationType();
40+
41+
if (Ty == Injected->getCanonicalTypeInternal().getTypePtr())
42+
return Record;
43+
}
44+
}
45+
}
46+
3347
if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
3448
CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
3549
if (!Record->isDependentContext() ||
3650
Record->isCurrentInstantiation(CurContext))
3751
return Record;
3852

3953
return nullptr;
40-
} else if (isa<InjectedClassNameType>(Ty))
54+
}
55+
56+
if (isa<InjectedClassNameType>(Ty))
4157
return cast<InjectedClassNameType>(Ty)->getDecl();
42-
else
43-
return nullptr;
58+
59+
return nullptr;
4460
}
4561

4662
/// Compute the DeclContext that is associated with the given type.

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3990,9 +3990,16 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
39903990
if (Inst.isInvalid())
39913991
return QualType();
39923992

3993-
CanonType = SubstType(Pattern->getUnderlyingType(),
3994-
TemplateArgLists, AliasTemplate->getLocation(),
3995-
AliasTemplate->getDeclName());
3993+
{
3994+
bool ForLambdaCallOperator = false;
3995+
if (const auto *Rec = dyn_cast<CXXRecordDecl>(Pattern->getDeclContext()))
3996+
ForLambdaCallOperator = Rec->isLambda();
3997+
Sema::ContextRAII SavedContext(*this, Pattern->getDeclContext(),
3998+
!ForLambdaCallOperator);
3999+
CanonType =
4000+
SubstType(Pattern->getUnderlyingType(), TemplateArgLists,
4001+
AliasTemplate->getLocation(), AliasTemplate->getDeclName());
4002+
}
39964003
if (CanonType.isNull()) {
39974004
// If this was enable_if and we failed to find the nested type
39984005
// within enable_if in a SFINAE context, dig out the specific

clang/test/CXX/temp/temp.decls/temp.alias/p3.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
// The example given in the standard (this is rejected for other reasons anyway).
44
template<class T> struct A;
5-
template<class T> using B = typename A<T>::U; // expected-error {{no type named 'U' in 'A<T>'}}
5+
template<class T> using B = typename A<T>::U; // expected-error {{no type named 'U' in 'A<short>'}}
6+
// expected-note@-1 {{in instantiation of template class 'A<short>' requested here}}
67
template<class T> struct A {
78
typedef B<T> U; // expected-note {{in instantiation of template type alias 'B' requested here}}
89
};
9-
B<short> b;
10+
B<short> b; // expected-note {{in instantiation of template type alias 'B' requested here}}
1011

1112
template<typename T> using U = int;
1213

clang/test/SemaCXX/alias-template.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,53 @@ int g = sfinae_me<int>(); // expected-error{{no matching function for call to 's
192192
namespace NullExceptionDecl {
193193
template<int... I> auto get = []() { try { } catch(...) {}; return I; }; // expected-error{{initializer contains unexpanded parameter pack 'I'}}
194194
}
195+
196+
namespace GH41693 {
197+
struct S {
198+
private:
199+
template <typename> static constexpr void Impl() {}
200+
201+
public:
202+
template <typename X> using U = decltype(Impl<X>());
203+
};
204+
205+
using X = S::U<void>;
206+
struct Y {
207+
private:
208+
static constexpr int x=0;
209+
210+
template <typename>
211+
static constexpr int y=0;
212+
213+
template <typename>
214+
static constexpr int foo();
215+
216+
public:
217+
template <typename U>
218+
using bar1 = decltype(foo<U>());
219+
using bar2 = decltype(x);
220+
template <typename U>
221+
using bar3 = decltype(y<U>);
222+
};
223+
224+
225+
using type1 = Y::bar1<float>;
226+
using type2 = Y::bar2;
227+
using type3 = Y::bar3<float>;
228+
229+
struct theFriend{
230+
template<class T>
231+
using theAlias = decltype(&T::i);
232+
};
233+
234+
class theC{
235+
int i;
236+
public:
237+
friend struct theFriend;
238+
};
239+
240+
int foo(){
241+
(void)sizeof(theFriend::theAlias<theC>);
242+
}
243+
244+
}

0 commit comments

Comments
 (0)