Skip to content

Commit 7bb90d1

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 e28e935 commit 7bb90d1

File tree

17 files changed

+652
-265
lines changed

17 files changed

+652
-265
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ C++20 Feature Support
325325

326326
- Implemented module level lookup for C++20 modules. (#GH90154)
327327

328+
C++17 Feature Support
329+
^^^^^^^^^^^^^^^^^^^^^
330+
- The implementation of the relaxed template template argument matching rules is
331+
more complete and reliable, and should provide more accurate diagnostics.
328332

329333
Resolutions to C++ Defect Reports
330334
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -636,6 +640,10 @@ Improvements to Clang's diagnostics
636640

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

643+
- Clang now properly explains the reason a template template argument failed to
644+
match a template template parameter, in terms of the C++17 relaxed matching rules
645+
instead of the old ones.
646+
639647
- Don't emit duplicated dangling diagnostics. (#GH93386).
640648

641649
- Improved diagnostic when trying to befriend a concept. (#GH45182).
@@ -885,6 +893,8 @@ Bug Fixes to C++ Support
885893
- Correctly check constraints of explicit instantiations of member functions. (#GH46029)
886894
- When performing partial ordering of function templates, clang now checks that
887895
the deduction was consistent. Fixes (#GH18291).
896+
- Fixes to several issues in partial ordering of template template parameters, which
897+
were documented in the test suite.
888898
- Fixed an assertion failure about a constraint of a friend function template references to a value with greater
889899
template depth than the friend function template. (#GH98258)
890900
- 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
@@ -5323,6 +5323,13 @@ def note_template_arg_refers_here_func : Note<
53235323
def err_template_arg_template_params_mismatch : Error<
53245324
"template template argument has different template parameters than its "
53255325
"corresponding template template parameter">;
5326+
def note_template_arg_template_params_mismatch : Note<
5327+
"template template argument has different template parameters than its "
5328+
"corresponding template template parameter">;
5329+
def err_non_deduced_mismatch : Error<
5330+
"could not match %diff{$ against $|types}0,1">;
5331+
def err_inconsistent_deduction : Error<
5332+
"conflicting deduction %diff{$ against $|types}0,1 for parameter">;
53265333
def err_template_arg_not_integral_or_enumeral : Error<
53275334
"non-type template argument of type %0 must have an integral or enumeration"
53285335
" type">;

clang/include/clang/Sema/Sema.h

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

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

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

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

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

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

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
459459
return "BuildingDeductionGuides";
460460
case CodeSynthesisContext::TypeAliasTemplateInstantiation:
461461
return "TypeAliasTemplateInstantiation";
462+
case CodeSynthesisContext::PartialOrderingTTP:
463+
return "PartialOrderingTTP";
462464
}
463465
return "";
464466
}

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5527,8 +5527,7 @@ bool Sema::CheckTemplateArgumentList(
55275527
DefaultArgs && ParamIdx >= DefaultArgs.StartPos) {
55285528
// All written arguments should have been consumed by this point.
55295529
assert(ArgIdx == NumArgs && "bad default argument deduction");
5530-
// FIXME: Don't ignore parameter packs.
5531-
if (ParamIdx == DefaultArgs.StartPos && !(*Param)->isParameterPack()) {
5530+
if (ParamIdx == DefaultArgs.StartPos) {
55325531
assert(Param + DefaultArgs.Args.size() <= ParamEnd);
55335532
// Default arguments from a DeducedTemplateName are already converted.
55345533
for (const TemplateArgument &DefArg : DefaultArgs.Args) {
@@ -5753,8 +5752,9 @@ bool Sema::CheckTemplateArgumentList(
57535752
// pack expansions; they might be empty. This can happen even if
57545753
// PartialTemplateArgs is false (the list of arguments is complete but
57555754
// still dependent).
5756-
if (ArgIdx < NumArgs && CurrentInstantiationScope &&
5757-
CurrentInstantiationScope->getPartiallySubstitutedPack()) {
5755+
if (PartialOrderingTTP ||
5756+
(CurrentInstantiationScope &&
5757+
CurrentInstantiationScope->getPartiallySubstitutedPack())) {
57585758
while (ArgIdx < NumArgs &&
57595759
NewArgs[ArgIdx].getArgument().isPackExpansion()) {
57605760
const TemplateArgument &Arg = NewArgs[ArgIdx++].getArgument();
@@ -7359,64 +7359,46 @@ bool Sema::CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param,
73597359
<< Template;
73607360
}
73617361

7362+
if (!getLangOpts().RelaxedTemplateTemplateArgs)
7363+
return !TemplateParameterListsAreEqual(
7364+
Template->getTemplateParameters(), Params, /*Complain=*/true,
7365+
TPL_TemplateTemplateArgumentMatch, Arg.getLocation());
7366+
73627367
// C++1z [temp.arg.template]p3: (DR 150)
73637368
// A template-argument matches a template template-parameter P when P
73647369
// is at least as specialized as the template-argument A.
7365-
if (getLangOpts().RelaxedTemplateTemplateArgs) {
7366-
// Quick check for the common case:
7367-
// If P contains a parameter pack, then A [...] matches P if each of A's
7368-
// template parameters matches the corresponding template parameter in
7369-
// the template-parameter-list of P.
7370-
if (TemplateParameterListsAreEqual(
7371-
Template->getTemplateParameters(), Params, false,
7372-
TPL_TemplateTemplateArgumentMatch, Arg.getLocation()) &&
7373-
// If the argument has no associated constraints, then the parameter is
7374-
// definitely at least as specialized as the argument.
7375-
// Otherwise - we need a more thorough check.
7376-
!Template->hasAssociatedConstraints())
7377-
return false;
7378-
7379-
if (isTemplateTemplateParameterAtLeastAsSpecializedAs(
7380-
Params, Template, DefaultArgs, Arg.getLocation(), IsDeduced)) {
7381-
// P2113
7382-
// C++20[temp.func.order]p2
7383-
// [...] If both deductions succeed, the partial ordering selects the
7384-
// more constrained template (if one exists) as determined below.
7385-
SmallVector<const Expr *, 3> ParamsAC, TemplateAC;
7386-
Params->getAssociatedConstraints(ParamsAC);
7387-
// C++2a[temp.arg.template]p3
7388-
// [...] In this comparison, if P is unconstrained, the constraints on A
7389-
// are not considered.
7390-
if (ParamsAC.empty())
7391-
return false;
7370+
if (!isTemplateTemplateParameterAtLeastAsSpecializedAs(
7371+
Params, Param, Template, DefaultArgs, Arg.getLocation(), IsDeduced))
7372+
return true;
7373+
// P2113
7374+
// C++20[temp.func.order]p2
7375+
// [...] If both deductions succeed, the partial ordering selects the
7376+
// more constrained template (if one exists) as determined below.
7377+
SmallVector<const Expr *, 3> ParamsAC, TemplateAC;
7378+
Params->getAssociatedConstraints(ParamsAC);
7379+
// C++20[temp.arg.template]p3
7380+
// [...] In this comparison, if P is unconstrained, the constraints on A
7381+
// are not considered.
7382+
if (ParamsAC.empty())
7383+
return false;
73927384

7393-
Template->getAssociatedConstraints(TemplateAC);
7385+
Template->getAssociatedConstraints(TemplateAC);
73947386

7395-
bool IsParamAtLeastAsConstrained;
7396-
if (IsAtLeastAsConstrained(Param, ParamsAC, Template, TemplateAC,
7397-
IsParamAtLeastAsConstrained))
7398-
return true;
7399-
if (!IsParamAtLeastAsConstrained) {
7400-
Diag(Arg.getLocation(),
7401-
diag::err_template_template_parameter_not_at_least_as_constrained)
7402-
<< Template << Param << Arg.getSourceRange();
7403-
Diag(Param->getLocation(), diag::note_entity_declared_at) << Param;
7404-
Diag(Template->getLocation(), diag::note_entity_declared_at)
7405-
<< Template;
7406-
MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Param, ParamsAC, Template,
7407-
TemplateAC);
7408-
return true;
7409-
}
7410-
return false;
7411-
}
7412-
// FIXME: Produce better diagnostics for deduction failures.
7387+
bool IsParamAtLeastAsConstrained;
7388+
if (IsAtLeastAsConstrained(Param, ParamsAC, Template, TemplateAC,
7389+
IsParamAtLeastAsConstrained))
7390+
return true;
7391+
if (!IsParamAtLeastAsConstrained) {
7392+
Diag(Arg.getLocation(),
7393+
diag::err_template_template_parameter_not_at_least_as_constrained)
7394+
<< Template << Param << Arg.getSourceRange();
7395+
Diag(Param->getLocation(), diag::note_entity_declared_at) << Param;
7396+
Diag(Template->getLocation(), diag::note_entity_declared_at) << Template;
7397+
MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Param, ParamsAC, Template,
7398+
TemplateAC);
7399+
return true;
74137400
}
7414-
7415-
return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
7416-
Params,
7417-
true,
7418-
TPL_TemplateTemplateArgumentMatch,
7419-
Arg.getLocation());
7401+
return false;
74207402
}
74217403

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

0 commit comments

Comments
 (0)