Skip to content

Commit 958703d

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 530f884 commit 958703d

File tree

17 files changed

+652
-266
lines changed

17 files changed

+652
-266
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ C++23 Feature Support
129129
C++20 Feature Support
130130
^^^^^^^^^^^^^^^^^^^^^
131131

132+
C++17 Feature Support
133+
^^^^^^^^^^^^^^^^^^^^^
134+
- The implementation of the relaxed template template argument matching rules is
135+
more complete and reliable, and should provide more accurate diagnostics.
132136

133137
Resolutions to C++ Defect Reports
134138
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -260,6 +264,10 @@ Improvements to Clang's diagnostics
260264

261265
- Clang now diagnoses when the result of a [[nodiscard]] function is discarded after being cast in C. Fixes #GH104391.
262266

267+
- Clang now properly explains the reason a template template argument failed to
268+
match a template template parameter, in terms of the C++17 relaxed matching rules
269+
instead of the old ones.
270+
263271
- Don't emit duplicated dangling diagnostics. (#GH93386).
264272

265273
- Improved diagnostic when trying to befriend a concept. (#GH45182).
@@ -341,6 +349,8 @@ Bug Fixes to C++ Support
341349
- Correctly check constraints of explicit instantiations of member functions. (#GH46029)
342350
- When performing partial ordering of function templates, clang now checks that
343351
the deduction was consistent. Fixes (#GH18291).
352+
- Fixes to several issues in partial ordering of template template parameters, which
353+
were documented in the test suite.
344354
- Fixed an assertion failure about a constraint of a friend function template references to a value with greater
345355
template depth than the friend function template. (#GH98258)
346356
- Clang now rebuilds the template parameters of out-of-line declarations and specializations in the context

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5256,6 +5256,13 @@ def note_template_arg_refers_here_func : Note<
52565256
def err_template_arg_template_params_mismatch : Error<
52575257
"template template argument has different template parameters than its "
52585258
"corresponding template template parameter">;
5259+
def note_template_arg_template_params_mismatch : Note<
5260+
"template template argument has different template parameters than its "
5261+
"corresponding template template parameter">;
5262+
def err_non_deduced_mismatch : Error<
5263+
"could not match %diff{$ against $|types}0,1">;
5264+
def err_inconsistent_deduction : Error<
5265+
"conflicting deduction %diff{$ against $|types}0,1 for parameter">;
52595266
def err_template_arg_not_integral_or_enumeral : Error<
52605267
"non-type template argument of type %0 must have an integral or enumeration"
52615268
" type">;

clang/include/clang/Sema/Sema.h

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

1248512485
bool isTemplateTemplateParameterAtLeastAsSpecializedAs(
12486-
TemplateParameterList *PParam, TemplateDecl *AArg,
12487-
const DefaultArguments &DefaultArgs, SourceLocation Loc, bool IsDeduced);
12486+
TemplateParameterList *PParam, TemplateDecl *PArg, TemplateDecl *AArg,
12487+
const DefaultArguments &DefaultArgs, SourceLocation ArgLoc,
12488+
bool IsDeduced);
1248812489

1248912490
/// Mark which template parameters are used in a given expression.
1249012491
///
@@ -12793,6 +12794,9 @@ class Sema final : public SemaBase {
1279312794

1279412795
/// We are instantiating a type alias template declaration.
1279512796
TypeAliasTemplateInstantiation,
12797+
12798+
/// We are performing partial ordering for template template parameters.
12799+
PartialOrderingTTP,
1279612800
} Kind;
1279712801

1279812802
/// Was the enclosing context a non-instantiation SFINAE context?
@@ -13014,6 +13018,12 @@ class Sema final : public SemaBase {
1301413018
TemplateDecl *Entity, BuildingDeductionGuidesTag,
1301513019
SourceRange InstantiationRange = SourceRange());
1301613020

13021+
struct PartialOrderingTTP {};
13022+
/// \brief Note that we are partial ordering template template parameters.
13023+
InstantiatingTemplate(Sema &SemaRef, SourceLocation ArgLoc,
13024+
PartialOrderingTTP, TemplateDecl *PArg,
13025+
SourceRange InstantiationRange = SourceRange());
13026+
1301713027
/// Note that we have finished instantiating this template.
1301813028
void Clear();
1301913029

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
@@ -5341,8 +5341,7 @@ bool Sema::CheckTemplateArgumentList(
53415341
DefaultArgs && ParamIdx >= DefaultArgs.StartPos) {
53425342
// All written arguments should have been consumed by this point.
53435343
assert(ArgIdx == NumArgs && "bad default argument deduction");
5344-
// FIXME: Don't ignore parameter packs.
5345-
if (ParamIdx == DefaultArgs.StartPos && !(*Param)->isParameterPack()) {
5344+
if (ParamIdx == DefaultArgs.StartPos) {
53465345
assert(Param + DefaultArgs.Args.size() <= ParamEnd);
53475346
// Default arguments from a DeducedTemplateName are already converted.
53485347
for (const TemplateArgument &DefArg : DefaultArgs.Args) {
@@ -5586,8 +5585,9 @@ bool Sema::CheckTemplateArgumentList(
55865585
// pack expansions; they might be empty. This can happen even if
55875586
// PartialTemplateArgs is false (the list of arguments is complete but
55885587
// still dependent).
5589-
if (ArgIdx < NumArgs && CurrentInstantiationScope &&
5590-
CurrentInstantiationScope->getPartiallySubstitutedPack()) {
5588+
if (PartialOrderingTTP ||
5589+
(CurrentInstantiationScope &&
5590+
CurrentInstantiationScope->getPartiallySubstitutedPack())) {
55915591
while (ArgIdx < NumArgs &&
55925592
NewArgs[ArgIdx].getArgument().isPackExpansion()) {
55935593
const TemplateArgument &Arg = NewArgs[ArgIdx++].getArgument();
@@ -7185,64 +7185,46 @@ bool Sema::CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param,
71857185
<< Template;
71867186
}
71877187

7188+
if (!getLangOpts().RelaxedTemplateTemplateArgs)
7189+
return !TemplateParameterListsAreEqual(
7190+
Template->getTemplateParameters(), Params, /*Complain=*/true,
7191+
TPL_TemplateTemplateArgumentMatch, Arg.getLocation());
7192+
71887193
// C++1z [temp.arg.template]p3: (DR 150)
71897194
// A template-argument matches a template template-parameter P when P
71907195
// is at least as specialized as the template-argument A.
7191-
if (getLangOpts().RelaxedTemplateTemplateArgs) {
7192-
// Quick check for the common case:
7193-
// If P contains a parameter pack, then A [...] matches P if each of A's
7194-
// template parameters matches the corresponding template parameter in
7195-
// the template-parameter-list of P.
7196-
if (TemplateParameterListsAreEqual(
7197-
Template->getTemplateParameters(), Params, false,
7198-
TPL_TemplateTemplateArgumentMatch, Arg.getLocation()) &&
7199-
// If the argument has no associated constraints, then the parameter is
7200-
// definitely at least as specialized as the argument.
7201-
// Otherwise - we need a more thorough check.
7202-
!Template->hasAssociatedConstraints())
7203-
return false;
7204-
7205-
if (isTemplateTemplateParameterAtLeastAsSpecializedAs(
7206-
Params, Template, DefaultArgs, Arg.getLocation(), IsDeduced)) {
7207-
// P2113
7208-
// C++20[temp.func.order]p2
7209-
// [...] If both deductions succeed, the partial ordering selects the
7210-
// more constrained template (if one exists) as determined below.
7211-
SmallVector<const Expr *, 3> ParamsAC, TemplateAC;
7212-
Params->getAssociatedConstraints(ParamsAC);
7213-
// C++2a[temp.arg.template]p3
7214-
// [...] In this comparison, if P is unconstrained, the constraints on A
7215-
// are not considered.
7216-
if (ParamsAC.empty())
7217-
return false;
7196+
if (!isTemplateTemplateParameterAtLeastAsSpecializedAs(
7197+
Params, Param, Template, DefaultArgs, Arg.getLocation(), IsDeduced))
7198+
return true;
7199+
// P2113
7200+
// C++20[temp.func.order]p2
7201+
// [...] If both deductions succeed, the partial ordering selects the
7202+
// more constrained template (if one exists) as determined below.
7203+
SmallVector<const Expr *, 3> ParamsAC, TemplateAC;
7204+
Params->getAssociatedConstraints(ParamsAC);
7205+
// C++20[temp.arg.template]p3
7206+
// [...] In this comparison, if P is unconstrained, the constraints on A
7207+
// are not considered.
7208+
if (ParamsAC.empty())
7209+
return false;
72187210

7219-
Template->getAssociatedConstraints(TemplateAC);
7211+
Template->getAssociatedConstraints(TemplateAC);
72207212

7221-
bool IsParamAtLeastAsConstrained;
7222-
if (IsAtLeastAsConstrained(Param, ParamsAC, Template, TemplateAC,
7223-
IsParamAtLeastAsConstrained))
7224-
return true;
7225-
if (!IsParamAtLeastAsConstrained) {
7226-
Diag(Arg.getLocation(),
7227-
diag::err_template_template_parameter_not_at_least_as_constrained)
7228-
<< Template << Param << Arg.getSourceRange();
7229-
Diag(Param->getLocation(), diag::note_entity_declared_at) << Param;
7230-
Diag(Template->getLocation(), diag::note_entity_declared_at)
7231-
<< Template;
7232-
MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Param, ParamsAC, Template,
7233-
TemplateAC);
7234-
return true;
7235-
}
7236-
return false;
7237-
}
7238-
// FIXME: Produce better diagnostics for deduction failures.
7213+
bool IsParamAtLeastAsConstrained;
7214+
if (IsAtLeastAsConstrained(Param, ParamsAC, Template, TemplateAC,
7215+
IsParamAtLeastAsConstrained))
7216+
return true;
7217+
if (!IsParamAtLeastAsConstrained) {
7218+
Diag(Arg.getLocation(),
7219+
diag::err_template_template_parameter_not_at_least_as_constrained)
7220+
<< Template << Param << Arg.getSourceRange();
7221+
Diag(Param->getLocation(), diag::note_entity_declared_at) << Param;
7222+
Diag(Template->getLocation(), diag::note_entity_declared_at) << Template;
7223+
MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Param, ParamsAC, Template,
7224+
TemplateAC);
7225+
return true;
72397226
}
7240-
7241-
return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
7242-
Params,
7243-
true,
7244-
TPL_TemplateTemplateArgumentMatch,
7245-
Arg.getLocation());
7227+
return false;
72467228
}
72477229

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

0 commit comments

Comments
 (0)