Skip to content

Commit 0732693

Browse files
authored
Revert "[Clang] Only remove lambda scope after computing evaluation context" (#154382)
Revert due to breakage as reported in #154106 (comment) Reverts #154106
1 parent ae434cd commit 0732693

File tree

6 files changed

+28
-53
lines changed

6 files changed

+28
-53
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ Bug Fixes to C++ Support
225225
- Diagnose binding a reference to ``*nullptr`` during constant evaluation. (#GH48665)
226226
- Suppress ``-Wdeprecated-declarations`` in implicitly generated functions. (#GH147293)
227227
- Fix a crash when deleting a pointer to an incomplete array (#GH150359).
228-
- Fixed a mismatched lambda scope bug when propagating up ``consteval`` within nested lambdas. (#GH145776)
229228
- Fix an assertion failure when expression in assumption attribute
230229
(``[[assume(expr)]]``) creates temporary objects.
231230
- Fix the dynamic_cast to final class optimization to correctly handle

clang/include/clang/Sema/Sema.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4176,15 +4176,8 @@ class Sema final : public SemaBase {
41764176
/// return statement in the scope of a variable has the same NRVO candidate,
41774177
/// that candidate is an NRVO variable.
41784178
void computeNRVO(Stmt *Body, sema::FunctionScopeInfo *Scope);
4179-
4180-
/// Performs semantic analysis at the end of a function body.
4181-
///
4182-
/// \param RetainFunctionScopeInfo If \c true, the client is responsible for
4183-
/// releasing the associated \p FunctionScopeInfo. This is useful when
4184-
/// building e.g. LambdaExprs.
4185-
Decl *ActOnFinishFunctionBody(Decl *Decl, Stmt *Body,
4186-
bool IsInstantiation = false,
4187-
bool RetainFunctionScopeInfo = false);
4179+
Decl *ActOnFinishFunctionBody(Decl *Decl, Stmt *Body);
4180+
Decl *ActOnFinishFunctionBody(Decl *Decl, Stmt *Body, bool IsInstantiation);
41884181
Decl *ActOnSkippedFunctionBody(Decl *Decl);
41894182
void ActOnFinishInlineFunctionDef(FunctionDecl *D);
41904183

@@ -6881,23 +6874,23 @@ class Sema final : public SemaBase {
68816874
assert(!ExprEvalContexts.empty() &&
68826875
"Must be in an expression evaluation context");
68836876
return ExprEvalContexts.back();
6884-
}
6877+
};
68856878

68866879
ExpressionEvaluationContextRecord &currentEvaluationContext() {
68876880
assert(!ExprEvalContexts.empty() &&
68886881
"Must be in an expression evaluation context");
68896882
return ExprEvalContexts.back();
6890-
}
6883+
};
68916884

68926885
ExpressionEvaluationContextRecord &parentEvaluationContext() {
68936886
assert(ExprEvalContexts.size() >= 2 &&
68946887
"Must be in an expression evaluation context");
68956888
return ExprEvalContexts[ExprEvalContexts.size() - 2];
6896-
}
6889+
};
68976890

68986891
const ExpressionEvaluationContextRecord &parentEvaluationContext() const {
68996892
return const_cast<Sema *>(this)->parentEvaluationContext();
6900-
}
6893+
};
69016894

69026895
bool isAttrContext() const {
69036896
return ExprEvalContexts.back().ExprContext ==
@@ -9147,7 +9140,8 @@ class Sema final : public SemaBase {
91479140

91489141
/// Complete a lambda-expression having processed and attached the
91499142
/// lambda body.
9150-
ExprResult BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc);
9143+
ExprResult BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc,
9144+
sema::LambdaScopeInfo *LSI);
91519145

91529146
/// Get the return type to use for a lambda's conversion function(s) to
91539147
/// function pointer type, given the type of the call operator.

clang/lib/Sema/SemaDecl.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16228,6 +16228,10 @@ Decl *Sema::ActOnSkippedFunctionBody(Decl *Decl) {
1622816228
return Decl;
1622916229
}
1623016230

16231+
Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) {
16232+
return ActOnFinishFunctionBody(D, BodyArg, /*IsInstantiation=*/false);
16233+
}
16234+
1623116235
/// RAII object that pops an ExpressionEvaluationContext when exiting a function
1623216236
/// body.
1623316237
class ExitFunctionBodyRAII {
@@ -16298,8 +16302,8 @@ void Sema::CheckCoroutineWrapper(FunctionDecl *FD) {
1629816302
Diag(FD->getLocation(), diag::err_coroutine_return_type) << RD;
1629916303
}
1630016304

16301-
Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, bool IsInstantiation,
16302-
bool RetainFunctionScopeInfo) {
16305+
Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
16306+
bool IsInstantiation) {
1630316307
FunctionScopeInfo *FSI = getCurFunction();
1630416308
FunctionDecl *FD = dcl ? dcl->getAsFunction() : nullptr;
1630516309

@@ -16756,8 +16760,7 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, bool IsInstantiation,
1675616760
if (!IsInstantiation)
1675716761
PopDeclContext();
1675816762

16759-
if (!RetainFunctionScopeInfo)
16760-
PopFunctionScopeInfo(ActivePolicy, dcl);
16763+
PopFunctionScopeInfo(ActivePolicy, dcl);
1676116764
// If any errors have occurred, clear out any temporaries that may have
1676216765
// been leftover. This ensures that these temporaries won't be picked up for
1676316766
// deletion in some later function.

clang/lib/Sema/SemaLambda.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,15 +1968,14 @@ ExprResult Sema::BuildCaptureInit(const Capture &Cap,
19681968
}
19691969

19701970
ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body) {
1971-
LambdaScopeInfo &LSI = *cast<LambdaScopeInfo>(FunctionScopes.back());
1971+
LambdaScopeInfo LSI = *cast<LambdaScopeInfo>(FunctionScopes.back());
19721972

19731973
if (LSI.CallOperator->hasAttr<SYCLKernelEntryPointAttr>())
19741974
SYCL().CheckSYCLEntryPointFunctionDecl(LSI.CallOperator);
19751975

1976-
ActOnFinishFunctionBody(LSI.CallOperator, Body, /*IsInstantiation=*/false,
1977-
/*RetainFunctionScopeInfo=*/true);
1976+
ActOnFinishFunctionBody(LSI.CallOperator, Body);
19781977

1979-
return BuildLambdaExpr(StartLoc, Body->getEndLoc());
1978+
return BuildLambdaExpr(StartLoc, Body->getEndLoc(), &LSI);
19801979
}
19811980

19821981
static LambdaCaptureDefault
@@ -2133,9 +2132,8 @@ ConstructFixItRangeForUnusedCapture(Sema &S, SourceRange CaptureRange,
21332132
return SourceRange(FixItStart, FixItEnd);
21342133
}
21352134

2136-
ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc,
2137-
SourceLocation EndLoc) {
2138-
LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(FunctionScopes.back());
2135+
ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc,
2136+
LambdaScopeInfo *LSI) {
21392137
// Collect information from the lambda scope.
21402138
SmallVector<LambdaCapture, 4> Captures;
21412139
SmallVector<Expr *, 4> CaptureInits;
@@ -2172,12 +2170,6 @@ ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc,
21722170

21732171
PopExpressionEvaluationContext();
21742172

2175-
sema::AnalysisBasedWarnings::Policy WP =
2176-
AnalysisWarnings.getPolicyInEffectAt(EndLoc);
2177-
// We cannot release LSI until we finish computing captures, which
2178-
// requires the scope to be popped.
2179-
PoppedFunctionScopePtr _ = PopFunctionScopeInfo(&WP, LSI->CallOperator);
2180-
21812173
// True if the current capture has a used capture or default before it.
21822174
bool CurHasPreviousCapture = CaptureDefault != LCD_None;
21832175
SourceLocation PrevCaptureLoc = CurHasPreviousCapture ?

clang/lib/Sema/TreeTransform.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4051,7 +4051,7 @@ class TreeTransform {
40514051
PVD->getUninstantiatedDefaultArg()
40524052
->containsUnexpandedParameterPack();
40534053
}
4054-
return getSema().BuildLambdaExpr(StartLoc, EndLoc);
4054+
return getSema().BuildLambdaExpr(StartLoc, EndLoc, LSI);
40554055
}
40564056

40574057
/// Build an empty C++1z fold-expression with the given operator.
@@ -15712,9 +15712,12 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
1571215712
return ExprError();
1571315713
}
1571415714

15715+
// Copy the LSI before ActOnFinishFunctionBody removes it.
15716+
// FIXME: This is dumb. Store the lambda information somewhere that outlives
15717+
// the call operator.
15718+
auto LSICopy = *LSI;
1571515719
getSema().ActOnFinishFunctionBody(NewCallOperator, Body.get(),
15716-
/*IsInstantiation=*/true,
15717-
/*RetainFunctionScopeInfo=*/true);
15720+
/*IsInstantiation*/ true);
1571815721
SavedContext.pop();
1571915722

1572015723
// Recompute the dependency of the lambda so that we can defer the lambda call
@@ -15750,11 +15753,11 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
1575015753
// *after* the substitution in case we can't decide the dependency
1575115754
// so early, e.g. because we want to see if any of the *substituted*
1575215755
// parameters are dependent.
15753-
DependencyKind = getDerived().ComputeLambdaDependency(LSI);
15756+
DependencyKind = getDerived().ComputeLambdaDependency(&LSICopy);
1575415757
Class->setLambdaDependencyKind(DependencyKind);
1575515758

1575615759
return getDerived().RebuildLambdaExpr(E->getBeginLoc(),
15757-
Body.get()->getEndLoc(), LSI);
15760+
Body.get()->getEndLoc(), &LSICopy);
1575815761
}
1575915762

1576015763
template<typename Derived>

clang/test/SemaCXX/cxx2b-consteval-propagate.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -610,19 +610,3 @@ namespace GH135281 {
610610
void (*ff)() = f2<B>; // expected-note {{instantiation of function template specialization}}
611611
}
612612
#endif
613-
614-
namespace GH145776 {
615-
616-
void runtime_only() {}
617-
consteval void comptime_only() {}
618-
619-
void fn() {
620-
[]() {
621-
runtime_only();
622-
[]() {
623-
&comptime_only;
624-
}();
625-
}();
626-
}
627-
628-
}

0 commit comments

Comments
 (0)