Skip to content

Commit ddc2803

Browse files
committed
Fix more ByteCode tests
Only 5 tests left now: Failed Tests (5): Clang :: CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp Clang :: Modules/pr62943.cppm Clang :: SemaCXX/cxx2c-fold-exprs.cpp Clang :: SemaTemplate/concepts-recursive-inst.cpp Clang :: SemaTemplate/instantiate-template-argument.cpp
1 parent 3f14172 commit ddc2803

File tree

7 files changed

+104
-53
lines changed

7 files changed

+104
-53
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12065,6 +12065,13 @@ class Sema final : public SemaBase {
1206512065
bool UpdateArgsWithConversions = true,
1206612066
bool *ConstraintsNotSatisfied = nullptr);
1206712067

12068+
bool CheckTemplateArgumentList(
12069+
TemplateDecl *Template, TemplateParameterList *Params,
12070+
SourceLocation TemplateLoc, TemplateArgumentListInfo &TemplateArgs,
12071+
const DefaultArguments &DefaultArgs, bool PartialTemplateArgs,
12072+
CheckTemplateArgumentInfo &CTAI, bool UpdateArgsWithConversions = true,
12073+
bool *ConstraintsNotSatisfied = nullptr);
12074+
1206812075
bool CheckTemplateTypeArgument(
1206912076
TemplateTypeParmDecl *Param, TemplateArgumentLoc &Arg,
1207012077
SmallVectorImpl<TemplateArgument> &SugaredConverted,
@@ -13402,7 +13409,7 @@ class Sema final : public SemaBase {
1340213409
TemplateArgumentListInfo &Outputs);
1340313410

1340413411
bool SubstTemplateArgumentsInParameterMapping(
13405-
ArrayRef<TemplateArgumentLoc> Args,
13412+
ArrayRef<TemplateArgumentLoc> Args, SourceLocation BaseLoc,
1340613413
const MultiLevelTemplateArgumentList &TemplateArgs,
1340713414
TemplateArgumentListInfo &Out);
1340813415

clang/include/clang/Sema/SemaConcept.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct NormalizedConstraint {
6969
unsigned PackSubstitutionIndex : 26;
7070
llvm::SmallBitVector Indexes;
7171
TemplateArgumentLoc *Args;
72+
TemplateParameterList *ParamList;
7273
ExprOrConcept ConstraintExpr;
7374
const NamedDecl *ConstraintDecl;
7475
};
@@ -81,6 +82,7 @@ struct NormalizedConstraint {
8182
unsigned Placeholder : 26;
8283
OccurenceList Indexes;
8384
TemplateArgumentLoc *Args;
85+
TemplateParameterList *ParamList;
8486
const Expr *Pattern;
8587
NormalizedConstraint *Constraint;
8688
};
@@ -118,6 +120,7 @@ struct NormalizedConstraint {
118120
PackIndex.toInternalRepresentation(),
119121
/*Indexes=*/{},
120122
/*Args=*/nullptr,
123+
/*ParamList=*/nullptr,
121124
ConstraintExpr,
122125
ConstraintDecl} {}
123126

@@ -128,6 +131,7 @@ struct NormalizedConstraint {
128131
/*Placeholder=*/0,
129132
/*Indexes=*/{},
130133
/*Args=*/nullptr,
134+
/*ParamList=*/nullptr,
131135
Pattern,
132136
Constraint} {}
133137

@@ -138,7 +142,8 @@ struct NormalizedConstraint {
138142
: ConceptId{{llvm::to_underlying(ConstraintKind::ConceptId),
139143
/*Placeholder=*/0, PackIndex.toInternalRepresentation(),
140144
/*Indexes=*/{},
141-
/*Args=*/nullptr, ConceptId, ConstraintDecl},
145+
/*Args=*/nullptr, /*ParamList=*/nullptr, ConceptId,
146+
ConstraintDecl},
142147
SubConstraint} {}
143148

144149
NormalizedConstraint(NormalizedConstraint *LHS, CompoundConstraintKind CCK,
@@ -160,15 +165,18 @@ struct NormalizedConstraint {
160165
return {Atomic.Args, Atomic.Indexes.count()};
161166
}
162167

163-
void InitParameterMapping(TemplateParameterList *TemplateParams, const Expr *,
164-
const ASTTemplateArgumentListInfo *ArgsAsWritten);
168+
TemplateParameterList *getUsedTemplateParamList() const {
169+
return Atomic.ParamList;
170+
}
165171

166172
void updateParameterMapping(OccurenceList Indexes,
167-
llvm::MutableArrayRef<TemplateArgumentLoc> Args) {
173+
llvm::MutableArrayRef<TemplateArgumentLoc> Args,
174+
TemplateParameterList *ParamList) {
168175
assert(getKind() != ConstraintKind::Compound);
169176
assert(Indexes.count() == Args.size());
170177
Atomic.Indexes = Indexes;
171178
Atomic.Args = Args.data();
179+
Atomic.ParamList = ParamList;
172180
}
173181

174182
bool hasMatchingParameterMapping(ASTContext &C,
@@ -283,6 +291,7 @@ class NormalizedConstraintWithParamMapping : public NormalizedConstraint {
283291
using NormalizedConstraint::hasParameterMapping;
284292
using NormalizedConstraint::mappingOccurenceList;
285293
using NormalizedConstraint::updateParameterMapping;
294+
using NormalizedConstraint::getUsedTemplateParamList;
286295

287296
const NamedDecl *getConstraintDecl() const { return Atomic.ConstraintDecl; }
288297

@@ -307,12 +316,6 @@ class AtomicConstraint : public NormalizedConstraintWithParamMapping {
307316
const Expr *getConstraintExpr() const {
308317
return cast<const Expr *>(Atomic.ConstraintExpr);
309318
}
310-
311-
void InitParameterMapping(const ASTTemplateArgumentListInfo *ArgsAsWritten) {
312-
NormalizedConstraint::InitParameterMapping(
313-
cast<TemplateDecl>(Atomic.ConstraintDecl)->getTemplateParameters(),
314-
getConstraintExpr(), ArgsAsWritten);
315-
}
316319
};
317320

318321
class FoldExpandedConstraint : public NormalizedConstraint {

clang/lib/Sema/SemaConcept.cpp

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,18 @@ SubstitutionInTemplateArguments(
377377
? Constraint.getPackSubstitutionIndex()
378378
: PackSubstitutionIndex);
379379
if (S.SubstTemplateArgumentsInParameterMapping(
380-
Constraint.getParameterMapping(), MLTAL, SubstArgs) ||
380+
Constraint.getParameterMapping(), Constraint.getBeginLoc(), MLTAL,
381+
SubstArgs) ||
381382
Trap.hasErrorOccurred())
382383
return std::nullopt;
383-
// Sema::CheckTemplateArgumentInfo CTAI;
384-
// auto *TD = const_cast<TemplateDecl *>(
385-
// cast<TemplateDecl>(Constraint.getConstraintDecl()));
386-
// if (S.CheckTemplateArgumentList(TD, TD->getLocation(), SubstArgs,
387-
// /*DefaultArguments=*/{},
388-
// /*PartialTemplateArgs=*/true, CTAI))
389-
// return std::nullopt;
384+
Sema::CheckTemplateArgumentInfo CTAI;
385+
auto *TD = const_cast<TemplateDecl *>(
386+
cast<TemplateDecl>(Constraint.getConstraintDecl()));
387+
if (S.CheckTemplateArgumentList(TD, Constraint.getUsedTemplateParamList(),
388+
TD->getLocation(), SubstArgs,
389+
/*DefaultArguments=*/{},
390+
/*PartialTemplateArgs=*/false, CTAI))
391+
return std::nullopt;
390392
NormalizedConstraint::OccurenceList Used =
391393
Constraint.mappingOccurenceList();
392394
SubstitutedOuterMost =
@@ -396,8 +398,10 @@ SubstitutionInTemplateArguments(
396398
if (Used[I])
397399
// SubstitutedOuterMost[I].dump();
398400
// SubstArgs[MappedIndex].getArgument().dump();
401+
// Arg = S.Context.getCanonicalTemplateArgument(
402+
// SubstArgs[MappedIndex++].getArgument());
399403
Arg = S.Context.getCanonicalTemplateArgument(
400-
SubstArgs[MappedIndex++].getArgument());
404+
CTAI.SugaredConverted[MappedIndex++]);
401405
if (I < SubstitutedOuterMost.size())
402406
SubstitutedOuterMost[I] = Arg;
403407
else
@@ -1591,17 +1595,28 @@ substituteParameterMappings(Sema &S, NormalizedConstraintWithParamMapping &N,
15911595
}
15921596
TemplateArgumentLoc *TempArgs =
15931597
new (S.Context) TemplateArgumentLoc[OccurringIndices.count()];
1598+
llvm::SmallVector<NamedDecl *> UsedParams;
15941599
for (unsigned I = 0, J = 0, C = TemplateParams->size(); I != C; ++I) {
15951600
SourceLocation Loc = ArgsAsWritten->NumTemplateArgs > I
15961601
? ArgsAsWritten->arguments()[I].getLocation()
15971602
: SourceLocation();
1598-
if (OccurringIndices[I])
1599-
new (&(TempArgs)[J++]) TemplateArgumentLoc(
1600-
S.getIdentityTemplateArgumentLoc(TemplateParams->begin()[I], Loc));
1603+
if (OccurringIndices[I]) {
1604+
NamedDecl *Param = TemplateParams->begin()[I];
1605+
new (&(TempArgs)[J])
1606+
TemplateArgumentLoc(S.getIdentityTemplateArgumentLoc(Param, Loc));
1607+
UsedParams.push_back(Param);
1608+
J++;
1609+
}
16011610
}
1611+
auto *UsedList = TemplateParameterList::Create(
1612+
S.Context, TemplateParams->getTemplateLoc(),
1613+
TemplateParams->getLAngleLoc(), UsedParams,
1614+
/*RAngleLoc=*/SourceLocation(),
1615+
/*RequiresClause=*/nullptr);
16021616
N.updateParameterMapping(OccurringIndices,
16031617
MutableArrayRef<TemplateArgumentLoc>{
1604-
TempArgs, OccurringIndices.count()});
1618+
TempArgs, OccurringIndices.count()},
1619+
UsedList);
16051620
}
16061621
SourceLocation InstLocBegin =
16071622
ArgsAsWritten->arguments().empty()
@@ -1618,28 +1633,39 @@ substituteParameterMappings(Sema &S, NormalizedConstraintWithParamMapping &N,
16181633
{InstLocBegin, InstLocEnd});
16191634
if (Inst.isInvalid())
16201635
return true;
1621-
if (S.SubstTemplateArgumentsInParameterMapping(N.getParameterMapping(), MLTAL,
1622-
SubstArgs))
1636+
// TransformTemplateArguments is unable to preserve the source location of a
1637+
// pack. The SourceLocation is necessary for the instantiation location.
1638+
// FIXME: The BaseLoc will be used as the location of the pack expansion,
1639+
// which is wrong.
1640+
if (S.SubstTemplateArgumentsInParameterMapping(
1641+
N.getParameterMapping(), N.getBeginLoc(), MLTAL, SubstArgs))
1642+
return true;
1643+
Sema::CheckTemplateArgumentInfo CTAI;
1644+
auto *TD =
1645+
const_cast<TemplateDecl *>(cast<TemplateDecl>(N.getConstraintDecl()));
1646+
if (S.CheckTemplateArgumentList(TD, N.getUsedTemplateParamList(),
1647+
TD->getLocation(), SubstArgs,
1648+
/*DefaultArguments=*/{},
1649+
/*PartialTemplateArgs=*/false, CTAI))
16231650
return true;
1624-
// Sema::CheckTemplateArgumentInfo CTAI;
1625-
// auto *TD =
1626-
// const_cast<TemplateDecl *>(cast<TemplateDecl>(N.getConstraintDecl()));
1627-
// if (S.CheckTemplateArgumentList(TD, TD->getLocation(), SubstArgs,
1628-
// /*DefaultArguments=*/{},
1629-
// /*PartialTemplateArgs=*/true, CTAI))
1630-
// return true;
16311651
TemplateArgumentLoc *TempArgs =
1632-
new (S.Context) TemplateArgumentLoc[SubstArgs.size()];
1633-
// for (unsigned I = 0; I < CTAI.SugaredConverted.size(); ++I)
1634-
// TempArgs[I] = S.getTrivialTemplateArgumentLoc(CTAI.SugaredConverted[I],
1635-
// QualType(), SourceLocation());
1636-
llvm::copy(SubstArgs.arguments(), TempArgs);
1637-
N.updateParameterMapping(
1638-
N.mappingOccurenceList(),
1639-
MutableArrayRef<TemplateArgumentLoc>(TempArgs, SubstArgs.size()));
1640-
// N.updateParameterMapping(N.mappingOccurenceList(),
1641-
// MutableArrayRef<TemplateArgumentLoc>(
1642-
// TempArgs, CTAI.SugaredConverted.size()));
1652+
new (S.Context) TemplateArgumentLoc[CTAI.SugaredConverted.size()];
1653+
for (unsigned I = 0; I < CTAI.SugaredConverted.size(); ++I) {
1654+
SourceLocation Loc;
1655+
// If this is an empty pack, we have no corresponding SubstArgs.
1656+
if (I < SubstArgs.size())
1657+
Loc = SubstArgs.arguments()[I].getLocation();
1658+
TempArgs[I] = S.getTrivialTemplateArgumentLoc(CTAI.SugaredConverted[I],
1659+
QualType(), Loc);
1660+
}
1661+
// llvm::copy(SubstArgs.arguments(), TempArgs);
1662+
// N.updateParameterMapping(
1663+
// N.mappingOccurenceList(),
1664+
// MutableArrayRef<TemplateArgumentLoc>(TempArgs, SubstArgs.size()));
1665+
N.updateParameterMapping(N.mappingOccurenceList(),
1666+
MutableArrayRef<TemplateArgumentLoc>(
1667+
TempArgs, CTAI.SugaredConverted.size()),
1668+
N.getUsedTemplateParamList());
16431669
return false;
16441670
}
16451671

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5682,6 +5682,20 @@ bool Sema::CheckTemplateArgumentList(
56825682
TemplateArgumentListInfo &TemplateArgs, const DefaultArguments &DefaultArgs,
56835683
bool PartialTemplateArgs, CheckTemplateArgumentInfo &CTAI,
56845684
bool UpdateArgsWithConversions, bool *ConstraintsNotSatisfied) {
5685+
return CheckTemplateArgumentList(
5686+
Template, GetTemplateParameterList(Template), TemplateLoc, TemplateArgs,
5687+
DefaultArgs, PartialTemplateArgs, CTAI, UpdateArgsWithConversions,
5688+
ConstraintsNotSatisfied);
5689+
}
5690+
5691+
/// Check that the given template argument list is well-formed
5692+
/// for specializing the given template.
5693+
bool Sema::CheckTemplateArgumentList(
5694+
TemplateDecl *Template, TemplateParameterList *Params,
5695+
SourceLocation TemplateLoc, TemplateArgumentListInfo &TemplateArgs,
5696+
const DefaultArguments &DefaultArgs, bool PartialTemplateArgs,
5697+
CheckTemplateArgumentInfo &CTAI, bool UpdateArgsWithConversions,
5698+
bool *ConstraintsNotSatisfied) {
56855699

56865700
if (ConstraintsNotSatisfied)
56875701
*ConstraintsNotSatisfied = false;
@@ -5691,8 +5705,6 @@ bool Sema::CheckTemplateArgumentList(
56915705
// template.
56925706
TemplateArgumentListInfo NewArgs = TemplateArgs;
56935707

5694-
TemplateParameterList *Params = GetTemplateParameterList(Template);
5695-
56965708
SourceLocation RAngleLoc = NewArgs.getRAngleLoc();
56975709

56985710
// C++23 [temp.arg.general]p1:

clang/lib/Sema/SemaTemplateDeduction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6634,6 +6634,7 @@ struct MarkUsedTemplateParameterVisitor : DynamicRecursiveASTVisitor {
66346634
if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl()))
66356635
if (NTTP->getDepth() == Depth)
66366636
Used[NTTP->getIndex()] = true;
6637+
DynamicRecursiveASTVisitor::TraverseType(E->getType());
66376638
return true;
66386639
}
66396640
};

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,8 +1421,9 @@ class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
14211421
} ForParameterMappingSubstitution;
14221422

14231423
TemplateInstantiator(ForParameterMappingSubstitution_t, Sema &SemaRef,
1424+
SourceLocation Loc,
14241425
const MultiLevelTemplateArgumentList &TemplateArgs)
1425-
: inherited(SemaRef), TemplateArgs(TemplateArgs),
1426+
: inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
14261427
BailOutOnIncomplete(false), PreserveArgumentPacks(true) {}
14271428

14281429
void setEvaluateConstraints(bool B) { EvaluateConstraints = B; }
@@ -2235,11 +2236,11 @@ TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
22352236
Arg = getTemplateArgumentPackPatternForRewrite(Arg);
22362237
if (Arg.getKind() != TemplateArgument::Expression) {
22372238
assert(SemaRef.inParameterMappingSubstitution());
2238-
// FIXME: SourceLocation()?
2239-
ExprResult E = SemaRef.BuildExpressionFromNonTypeTemplateArgument(Arg, SourceLocation());
2240-
if (E.isInvalid())
2239+
ExprResult Expr = SemaRef.BuildExpressionFromNonTypeTemplateArgument(
2240+
Arg, E->getLocation());
2241+
if (Expr.isInvalid())
22412242
return E;
2242-
Arg = TemplateArgument(E.get(), /*IsCanonical=*/false);
2243+
Arg = TemplateArgument(Expr.get(), /*IsCanonical=*/false);
22432244
}
22442245
assert(Arg.getKind() == TemplateArgument::Expression &&
22452246
"unexpected nontype template argument kind in template rewrite");
@@ -4508,11 +4509,11 @@ bool Sema::SubstTemplateArguments(
45084509
}
45094510

45104511
bool Sema::SubstTemplateArgumentsInParameterMapping(
4511-
ArrayRef<TemplateArgumentLoc> Args,
4512+
ArrayRef<TemplateArgumentLoc> Args, SourceLocation BaseLoc,
45124513
const MultiLevelTemplateArgumentList &TemplateArgs,
45134514
TemplateArgumentListInfo &Out) {
45144515
TemplateInstantiator Instantiator(
4515-
TemplateInstantiator::ForParameterMappingSubstitution, *this,
4516+
TemplateInstantiator::ForParameterMappingSubstitution, *this, BaseLoc,
45164517
TemplateArgs);
45174518
return Instantiator.TransformTemplateArguments(Args.begin(), Args.end(), Out);
45184519
}

clang/test/SemaTemplate/instantiate-template-argument.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ template<char X>
1717
constexpr int foo() requires C1<1, X> && true { return 2; }
1818
// sizeof(U) >= 4 [U = X (decltype(1))]
1919

20+
// GCC rejects it: https://godbolt.org/z/MWG756K8c
2021
static_assert(foo<'a'>() == 2);
2122

2223
template<char Z>

0 commit comments

Comments
 (0)