[clang] Reland: [clang] Finish implementation of P0522 (PR #111711)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 9 09:49:24 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clang-modules
Author: Matheus Izvekov (mizvekov)
<details>
<summary>Changes</summary>
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.
---
Patch is 73.23 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/111711.diff
17 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+10)
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+7)
- (modified) clang/include/clang/Sema/Sema.h (+12-2)
- (modified) clang/lib/Frontend/FrontendActions.cpp (+2)
- (modified) clang/lib/Sema/SemaTemplate.cpp (+38-56)
- (modified) clang/lib/Sema/SemaTemplateDeduction.cpp (+250-103)
- (modified) clang/lib/Sema/SemaTemplateInstantiate.cpp (+15)
- (modified) clang/test/CXX/temp/temp.arg/temp.arg.template/p3-0x.cpp (+18-13)
- (modified) clang/test/CXX/temp/temp.param/p12.cpp (+11-10)
- (modified) clang/test/Modules/cxx-templates.cpp (+3-12)
- (modified) clang/test/SemaCXX/make_integer_seq.cpp (+2-3)
- (modified) clang/test/SemaTemplate/cwg2398.cpp (+156-4)
- (modified) clang/test/SemaTemplate/temp_arg_nontype.cpp (+18-8)
- (modified) clang/test/SemaTemplate/temp_arg_template.cpp (+23-15)
- (modified) clang/test/SemaTemplate/temp_arg_template_p0522.cpp (+52-30)
- (modified) clang/test/Templight/templight-empty-entries-fix.cpp (+12)
- (modified) clang/test/Templight/templight-prior-template-arg.cpp (+23-10)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a4bb303a2bc42b..7270e6898dbc7f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -174,6 +174,10 @@ C++23 Feature Support
C++20 Feature Support
^^^^^^^^^^^^^^^^^^^^^
+C++17 Feature Support
+^^^^^^^^^^^^^^^^^^^^^
+- The implementation of the relaxed template template argument matching rules is
+ more complete and reliable, and should provide more accurate diagnostics.
Resolutions to C++ Defect Reports
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -331,6 +335,10 @@ Improvements to Clang's diagnostics
- Clang now diagnoses when the result of a [[nodiscard]] function is discarded after being cast in C. Fixes #GH104391.
+- Clang now properly explains the reason a template template argument failed to
+ match a template template parameter, in terms of the C++17 relaxed matching rules
+ instead of the old ones.
+
- Don't emit duplicated dangling diagnostics. (#GH93386).
- Improved diagnostic when trying to befriend a concept. (#GH45182).
@@ -440,6 +448,8 @@ Bug Fixes to C++ Support
- Correctly check constraints of explicit instantiations of member functions. (#GH46029)
- When performing partial ordering of function templates, clang now checks that
the deduction was consistent. Fixes (#GH18291).
+- Fixes to several issues in partial ordering of template template parameters, which
+ were documented in the test suite.
- Fixed an assertion failure about a constraint of a friend function template references to a value with greater
template depth than the friend function template. (#GH98258)
- Clang now rebuilds the template parameters of out-of-line declarations and specializations in the context
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 777ea1f37cea46..f4a2d4a3f0656a 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5262,6 +5262,13 @@ def note_template_arg_refers_here_func : Note<
def err_template_arg_template_params_mismatch : Error<
"template template argument has different template parameters than its "
"corresponding template template parameter">;
+def note_template_arg_template_params_mismatch : Note<
+ "template template argument has different template parameters than its "
+ "corresponding template template parameter">;
+def err_non_deduced_mismatch : Error<
+ "could not match %diff{$ against $|types}0,1">;
+def err_inconsistent_deduction : Error<
+ "conflicting deduction %diff{$ against $|types}0,1 for parameter">;
def err_template_arg_not_integral_or_enumeral : Error<
"non-type template argument of type %0 must have an integral or enumeration"
" type">;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 86053bd7da1725..043456438b6d03 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -12417,8 +12417,9 @@ class Sema final : public SemaBase {
sema::TemplateDeductionInfo &Info);
bool isTemplateTemplateParameterAtLeastAsSpecializedAs(
- TemplateParameterList *PParam, TemplateDecl *AArg,
- const DefaultArguments &DefaultArgs, SourceLocation Loc, bool IsDeduced);
+ TemplateParameterList *PParam, TemplateDecl *PArg, TemplateDecl *AArg,
+ const DefaultArguments &DefaultArgs, SourceLocation ArgLoc,
+ bool IsDeduced);
/// Mark which template parameters are used in a given expression.
///
@@ -12727,6 +12728,9 @@ class Sema final : public SemaBase {
/// We are instantiating a type alias template declaration.
TypeAliasTemplateInstantiation,
+
+ /// We are performing partial ordering for template template parameters.
+ PartialOrderingTTP,
} Kind;
/// Was the enclosing context a non-instantiation SFINAE context?
@@ -12948,6 +12952,12 @@ class Sema final : public SemaBase {
TemplateDecl *Entity, BuildingDeductionGuidesTag,
SourceRange InstantiationRange = SourceRange());
+ struct PartialOrderingTTP {};
+ /// \brief Note that we are partial ordering template template parameters.
+ InstantiatingTemplate(Sema &SemaRef, SourceLocation ArgLoc,
+ PartialOrderingTTP, TemplateDecl *PArg,
+ SourceRange InstantiationRange = SourceRange());
+
/// Note that we have finished instantiating this template.
void Clear();
diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp
index 64f90c493c1055..e4b462b9b0fd81 100644
--- a/clang/lib/Frontend/FrontendActions.cpp
+++ b/clang/lib/Frontend/FrontendActions.cpp
@@ -457,6 +457,8 @@ class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
return "BuildingDeductionGuides";
case CodeSynthesisContext::TypeAliasTemplateInstantiation:
return "TypeAliasTemplateInstantiation";
+ case CodeSynthesisContext::PartialOrderingTTP:
+ return "PartialOrderingTTP";
}
return "";
}
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index dfd56debc75e99..fc56d737fb2325 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -5498,8 +5498,7 @@ bool Sema::CheckTemplateArgumentList(
DefaultArgs && ParamIdx >= DefaultArgs.StartPos) {
// All written arguments should have been consumed by this point.
assert(ArgIdx == NumArgs && "bad default argument deduction");
- // FIXME: Don't ignore parameter packs.
- if (ParamIdx == DefaultArgs.StartPos && !(*Param)->isParameterPack()) {
+ if (ParamIdx == DefaultArgs.StartPos) {
assert(Param + DefaultArgs.Args.size() <= ParamEnd);
// Default arguments from a DeducedTemplateName are already converted.
for (const TemplateArgument &DefArg : DefaultArgs.Args) {
@@ -5724,8 +5723,9 @@ bool Sema::CheckTemplateArgumentList(
// pack expansions; they might be empty. This can happen even if
// PartialTemplateArgs is false (the list of arguments is complete but
// still dependent).
- if (ArgIdx < NumArgs && CurrentInstantiationScope &&
- CurrentInstantiationScope->getPartiallySubstitutedPack()) {
+ if (PartialOrderingTTP ||
+ (CurrentInstantiationScope &&
+ CurrentInstantiationScope->getPartiallySubstitutedPack())) {
while (ArgIdx < NumArgs &&
NewArgs[ArgIdx].getArgument().isPackExpansion()) {
const TemplateArgument &Arg = NewArgs[ArgIdx++].getArgument();
@@ -7321,64 +7321,46 @@ bool Sema::CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param,
<< Template;
}
+ if (!getLangOpts().RelaxedTemplateTemplateArgs)
+ return !TemplateParameterListsAreEqual(
+ Template->getTemplateParameters(), Params, /*Complain=*/true,
+ TPL_TemplateTemplateArgumentMatch, Arg.getLocation());
+
// C++1z [temp.arg.template]p3: (DR 150)
// A template-argument matches a template template-parameter P when P
// is at least as specialized as the template-argument A.
- if (getLangOpts().RelaxedTemplateTemplateArgs) {
- // Quick check for the common case:
- // If P contains a parameter pack, then A [...] matches P if each of A's
- // template parameters matches the corresponding template parameter in
- // the template-parameter-list of P.
- if (TemplateParameterListsAreEqual(
- Template->getTemplateParameters(), Params, false,
- TPL_TemplateTemplateArgumentMatch, Arg.getLocation()) &&
- // If the argument has no associated constraints, then the parameter is
- // definitely at least as specialized as the argument.
- // Otherwise - we need a more thorough check.
- !Template->hasAssociatedConstraints())
- return false;
-
- if (isTemplateTemplateParameterAtLeastAsSpecializedAs(
- Params, Template, DefaultArgs, Arg.getLocation(), IsDeduced)) {
- // P2113
- // C++20[temp.func.order]p2
- // [...] If both deductions succeed, the partial ordering selects the
- // more constrained template (if one exists) as determined below.
- SmallVector<const Expr *, 3> ParamsAC, TemplateAC;
- Params->getAssociatedConstraints(ParamsAC);
- // C++2a[temp.arg.template]p3
- // [...] In this comparison, if P is unconstrained, the constraints on A
- // are not considered.
- if (ParamsAC.empty())
- return false;
+ if (!isTemplateTemplateParameterAtLeastAsSpecializedAs(
+ Params, Param, Template, DefaultArgs, Arg.getLocation(), IsDeduced))
+ return true;
+ // P2113
+ // C++20[temp.func.order]p2
+ // [...] If both deductions succeed, the partial ordering selects the
+ // more constrained template (if one exists) as determined below.
+ SmallVector<const Expr *, 3> ParamsAC, TemplateAC;
+ Params->getAssociatedConstraints(ParamsAC);
+ // C++20[temp.arg.template]p3
+ // [...] In this comparison, if P is unconstrained, the constraints on A
+ // are not considered.
+ if (ParamsAC.empty())
+ return false;
- Template->getAssociatedConstraints(TemplateAC);
+ Template->getAssociatedConstraints(TemplateAC);
- bool IsParamAtLeastAsConstrained;
- if (IsAtLeastAsConstrained(Param, ParamsAC, Template, TemplateAC,
- IsParamAtLeastAsConstrained))
- return true;
- if (!IsParamAtLeastAsConstrained) {
- Diag(Arg.getLocation(),
- diag::err_template_template_parameter_not_at_least_as_constrained)
- << Template << Param << Arg.getSourceRange();
- Diag(Param->getLocation(), diag::note_entity_declared_at) << Param;
- Diag(Template->getLocation(), diag::note_entity_declared_at)
- << Template;
- MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Param, ParamsAC, Template,
- TemplateAC);
- return true;
- }
- return false;
- }
- // FIXME: Produce better diagnostics for deduction failures.
+ bool IsParamAtLeastAsConstrained;
+ if (IsAtLeastAsConstrained(Param, ParamsAC, Template, TemplateAC,
+ IsParamAtLeastAsConstrained))
+ return true;
+ if (!IsParamAtLeastAsConstrained) {
+ Diag(Arg.getLocation(),
+ diag::err_template_template_parameter_not_at_least_as_constrained)
+ << Template << Param << Arg.getSourceRange();
+ Diag(Param->getLocation(), diag::note_entity_declared_at) << Param;
+ Diag(Template->getLocation(), diag::note_entity_declared_at) << Template;
+ MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Param, ParamsAC, Template,
+ TemplateAC);
+ return true;
}
-
- return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
- Params,
- true,
- TPL_TemplateTemplateArgumentMatch,
- Arg.getLocation());
+ return false;
}
static Sema::SemaDiagnosticBuilder noteLocation(Sema &S, const NamedDecl &Decl,
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index dfae0d6cda0d9b..2d527e662fe3b4 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -145,7 +145,9 @@ static TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch(
PartialOrderingKind POK, bool DeducedFromArrayBound,
bool *HasDeducedAnyParam);
-enum class PackFold { ParameterToArgument, ArgumentToParameter };
+/// What directions packs are allowed to match non-packs.
+enum class PackFold { ParameterToArgument, ArgumentToParameter, Both };
+
static TemplateDeductionResult
DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
ArrayRef<TemplateArgument> Ps,
@@ -1711,7 +1713,21 @@ static TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch(
DeducedTemplateArgument Result =
checkDeducedTemplateArguments(S.Context, Deduced[Index], NewDeduced);
if (Result.isNull()) {
- Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
+ // We can also get inconsistencies when matching NTTP type.
+ switch (NamedDecl *Param = TemplateParams->getParam(Index);
+ Param->getKind()) {
+ case Decl::TemplateTypeParm:
+ Info.Param = cast<TemplateTypeParmDecl>(Param);
+ break;
+ case Decl::NonTypeTemplateParm:
+ Info.Param = cast<NonTypeTemplateParmDecl>(Param);
+ break;
+ case Decl::TemplateTemplateParm:
+ Info.Param = cast<TemplateTemplateParmDecl>(Param);
+ break;
+ default:
+ llvm_unreachable("unexpected kind");
+ }
Info.FirstArg = Deduced[Index];
Info.SecondArg = NewDeduced;
return TemplateDeductionResult::Inconsistent;
@@ -2549,8 +2565,31 @@ DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
if (const NonTypeTemplateParmDecl *NTTP =
getDeducedParameterFromExpr(Info, P.getAsExpr())) {
switch (A.getKind()) {
+ case TemplateArgument::Expression: {
+ const Expr *E = A.getAsExpr();
+ // When checking NTTP, if either the parameter or the argument is
+ // dependent, as there would be otherwise nothing to deduce, we force
+ // the argument to the parameter type using this dependent implicit
+ // cast, in order to maintain invariants. Now we can deduce the
+ // resulting type from the original type, and deduce the original type
+ // against the parameter we are checking.
+ if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
+ ICE && ICE->getCastKind() == clang::CK_Dependent) {
+ E = ICE->getSubExpr();
+ if (auto Result = DeduceTemplateArgumentsByTypeMatch(
+ S, TemplateParams, ICE->getType(), E->getType(), Info,
+ Deduced, TDF_SkipNonDependent,
+ PartialOrdering ? PartialOrderingKind::NonCall
+ : PartialOrderingKind::None,
+ /*DeducedFromArrayBound=*/false, HasDeducedAnyParam);
+ Result != TemplateDeductionResult::Success)
+ return Result;
+ }
+ return DeduceNonTypeTemplateArgument(
+ S, TemplateParams, NTTP, DeducedTemplateArgument(A), E->getType(),
+ Info, PartialOrdering, Deduced, HasDeducedAnyParam);
+ }
case TemplateArgument::Integral:
- case TemplateArgument::Expression:
case TemplateArgument::StructuralValue:
return DeduceNonTypeTemplateArgument(
S, TemplateParams, NTTP, DeducedTemplateArgument(A),
@@ -2639,50 +2678,75 @@ DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
SmallVectorImpl<DeducedTemplateArgument> &Deduced,
bool NumberOfArgumentsMustMatch, bool PartialOrdering,
PackFold PackFold, bool *HasDeducedAnyParam) {
- if (PackFold == PackFold::ArgumentToParameter)
- std::swap(Ps, As);
+ bool FoldPackParameter = PackFold == PackFold::ParameterToArgument ||
+ PackFold == PackFold::Both,
+ FoldPackArgument = PackFold == PackFold::ArgumentToParameter ||
+ PackFold == PackFold::Both;
+
// C++0x [temp.deduct.type]p9:
// If the template argument list of P contains a pack expansion that is not
// the last template argument, the entire template argument list is a
// non-deduced context.
- if (hasPackExpansionBeforeEnd(Ps))
+ if (FoldPackParameter && hasPackExpansionBeforeEnd(Ps))
+ return TemplateDeductionResult::Success;
+
+ if (FoldPackArgument && hasPackExpansionBeforeEnd(As))
return TemplateDeductionResult::Success;
// C++0x [temp.deduct.type]p9:
// If P has a form that contains <T> or <i>, then each argument Pi of the
// respective template argument list P is compared with the corresponding
// argument Ai of the corresponding template argument list of A.
- unsigned ArgIdx = 0, ParamIdx = 0;
- for (; hasTemplateArgumentForDeduction(Ps, ParamIdx); ++ParamIdx) {
- const TemplateArgument &P = Ps[ParamIdx];
- if (!P.isPackExpansion()) {
+ for (unsigned ArgIdx = 0, ParamIdx = 0; /**/; /**/) {
+ if (!hasTemplateArgumentForDeduction(Ps, ParamIdx))
+ return !FoldPackParameter && hasTemplateArgumentForDeduction(As, ArgIdx)
+ ? TemplateDeductionResult::MiscellaneousDeductionFailure
+ : TemplateDeductionResult::Success;
+
+ if (!Ps[ParamIdx].isPackExpansion()) {
// The simple case: deduce template arguments by matching Pi and Ai.
// Check whether we have enough arguments.
if (!hasTemplateArgumentForDeduction(As, ArgIdx))
- return NumberOfArgumentsMustMatch
+ return !FoldPackArgument && NumberOfArgumentsMustMatch
? TemplateDeductionResult::MiscellaneousDeductionFailure
: TemplateDeductionResult::Success;
- // C++1z [temp.deduct.type]p9:
- // During partial ordering, if Ai was originally a pack expansion [and]
- // Pi is not a pack expansion, template argument deduction fails.
- if (As[ArgIdx].isPackExpansion())
- return TemplateDeductionResult::MiscellaneousDeductionFailure;
+ if (As[ArgIdx].isPackExpansion()) {
+ // C++1z [temp.deduct.type]p9:
+ // During partial ordering, if Ai was originally a pack expansion
+ // [and] Pi is not a pack expansion, template argument deduction
+ // fails.
+ if (!FoldPackArgument)
+ return TemplateDeductionResult::MiscellaneousDeductionFailure;
+
+ TemplateArgument Pattern = As[ArgIdx].getPackExpansionPattern();
+ for (;;) {
+ // Deduce template parameters from the pattern.
+ if (auto Result = DeduceTemplateArguments(
+ S, TemplateParams, Ps[ParamIdx], Pattern, Info,
+ PartialOrdering, Deduced, HasDeducedAnyParam);
+ Result != TemplateDeductionResult::Success)
+ return Result;
- // Perform deduction for this Pi/Ai pair.
- TemplateArgument Pi = P, Ai = As[ArgIdx];
- if (PackFold == PackFold::ArgumentToParameter)
- std::swap(Pi, Ai);
- if (auto Result = DeduceTemplateArguments(S, TemplateParams, Pi, Ai, Info,
- PartialOrdering, Deduced,
- HasDeducedAnyParam);
- Result != TemplateDeductionResult::Success)
- return Result;
+ ++ParamIdx;
+ if (!hasTemplateArgumentForDeduction(Ps, ParamIdx))
+ return TemplateDeductionResult::Success;
+ if (Ps[ParamIdx].isPackExpansion())
+ break;
+ }
+ } else {
+ // Perform deduction for this Pi/Ai pair.
+ if (auto Result = DeduceTemplateArguments(
+ S, TemplateParams, Ps[ParamIdx], As[ArgIdx], Info,
+ PartialOrdering, Deduced, HasDeducedAnyParam);
+ Result != TemplateDeductionResult::Success)
+ return Result;
- // Move to the next argument.
- ++ArgIdx;
- continue;
+ ++ArgIdx;
+ ++ParamIdx;
+ continue;
+ }
}
// The parameter is a pack expansion.
@@ -2692,7 +2756,7 @@ DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
// each remaining argument in the template argument list of A. Each
// comparison deduces template arguments for subsequent positions in the
// template parameter packs expanded by Pi.
- TemplateArgument Pattern = P.getPackExpansionPattern();
+ TemplateArgument Pattern = Ps[ParamIdx].getPackExpansionPattern();
// Prepare to deduce the packs within the pattern.
PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern);
@@ -2703,13 +2767,12 @@ DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
for (; hasTemplateArgumentForDeduction(As, ArgIdx) &&
PackScope.hasNextElement();
++ArgIdx) {
- TemplateArgument Pi = Pattern, A...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/111711
More information about the cfe-commits
mailing list