Skip to content
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

[Clang] Fix Handling of Init Capture with Parameter Packs in LambdaScopeForCallOperatorInstantiationRAII #100766

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Bug Fixes to C++ Support
^^^^^^^^^^^^^^^^^^^^^^^^

- Fixed a crash when an expression with a dependent ``__typeof__`` type is used as the operand of a unary operator. (#GH97646)
- Fixed incorrect pack expansion of init-capture references in requires expresssions.
- Fixed a failed assertion when checking invalid delete operator declaration. (#GH96191)

Bug Fixes to AST Handling
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -14186,6 +14186,10 @@ class Sema final : public SemaBase {
std::optional<unsigned> getNumArgumentsInExpansion(
QualType T, const MultiLevelTemplateArgumentList &TemplateArgs);

std::optional<unsigned> getNumArgumentsInExpansionFromUnexpanded(
llvm::ArrayRef<UnexpandedParameterPack> Unexpanded,
const MultiLevelTemplateArgumentList &TemplateArgs);

/// Determine whether the given declarator contains any unexpanded
/// parameter packs.
///
Expand Down
18 changes: 13 additions & 5 deletions clang/lib/Sema/SemaConcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,22 +712,30 @@ bool Sema::addInstantiatedCapturesToScope(
auto AddSingleCapture = [&](const ValueDecl *CapturedPattern,
unsigned Index) {
ValueDecl *CapturedVar = LambdaClass->getCapture(Index)->getCapturedVar();
if (CapturedVar->isInitCapture())
Scope.InstantiatedLocal(CapturedPattern, CapturedVar);
assert(CapturedVar->isInitCapture());
Scope.InstantiatedLocal(CapturedPattern, CapturedVar);
};

for (const LambdaCapture &CapturePattern : LambdaPattern->captures()) {
if (!CapturePattern.capturesVariable()) {
Instantiated++;
continue;
}
const ValueDecl *CapturedPattern = CapturePattern.getCapturedVar();
ValueDecl *CapturedPattern = CapturePattern.getCapturedVar();

if (!CapturedPattern->isInitCapture()) {
continue;
}
Comment on lines +726 to +728
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 CapturedPattern not an initCapture?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

captures like

[a]() {}

Copy link
Contributor

Choose a reason for hiding this comment

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

Hum, nvm, I misread that code , I failed to realize the assert L715 was in a different scope, so I was confused.


if (!CapturedPattern->isParameterPack()) {
AddSingleCapture(CapturedPattern, Instantiated++);
} else {
Scope.MakeInstantiatedLocalArgPack(CapturedPattern);
std::optional<unsigned> NumArgumentsInExpansion =
getNumArgumentsInExpansion(CapturedPattern->getType(), TemplateArgs);
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
SemaRef.collectUnexpandedParameterPacks(
dyn_cast<VarDecl>(CapturedPattern)->getInit(), Unexpanded);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use cast?

auto NumArgumentsInExpansion =
getNumArgumentsInExpansionFromUnexpanded(Unexpanded, TemplateArgs);
if (!NumArgumentsInExpansion)
continue;
for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg)
Expand Down
9 changes: 7 additions & 2 deletions clang/lib/Sema/SemaLambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2389,11 +2389,10 @@ Sema::LambdaScopeForCallOperatorInstantiationRAII::
if (!FDPattern)
return;

SemaRef.addInstantiatedCapturesToScope(FD, FDPattern, Scope, MLTAL);

if (!ShouldAddDeclsFromParentScope)
return;

FunctionDecl *InnermostFD = FD, *InnermostFDPattern = FDPattern;
llvm::SmallVector<std::pair<FunctionDecl *, FunctionDecl *>, 4>
ParentInstantiations;
while (true) {
Expand All @@ -2417,5 +2416,11 @@ Sema::LambdaScopeForCallOperatorInstantiationRAII::
for (const auto &[FDPattern, FD] : llvm::reverse(ParentInstantiations)) {
SemaRef.addInstantiatedParametersToScope(FD, FDPattern, Scope, MLTAL);
SemaRef.addInstantiatedLocalVarsToScope(FD, FDPattern, Scope);

if (isLambdaCallOperator(FD))
SemaRef.addInstantiatedCapturesToScope(FD, FDPattern, Scope, MLTAL);
}

SemaRef.addInstantiatedCapturesToScope(InnermostFD, InnermostFDPattern, Scope,
MLTAL);
}
17 changes: 11 additions & 6 deletions clang/lib/Sema/SemaTemplateVariadic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,12 +825,9 @@ bool Sema::CheckParameterPacksForExpansion(
return false;
}

std::optional<unsigned> Sema::getNumArgumentsInExpansion(
QualType T, const MultiLevelTemplateArgumentList &TemplateArgs) {
QualType Pattern = cast<PackExpansionType>(T)->getPattern();
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);

std::optional<unsigned> Sema::getNumArgumentsInExpansionFromUnexpanded(
llvm::ArrayRef<UnexpandedParameterPack> Unexpanded,
const MultiLevelTemplateArgumentList &TemplateArgs) {
std::optional<unsigned> Result;
for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
// Compute the depth and index for this parameter pack.
Expand Down Expand Up @@ -878,6 +875,14 @@ std::optional<unsigned> Sema::getNumArgumentsInExpansion(
return Result;
}

std::optional<unsigned> Sema::getNumArgumentsInExpansion(
QualType T, const MultiLevelTemplateArgumentList &TemplateArgs) {
QualType Pattern = cast<PackExpansionType>(T)->getPattern();
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
return getNumArgumentsInExpansionFromUnexpanded(Unexpanded, TemplateArgs);
}

bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
const DeclSpec &DS = D.getDeclSpec();
switch (DS.getTypeSpecType()) {
Expand Down
28 changes: 28 additions & 0 deletions clang/test/SemaTemplate/concepts-lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,31 @@ void dependent_param() {
L(0, 1)(1, 2)(1);
}
} // namespace dependent_param_concept

namespace init_captures {
template <int N> struct V {};

void sink(V<0>, V<1>, V<2>, V<3>, V<4>) {}

void init_capture_pack() {
auto L = [](auto... z) {
return [=](auto... y) {
return [... w = z, y...](auto)
requires requires { sink(w..., y...); }
{};
};
};
L(V<0>{}, V<1>{}, V<2>{})(V<3>{}, V<4>{})(1);
}

void dependent_capture_packs() {
auto L = [](auto... z) {
return [... w = z](auto... y) {
return [... c = w](auto)
requires requires { sink(c..., y...); }
{};
};
};
L(V<0>{}, V<1>{}, V<2>{})(V<3>{}, V<4>{})(1);
}
} // namespace init_captures
Loading