Skip to content

Commit 3b5e7c8

Browse files
authored
[Clang][Sema] Fix a bug on type constraint checking (llvm#84671)
Try to fix llvm#84368 When visiting class members in `TemplateDeclInstantiator::VisitClassTemplateDecl` during `Sema::InstantiateClass`, we miss to set attribute of friend declaration if it is(`isFriend` is true). This will lead to `Sema::AreConstraintExpressionsEqual` return false when invoked in `MatchTemplateParameterKind`. Because it makes `Sema::getTemplateInstantiationArgs` returns incorrect template argument(`MultiLevelTemplateArgumentList`). When we handle `CXXRecordDecl` In `Sema::getTemplateInstantiationArgs`, friend declaration(its parent context is `FileContext`) makes us to choose `LexicalDeclContext` not `DeclContext` and this is what we want. Co-authored-by: huqizhi <[email protected]>
1 parent fef62be commit 3b5e7c8

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ Bug Fixes to C++ Support
376376
- Fixed a crash in constant evaluation when trying to access a
377377
captured ``this`` pointer in a lambda with an explicit object parameter.
378378
Fixes (#GH80997)
379+
- Fix an issue where missing set friend declaration in template class instantiation.
380+
Fixes (#GH84368).
379381

380382
Bug Fixes to AST Handling
381383
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,7 @@ Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
17271727
assert(!Owner->isDependentContext());
17281728
Inst->setLexicalDeclContext(Owner);
17291729
RecordInst->setLexicalDeclContext(Owner);
1730+
Inst->setObjectOfFriendDecl();
17301731

17311732
if (PrevClassTemplate) {
17321733
Inst->setCommonPtr(PrevClassTemplate->getCommonPtr());

clang/test/Sema/PR84368.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -std=c++20 -verify %s
2+
// RUN: %clang_cc1 -std=c++23 -verify %s
3+
// expected-no-diagnostics
4+
5+
template<class T> concept IsOk = requires() { typename T::Float; };
6+
7+
template<IsOk T> struct Thing;
8+
9+
template<IsOk T> struct Foobar {
10+
template<int> struct Inner {
11+
template<IsOk T2> friend struct Thing;
12+
};
13+
};
14+
15+
struct MyType { using Float=float; };
16+
Foobar<MyType>::Inner<0> foobar;

0 commit comments

Comments
 (0)