Skip to content

Commit 7e98320

Browse files
committed
[clang] Finish implementation of P0522
This finishes the clang implementation of P0522, getting rid of the fallback to the old, pre-P0522 rules. Before this patch, when partial ordering template template parameters, we would perform, in order: * If the old rules would match, we would accept it. Otherwise, don't generate diagnostics yet. * If the new rules would match, just accept it. Otherwise, don't generate any diagnostics yet again. * Apply the old rules again, this time with diagnostics. This situation was far from ideal, as we would sometimes: * Accept some things we shouldn't. * Reject some things we shouldn't. * Only diagnose rejection in terms of the old rules. With this patch, we apply the P0522 rules throughout. This needed to extend template argument deduction in order to accept the historial rule for TTP matching pack parameter to non-pack arguments. This change also makes us accept some combinations of historical and P0522 allowances we wouldn't before. It also fixes a bunch of bugs that were documented in the test suite, which I am not sure there are issues already created for them. This causes a lot of changes to the way these failures are diagnosed, with related test suite churn. The problem here is that the old rules were very simple and non-recursive, making it easy to provide customized diagnostics, and to keep them consistent with each other. The new rules are a lot more complex and rely on template argument deduction, substitutions, and they are recursive. The approach taken here is to mostly rely on existing diagnostics, and create a new instantiation context that keeps track of this context. So for example when a substitution failure occurs, we use the error produced there unmodified, and just attach notes to it explaining that it occurred in the context of partial ordering this template argument against that template parameter. This diverges from the old diagnostics, which would lead with an error pointing to the template argument, explain the problem in subsequent notes, and produce a final note pointing to the parameter.
1 parent 9d06fd6 commit 7e98320

File tree

18 files changed

+512
-273
lines changed

18 files changed

+512
-273
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ C++ Language Changes
6767

6868
C++17 Feature Support
6969
^^^^^^^^^^^^^^^^^^^^^
70+
- The implementation of the relaxed template template argument matching rules is
71+
more complete and reliable, and should provide more accurate diagnostics.
7072

7173
C++14 Feature Support
7274
^^^^^^^^^^^^^^^^^^^^^
@@ -133,6 +135,10 @@ Improvements to Clang's diagnostics
133135

134136
- Clang now diagnoses undefined behavior in constant expressions more consistently. This includes invalid shifts, and signed overflow in arithmetic.
135137

138+
- Clang now properly explains the reason a template template argument failed to
139+
match a template template parameter, in terms of the C++17 relaxed matching rules
140+
instead of the old ones.
141+
136142
Improvements to Clang's time-trace
137143
----------------------------------
138144

@@ -158,6 +164,8 @@ Bug Fixes to C++ Support
158164
- Fixed a failed assertion when checking invalid delete operator declaration. (#GH96191)
159165
- When performing partial ordering of function templates, clang now checks that
160166
the deduction was consistent. Fixes (#GH18291).
167+
- Fixes to several issues in partial ordering of template template parameters, which
168+
were documented in the test suite.
161169

162170
Bug Fixes to AST Handling
163171
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5244,6 +5244,13 @@ def note_template_arg_refers_here_func : Note<
52445244
def err_template_arg_template_params_mismatch : Error<
52455245
"template template argument has different template parameters than its "
52465246
"corresponding template template parameter">;
5247+
def note_template_arg_template_params_mismatch : Note<
5248+
"template template argument has different template parameters than its "
5249+
"corresponding template template parameter">;
5250+
def err_non_deduced_mismatch : Error<
5251+
"could not match %diff{$ against $|types}0,1">;
5252+
def err_inconsistent_deduction : Error<
5253+
"conflicting deduction %diff{$ against $|types}0,1 for parameter">;
52475254
def err_template_arg_not_integral_or_enumeral : Error<
52485255
"non-type template argument of type %0 must have an integral or enumeration"
52495256
" type">;

clang/include/clang/Sema/Sema.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12457,8 +12457,9 @@ class Sema final : public SemaBase {
1245712457
sema::TemplateDeductionInfo &Info);
1245812458

1245912459
bool isTemplateTemplateParameterAtLeastAsSpecializedAs(
12460-
TemplateParameterList *PParam, TemplateDecl *AArg,
12461-
const DefaultArguments &DefaultArgs, SourceLocation Loc, bool IsDeduced);
12460+
TemplateParameterList *PParam, TemplateDecl *PArg, TemplateDecl *AArg,
12461+
const DefaultArguments &DefaultArgs, SourceLocation ArgLoc,
12462+
bool IsDeduced);
1246212463

1246312464
/// Mark which template parameters are used in a given expression.
1246412465
///
@@ -12767,6 +12768,9 @@ class Sema final : public SemaBase {
1276712768

1276812769
/// We are instantiating a type alias template declaration.
1276912770
TypeAliasTemplateInstantiation,
12771+
12772+
/// We are performing partial ordering for template template parameters.
12773+
PartialOrderingTTP,
1277012774
} Kind;
1277112775

1277212776
/// Was the enclosing context a non-instantiation SFINAE context?
@@ -12988,6 +12992,12 @@ class Sema final : public SemaBase {
1298812992
TemplateDecl *Entity, BuildingDeductionGuidesTag,
1298912993
SourceRange InstantiationRange = SourceRange());
1299012994

12995+
struct PartialOrderingTTP {};
12996+
/// \brief Note that we are partial ordering template template parameters.
12997+
InstantiatingTemplate(Sema &SemaRef, SourceLocation ArgLoc,
12998+
PartialOrderingTTP, TemplateDecl *PArg,
12999+
SourceRange InstantiationRange = SourceRange());
13000+
1299113001
/// Note that we have finished instantiating this template.
1299213002
void Clear();
1299313003

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
456456
return "BuildingDeductionGuides";
457457
case CodeSynthesisContext::TypeAliasTemplateInstantiation:
458458
return "TypeAliasTemplateInstantiation";
459+
case CodeSynthesisContext::PartialOrderingTTP:
460+
return "PartialOrderingTTP";
459461
}
460462
return "";
461463
}

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5316,8 +5316,7 @@ bool Sema::CheckTemplateArgumentList(
53165316
DefaultArgs && ParamIdx >= DefaultArgs.StartPos) {
53175317
// All written arguments should have been consumed by this point.
53185318
assert(ArgIdx == NumArgs && "bad default argument deduction");
5319-
// FIXME: Don't ignore parameter packs.
5320-
if (ParamIdx == DefaultArgs.StartPos && !(*Param)->isParameterPack()) {
5319+
if (ParamIdx == DefaultArgs.StartPos) {
53215320
assert(Param + DefaultArgs.Args.size() <= ParamEnd);
53225321
// Default arguments from a DeducedTemplateName are already converted.
53235322
for (const TemplateArgument &DefArg : DefaultArgs.Args) {
@@ -5561,8 +5560,9 @@ bool Sema::CheckTemplateArgumentList(
55615560
// pack expansions; they might be empty. This can happen even if
55625561
// PartialTemplateArgs is false (the list of arguments is complete but
55635562
// still dependent).
5564-
if (ArgIdx < NumArgs && CurrentInstantiationScope &&
5565-
CurrentInstantiationScope->getPartiallySubstitutedPack()) {
5563+
if (PartialOrderingTTP ||
5564+
(CurrentInstantiationScope &&
5565+
CurrentInstantiationScope->getPartiallySubstitutedPack())) {
55665566
while (ArgIdx < NumArgs &&
55675567
NewArgs[ArgIdx].getArgument().isPackExpansion()) {
55685568
const TemplateArgument &Arg = NewArgs[ArgIdx++].getArgument();
@@ -7160,64 +7160,46 @@ bool Sema::CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param,
71607160
<< Template;
71617161
}
71627162

7163+
if (!getLangOpts().RelaxedTemplateTemplateArgs)
7164+
return !TemplateParameterListsAreEqual(
7165+
Template->getTemplateParameters(), Params, /*Complain=*/true,
7166+
TPL_TemplateTemplateArgumentMatch, Arg.getLocation());
7167+
71637168
// C++1z [temp.arg.template]p3: (DR 150)
71647169
// A template-argument matches a template template-parameter P when P
71657170
// is at least as specialized as the template-argument A.
7166-
if (getLangOpts().RelaxedTemplateTemplateArgs) {
7167-
// Quick check for the common case:
7168-
// If P contains a parameter pack, then A [...] matches P if each of A's
7169-
// template parameters matches the corresponding template parameter in
7170-
// the template-parameter-list of P.
7171-
if (TemplateParameterListsAreEqual(
7172-
Template->getTemplateParameters(), Params, false,
7173-
TPL_TemplateTemplateArgumentMatch, Arg.getLocation()) &&
7174-
// If the argument has no associated constraints, then the parameter is
7175-
// definitely at least as specialized as the argument.
7176-
// Otherwise - we need a more thorough check.
7177-
!Template->hasAssociatedConstraints())
7178-
return false;
7179-
7180-
if (isTemplateTemplateParameterAtLeastAsSpecializedAs(
7181-
Params, Template, DefaultArgs, Arg.getLocation(), IsDeduced)) {
7182-
// P2113
7183-
// C++20[temp.func.order]p2
7184-
// [...] If both deductions succeed, the partial ordering selects the
7185-
// more constrained template (if one exists) as determined below.
7186-
SmallVector<const Expr *, 3> ParamsAC, TemplateAC;
7187-
Params->getAssociatedConstraints(ParamsAC);
7188-
// C++2a[temp.arg.template]p3
7189-
// [...] In this comparison, if P is unconstrained, the constraints on A
7190-
// are not considered.
7191-
if (ParamsAC.empty())
7192-
return false;
7171+
if (!isTemplateTemplateParameterAtLeastAsSpecializedAs(
7172+
Params, Param, Template, DefaultArgs, Arg.getLocation(), IsDeduced))
7173+
return true;
7174+
// P2113
7175+
// C++20[temp.func.order]p2
7176+
// [...] If both deductions succeed, the partial ordering selects the
7177+
// more constrained template (if one exists) as determined below.
7178+
SmallVector<const Expr *, 3> ParamsAC, TemplateAC;
7179+
Params->getAssociatedConstraints(ParamsAC);
7180+
// C++20[temp.arg.template]p3
7181+
// [...] In this comparison, if P is unconstrained, the constraints on A
7182+
// are not considered.
7183+
if (ParamsAC.empty())
7184+
return false;
71937185

7194-
Template->getAssociatedConstraints(TemplateAC);
7186+
Template->getAssociatedConstraints(TemplateAC);
71957187

7196-
bool IsParamAtLeastAsConstrained;
7197-
if (IsAtLeastAsConstrained(Param, ParamsAC, Template, TemplateAC,
7198-
IsParamAtLeastAsConstrained))
7199-
return true;
7200-
if (!IsParamAtLeastAsConstrained) {
7201-
Diag(Arg.getLocation(),
7202-
diag::err_template_template_parameter_not_at_least_as_constrained)
7203-
<< Template << Param << Arg.getSourceRange();
7204-
Diag(Param->getLocation(), diag::note_entity_declared_at) << Param;
7205-
Diag(Template->getLocation(), diag::note_entity_declared_at)
7206-
<< Template;
7207-
MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Param, ParamsAC, Template,
7208-
TemplateAC);
7209-
return true;
7210-
}
7211-
return false;
7212-
}
7213-
// FIXME: Produce better diagnostics for deduction failures.
7188+
bool IsParamAtLeastAsConstrained;
7189+
if (IsAtLeastAsConstrained(Param, ParamsAC, Template, TemplateAC,
7190+
IsParamAtLeastAsConstrained))
7191+
return true;
7192+
if (!IsParamAtLeastAsConstrained) {
7193+
Diag(Arg.getLocation(),
7194+
diag::err_template_template_parameter_not_at_least_as_constrained)
7195+
<< Template << Param << Arg.getSourceRange();
7196+
Diag(Param->getLocation(), diag::note_entity_declared_at) << Param;
7197+
Diag(Template->getLocation(), diag::note_entity_declared_at) << Template;
7198+
MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Param, ParamsAC, Template,
7199+
TemplateAC);
7200+
return true;
72147201
}
7215-
7216-
return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
7217-
Params,
7218-
true,
7219-
TPL_TemplateTemplateArgumentMatch,
7220-
Arg.getLocation());
7202+
return false;
72217203
}
72227204

72237205
static Sema::SemaDiagnosticBuilder noteLocation(Sema &S, const NamedDecl &Decl,

0 commit comments

Comments
 (0)