Skip to content

Commit 98062d8

Browse files
committed
Revert "[Clang] Add captures to the instantiation scope of lambda call operators"
The change causes some libcxx regressions This reverts commit eaf725b.
1 parent edae8f6 commit 98062d8

File tree

8 files changed

+33
-95
lines changed

8 files changed

+33
-95
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,6 @@ Bug Fixes to C++ Support
268268
- Fix crash when parsing the requires clause of some generic lambdas.
269269
(`#64689 <https://github.com/llvm/llvm-project/issues/64689>`_)
270270

271-
- Fix crash when the trailing return type of a generic and dependent
272-
lambda refers to an init-capture.
273-
(`#65067 <https://github.com/llvm/llvm-project/issues/65067>`_) and
274-
(`#63675 <https://github.com/llvm/llvm-project/issues/63675>`_).
275-
276271
Bug Fixes to AST Handling
277272
^^^^^^^^^^^^^^^^^^^^^^^^^
278273
- Fixed an import failure of recursive friend class template.

clang/include/clang/Sema/Sema.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7352,14 +7352,6 @@ class Sema final {
73527352

73537353
sema::LambdaScopeInfo *RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator);
73547354

7355-
class LambdaScopeForCallOperatorInstantiationRAII
7356-
: private FunctionScopeRAII {
7357-
public:
7358-
LambdaScopeForCallOperatorInstantiationRAII(
7359-
Sema &SemasRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
7360-
LocalInstantiationScope &Scope);
7361-
};
7362-
73637355
/// Check whether the given expression is a valid constraint expression.
73647356
/// A diagnostic is emitted if it is not, false is returned, and
73657357
/// PossibleNonPrimary will be set to true if the failure might be due to a

clang/lib/Sema/SemaConcept.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,11 @@ bool Sema::SetupConstraintScope(
600600
if (addInstantiatedParametersToScope(FD, FromMemTempl->getTemplatedDecl(),
601601
Scope, MLTAL))
602602
return true;
603+
// Make sure the captures are also added to the instantiation scope.
604+
if (isLambdaCallOperator(FD) &&
605+
addInstantiatedCapturesToScope(FD, FromMemTempl->getTemplatedDecl(),
606+
Scope, MLTAL))
607+
return true;
603608
}
604609

605610
return false;
@@ -624,6 +629,11 @@ bool Sema::SetupConstraintScope(
624629
// child-function.
625630
if (addInstantiatedParametersToScope(FD, InstantiatedFrom, Scope, MLTAL))
626631
return true;
632+
633+
// Make sure the captures are also added to the instantiation scope.
634+
if (isLambdaCallOperator(FD) &&
635+
addInstantiatedCapturesToScope(FD, InstantiatedFrom, Scope, MLTAL))
636+
return true;
627637
}
628638

629639
return false;
@@ -702,8 +712,20 @@ bool Sema::CheckFunctionConstraints(const FunctionDecl *FD,
702712
}
703713
CXXThisScopeRAII ThisScope(*this, Record, ThisQuals, Record != nullptr);
704714

705-
LambdaScopeForCallOperatorInstantiationRAII LambdaScope(
706-
*this, const_cast<FunctionDecl *>(FD), *MLTAL, Scope);
715+
// When checking the constraints of a lambda, we need to restore a
716+
// LambdaScopeInfo populated with correct capture information so that the type
717+
// of a variable referring to a capture is correctly const-adjusted.
718+
FunctionScopeRAII FuncScope(*this);
719+
if (isLambdaCallOperator(FD)) {
720+
LambdaScopeInfo *LSI = RebuildLambdaScopeInfo(
721+
const_cast<CXXMethodDecl *>(cast<CXXMethodDecl>(FD)));
722+
// Constraints are checked from the parent context of the lambda, so we set
723+
// AfterParameterList to false, so that `tryCaptureVariable` finds
724+
// explicit captures in the appropriate context.
725+
LSI->AfterParameterList = false;
726+
} else {
727+
FuncScope.disable();
728+
}
707729

708730
return CheckConstraintSatisfaction(
709731
FD, {FD->getTrailingRequiresClause()}, *MLTAL,
@@ -891,10 +913,15 @@ bool Sema::CheckInstantiatedFunctionTemplateConstraints(
891913
ThisQuals = Method->getMethodQualifiers();
892914
Record = Method->getParent();
893915
}
894-
895916
CXXThisScopeRAII ThisScope(*this, Record, ThisQuals, Record != nullptr);
896-
LambdaScopeForCallOperatorInstantiationRAII LambdaScope(
897-
*this, const_cast<FunctionDecl *>(Decl), *MLTAL, Scope);
917+
FunctionScopeRAII FuncScope(*this);
918+
919+
if (isLambdaCallOperator(Decl)) {
920+
LambdaScopeInfo *LSI = RebuildLambdaScopeInfo(cast<CXXMethodDecl>(Decl));
921+
LSI->AfterParameterList = false;
922+
} else {
923+
FuncScope.disable();
924+
}
898925

899926
llvm::SmallVector<Expr *, 1> Converted;
900927
return CheckConstraintSatisfaction(Template, TemplateAC, Converted, *MLTAL,

clang/lib/Sema/SemaDecl.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15380,10 +15380,6 @@ LambdaScopeInfo *Sema::RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator) {
1538015380
LSI->CallOperator = CallOperator;
1538115381
LSI->Lambda = LambdaClass;
1538215382
LSI->ReturnType = CallOperator->getReturnType();
15383-
// This function in calls in situation where the context of the call operator
15384-
// is not entered, so we set AfterParameterList to false, so that
15385-
// `tryCaptureVariable` finds explicit captures in the appropriate context.
15386-
LSI->AfterParameterList = false;
1538715383
const LambdaCaptureDefault LCD = LambdaClass->getLambdaCaptureDefault();
1538815384

1538915385
if (LCD == LCD_None)

clang/lib/Sema/SemaLambda.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "clang/Sema/ScopeInfo.h"
2121
#include "clang/Sema/SemaInternal.h"
2222
#include "clang/Sema/SemaLambda.h"
23-
#include "clang/Sema/Template.h"
2423
#include "llvm/ADT/STLExtras.h"
2524
#include <optional>
2625
using namespace clang;
@@ -2255,34 +2254,3 @@ ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation,
22552254

22562255
return BuildBlock;
22572256
}
2258-
2259-
Sema::LambdaScopeForCallOperatorInstantiationRAII::
2260-
LambdaScopeForCallOperatorInstantiationRAII(
2261-
Sema &SemasRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
2262-
LocalInstantiationScope &Scope)
2263-
: FunctionScopeRAII(SemasRef) {
2264-
if (!isLambdaCallOperator(FD)) {
2265-
FunctionScopeRAII::disable();
2266-
return;
2267-
}
2268-
2269-
if (FD->isTemplateInstantiation() && FD->getPrimaryTemplate()) {
2270-
FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate();
2271-
if (const auto *FromMemTempl =
2272-
PrimaryTemplate->getInstantiatedFromMemberTemplate()) {
2273-
SemasRef.addInstantiatedCapturesToScope(
2274-
FD, FromMemTempl->getTemplatedDecl(), Scope, MLTAL);
2275-
}
2276-
}
2277-
2278-
else if (FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization ||
2279-
FD->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate) {
2280-
FunctionDecl *InstantiatedFrom =
2281-
FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization
2282-
? FD->getInstantiatedFromMemberFunction()
2283-
: FD->getInstantiatedFromDecl();
2284-
SemasRef.addInstantiatedCapturesToScope(FD, InstantiatedFrom, Scope, MLTAL);
2285-
}
2286-
2287-
SemasRef.RebuildLambdaScopeInfo(cast<CXXMethodDecl>(FD));
2288-
}

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,9 +2426,6 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(
24262426
cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
24272427
LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
24282428

2429-
Sema::LambdaScopeForCallOperatorInstantiationRAII LambdaScope(
2430-
SemaRef, const_cast<CXXMethodDecl *>(D), TemplateArgs, Scope);
2431-
24322429
// Instantiate enclosing template arguments for friends.
24332430
SmallVector<TemplateParameterList *, 4> TempParamLists;
24342431
unsigned NumTempParamLists = 0;

clang/lib/Sema/TreeTransform.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12325,7 +12325,7 @@ TreeTransform<Derived>::TransformCXXNullPtrLiteralExpr(
1232512325
template<typename Derived>
1232612326
ExprResult
1232712327
TreeTransform<Derived>::TransformCXXThisExpr(CXXThisExpr *E) {
12328-
QualType T = getDerived().TransformType(E->getType());
12328+
QualType T = getSema().getCurrentThisType();
1232912329

1233012330
if (!getDerived().AlwaysRebuild() && T == E->getType()) {
1233112331
// Mark it referenced in the new context regardless.

clang/test/SemaCXX/lambda-capture-type-deduction.cpp

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -260,40 +260,3 @@ void f(int) {
260260
void test() { f<int>(0); }
261261

262262
}
263-
264-
namespace GH65067 {
265-
266-
template <typename> class a {
267-
public:
268-
template <typename b> void c(b f) { d<int>(f)(0); }
269-
template <typename, typename b> auto d(b f) {
270-
return [f = f](auto arg) -> a<decltype(f(arg))> { return {}; };
271-
}
272-
};
273-
a<void> e;
274-
auto fn1() {
275-
e.c([](int) {});
276-
}
277-
278-
}
279-
280-
namespace GH63675 {
281-
282-
template <class _Tp> _Tp __declval();
283-
struct __get_tag {
284-
template <class _Tag> void operator()(_Tag);
285-
};
286-
template <class _ImplFn> struct __basic_sender {
287-
using __tag_t = decltype(__declval<_ImplFn>()(__declval<__get_tag>()));
288-
_ImplFn __impl_;
289-
};
290-
auto __make_basic_sender = []<class... _Children>(
291-
_Children... __children) {
292-
return __basic_sender{[... __children = __children]<class _Fun>(
293-
_Fun __fun) -> decltype(__fun(__children...)) {}};
294-
};
295-
void __trans_tmp_1() {
296-
__make_basic_sender(__trans_tmp_1);
297-
}
298-
299-
}

0 commit comments

Comments
 (0)