Skip to content

Commit bdd46cc

Browse files
committed
Revert "[clang] CWG2398: improve overload resolution backwards compat (#107350)"
See discussion in #111711 This reverts commit 224519b.
1 parent ed7251b commit bdd46cc

File tree

8 files changed

+53
-97
lines changed

8 files changed

+53
-97
lines changed

clang/docs/ReleaseNotes.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ Resolutions to C++ Defect Reports
207207
(`CWG2351: void{} <https://cplusplus.github.io/CWG/issues/2351.html>`_).
208208

209209
- Clang now has improved resolution to CWG2398, allowing class templates to have
210-
default arguments deduced when partial ordering, and better backwards compatibility
211-
in overload resolution.
210+
default arguments deduced when partial ordering.
212211

213212
- Clang now allows comparing unequal object pointers that have been cast to ``void *``
214213
in constant expressions. These comparisons always worked in non-constant expressions.

clang/include/clang/Sema/Sema.h

+5-9
Original file line numberDiff line numberDiff line change
@@ -11638,8 +11638,7 @@ class Sema final : public SemaBase {
1163811638
SourceLocation RAngleLoc, unsigned ArgumentPackIndex,
1163911639
SmallVectorImpl<TemplateArgument> &SugaredConverted,
1164011640
SmallVectorImpl<TemplateArgument> &CanonicalConverted,
11641-
CheckTemplateArgumentKind CTAK,
11642-
bool *MatchedPackOnParmToNonPackOnArg);
11641+
CheckTemplateArgumentKind CTAK);
1164311642

1164411643
/// Check that the given template arguments can be provided to
1164511644
/// the given template, converting the arguments along the way.
@@ -11686,8 +11685,7 @@ class Sema final : public SemaBase {
1168611685
SmallVectorImpl<TemplateArgument> &SugaredConverted,
1168711686
SmallVectorImpl<TemplateArgument> &CanonicalConverted,
1168811687
bool UpdateArgsWithConversions = true,
11689-
bool *ConstraintsNotSatisfied = nullptr, bool PartialOrderingTTP = false,
11690-
bool *MatchedPackOnParmToNonPackOnArg = nullptr);
11688+
bool *ConstraintsNotSatisfied = nullptr, bool PartialOrderingTTP = false);
1169111689

1169211690
bool CheckTemplateTypeArgument(
1169311691
TemplateTypeParmDecl *Param, TemplateArgumentLoc &Arg,
@@ -11721,8 +11719,7 @@ class Sema final : public SemaBase {
1172111719
/// It returns true if an error occurred, and false otherwise.
1172211720
bool CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param,
1172311721
TemplateParameterList *Params,
11724-
TemplateArgumentLoc &Arg, bool IsDeduced,
11725-
bool *MatchedPackOnParmToNonPackOnArg);
11722+
TemplateArgumentLoc &Arg, bool IsDeduced);
1172611723

1172711724
void NoteTemplateLocation(const NamedDecl &Decl,
1172811725
std::optional<SourceRange> ParamRange = {});
@@ -12423,7 +12420,7 @@ class Sema final : public SemaBase {
1242312420
bool isTemplateTemplateParameterAtLeastAsSpecializedAs(
1242412421
TemplateParameterList *PParam, TemplateDecl *PArg, TemplateDecl *AArg,
1242512422
const DefaultArguments &DefaultArgs, SourceLocation ArgLoc,
12426-
bool IsDeduced, bool *MatchedPackOnParmToNonPackOnArg);
12423+
bool IsDeduced);
1242712424

1242812425
/// Mark which template parameters are used in a given expression.
1242912426
///
@@ -13422,8 +13419,7 @@ class Sema final : public SemaBase {
1342213419
bool InstantiateClassTemplateSpecialization(
1342313420
SourceLocation PointOfInstantiation,
1342413421
ClassTemplateSpecializationDecl *ClassTemplateSpec,
13425-
TemplateSpecializationKind TSK, bool Complain = true,
13426-
bool PrimaryHasMatchedPackOnParmToNonPackOnArg = false);
13422+
TemplateSpecializationKind TSK, bool Complain = true);
1342713423

1342813424
/// Instantiates the definitions of all of the member
1342913425
/// of the given class, which is an instantiation of a class template

clang/include/clang/Sema/TemplateDeduction.h

-13
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ class TemplateDeductionInfo {
5151
/// Have we suppressed an error during deduction?
5252
bool HasSFINAEDiagnostic = false;
5353

54-
/// Have we matched any packs on the parameter side, versus any non-packs on
55-
/// the argument side, in a context where the opposite matching is also
56-
/// allowed?
57-
bool MatchedPackOnParmToNonPackOnArg = false;
58-
5954
/// The template parameter depth for which we're performing deduction.
6055
unsigned DeducedDepth;
6156

@@ -92,14 +87,6 @@ class TemplateDeductionInfo {
9287
return DeducedDepth;
9388
}
9489

95-
bool hasMatchedPackOnParmToNonPackOnArg() const {
96-
return MatchedPackOnParmToNonPackOnArg;
97-
}
98-
99-
void setMatchedPackOnParmToNonPackOnArg() {
100-
MatchedPackOnParmToNonPackOnArg = true;
101-
}
102-
10390
/// Get the number of explicitly-specified arguments.
10491
unsigned getNumExplicitArgs() const {
10592
return ExplicitArgs;

clang/lib/Sema/SemaLookup.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -3666,8 +3666,7 @@ Sema::LookupLiteralOperator(Scope *S, LookupResult &R,
36663666
TemplateArgumentLoc Arg(TemplateArgument(StringLit), StringLit);
36673667
if (CheckTemplateArgument(
36683668
Params->getParam(0), Arg, FD, R.getNameLoc(), R.getNameLoc(),
3669-
0, SugaredChecked, CanonicalChecked, CTAK_Specified,
3670-
/*MatchedPackOnParmToNonPackOnArg=*/nullptr) ||
3669+
0, SugaredChecked, CanonicalChecked, CTAK_Specified) ||
36713670
Trap.hasErrorOccurred())
36723671
IsTemplate = false;
36733672
}

clang/lib/Sema/SemaTemplate.cpp

+18-26
Original file line numberDiff line numberDiff line change
@@ -5179,7 +5179,7 @@ bool Sema::CheckTemplateArgument(
51795179
unsigned ArgumentPackIndex,
51805180
SmallVectorImpl<TemplateArgument> &SugaredConverted,
51815181
SmallVectorImpl<TemplateArgument> &CanonicalConverted,
5182-
CheckTemplateArgumentKind CTAK, bool *MatchedPackOnParmToNonPackOnArg) {
5182+
CheckTemplateArgumentKind CTAK) {
51835183
// Check template type parameters.
51845184
if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
51855185
return CheckTemplateTypeArgument(TTP, Arg, SugaredConverted,
@@ -5395,8 +5395,7 @@ bool Sema::CheckTemplateArgument(
53955395
case TemplateArgument::Template:
53965396
case TemplateArgument::TemplateExpansion:
53975397
if (CheckTemplateTemplateArgument(TempParm, Params, Arg,
5398-
/*IsDeduced=*/CTAK != CTAK_Specified,
5399-
MatchedPackOnParmToNonPackOnArg))
5398+
/*IsDeduced=*/CTAK != CTAK_Specified))
54005399
return true;
54015400

54025401
SugaredConverted.push_back(Arg.getArgument());
@@ -5470,7 +5469,7 @@ bool Sema::CheckTemplateArgumentList(
54705469
SmallVectorImpl<TemplateArgument> &SugaredConverted,
54715470
SmallVectorImpl<TemplateArgument> &CanonicalConverted,
54725471
bool UpdateArgsWithConversions, bool *ConstraintsNotSatisfied,
5473-
bool PartialOrderingTTP, bool *MatchedPackOnParmToNonPackOnArg) {
5472+
bool PartialOrderingTTP) {
54745473

54755474
if (ConstraintsNotSatisfied)
54765475
*ConstraintsNotSatisfied = false;
@@ -5546,10 +5545,10 @@ bool Sema::CheckTemplateArgumentList(
55465545

55475546
if (ArgIdx < NumArgs) {
55485547
// Check the template argument we were given.
5549-
if (CheckTemplateArgument(
5550-
*Param, NewArgs[ArgIdx], Template, TemplateLoc, RAngleLoc,
5551-
SugaredArgumentPack.size(), SugaredConverted, CanonicalConverted,
5552-
CTAK_Specified, MatchedPackOnParmToNonPackOnArg))
5548+
if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, TemplateLoc,
5549+
RAngleLoc, SugaredArgumentPack.size(),
5550+
SugaredConverted, CanonicalConverted,
5551+
CTAK_Specified))
55535552
return true;
55545553

55555554
CanonicalConverted.back().setIsDefaulted(
@@ -5707,8 +5706,7 @@ bool Sema::CheckTemplateArgumentList(
57075706
// Check the default template argument.
57085707
if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, RAngleLoc, 0,
57095708
SugaredConverted, CanonicalConverted,
5710-
CTAK_Specified,
5711-
/*MatchedPackOnParmToNonPackOnArg=*/nullptr))
5709+
CTAK_Specified))
57125710
return true;
57135711

57145712
SugaredConverted.back().setIsDefaulted(true);
@@ -7291,10 +7289,10 @@ static void DiagnoseTemplateParameterListArityMismatch(
72917289
Sema &S, TemplateParameterList *New, TemplateParameterList *Old,
72927290
Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc);
72937291

7294-
bool Sema::CheckTemplateTemplateArgument(
7295-
TemplateTemplateParmDecl *Param, TemplateParameterList *Params,
7296-
TemplateArgumentLoc &Arg, bool IsDeduced,
7297-
bool *MatchedPackOnParmToNonPackOnArg) {
7292+
bool Sema::CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param,
7293+
TemplateParameterList *Params,
7294+
TemplateArgumentLoc &Arg,
7295+
bool IsDeduced) {
72987296
TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern();
72997297
auto [Template, DefaultArgs] = Name.getTemplateDeclAndDefaultArgs();
73007298
if (!Template) {
@@ -7338,8 +7336,7 @@ bool Sema::CheckTemplateTemplateArgument(
73387336
// A template-argument matches a template template-parameter P when P
73397337
// is at least as specialized as the template-argument A.
73407338
if (!isTemplateTemplateParameterAtLeastAsSpecializedAs(
7341-
Params, Param, Template, DefaultArgs, Arg.getLocation(), IsDeduced,
7342-
MatchedPackOnParmToNonPackOnArg))
7339+
Params, Param, Template, DefaultArgs, Arg.getLocation(), IsDeduced))
73437340
return true;
73447341
// P2113
73457342
// C++20[temp.func.order]p2
@@ -9757,14 +9754,11 @@ DeclResult Sema::ActOnExplicitInstantiation(
97579754

97589755
// Check that the template argument list is well-formed for this
97599756
// template.
9760-
bool PrimaryHasMatchedPackOnParmToNonPackOnArg = false;
97619757
SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted;
9762-
if (CheckTemplateArgumentList(
9763-
ClassTemplate, TemplateNameLoc, TemplateArgs,
9764-
/*DefaultArgs=*/{}, false, SugaredConverted, CanonicalConverted,
9765-
/*UpdateArgsWithConversions=*/true,
9766-
/*ConstraintsNotSatisfied=*/nullptr, /*PartialOrderingTTP=*/false,
9767-
&PrimaryHasMatchedPackOnParmToNonPackOnArg))
9758+
if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, TemplateArgs,
9759+
/*DefaultArgs=*/{}, false, SugaredConverted,
9760+
CanonicalConverted,
9761+
/*UpdateArgsWithConversions=*/true))
97689762
return true;
97699763

97709764
// Find the class template specialization declaration that
@@ -9885,9 +9879,7 @@ DeclResult Sema::ActOnExplicitInstantiation(
98859879
= cast_or_null<ClassTemplateSpecializationDecl>(
98869880
Specialization->getDefinition());
98879881
if (!Def)
9888-
InstantiateClassTemplateSpecialization(
9889-
TemplateNameLoc, Specialization, TSK,
9890-
/*Complain=*/true, PrimaryHasMatchedPackOnParmToNonPackOnArg);
9882+
InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
98919883
else if (TSK == TSK_ExplicitInstantiationDefinition) {
98929884
MarkVTableUsed(TemplateNameLoc, Specialization, true);
98939885
Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());

clang/lib/Sema/SemaTemplateDeduction.cpp

+13-30
Original file line numberDiff line numberDiff line change
@@ -2767,12 +2767,8 @@ DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
27672767
for (; hasTemplateArgumentForDeduction(As, ArgIdx) &&
27682768
PackScope.hasNextElement();
27692769
++ArgIdx) {
2770-
if (!As[ArgIdx].isPackExpansion()) {
2771-
if (!FoldPackParameter)
2772-
return TemplateDeductionResult::MiscellaneousDeductionFailure;
2773-
if (FoldPackArgument)
2774-
Info.setMatchedPackOnParmToNonPackOnArg();
2775-
}
2770+
if (!FoldPackParameter && !As[ArgIdx].isPackExpansion())
2771+
return TemplateDeductionResult::MiscellaneousDeductionFailure;
27762772
// Deduce template arguments from the pattern.
27772773
if (auto Result = DeduceTemplateArguments(
27782774
S, TemplateParams, Pattern, As[ArgIdx], Info, PartialOrdering,
@@ -2966,20 +2962,15 @@ static bool ConvertDeducedTemplateArgument(
29662962
TemplateArgumentLoc ArgLoc = S.getTrivialTemplateArgumentLoc(
29672963
Arg, QualType(), Info.getLocation(), Param);
29682964

2969-
bool MatchedPackOnParmToNonPackOnArg = false;
29702965
// Check the template argument, converting it as necessary.
2971-
auto Res = S.CheckTemplateArgument(
2966+
return S.CheckTemplateArgument(
29722967
Param, ArgLoc, Template, Template->getLocation(),
29732968
Template->getSourceRange().getEnd(), ArgumentPackIndex, SugaredOutput,
29742969
CanonicalOutput,
29752970
IsDeduced
29762971
? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound
29772972
: Sema::CTAK_Deduced)
2978-
: Sema::CTAK_Specified,
2979-
&MatchedPackOnParmToNonPackOnArg);
2980-
if (MatchedPackOnParmToNonPackOnArg)
2981-
Info.setMatchedPackOnParmToNonPackOnArg();
2982-
return Res;
2973+
: Sema::CTAK_Specified);
29832974
};
29842975

29852976
if (Arg.getKind() == TemplateArgument::Pack) {
@@ -3174,8 +3165,7 @@ static TemplateDeductionResult ConvertDeducedTemplateArguments(
31743165
// Check whether we can actually use the default argument.
31753166
if (S.CheckTemplateArgument(
31763167
Param, DefArg, TD, TD->getLocation(), TD->getSourceRange().getEnd(),
3177-
0, SugaredBuilder, CanonicalBuilder, Sema::CTAK_Specified,
3178-
/*MatchedPackOnParmToNonPackOnArg=*/nullptr)) {
3168+
0, SugaredBuilder, CanonicalBuilder, Sema::CTAK_Specified)) {
31793169
Info.Param = makeTemplateParameter(
31803170
const_cast<NamedDecl *>(TemplateParams->getParam(I)));
31813171
// FIXME: These template arguments are temporary. Free them!
@@ -3324,20 +3314,16 @@ FinishTemplateArgumentDeduction(
33243314
return TemplateDeductionResult::SubstitutionFailure;
33253315
}
33263316

3327-
bool MatchedPackOnParmToNonPackOnArg = false;
33283317
bool ConstraintsNotSatisfied;
33293318
SmallVector<TemplateArgument, 4> SugaredConvertedInstArgs,
33303319
CanonicalConvertedInstArgs;
33313320
if (S.CheckTemplateArgumentList(
33323321
Template, Partial->getLocation(), InstArgs, /*DefaultArgs=*/{}, false,
33333322
SugaredConvertedInstArgs, CanonicalConvertedInstArgs,
3334-
/*UpdateArgsWithConversions=*/true, &ConstraintsNotSatisfied,
3335-
/*PartialOrderingTTP=*/false, &MatchedPackOnParmToNonPackOnArg))
3323+
/*UpdateArgsWithConversions=*/true, &ConstraintsNotSatisfied))
33363324
return ConstraintsNotSatisfied
33373325
? TemplateDeductionResult::ConstraintsNotSatisfied
33383326
: TemplateDeductionResult::SubstitutionFailure;
3339-
if (MatchedPackOnParmToNonPackOnArg)
3340-
Info.setMatchedPackOnParmToNonPackOnArg();
33413327

33423328
TemplateParameterList *TemplateParams = Template->getTemplateParameters();
33433329
for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
@@ -6479,8 +6465,8 @@ bool Sema::isMoreSpecializedThanPrimary(
64796465

64806466
bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs(
64816467
TemplateParameterList *P, TemplateDecl *PArg, TemplateDecl *AArg,
6482-
const DefaultArguments &DefaultArgs, SourceLocation ArgLoc, bool IsDeduced,
6483-
bool *MatchedPackOnParmToNonPackOnArg) {
6468+
const DefaultArguments &DefaultArgs, SourceLocation ArgLoc,
6469+
bool IsDeduced) {
64846470
// C++1z [temp.arg.template]p4: (DR 150)
64856471
// A template template-parameter P is at least as specialized as a
64866472
// template template-argument A if, given the following rewrite to two
@@ -6532,11 +6518,11 @@ bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs(
65326518
// If the rewrite produces an invalid type, then P is not at least as
65336519
// specialized as A.
65346520
SmallVector<TemplateArgument, 4> CanonicalPArgs;
6535-
if (CheckTemplateArgumentList(
6536-
AArg, ArgLoc, PArgList, DefaultArgs, false, PArgs, CanonicalPArgs,
6537-
/*UpdateArgsWithConversions=*/true,
6538-
/*ConstraintsNotSatisfied=*/nullptr,
6539-
/*PartialOrderingTTP=*/true, MatchedPackOnParmToNonPackOnArg))
6521+
if (CheckTemplateArgumentList(AArg, ArgLoc, PArgList, DefaultArgs, false,
6522+
PArgs, CanonicalPArgs,
6523+
/*UpdateArgsWithConversions=*/true,
6524+
/*ConstraintsNotSatisfied=*/nullptr,
6525+
/*PartialOrderingTTP=*/true))
65406526
return false;
65416527
}
65426528

@@ -6562,9 +6548,6 @@ bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs(
65626548
IsDeduced ? PackFold::ArgumentToParameter : PackFold::Both,
65636549
/*HasDeducedAnyParam=*/nullptr)) {
65646550
case clang::TemplateDeductionResult::Success:
6565-
if (MatchedPackOnParmToNonPackOnArg &&
6566-
Info.hasMatchedPackOnParmToNonPackOnArg())
6567-
*MatchedPackOnParmToNonPackOnArg = true;
65686551
break;
65696552

65706553
case TemplateDeductionResult::MiscellaneousDeductionFailure:

clang/lib/Sema/SemaTemplateInstantiate.cpp

+10-14
Original file line numberDiff line numberDiff line change
@@ -4017,11 +4017,11 @@ bool Sema::usesPartialOrExplicitSpecialization(
40174017
/// Get the instantiation pattern to use to instantiate the definition of a
40184018
/// given ClassTemplateSpecializationDecl (either the pattern of the primary
40194019
/// template or of a partial specialization).
4020-
static ActionResult<CXXRecordDecl *> getPatternForClassTemplateSpecialization(
4020+
static ActionResult<CXXRecordDecl *>
4021+
getPatternForClassTemplateSpecialization(
40214022
Sema &S, SourceLocation PointOfInstantiation,
40224023
ClassTemplateSpecializationDecl *ClassTemplateSpec,
4023-
TemplateSpecializationKind TSK,
4024-
bool PrimaryHasMatchedPackOnParmToNonPackOnArg) {
4024+
TemplateSpecializationKind TSK) {
40254025
Sema::InstantiatingTemplate Inst(S, PointOfInstantiation, ClassTemplateSpec);
40264026
if (Inst.isInvalid())
40274027
return {/*Invalid=*/true};
@@ -4044,7 +4044,7 @@ static ActionResult<CXXRecordDecl *> getPatternForClassTemplateSpecialization(
40444044
// specialization with the template argument lists of the partial
40454045
// specializations.
40464046
typedef PartialSpecMatchResult MatchResult;
4047-
SmallVector<MatchResult, 4> Matched, ExtraMatched;
4047+
SmallVector<MatchResult, 4> Matched;
40484048
SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
40494049
Template->getPartialSpecializations(PartialSpecs);
40504050
TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation);
@@ -4061,13 +4061,11 @@ static ActionResult<CXXRecordDecl *> getPatternForClassTemplateSpecialization(
40614061
MakeDeductionFailureInfo(S.Context, Result, Info));
40624062
(void)Result;
40634063
} else {
4064-
auto &List =
4065-
Info.hasMatchedPackOnParmToNonPackOnArg() ? ExtraMatched : Matched;
4066-
List.push_back(MatchResult{Partial, Info.takeCanonical()});
4064+
Matched.push_back(PartialSpecMatchResult());
4065+
Matched.back().Partial = Partial;
4066+
Matched.back().Args = Info.takeCanonical();
40674067
}
40684068
}
4069-
if (Matched.empty() && PrimaryHasMatchedPackOnParmToNonPackOnArg)
4070-
Matched = std::move(ExtraMatched);
40714069

40724070
// If we're dealing with a member template where the template parameters
40734071
// have been instantiated, this provides the original template parameters
@@ -4170,18 +4168,16 @@ static ActionResult<CXXRecordDecl *> getPatternForClassTemplateSpecialization(
41704168
bool Sema::InstantiateClassTemplateSpecialization(
41714169
SourceLocation PointOfInstantiation,
41724170
ClassTemplateSpecializationDecl *ClassTemplateSpec,
4173-
TemplateSpecializationKind TSK, bool Complain,
4174-
bool PrimaryHasMatchedPackOnParmToNonPackOnArg) {
4171+
TemplateSpecializationKind TSK, bool Complain) {
41754172
// Perform the actual instantiation on the canonical declaration.
41764173
ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
41774174
ClassTemplateSpec->getCanonicalDecl());
41784175
if (ClassTemplateSpec->isInvalidDecl())
41794176
return true;
41804177

41814178
ActionResult<CXXRecordDecl *> Pattern =
4182-
getPatternForClassTemplateSpecialization(
4183-
*this, PointOfInstantiation, ClassTemplateSpec, TSK,
4184-
PrimaryHasMatchedPackOnParmToNonPackOnArg);
4179+
getPatternForClassTemplateSpecialization(*this, PointOfInstantiation,
4180+
ClassTemplateSpec, TSK);
41854181
if (!Pattern.isUsable())
41864182
return Pattern.isInvalid();
41874183

0 commit comments

Comments
 (0)