Skip to content

Commit c86a53d

Browse files
authored
[clang] Implement provisional wording for CWG2398 regarding packs (llvm#90820)
This solves some ambuguity introduced in P0522 regarding how template template parameters are partially ordered, and should reduce the negative impact of enabling `-frelaxed-template-template-args` by default. When performing template argument deduction, a template template parameter containing no packs should be more specialized than one that does. Given the following example: ```C++ template<class T2> struct A; template<template<class ...T3s> class TT1, class T4> struct A<TT1<T4>>; // #1 template<template<class T5 > class TT2, class T6> struct A<TT2<T6>>; // #2 template<class T1> struct B; template struct A<B<char>>; ``` Prior to P0522, candidate `#2` would be more specialized. After P0522, neither is more specialized, so this becomes ambiguous. With this change, `#2` becomes more specialized again, maintaining compatibility with pre-P0522 implementations. The problem is that in P0522, candidates are at least as specialized when matching packs to fixed-size lists both ways, whereas before, a fixed-size list is more specialized. This patch keeps the original behavior when checking template arguments outside deduction, but restores this aspect of pre-P0522 matching during deduction. --- Since this changes provisional implementation of CWG2398 which has not been released yet, and already contains a changelog entry, we don't provide a changelog entry here.
1 parent 997eae3 commit c86a53d

File tree

4 files changed

+65
-32
lines changed

4 files changed

+65
-32
lines changed

clang/include/clang/Sema/Sema.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -9133,7 +9133,7 @@ class Sema final : public SemaBase {
91339133
CheckTemplateArgumentKind CTAK);
91349134
bool CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param,
91359135
TemplateParameterList *Params,
9136-
TemplateArgumentLoc &Arg);
9136+
TemplateArgumentLoc &Arg, bool IsDeduced);
91379137

91389138
void NoteTemplateLocation(const NamedDecl &Decl,
91399139
std::optional<SourceRange> ParamRange = {});
@@ -9612,7 +9612,8 @@ class Sema final : public SemaBase {
96129612
sema::TemplateDeductionInfo &Info);
96139613

96149614
bool isTemplateTemplateParameterAtLeastAsSpecializedAs(
9615-
TemplateParameterList *PParam, TemplateDecl *AArg, SourceLocation Loc);
9615+
TemplateParameterList *PParam, TemplateDecl *AArg, SourceLocation Loc,
9616+
bool IsDeduced);
96169617

96179618
void MarkUsedTemplateParameters(const Expr *E, bool OnlyDeduced,
96189619
unsigned Depth, llvm::SmallBitVector &Used);

clang/lib/Sema/SemaTemplate.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -6554,7 +6554,8 @@ bool Sema::CheckTemplateArgument(
65546554

65556555
case TemplateArgument::Template:
65566556
case TemplateArgument::TemplateExpansion:
6557-
if (CheckTemplateTemplateArgument(TempParm, Params, Arg))
6557+
if (CheckTemplateTemplateArgument(TempParm, Params, Arg,
6558+
/*IsDeduced=*/CTAK != CTAK_Specified))
65586559
return true;
65596560

65606561
SugaredConverted.push_back(Arg.getArgument());
@@ -8472,7 +8473,8 @@ static void DiagnoseTemplateParameterListArityMismatch(
84728473
/// It returns true if an error occurred, and false otherwise.
84738474
bool Sema::CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param,
84748475
TemplateParameterList *Params,
8475-
TemplateArgumentLoc &Arg) {
8476+
TemplateArgumentLoc &Arg,
8477+
bool IsDeduced) {
84768478
TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern();
84778479
TemplateDecl *Template = Name.getAsTemplateDecl();
84788480
if (!Template) {
@@ -8524,8 +8526,8 @@ bool Sema::CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param,
85248526
!Template->hasAssociatedConstraints())
85258527
return false;
85268528

8527-
if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template,
8528-
Arg.getLocation())) {
8529+
if (isTemplateTemplateParameterAtLeastAsSpecializedAs(
8530+
Params, Template, Arg.getLocation(), IsDeduced)) {
85298531
// P2113
85308532
// C++20[temp.func.order]p2
85318533
// [...] If both deductions succeed, the partial ordering selects the

clang/lib/Sema/SemaTemplateDeduction.cpp

+53-14
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@ static TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch(
139139
SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF,
140140
bool PartialOrdering = false, bool DeducedFromArrayBound = false);
141141

142+
enum class PackFold { ParameterToArgument, ArgumentToParameter };
142143
static TemplateDeductionResult
143144
DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
144145
ArrayRef<TemplateArgument> Ps,
145146
ArrayRef<TemplateArgument> As,
146147
TemplateDeductionInfo &Info,
147148
SmallVectorImpl<DeducedTemplateArgument> &Deduced,
148-
bool NumberOfArgumentsMustMatch);
149+
bool NumberOfArgumentsMustMatch,
150+
PackFold PackFold = PackFold::ParameterToArgument);
149151

150152
static void MarkUsedTemplateParameters(ASTContext &Ctx,
151153
const TemplateArgument &TemplateArg,
@@ -2550,7 +2552,9 @@ DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
25502552
ArrayRef<TemplateArgument> As,
25512553
TemplateDeductionInfo &Info,
25522554
SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2553-
bool NumberOfArgumentsMustMatch) {
2555+
bool NumberOfArgumentsMustMatch, PackFold PackFold) {
2556+
if (PackFold == PackFold::ArgumentToParameter)
2557+
std::swap(Ps, As);
25542558
// C++0x [temp.deduct.type]p9:
25552559
// If the template argument list of P contains a pack expansion that is not
25562560
// the last template argument, the entire template argument list is a
@@ -2581,8 +2585,11 @@ DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
25812585
return TemplateDeductionResult::MiscellaneousDeductionFailure;
25822586

25832587
// Perform deduction for this Pi/Ai pair.
2584-
if (auto Result = DeduceTemplateArguments(S, TemplateParams, P,
2585-
As[ArgIdx], Info, Deduced);
2588+
TemplateArgument Pi = P, Ai = As[ArgIdx];
2589+
if (PackFold == PackFold::ArgumentToParameter)
2590+
std::swap(Pi, Ai);
2591+
if (auto Result =
2592+
DeduceTemplateArguments(S, TemplateParams, Pi, Ai, Info, Deduced);
25862593
Result != TemplateDeductionResult::Success)
25872594
return Result;
25882595

@@ -2609,9 +2616,12 @@ DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
26092616
for (; hasTemplateArgumentForDeduction(As, ArgIdx) &&
26102617
PackScope.hasNextElement();
26112618
++ArgIdx) {
2619+
TemplateArgument Pi = Pattern, Ai = As[ArgIdx];
2620+
if (PackFold == PackFold::ArgumentToParameter)
2621+
std::swap(Pi, Ai);
26122622
// Deduce template arguments from the pattern.
2613-
if (auto Result = DeduceTemplateArguments(S, TemplateParams, Pattern,
2614-
As[ArgIdx], Info, Deduced);
2623+
if (auto Result =
2624+
DeduceTemplateArguments(S, TemplateParams, Pi, Ai, Info, Deduced);
26152625
Result != TemplateDeductionResult::Success)
26162626
return Result;
26172627

@@ -6299,7 +6309,8 @@ bool Sema::isMoreSpecializedThanPrimary(
62996309
}
63006310

63016311
bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs(
6302-
TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc) {
6312+
TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc,
6313+
bool IsDeduced) {
63036314
// C++1z [temp.arg.template]p4: (DR 150)
63046315
// A template template-parameter P is at least as specialized as a
63056316
// template template-argument A if, given the following rewrite to two
@@ -6309,11 +6320,10 @@ bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs(
63096320
// equivalent partial ordering by performing deduction directly on
63106321
// the template parameter lists of the template template parameters.
63116322
//
6312-
// Given an invented class template X with the template parameter list of
6313-
// A (including default arguments):
6314-
TemplateName X = Context.getCanonicalTemplateName(TemplateName(AArg));
63156323
TemplateParameterList *A = AArg->getTemplateParameters();
63166324

6325+
// Given an invented class template X with the template parameter list of
6326+
// A (including default arguments):
63176327
// - Each function template has a single function parameter whose type is
63186328
// a specialization of X with template arguments corresponding to the
63196329
// template parameters from the respective function template
@@ -6356,14 +6366,43 @@ bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs(
63566366
return false;
63576367
}
63586368

6359-
QualType AType = Context.getCanonicalTemplateSpecializationType(X, AArgs);
6360-
QualType PType = Context.getCanonicalTemplateSpecializationType(X, PArgs);
6369+
// Determine whether P1 is at least as specialized as P2.
6370+
TemplateDeductionInfo Info(Loc, A->getDepth());
6371+
SmallVector<DeducedTemplateArgument, 4> Deduced;
6372+
Deduced.resize(A->size());
63616373

63626374
// ... the function template corresponding to P is at least as specialized
63636375
// as the function template corresponding to A according to the partial
63646376
// ordering rules for function templates.
6365-
TemplateDeductionInfo Info(Loc, A->getDepth());
6366-
return isAtLeastAsSpecializedAs(*this, PType, AType, AArg, Info);
6377+
6378+
// Provisional resolution for CWG2398: Regarding temp.arg.template]p4, when
6379+
// applying the partial ordering rules for function templates on
6380+
// the rewritten template template parameters:
6381+
// - In a deduced context, the matching of packs versus fixed-size needs to
6382+
// be inverted between Ps and As. On non-deduced context, matching needs to
6383+
// happen both ways, according to [temp.arg.template]p3, but this is
6384+
// currently implemented as a special case elsewhere.
6385+
if (::DeduceTemplateArguments(*this, A, AArgs, PArgs, Info, Deduced,
6386+
/*NumberOfArgumentsMustMatch=*/false,
6387+
IsDeduced ? PackFold::ArgumentToParameter
6388+
: PackFold::ParameterToArgument) !=
6389+
TemplateDeductionResult::Success)
6390+
return false;
6391+
6392+
SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end());
6393+
Sema::InstantiatingTemplate Inst(*this, Info.getLocation(), AArg, DeducedArgs,
6394+
Info);
6395+
if (Inst.isInvalid())
6396+
return false;
6397+
6398+
bool AtLeastAsSpecialized;
6399+
runWithSufficientStackSpace(Info.getLocation(), [&] {
6400+
AtLeastAsSpecialized =
6401+
::FinishTemplateArgumentDeduction(
6402+
*this, AArg, /*IsPartialOrdering=*/true, PArgs, Deduced, Info) ==
6403+
TemplateDeductionResult::Success;
6404+
});
6405+
return AtLeastAsSpecialized;
63676406
}
63686407

63696408
namespace {

clang/test/SemaTemplate/cwg2398.cpp

+3-12
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,19 @@ namespace templ {
6262
namespace type_pack1 {
6363
template<class T2> struct A;
6464
template<template<class ...T3s> class TT1, class T4> struct A<TT1<T4>> ;
65-
// new-note@-1 {{partial specialization matches}}
6665
template<template<class T5 > class TT2, class T6> struct A<TT2<T6>> {};
67-
// new-note@-1 {{partial specialization matches}}
6866

6967
template<class T1> struct B;
7068
template struct A<B<char>>;
71-
// new-error@-1 {{ambiguous partial specialization}}
7269
} // namespace type_pack1
7370

7471
namespace type_pack2 {
7572
template<class T2> struct A;
7673
template<template<class ...T3s> class TT1, class ...T4> struct A<TT1<T4...>> ;
77-
// new-note@-1 {{partial specialization matches}}
7874
template<template<class T5 > class TT2, class ...T6> struct A<TT2<T6...>> {};
79-
// new-note@-1 {{partial specialization matches}}
8075

8176
template<class T1> struct B;
8277
template struct A<B<char>>;
83-
// new-error@-1 {{ambiguous partial specialization}}
8478
} // namespace type_pack2
8579

8680
namespace type_pack3 {
@@ -140,13 +134,10 @@ namespace ttp_defaults {
140134

141135
namespace ttp_only {
142136
template <template <class... > class TT1> struct A { static constexpr int V = 0; };
143-
// new-note@-1 2{{template is declared here}}
144137
template <template <class > class TT2> struct A<TT2> { static constexpr int V = 1; };
145-
// new-error@-1 {{not more specialized than the primary template}}
146-
// new-note@-2 {{partial specialization matches}}
138+
// new-note@-1 {{partial specialization matches}}
147139
template <template <class, class> class TT3> struct A<TT3> { static constexpr int V = 2; };
148-
// new-error@-1 {{not more specialized than the primary template}}
149-
// new-note@-2 {{partial specialization matches}}
140+
// new-note@-1 {{partial specialization matches}}
150141

151142
template <class ... > struct B;
152143
template <class > struct C;
@@ -193,5 +184,5 @@ namespace consistency {
193184

194185
template struct A<B<int>, B<int>, B<int>>;
195186
// new-error@-1 {{ambiguous partial specializations}}
196-
} // namespace t1
187+
} // namespace t2
197188
} // namespace consistency

0 commit comments

Comments
 (0)