Skip to content

Commit 42239d2

Browse files
authored
[clang] Fix CTAD not respect default template arguments that were added after the definition. (#75569)
Fixes #69987
1 parent 465ecf8 commit 42239d2

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

clang/docs/ReleaseNotes.rst

+3
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ Bug Fixes in This Version
688688
(`#62157 <https://github.com/llvm/llvm-project/issues/62157>`_) and
689689
(`#64885 <https://github.com/llvm/llvm-project/issues/64885>`_) and
690690
(`#65568 <https://github.com/llvm/llvm-project/issues/65568>`_)
691+
- Fix an issue where clang doesn't respect detault template arguments that
692+
are added in a later redeclaration for CTAD.
693+
Fixes (#69987 <https://github.com/llvm/llvm-project/issues/69987>`_)
691694

692695
Bug Fixes to Compiler Builtins
693696
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaTemplate.cpp

+16-12
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,15 @@ static void SetNestedNameSpecifier(Sema &S, TagDecl *T,
18241824
T->setQualifierInfo(SS.getWithLocInContext(S.Context));
18251825
}
18261826

1827+
// Returns the template parameter list with all default template argument
1828+
// information.
1829+
static TemplateParameterList *GetTemplateParameterList(TemplateDecl *TD) {
1830+
// Make sure we get the template parameter list from the most
1831+
// recent declaration, since that is the only one that is guaranteed to
1832+
// have all the default template argument information.
1833+
return cast<TemplateDecl>(TD->getMostRecentDecl())->getTemplateParameters();
1834+
}
1835+
18271836
DeclResult Sema::CheckClassTemplate(
18281837
Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
18291838
CXXScopeSpec &SS, IdentifierInfo *Name, SourceLocation NameLoc,
@@ -2061,13 +2070,13 @@ DeclResult Sema::CheckClassTemplate(
20612070
if (!(TUK == TUK_Friend && CurContext->isDependentContext()) &&
20622071
CheckTemplateParameterList(
20632072
TemplateParams,
2064-
PrevClassTemplate
2065-
? PrevClassTemplate->getMostRecentDecl()->getTemplateParameters()
2066-
: nullptr,
2073+
PrevClassTemplate ? GetTemplateParameterList(PrevClassTemplate)
2074+
: nullptr,
20672075
(SS.isSet() && SemanticContext && SemanticContext->isRecord() &&
20682076
SemanticContext->isDependentContext())
20692077
? TPC_ClassTemplateMember
2070-
: TUK == TUK_Friend ? TPC_FriendClassTemplate : TPC_ClassTemplate,
2078+
: TUK == TUK_Friend ? TPC_FriendClassTemplate
2079+
: TPC_ClassTemplate,
20712080
SkipBody))
20722081
Invalid = true;
20732082

@@ -2298,7 +2307,7 @@ struct ConvertConstructorToDeductionGuideTransform {
22982307
// -- The template parameters are the template parameters of the class
22992308
// template followed by the template parameters (including default
23002309
// template arguments) of the constructor, if any.
2301-
TemplateParameterList *TemplateParams = Template->getTemplateParameters();
2310+
TemplateParameterList *TemplateParams = GetTemplateParameterList(Template);
23022311
if (FTD) {
23032312
TemplateParameterList *InnerParams = FTD->getTemplateParameters();
23042313
SmallVector<NamedDecl *, 16> AllParams;
@@ -2424,7 +2433,7 @@ struct ConvertConstructorToDeductionGuideTransform {
24242433
Params.push_back(NewParam);
24252434
}
24262435

2427-
return buildDeductionGuide(Template->getTemplateParameters(), nullptr,
2436+
return buildDeductionGuide(GetTemplateParameterList(Template), nullptr,
24282437
ExplicitSpecifier(), TSI, Loc, Loc, Loc);
24292438
}
24302439

@@ -5956,12 +5965,7 @@ bool Sema::CheckTemplateArgumentList(
59565965
// template.
59575966
TemplateArgumentListInfo NewArgs = TemplateArgs;
59585967

5959-
// Make sure we get the template parameter list from the most
5960-
// recent declaration, since that is the only one that is guaranteed to
5961-
// have all the default template argument information.
5962-
TemplateParameterList *Params =
5963-
cast<TemplateDecl>(Template->getMostRecentDecl())
5964-
->getTemplateParameters();
5968+
TemplateParameterList *Params = GetTemplateParameterList(Template);
59655969

59665970
SourceLocation RAngleLoc = NewArgs.getRAngleLoc();
59675971

clang/test/SemaTemplate/ctad.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ namespace Access {
4444
};
4545
D z = {Z(), {}};
4646
}
47+
48+
namespace GH69987 {
49+
template<class> struct X {};
50+
template<class = void> struct X;
51+
X x;
52+
53+
template<class T, class B> struct Y { Y(T); };
54+
template<class T, class B=void> struct Y ;
55+
Y y(1);
56+
};

0 commit comments

Comments
 (0)