Skip to content

Commit 954ccee

Browse files
authored
[clang] fix partial ordering of NTTP packs (llvm#134461)
This fixes partial ordering of pack expansions of NTTPs, by procedding with the check using the pattern of the NTTP through the rules of the non-pack case. This also unifies almost all of the different versions of FinishTemplateArgumentDeduction (except the function template case). This makes sure they all follow the rules consistently, instantiating the parameters and comparing those with the argument. Fixes llvm#132562
1 parent 7fa388d commit 954ccee

File tree

15 files changed

+357
-337
lines changed

15 files changed

+357
-337
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ Bug Fixes to C++ Support
396396
- Improved fix for an issue with pack expansions of type constraints, where this
397397
now also works if the constraint has non-type or template template parameters.
398398
(#GH131798)
399+
- Fixes to partial ordering of non-type template parameter packs. (#GH132562)
399400
- Fix crash when evaluating the trailing requires clause of generic lambdas which are part of
400401
a pack expansion.
401402
- Fixes matching of nested template template parameters. (#GH130362)

clang/include/clang/AST/ExprCXX.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4209,10 +4209,10 @@ class PackExpansionExpr : public Expr {
42094209
Stmt *Pattern;
42104210

42114211
public:
4212-
PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc,
4212+
PackExpansionExpr(Expr *Pattern, SourceLocation EllipsisLoc,
42134213
UnsignedOrNone NumExpansions)
4214-
: Expr(PackExpansionExprClass, T, Pattern->getValueKind(),
4215-
Pattern->getObjectKind()),
4214+
: Expr(PackExpansionExprClass, Pattern->getType(),
4215+
Pattern->getValueKind(), Pattern->getObjectKind()),
42164216
EllipsisLoc(EllipsisLoc),
42174217
NumExpansions(NumExpansions ? *NumExpansions + 1 : 0),
42184218
Pattern(Pattern) {

clang/include/clang/Sema/Sema.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10127,13 +10127,14 @@ class Sema final : public SemaBase {
1012710127

1012810128
/// Contexts in which a converted constant expression is required.
1012910129
enum CCEKind {
10130-
CCEK_CaseValue, ///< Expression in a case label.
10131-
CCEK_Enumerator, ///< Enumerator value with fixed underlying type.
10132-
CCEK_TemplateArg, ///< Value of a non-type template parameter.
10133-
CCEK_InjectedTTP, ///< Injected parameter of a template template parameter.
10134-
CCEK_ArrayBound, ///< Array bound in array declarator or new-expression.
10135-
CCEK_ExplicitBool, ///< Condition in an explicit(bool) specifier.
10136-
CCEK_Noexcept, ///< Condition in a noexcept(bool) specifier.
10130+
CCEK_CaseValue, ///< Expression in a case label.
10131+
CCEK_Enumerator, ///< Enumerator value with fixed underlying type.
10132+
CCEK_TemplateArg, ///< Value of a non-type template parameter.
10133+
CCEK_TempArgStrict, ///< As above, but applies strict template checking
10134+
///< rules.
10135+
CCEK_ArrayBound, ///< Array bound in array declarator or new-expression.
10136+
CCEK_ExplicitBool, ///< Condition in an explicit(bool) specifier.
10137+
CCEK_Noexcept, ///< Condition in a noexcept(bool) specifier.
1013710138
CCEK_StaticAssertMessageSize, ///< Call to size() in a static assert
1013810139
///< message.
1013910140
CCEK_StaticAssertMessageData, ///< Call to data() in a static assert
@@ -11895,7 +11896,7 @@ class Sema final : public SemaBase {
1189511896
QualType InstantiatedParamType, Expr *Arg,
1189611897
TemplateArgument &SugaredConverted,
1189711898
TemplateArgument &CanonicalConverted,
11898-
bool MatchingTTP,
11899+
bool StrictCheck,
1189911900
CheckTemplateArgumentKind CTAK);
1190011901

1190111902
/// Check a template argument against its corresponding

clang/lib/AST/ASTContext.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5852,8 +5852,7 @@ TemplateArgument ASTContext::getInjectedTemplateArg(NamedDecl *Param) const {
58525852
T, VK, NTTP->getLocation());
58535853

58545854
if (NTTP->isParameterPack())
5855-
E = new (*this)
5856-
PackExpansionExpr(DependentTy, E, NTTP->getLocation(), std::nullopt);
5855+
E = new (*this) PackExpansionExpr(E, NTTP->getLocation(), std::nullopt);
58575856
Arg = TemplateArgument(E);
58585857
} else {
58595858
auto *TTP = cast<TemplateTemplateParmDecl>(Param);

clang/lib/AST/ASTImporter.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8273,14 +8273,13 @@ ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
82738273

82748274
ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
82758275
Error Err = Error::success();
8276-
auto ToType = importChecked(Err, E->getType());
8277-
auto ToPattern = importChecked(Err, E->getPattern());
8276+
auto *ToPattern = importChecked(Err, E->getPattern());
82788277
auto ToEllipsisLoc = importChecked(Err, E->getEllipsisLoc());
82798278
if (Err)
82808279
return std::move(Err);
82818280

8282-
return new (Importer.getToContext()) PackExpansionExpr(
8283-
ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
8281+
return new (Importer.getToContext())
8282+
PackExpansionExpr(ToPattern, ToEllipsisLoc, E->getNumExpansions());
82848283
}
82858284

82868285
ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {

clang/lib/Sema/SemaOverload.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6201,7 +6201,7 @@ static ExprResult BuildConvertedConstantExpression(Sema &S, Expr *From,
62016201
Sema::CCEKind CCE,
62026202
NamedDecl *Dest,
62036203
APValue &PreNarrowingValue) {
6204-
assert((S.getLangOpts().CPlusPlus11 || CCE == Sema::CCEK_InjectedTTP) &&
6204+
assert((S.getLangOpts().CPlusPlus11 || CCE == Sema::CCEK_TempArgStrict) &&
62056205
"converted constant expression outside C++11 or TTP matching");
62066206

62076207
if (checkPlaceholderForOverload(S, From))
@@ -6272,7 +6272,7 @@ static ExprResult BuildConvertedConstantExpression(Sema &S, Expr *From,
62726272
// class type.
62736273
ExprResult Result;
62746274
bool IsTemplateArgument =
6275-
CCE == Sema::CCEK_TemplateArg || CCE == Sema::CCEK_InjectedTTP;
6275+
CCE == Sema::CCEK_TemplateArg || CCE == Sema::CCEK_TempArgStrict;
62766276
if (T->isRecordType()) {
62776277
assert(IsTemplateArgument &&
62786278
"unexpected class type converted constant expr");
@@ -6325,7 +6325,7 @@ static ExprResult BuildConvertedConstantExpression(Sema &S, Expr *From,
63256325
// value-dependent so we can't tell whether it's actually narrowing.
63266326
// For matching the parameters of a TTP, the conversion is ill-formed
63276327
// if it may narrow.
6328-
if (CCE != Sema::CCEK_InjectedTTP)
6328+
if (CCE != Sema::CCEK_TempArgStrict)
63296329
break;
63306330
[[fallthrough]];
63316331
case NK_Type_Narrowing:
@@ -6400,7 +6400,7 @@ Sema::EvaluateConvertedConstantExpression(Expr *E, QualType T, APValue &Value,
64006400
Expr::EvalResult Eval;
64016401
Eval.Diag = &Notes;
64026402

6403-
assert(CCE != Sema::CCEK_InjectedTTP && "unnexpected CCE Kind");
6403+
assert(CCE != Sema::CCEK_TempArgStrict && "unnexpected CCE Kind");
64046404

64056405
ConstantExprKind Kind;
64066406
if (CCE == Sema::CCEK_TemplateArg && T->isRecordType())

0 commit comments

Comments
 (0)