Skip to content

[Clang] Implement CWG2369 "Ordering between constraints and substitution" #102857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1119f0a
[Clang][NFCI] Slightly refactor getTemplateInstantiationArgs()
zyn0217 Aug 12, 2024
ca00718
Merge branch 'main' into cwg-2369-2
zyn0217 Aug 13, 2024
2dd772d
Resolve a merge conflict
zyn0217 Aug 13, 2024
96bf64c
CWG 2369
zyn0217 Aug 11, 2024
6b7072d
Rectify diagnostics
zyn0217 Aug 12, 2024
bb31d36
Instantiate function parameters as needed
zyn0217 Aug 12, 2024
631be75
Remove debugging logs
zyn0217 Aug 17, 2024
7d484e2
Revert unrelated changes
zyn0217 Aug 17, 2024
c515407
Fix tests
zyn0217 Aug 17, 2024
a9da5a3
Fix libcxx tests
zyn0217 Aug 17, 2024
cebe706
Instantiate the decls as we substituting constraints
zyn0217 Aug 18, 2024
e0b21a5
[libc++][chono] Use hidden friends for leap_second comparison.
mordante Aug 18, 2024
30dc1d1
clang-format
zyn0217 Aug 19, 2024
9d3981f
Merge branch 'main' into cwg-2369
zyn0217 Aug 21, 2024
afb8a90
Merge branch 'main' into cwg-2369
zyn0217 Aug 28, 2024
6a5085a
Quote the standard wordings, update the DR status page
zyn0217 Aug 28, 2024
974a0ad
Revert unrelated changes
zyn0217 Aug 28, 2024
d19e700
oops, I forgot it
zyn0217 Aug 28, 2024
5be9921
Merge branch 'main' into cwg-2369
zyn0217 Oct 8, 2024
8ace714
Simplify the implementation
zyn0217 Oct 8, 2024
da61fab
Add parentheses to sizeof expression
zyn0217 Oct 8, 2024
18da86a
Merge branch 'main' into cwg-2369
zyn0217 Oct 9, 2024
1b8f0f6
Revert comment changes
zyn0217 Oct 9, 2024
ef5acad
Rebase on top of the new getTemplateInstantiationArgs()
zyn0217 Oct 9, 2024
6fc2f7d
Merge branch 'main' into cwg-2369
zyn0217 Oct 9, 2024
d457f02
Merge branch 'main' into cwg-2369
zyn0217 Oct 21, 2024
f07c4a9
Address a few comments
zyn0217 Oct 21, 2024
a2818ba
Merge branch 'main' into cwg-2369
zyn0217 Nov 3, 2024
8f2dc77
Address more comments
zyn0217 Nov 3, 2024
d16f2cf
Align the comment formats in FinishTemplateArgumentDeduction()
zyn0217 Nov 3, 2024
7e7ed8e
Merge branch 'main' into cwg-2369
zyn0217 Nov 17, 2024
7bc252a
Decouple from the new version of getTemplateInstantiationArgs()
zyn0217 Nov 17, 2024
f0b1e7c
Merge branch 'main' into cwg-2369
zyn0217 Jan 3, 2025
2e9061b
Don't call TransformFunctionTypeParams; expand the parameters ourselves
zyn0217 Jan 3, 2025
9f935ab
Remove unnecessary code
zyn0217 Jan 3, 2025
e623bf5
Use MLTAL.replaceInnermostTemplateArguments to avoid the special-casing
zyn0217 Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -13055,6 +13055,7 @@ class Sema final : public SemaBase {
///
/// \param SkipForSpecialization when specified, any template specializations
/// in a traversal would be ignored.
///
/// \param ForDefaultArgumentSubstitution indicates we should continue looking
/// when encountering a specialized member function template, rather than
/// returning immediately.
Expand All @@ -13066,6 +13067,17 @@ class Sema final : public SemaBase {
bool SkipForSpecialization = false,
bool ForDefaultArgumentSubstitution = false);

/// Apart from storing the result to \p Result, this behaves the same as
/// another overload.
void getTemplateInstantiationArgs(
MultiLevelTemplateArgumentList &Result, const NamedDecl *D,
const DeclContext *DC = nullptr, bool Final = false,
std::optional<ArrayRef<TemplateArgument>> Innermost = std::nullopt,
bool RelativeToPrimary = false, const FunctionDecl *Pattern = nullptr,
bool ForConstraintInstantiation = false,
bool SkipForSpecialization = false,
bool ForDefaultArgumentSubstitution = false);

Comment on lines +13070 to +13080
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was taken from #110387, which has also been reverted due to its dependency on the refactoring work.

The extra parameters can certainly be removed alongside the relanding of that huge patch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove that now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No? Otherwise the MLTAL would be created internally and we wouldn't have a chance to put an empty args outside.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(For clarity, this function doesn't insert an empty list for FunctionDecl that is not a specialization. So we need to do that right before the call)

/// RAII object to handle the state changes required to synthesize
/// a function body.
class SynthesizedFunctionScope {
Expand Down Expand Up @@ -13335,7 +13347,7 @@ class Sema final : public SemaBase {
ExprResult
SubstConstraintExpr(Expr *E,
const MultiLevelTemplateArgumentList &TemplateArgs);
// Unlike the above, this does not evaluates constraints.
// Unlike the above, this does not evaluate constraints.
ExprResult SubstConstraintExprWithoutSatisfaction(
Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs);

Expand Down Expand Up @@ -14456,10 +14468,10 @@ class Sema final : public SemaBase {
const MultiLevelTemplateArgumentList &TemplateArgs,
SourceRange TemplateIDRange);

bool CheckInstantiatedFunctionTemplateConstraints(
SourceLocation PointOfInstantiation, FunctionDecl *Decl,
ArrayRef<TemplateArgument> TemplateArgs,
ConstraintSatisfaction &Satisfaction);
bool CheckFunctionTemplateConstraints(SourceLocation PointOfInstantiation,
FunctionDecl *Decl,
ArrayRef<TemplateArgument> TemplateArgs,
ConstraintSatisfaction &Satisfaction);

/// \brief Emit diagnostics explaining why a constraint expression was deemed
/// unsatisfied.
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Sema/Template.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,12 @@ enum class TemplateSubstitutionKind : char {
llvm::PointerUnion<Decl *, DeclArgumentPack *> *
findInstantiationOf(const Decl *D);

/// Similar to \p findInstantiationOf(), but it wouldn't assert if the
/// instantiation was not found within the current instantiation scope. This
/// is helpful for on-demand declaration instantiation.
llvm::PointerUnion<Decl *, DeclArgumentPack *> *
findInstantiationUnsafe(const Decl *D);

void InstantiatedLocal(const Decl *D, Decl *Inst);
void InstantiatedLocalPackArg(const Decl *D, VarDecl *Inst);
void MakeInstantiatedLocalArgPack(const Decl *D);
Expand Down
47 changes: 45 additions & 2 deletions clang/lib/Sema/SemaConcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ bool Sema::CheckFunctionConstraints(const FunctionDecl *FD,
bool ForOverloadResolution) {
// Don't check constraints if the function is dependent. Also don't check if
// this is a function template specialization, as the call to
// CheckinstantiatedFunctionTemplateConstraints after this will check it
// CheckFunctionTemplateConstraints after this will check it
// better.
if (FD->isDependentContext() ||
FD->getTemplatedKind() ==
Expand Down Expand Up @@ -1111,12 +1111,55 @@ bool Sema::EnsureTemplateArgumentListConstraints(
return false;
}

bool Sema::CheckInstantiatedFunctionTemplateConstraints(
static bool CheckFunctionConstraintsWithoutInstantiation(
Sema &SemaRef, SourceLocation PointOfInstantiation,
FunctionTemplateDecl *Template, ArrayRef<TemplateArgument> TemplateArgs,
ConstraintSatisfaction &Satisfaction) {
SmallVector<const Expr *, 3> TemplateAC;
Template->getAssociatedConstraints(TemplateAC);
if (TemplateAC.empty()) {
Satisfaction.IsSatisfied = true;
return false;
}

LocalInstantiationScope Scope(SemaRef);

FunctionDecl *FD = Template->getTemplatedDecl();
// Collect the list of template arguments relative to the 'primary'
// template. We need the entire list, since the constraint is completely
// uninstantiated at this point.

// FIXME: Add TemplateArgs through the 'Innermost' parameter once
// the refactoring of getTemplateInstantiationArgs() relands.
MultiLevelTemplateArgumentList MLTAL;
MLTAL.addOuterTemplateArguments(Template, std::nullopt, /*Final=*/false);
SemaRef.getTemplateInstantiationArgs(
MLTAL, /*D=*/FD, FD,
/*Final=*/false, /*Innermost=*/std::nullopt, /*RelativeToPrimary=*/true,
/*Pattern=*/nullptr, /*ForConstraintInstantiation=*/true);
MLTAL.replaceInnermostTemplateArguments(Template, TemplateArgs);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to enter an Unevaluated ExpressionEvaluationContext
Given the windows bug, I think the SavedContext might not be correct. Can you play with that?

Sema::ContextRAII SavedContext(SemaRef, FD);
std::optional<Sema::CXXThisScopeRAII> ThisScope;
if (auto *Method = dyn_cast<CXXMethodDecl>(FD))
ThisScope.emplace(SemaRef, /*Record=*/Method->getParent(),
/*ThisQuals=*/Method->getMethodQualifiers());
Comment on lines +1144 to +1146
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is that useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I don't know. :(

I copied that from the previous implementation, and I was expecting it to make a difference in situations where member function calls are involved in a constraint evaluation. But I ran the regression tests with that removed and everything seemed fine.

I will have another try on a build without this patch. It could also be the case we're missing some tests, but I don't know...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I removed these CXXThisScopeRAII lines in CheckInstantiatedFunctionTemplateConstraints() and all libcxx and clang regressions still passed. I guess we probably could remove these setups there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe try to play with things like requires(__is_same_as(s, decltype(*this)));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to contrive a case like

template <class T>
struct S {
  void foo() requires(__is_same_as(S&, decltype(*this))) {}
};

void g() {
  S<int>().foo();
}

But there would still be no difference if we did the CXXThisScopeRAII things away. (It always compiles.)

Maybe I'm running out of ideas right now, so let's just preserve these "strange but plausible" things?

return SemaRef.CheckConstraintSatisfaction(
Template, TemplateAC, MLTAL, PointOfInstantiation, Satisfaction);
}

bool Sema::CheckFunctionTemplateConstraints(
SourceLocation PointOfInstantiation, FunctionDecl *Decl,
ArrayRef<TemplateArgument> TemplateArgs,
ConstraintSatisfaction &Satisfaction) {
// In most cases we're not going to have constraints, so check for that first.
FunctionTemplateDecl *Template = Decl->getPrimaryTemplate();

if (!Template)
return ::CheckFunctionConstraintsWithoutInstantiation(
*this, PointOfInstantiation, Decl->getDescribedFunctionTemplate(),
TemplateArgs, Satisfaction);

// Note - code synthesis context for the constraints check is created
// inside CheckConstraintsSatisfaction.
SmallVector<const Expr *, 3> TemplateAC;
Expand Down
49 changes: 35 additions & 14 deletions clang/lib/Sema/SemaTemplateDeduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3933,18 +3933,6 @@ TemplateDeductionResult Sema::FinishTemplateArgumentDeduction(
Result != TemplateDeductionResult::Success)
return Result;

// C++ [temp.deduct.call]p10: [DR1391]
// If deduction succeeds for all parameters that contain
// template-parameters that participate in template argument deduction,
// and all template arguments are explicitly specified, deduced, or
// obtained from default template arguments, remaining parameters are then
// compared with the corresponding arguments. For each remaining parameter
// P with a type that was non-dependent before substitution of any
// explicitly-specified template arguments, if the corresponding argument
// A cannot be implicitly converted to P, deduction fails.
if (CheckNonDependent())
return TemplateDeductionResult::NonDependentConversionFailure;

// Form the template argument list from the deduced template arguments.
TemplateArgumentList *SugaredDeducedArgumentList =
TemplateArgumentList::CreateCopy(Context, SugaredBuilder);
Expand Down Expand Up @@ -3974,6 +3962,39 @@ TemplateDeductionResult Sema::FinishTemplateArgumentDeduction(
FD = const_cast<FunctionDecl *>(FDFriend);
Owner = FD->getLexicalDeclContext();
}
// C++20 [temp.deduct.general]p5: [CWG2369]
// If the function template has associated constraints, those constraints
// are checked for satisfaction. If the constraints are not satisfied, type
// deduction fails.
//
// FIXME: We haven't implemented CWG2369 for lambdas yet, because we need
// to figure out how to instantiate lambda captures to the scope without
// first instantiating the lambda.
bool IsLambda = isLambdaCallOperator(FD) || isLambdaConversionOperator(FD);
if (!IsLambda && !IsIncomplete) {
if (CheckFunctionTemplateConstraints(
Info.getLocation(),
FunctionTemplate->getCanonicalDecl()->getTemplatedDecl(),
CanonicalBuilder, Info.AssociatedConstraintsSatisfaction))
return TemplateDeductionResult::MiscellaneousDeductionFailure;
if (!Info.AssociatedConstraintsSatisfaction.IsSatisfied) {
Info.reset(Info.takeSugared(),
TemplateArgumentList::CreateCopy(Context, CanonicalBuilder));
Comment on lines +3981 to +3982
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Frankly these lines are also copied from the previous failure handling)

I guess the instantiated concept-related nodes that were created during the checking would use the TemplateArgumentLists created on the ASTContext, or rather they would take ownership of the TemplateArgumentList. So, in order for the pilfered argument lists to work for other clients of TemplateDeductionInfo, we have to make a copy of the list.

(I saw the patch that tried to preserve sugars in concepts turn this line into "copy sugared arguments, but do nothing for canonical arguments")

But I'm not entirely sure of such deduction things, so probably @mizvekov would correct my understanding in some way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @mizvekov

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, what @zyn0217 said is correct, the canonical argument list will have references persist in AST nodes, so it needs to be copied here.

return TemplateDeductionResult::ConstraintsNotSatisfied;
}
}
// C++ [temp.deduct.call]p10: [CWG1391]
// If deduction succeeds for all parameters that contain
// template-parameters that participate in template argument deduction,
// and all template arguments are explicitly specified, deduced, or
// obtained from default template arguments, remaining parameters are then
// compared with the corresponding arguments. For each remaining parameter
// P with a type that was non-dependent before substitution of any
// explicitly-specified template arguments, if the corresponding argument
// A cannot be implicitly converted to P, deduction fails.
if (CheckNonDependent())
return TemplateDeductionResult::NonDependentConversionFailure;

MultiLevelTemplateArgumentList SubstArgs(
FunctionTemplate, CanonicalDeducedArgumentList->asArray(),
/*Final=*/false);
Expand Down Expand Up @@ -4008,8 +4029,8 @@ TemplateDeductionResult Sema::FinishTemplateArgumentDeduction(
// ([temp.constr.decl]), those constraints are checked for satisfaction
// ([temp.constr.constr]). If the constraints are not satisfied, type
// deduction fails.
if (!IsIncomplete) {
if (CheckInstantiatedFunctionTemplateConstraints(
if (IsLambda && !IsIncomplete) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a fixme comment here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one on line 4003, maybe that just suffices?

if (CheckFunctionTemplateConstraints(
Info.getLocation(), Specialization, CanonicalBuilder,
Info.AssociatedConstraintsSatisfaction))
return TemplateDeductionResult::MiscellaneousDeductionFailure;
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Sema/SemaTemplateDeductionGuide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,10 +902,12 @@ Expr *buildIsDeducibleConstraint(Sema &SemaRef,
Context.getTrivialTypeSourceInfo(
Context.getDeducedTemplateSpecializationType(
TemplateName(AliasTemplate), /*DeducedType=*/QualType(),
/*IsDependent=*/true)), // template specialization type whose
// arguments will be deduced.
/*IsDependent=*/true),
AliasTemplate->getLocation()), // template specialization type whose
// arguments will be deduced.
Context.getTrivialTypeSourceInfo(
ReturnType), // type from which template arguments are deduced.
ReturnType, AliasTemplate->getLocation()), // type from which template
// arguments are deduced.
};
return TypeTraitExpr::Create(
Context, Context.getLogicalOperationType(), AliasTemplate->getLocation(),
Expand Down
115 changes: 104 additions & 11 deletions clang/lib/Sema/SemaTemplateInstantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,21 @@ MultiLevelTemplateArgumentList Sema::getTemplateInstantiationArgs(
assert((ND || DC) && "Can't find arguments for a decl if one isn't provided");
// Accumulate the set of template argument lists in this structure.
MultiLevelTemplateArgumentList Result;
getTemplateInstantiationArgs(
Result, ND, DC, Final, Innermost, RelativeToPrimary, Pattern,
ForConstraintInstantiation, SkipForSpecialization,
ForDefaultArgumentSubstitution);
return Result;
}

void Sema::getTemplateInstantiationArgs(
MultiLevelTemplateArgumentList &Result, const NamedDecl *ND,
const DeclContext *DC, bool Final,
std::optional<ArrayRef<TemplateArgument>> Innermost, bool RelativeToPrimary,
const FunctionDecl *Pattern, bool ForConstraintInstantiation,
bool SkipForSpecialization, bool ForDefaultArgumentSubstitution) {
assert((ND || DC) && "Can't find arguments for a decl if one isn't provided");
// Accumulate the set of template argument lists in this structure.

using namespace TemplateInstArgsHelpers;
const Decl *CurDecl = ND;
Expand Down Expand Up @@ -535,14 +550,12 @@ MultiLevelTemplateArgumentList Sema::getTemplateInstantiationArgs(
}

if (R.IsDone)
return Result;
return;
if (R.ClearRelativeToPrimary)
RelativeToPrimary = false;
assert(R.NextDecl);
CurDecl = R.NextDecl;
}

return Result;
}

bool Sema::CodeSynthesisContext::isInstantiationRecord() const {
Expand Down Expand Up @@ -1349,6 +1362,19 @@ namespace {
// Whether an incomplete substituion should be treated as an error.
bool BailOutOnIncomplete;

private:
bool isSubstitutingConstraints() const {
return llvm::any_of(SemaRef.CodeSynthesisContexts, [](auto &Context) {
return Context.Kind ==
Sema::CodeSynthesisContext::ConstraintSubstitution;
});
}

// CWG2770: Function parameters should be instantiated when they are
// needed by a satisfaction check of an atomic constraint or
// (recursively) by another function parameter.
bool maybeInstantiateFunctionParameterToScope(ParmVarDecl *OldParm);

public:
typedef TreeTransform<TemplateInstantiator> inherited;

Expand Down Expand Up @@ -1405,12 +1431,19 @@ namespace {
ArrayRef<UnexpandedParameterPack> Unexpanded,
bool &ShouldExpand, bool &RetainExpansion,
std::optional<unsigned> &NumExpansions) {
return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
PatternRange, Unexpanded,
TemplateArgs,
ShouldExpand,
RetainExpansion,
NumExpansions);
if (SemaRef.CurrentInstantiationScope && isSubstitutingConstraints()) {
for (UnexpandedParameterPack ParmPack : Unexpanded) {
NamedDecl *VD = ParmPack.first.dyn_cast<NamedDecl *>();
if (!isa_and_present<ParmVarDecl>(VD))
continue;
if (maybeInstantiateFunctionParameterToScope(cast<ParmVarDecl>(VD)))
return true;
}
}

return getSema().CheckParameterPacksForExpansion(
EllipsisLoc, PatternRange, Unexpanded, TemplateArgs, ShouldExpand,
RetainExpansion, NumExpansions);
}

void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
Expand Down Expand Up @@ -1911,9 +1944,62 @@ Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
// template parameter.
}

if (SemaRef.CurrentInstantiationScope) {
if (isSubstitutingConstraints() && isa<ParmVarDecl>(D) &&
maybeInstantiateFunctionParameterToScope(cast<ParmVarDecl>(D)))
return nullptr;
}

return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
}

bool TemplateInstantiator::maybeInstantiateFunctionParameterToScope(
ParmVarDecl *OldParm) {
if (SemaRef.CurrentInstantiationScope->findInstantiationUnsafe(OldParm))
return false;
// We're instantiating a function parameter whose associated function template
// has not been instantiated at this point for constraint evaluation, so make
// sure the instantiated parameters are owned by a function declaration such
// that they can be correctly 'captured' in tryCaptureVariable().
Sema::ContextRAII Context(SemaRef, OldParm->getDeclContext());

if (!OldParm->isParameterPack())
return !TransformFunctionTypeParam(OldParm, /*indexAdjustment=*/0,
/*NumExpansions=*/std::nullopt,
/*ExpectParameterPack=*/false);

SmallVector<UnexpandedParameterPack, 2> Unexpanded;

// Find the parameter packs that could be expanded.
TypeLoc TL = OldParm->getTypeSourceInfo()->getTypeLoc();
PackExpansionTypeLoc ExpansionTL = TL.castAs<PackExpansionTypeLoc>();
TypeLoc Pattern = ExpansionTL.getPatternLoc();
SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");

bool ShouldExpand = false;
bool RetainExpansion = false;
std::optional<unsigned> OrigNumExpansions =
ExpansionTL.getTypePtr()->getNumExpansions();
std::optional<unsigned> NumExpansions = OrigNumExpansions;
if (TryExpandParameterPacks(ExpansionTL.getEllipsisLoc(),
Pattern.getSourceRange(), Unexpanded,
ShouldExpand, RetainExpansion, NumExpansions))
return true;

assert(ShouldExpand && !RetainExpansion &&
"Shouldn't preserve pack expansion when evaluating constraints");
ExpandingFunctionParameterPack(OldParm);
for (unsigned I = 0; I != *NumExpansions; ++I) {
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
if (!TransformFunctionTypeParam(OldParm, /*indexAdjustment=*/0,
/*NumExpansions=*/OrigNumExpansions,
/*ExpectParameterPack=*/false))
return true;
}
return false;
}

Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
if (!Inst)
Expand Down Expand Up @@ -4591,9 +4677,8 @@ static const Decl *getCanonicalParmVarDecl(const Decl *D) {
return D;
}


llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
LocalInstantiationScope::findInstantiationOf(const Decl *D) {
LocalInstantiationScope::findInstantiationUnsafe(const Decl *D) {
D = getCanonicalParmVarDecl(D);
for (LocalInstantiationScope *Current = this; Current;
Current = Current->Outer) {
Expand All @@ -4618,6 +4703,14 @@ LocalInstantiationScope::findInstantiationOf(const Decl *D) {
break;
}

return nullptr;
}

llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
LocalInstantiationScope::findInstantiationOf(const Decl *D) {
auto *Result = findInstantiationUnsafe(D);
if (Result)
return Result;
// If we're performing a partial substitution during template argument
// deduction, we may not have values for template parameters yet.
if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ class TreeTransform {
/// variables vector are acceptable.
///
/// LastParamTransformed, if non-null, will be set to the index of the last
/// parameter on which transfromation was started. In the event of an error,
/// parameter on which transformation was started. In the event of an error,
/// this will contain the parameter which failed to instantiate.
///
/// Return true on error.
Expand Down
Loading
Loading