[PATCH] D158433: [Clang] Do not change the type of captured vars when checking lambda constraints

2023-08-24 Thread Corentin Jabot via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG158f4f30adb4: [Clang] Do not change the type of captured 
vars when checking lambda constraints (authored by cor3ntin).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158433/new/

https://reviews.llvm.org/D158433

Files:
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaConcept.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaCXX/lambda-capture-type-deduction.cpp

Index: clang/test/SemaCXX/lambda-capture-type-deduction.cpp
===
--- clang/test/SemaCXX/lambda-capture-type-deduction.cpp
+++ clang/test/SemaCXX/lambda-capture-type-deduction.cpp
@@ -246,3 +246,17 @@
 static_assert(is_same);
   };
 }
+
+namespace GH61267 {
+template  concept C = true;
+
+template
+void f(int) {
+  int i;
+  [i](P) {}(0);
+  i = 4;
+}
+
+void test() { f(0);  }
+
+}
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -19722,13 +19722,6 @@
 FunctionScopesIndex == MaxFunctionScopesIndex && VarDC == DC)
   return true;
 
-// When evaluating some attributes (like enable_if) we might refer to a
-// function parameter appertaining to the same declaration as that
-// attribute.
-if (const auto *Parm = dyn_cast(Var);
-Parm && Parm->getDeclContext() == DC)
-  return true;
-
 // Only block literals, captured statements, and lambda expressions can
 // capture; other scopes don't work.
 DeclContext *ParentDC =
@@ -19756,6 +19749,14 @@
   CSI->getCapture(Var).markUsed(BuildAndDiagnose);
   break;
 }
+
+// When evaluating some attributes (like enable_if) we might refer to a
+// function parameter appertaining to the same declaration as that
+// attribute.
+if (const auto *Parm = dyn_cast(Var);
+Parm && Parm->getDeclContext() == DC)
+  return true;
+
 // If we are instantiating a generic lambda call operator body,
 // we do not want to capture new variables.  What was captured
 // during either a lambdas transformation or initial parsing
Index: clang/lib/Sema/SemaDecl.cpp
===
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -15289,11 +15289,10 @@
   FD->setInvalidDecl();
 }
 
-static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator,
-   Sema ) {
-  CXXRecordDecl *const LambdaClass = CallOperator->getParent();
+LambdaScopeInfo *Sema::RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator) {
+  CXXRecordDecl *LambdaClass = CallOperator->getParent();
 
-  LambdaScopeInfo *LSI = S.PushLambdaScope();
+  LambdaScopeInfo *LSI = PushLambdaScope();
   LSI->CallOperator = CallOperator;
   LSI->Lambda = LambdaClass;
   LSI->ReturnType = CallOperator->getReturnType();
@@ -15317,7 +15316,7 @@
 if (C.capturesVariable()) {
   ValueDecl *VD = C.getCapturedVar();
   if (VD->isInitCapture())
-S.CurrentInstantiationScope->InstantiatedLocal(VD, VD);
+CurrentInstantiationScope->InstantiatedLocal(VD, VD);
   const bool ByRef = C.getCaptureKind() == LCK_ByRef;
   LSI->addCapture(VD, /*IsBlock*/false, ByRef,
   /*RefersToEnclosingVariableOrCapture*/true, C.getLocation(),
@@ -15334,6 +15333,7 @@
 }
 ++I;
   }
+  return LSI;
 }
 
 Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
@@ -15437,7 +15437,7 @@
 assert(inTemplateInstantiation() &&
"There should be an active template instantiation on the stack "
"when instantiating a generic lambda!");
-RebuildLambdaScopeInfo(cast(D), *this);
+RebuildLambdaScopeInfo(cast(D));
   } else {
 // Enter a new function scope
 PushFunctionScope();
Index: clang/lib/Sema/SemaConcept.cpp
===
--- clang/lib/Sema/SemaConcept.cpp
+++ clang/lib/Sema/SemaConcept.cpp
@@ -13,12 +13,14 @@
 #include "clang/Sema/SemaConcept.h"
 #include "TreeTransform.h"
 #include "clang/AST/ASTLambda.h"
+#include "clang/AST/DeclCXX.h"
 #include "clang/AST/ExprConcepts.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Basic/OperatorPrecedence.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
 #include "clang/Sema/Initialization.h"
 #include "clang/Sema/Overload.h"
+#include "clang/Sema/ScopeInfo.h"
 #include "clang/Sema/Sema.h"
 #include "clang/Sema/SemaDiagnostic.h"
 #include "clang/Sema/SemaInternal.h"
@@ -540,11 +542,6 @@
   auto AddSingleCapture = [&](const ValueDecl *CapturedPattern,
   unsigned Index) {
 ValueDecl *CapturedVar = LambdaClass->getCapture(Index)->getCapturedVar();
-if 

[PATCH] D158433: [Clang] Do not change the type of captured vars when checking lambda constraints

2023-08-24 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman accepted this revision.
aaron.ballman added a comment.
This revision is now accepted and ready to land.

LGTM!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158433/new/

https://reviews.llvm.org/D158433

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158433: [Clang] Do not change the type of captured vars when checking lambda constraints

2023-08-24 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin updated this revision to Diff 553047.
cor3ntin added a comment.

Missing curlies


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158433/new/

https://reviews.llvm.org/D158433

Files:
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaConcept.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaCXX/lambda-capture-type-deduction.cpp

Index: clang/test/SemaCXX/lambda-capture-type-deduction.cpp
===
--- clang/test/SemaCXX/lambda-capture-type-deduction.cpp
+++ clang/test/SemaCXX/lambda-capture-type-deduction.cpp
@@ -246,3 +246,17 @@
 static_assert(is_same);
   };
 }
+
+namespace GH61267 {
+template  concept C = true;
+
+template
+void f(int) {
+  int i;
+  [i](P) {}(0);
+  i = 4;
+}
+
+void test() { f(0);  }
+
+}
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -19723,13 +19723,6 @@
 FunctionScopesIndex == MaxFunctionScopesIndex && VarDC == DC)
   return true;
 
-// When evaluating some attributes (like enable_if) we might refer to a
-// function parameter appertaining to the same declaration as that
-// attribute.
-if (const auto *Parm = dyn_cast(Var);
-Parm && Parm->getDeclContext() == DC)
-  return true;
-
 // Only block literals, captured statements, and lambda expressions can
 // capture; other scopes don't work.
 DeclContext *ParentDC =
@@ -19757,6 +19750,14 @@
   CSI->getCapture(Var).markUsed(BuildAndDiagnose);
   break;
 }
+
+// When evaluating some attributes (like enable_if) we might refer to a
+// function parameter appertaining to the same declaration as that
+// attribute.
+if (const auto *Parm = dyn_cast(Var);
+Parm && Parm->getDeclContext() == DC)
+  return true;
+
 // If we are instantiating a generic lambda call operator body,
 // we do not want to capture new variables.  What was captured
 // during either a lambdas transformation or initial parsing
Index: clang/lib/Sema/SemaDecl.cpp
===
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -15289,11 +15289,10 @@
   FD->setInvalidDecl();
 }
 
-static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator,
-   Sema ) {
-  CXXRecordDecl *const LambdaClass = CallOperator->getParent();
+LambdaScopeInfo *Sema::RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator) {
+  CXXRecordDecl *LambdaClass = CallOperator->getParent();
 
-  LambdaScopeInfo *LSI = S.PushLambdaScope();
+  LambdaScopeInfo *LSI = PushLambdaScope();
   LSI->CallOperator = CallOperator;
   LSI->Lambda = LambdaClass;
   LSI->ReturnType = CallOperator->getReturnType();
@@ -15317,7 +15316,7 @@
 if (C.capturesVariable()) {
   ValueDecl *VD = C.getCapturedVar();
   if (VD->isInitCapture())
-S.CurrentInstantiationScope->InstantiatedLocal(VD, VD);
+CurrentInstantiationScope->InstantiatedLocal(VD, VD);
   const bool ByRef = C.getCaptureKind() == LCK_ByRef;
   LSI->addCapture(VD, /*IsBlock*/false, ByRef,
   /*RefersToEnclosingVariableOrCapture*/true, C.getLocation(),
@@ -15334,6 +15333,7 @@
 }
 ++I;
   }
+  return LSI;
 }
 
 Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
@@ -15437,7 +15437,7 @@
 assert(inTemplateInstantiation() &&
"There should be an active template instantiation on the stack "
"when instantiating a generic lambda!");
-RebuildLambdaScopeInfo(cast(D), *this);
+RebuildLambdaScopeInfo(cast(D));
   } else {
 // Enter a new function scope
 PushFunctionScope();
Index: clang/lib/Sema/SemaConcept.cpp
===
--- clang/lib/Sema/SemaConcept.cpp
+++ clang/lib/Sema/SemaConcept.cpp
@@ -13,12 +13,14 @@
 #include "clang/Sema/SemaConcept.h"
 #include "TreeTransform.h"
 #include "clang/AST/ASTLambda.h"
+#include "clang/AST/DeclCXX.h"
 #include "clang/AST/ExprConcepts.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Basic/OperatorPrecedence.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
 #include "clang/Sema/Initialization.h"
 #include "clang/Sema/Overload.h"
+#include "clang/Sema/ScopeInfo.h"
 #include "clang/Sema/Sema.h"
 #include "clang/Sema/SemaDiagnostic.h"
 #include "clang/Sema/SemaInternal.h"
@@ -540,11 +542,6 @@
   auto AddSingleCapture = [&](const ValueDecl *CapturedPattern,
   unsigned Index) {
 ValueDecl *CapturedVar = LambdaClass->getCapture(Index)->getCapturedVar();
-if (cast(Function)->isConst()) {
-  QualType T = CapturedVar->getType();
-  T.addConst();
-  CapturedVar->setType(T);
-}
 if (CapturedVar->isInitCapture())
   

[PATCH] D158433: [Clang] Do not change the type of captured vars when checking lambda constraints

2023-08-23 Thread Erich Keane via Phabricator via cfe-commits
erichkeane added a comment.

I agree with this approach, and think we should backport this if at all 
possible.  The original solution is a bit problematic, and I don't believe this 
solution is particularly risky.




Comment at: clang/lib/Sema/SemaConcept.cpp:727
+  } else
+FuncScope.disable();
+

curleys required here.



Comment at: clang/lib/Sema/SemaConcept.cpp:922
+  } else
 FuncScope.disable();
 

here too



Comment at: clang/lib/Sema/SemaExpr.cpp:19754
+
+// When evaluating some attributes (like enable_if) we might refer to a
+// function parameter appertaining to the same declaration as that

cor3ntin wrote:
> shafik wrote:
> > Why move this block of code for?
> `isVariableAlreadyCapturedInScopeInfo` is kinda badly name because it will 
> adjust `DeclRefType` by adding `const` as necessary.
> 
> if we capture a parameter, and then use it in a concept - which are not 
> checked from within the scope of the lambda, we need to add const to it, 
> which we can only do by reordering these paths. It's a bit subtle, and could 
> do with some improvement as I'm not sure parameters will always be const 
> correct in attributes currently. 
> But I tried to do a minimal fix
> 
> 
> 
> 
Yeah, this function is unfortunately doing a lot of work and is a bit of a mess 
unfortunately.  I think I see the logic of the reorder, but its a sensitive 
enough function that a minimal fix is prudent.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158433/new/

https://reviews.llvm.org/D158433

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158433: [Clang] Do not change the type of captured vars when checking lambda constraints

2023-08-23 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin added a comment.

@aaron.ballman Should we land that? it already missed rc3...


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158433/new/

https://reviews.llvm.org/D158433

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158433: [Clang] Do not change the type of captured vars when checking lambda constraints

2023-08-22 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin updated this revision to Diff 552329.
cor3ntin added a comment.

Improve comment


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158433/new/

https://reviews.llvm.org/D158433

Files:
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaConcept.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaCXX/lambda-capture-type-deduction.cpp

Index: clang/test/SemaCXX/lambda-capture-type-deduction.cpp
===
--- clang/test/SemaCXX/lambda-capture-type-deduction.cpp
+++ clang/test/SemaCXX/lambda-capture-type-deduction.cpp
@@ -246,3 +246,17 @@
 static_assert(is_same);
   };
 }
+
+namespace GH61267 {
+template  concept C = true;
+
+template
+void f(int) {
+  int i;
+  [i](P) {}(0);
+  i = 4;
+}
+
+void test() { f(0);  }
+
+}
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -19723,13 +19723,6 @@
 FunctionScopesIndex == MaxFunctionScopesIndex && VarDC == DC)
   return true;
 
-// When evaluating some attributes (like enable_if) we might refer to a
-// function parameter appertaining to the same declaration as that
-// attribute.
-if (const auto *Parm = dyn_cast(Var);
-Parm && Parm->getDeclContext() == DC)
-  return true;
-
 // Only block literals, captured statements, and lambda expressions can
 // capture; other scopes don't work.
 DeclContext *ParentDC =
@@ -19757,6 +19750,14 @@
   CSI->getCapture(Var).markUsed(BuildAndDiagnose);
   break;
 }
+
+// When evaluating some attributes (like enable_if) we might refer to a
+// function parameter appertaining to the same declaration as that
+// attribute.
+if (const auto *Parm = dyn_cast(Var);
+Parm && Parm->getDeclContext() == DC)
+  return true;
+
 // If we are instantiating a generic lambda call operator body,
 // we do not want to capture new variables.  What was captured
 // during either a lambdas transformation or initial parsing
Index: clang/lib/Sema/SemaDecl.cpp
===
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -15289,11 +15289,10 @@
   FD->setInvalidDecl();
 }
 
-static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator,
-   Sema ) {
-  CXXRecordDecl *const LambdaClass = CallOperator->getParent();
+LambdaScopeInfo *Sema::RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator) {
+  CXXRecordDecl *LambdaClass = CallOperator->getParent();
 
-  LambdaScopeInfo *LSI = S.PushLambdaScope();
+  LambdaScopeInfo *LSI = PushLambdaScope();
   LSI->CallOperator = CallOperator;
   LSI->Lambda = LambdaClass;
   LSI->ReturnType = CallOperator->getReturnType();
@@ -15317,7 +15316,7 @@
 if (C.capturesVariable()) {
   ValueDecl *VD = C.getCapturedVar();
   if (VD->isInitCapture())
-S.CurrentInstantiationScope->InstantiatedLocal(VD, VD);
+CurrentInstantiationScope->InstantiatedLocal(VD, VD);
   const bool ByRef = C.getCaptureKind() == LCK_ByRef;
   LSI->addCapture(VD, /*IsBlock*/false, ByRef,
   /*RefersToEnclosingVariableOrCapture*/true, C.getLocation(),
@@ -15334,6 +15333,7 @@
 }
 ++I;
   }
+  return LSI;
 }
 
 Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
@@ -15437,7 +15437,7 @@
 assert(inTemplateInstantiation() &&
"There should be an active template instantiation on the stack "
"when instantiating a generic lambda!");
-RebuildLambdaScopeInfo(cast(D), *this);
+RebuildLambdaScopeInfo(cast(D));
   } else {
 // Enter a new function scope
 PushFunctionScope();
Index: clang/lib/Sema/SemaConcept.cpp
===
--- clang/lib/Sema/SemaConcept.cpp
+++ clang/lib/Sema/SemaConcept.cpp
@@ -13,12 +13,14 @@
 #include "clang/Sema/SemaConcept.h"
 #include "TreeTransform.h"
 #include "clang/AST/ASTLambda.h"
+#include "clang/AST/DeclCXX.h"
 #include "clang/AST/ExprConcepts.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Basic/OperatorPrecedence.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
 #include "clang/Sema/Initialization.h"
 #include "clang/Sema/Overload.h"
+#include "clang/Sema/ScopeInfo.h"
 #include "clang/Sema/Sema.h"
 #include "clang/Sema/SemaDiagnostic.h"
 #include "clang/Sema/SemaInternal.h"
@@ -540,11 +542,6 @@
   auto AddSingleCapture = [&](const ValueDecl *CapturedPattern,
   unsigned Index) {
 ValueDecl *CapturedVar = LambdaClass->getCapture(Index)->getCapturedVar();
-if (cast(Function)->isConst()) {
-  QualType T = CapturedVar->getType();
-  T.addConst();
-  CapturedVar->setType(T);
-}
 if (CapturedVar->isInitCapture())
   

[PATCH] D158433: [Clang] Do not change the type of captured vars when checking lambda constraints

2023-08-22 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin added inline comments.



Comment at: clang/lib/Sema/SemaConcept.cpp:722
+const_cast(cast(FD)));
+// Captures are not checked from within the lambda.
+LSI->AfterParameterList = false;

shafik wrote:
> This comment does not really explain to me the effect of 
> `LSI->AfterParameterList = false;`
`AfterParameterList` affects in which context we look for captured variables. I 
will try to think of a better comment.



Comment at: clang/lib/Sema/SemaExpr.cpp:19754
+
+// When evaluating some attributes (like enable_if) we might refer to a
+// function parameter appertaining to the same declaration as that

shafik wrote:
> Why move this block of code for?
`isVariableAlreadyCapturedInScopeInfo` is kinda badly name because it will 
adjust `DeclRefType` by adding `const` as necessary.

if we capture a parameter, and then use it in a concept - which are not checked 
from within the scope of the lambda, we need to add const to it, which we can 
only do by reordering these paths. It's a bit subtle, and could do with some 
improvement as I'm not sure parameters will always be const correct in 
attributes currently. 
But I tried to do a minimal fix






Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158433/new/

https://reviews.llvm.org/D158433

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158433: [Clang] Do not change the type of captured vars when checking lambda constraints

2023-08-21 Thread Shafik Yaghmour via Phabricator via cfe-commits
shafik added inline comments.



Comment at: clang/lib/Sema/SemaConcept.cpp:722
+const_cast(cast(FD)));
+// Captures are not checked from within the lambda.
+LSI->AfterParameterList = false;

This comment does not really explain to me the effect of 
`LSI->AfterParameterList = false;`



Comment at: clang/lib/Sema/SemaExpr.cpp:19754
+
+// When evaluating some attributes (like enable_if) we might refer to a
+// function parameter appertaining to the same declaration as that

Why move this block of code for?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158433/new/

https://reviews.llvm.org/D158433

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158433: [Clang] Do not change the type of captured vars when checking lambda constraints

2023-08-21 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added reviewers: erichkeane, hubert.reinterpretcast, 
clang-language-wg.
aaron.ballman added a comment.

Adding a few more reviewers for extra sets of eyes given that we'd like this to 
land in 17.x. The changes look correct to me, but a confirmation would be 
appreciated.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158433/new/

https://reviews.llvm.org/D158433

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158433: [Clang] Do not change the type of captured vars when checking lambda constraints

2023-08-21 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin created this revision.
Herald added a subscriber: ChuanqiXu.
Herald added a project: All.
cor3ntin requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When checking the constraint of a lambda, we need to respect the constness
of the call operator when establishing the type of capture variables.

In D124351 , this was done by adding const to 
the captured variable...
However, that would change the type of the variable outside of the scope
of the lambda, which is clearly not the desired outcome.

Instead, to ensure const-correctness, we need to populate
a LambdaScopeInfo with the capture variables before checking the
constraints of a generic lambda.

There is no changelog as I'd like to tentatively propose we backport
this change to RC3 as it is a regression introduced in the Clang 17
cycle.

Fixes #61267


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158433

Files:
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaConcept.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaCXX/lambda-capture-type-deduction.cpp

Index: clang/test/SemaCXX/lambda-capture-type-deduction.cpp
===
--- clang/test/SemaCXX/lambda-capture-type-deduction.cpp
+++ clang/test/SemaCXX/lambda-capture-type-deduction.cpp
@@ -246,3 +246,17 @@
 static_assert(is_same);
   };
 }
+
+namespace GH61267 {
+template  concept C = true;
+
+template
+void f(int) {
+  int i;
+  [i](P) {}(0);
+  i = 4;
+}
+
+void test() { f(0);  }
+
+}
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -19723,13 +19723,6 @@
 FunctionScopesIndex == MaxFunctionScopesIndex && VarDC == DC)
   return true;
 
-// When evaluating some attributes (like enable_if) we might refer to a
-// function parameter appertaining to the same declaration as that
-// attribute.
-if (const auto *Parm = dyn_cast(Var);
-Parm && Parm->getDeclContext() == DC)
-  return true;
-
 // Only block literals, captured statements, and lambda expressions can
 // capture; other scopes don't work.
 DeclContext *ParentDC =
@@ -19757,6 +19750,14 @@
   CSI->getCapture(Var).markUsed(BuildAndDiagnose);
   break;
 }
+
+// When evaluating some attributes (like enable_if) we might refer to a
+// function parameter appertaining to the same declaration as that
+// attribute.
+if (const auto *Parm = dyn_cast(Var);
+Parm && Parm->getDeclContext() == DC)
+  return true;
+
 // If we are instantiating a generic lambda call operator body,
 // we do not want to capture new variables.  What was captured
 // during either a lambdas transformation or initial parsing
Index: clang/lib/Sema/SemaDecl.cpp
===
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -15289,11 +15289,10 @@
   FD->setInvalidDecl();
 }
 
-static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator,
-   Sema ) {
-  CXXRecordDecl *const LambdaClass = CallOperator->getParent();
+LambdaScopeInfo *Sema::RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator) {
+  CXXRecordDecl *LambdaClass = CallOperator->getParent();
 
-  LambdaScopeInfo *LSI = S.PushLambdaScope();
+  LambdaScopeInfo *LSI = PushLambdaScope();
   LSI->CallOperator = CallOperator;
   LSI->Lambda = LambdaClass;
   LSI->ReturnType = CallOperator->getReturnType();
@@ -15317,7 +15316,7 @@
 if (C.capturesVariable()) {
   ValueDecl *VD = C.getCapturedVar();
   if (VD->isInitCapture())
-S.CurrentInstantiationScope->InstantiatedLocal(VD, VD);
+CurrentInstantiationScope->InstantiatedLocal(VD, VD);
   const bool ByRef = C.getCaptureKind() == LCK_ByRef;
   LSI->addCapture(VD, /*IsBlock*/false, ByRef,
   /*RefersToEnclosingVariableOrCapture*/true, C.getLocation(),
@@ -15334,6 +15333,7 @@
 }
 ++I;
   }
+  return LSI;
 }
 
 Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
@@ -15437,7 +15437,7 @@
 assert(inTemplateInstantiation() &&
"There should be an active template instantiation on the stack "
"when instantiating a generic lambda!");
-RebuildLambdaScopeInfo(cast(D), *this);
+RebuildLambdaScopeInfo(cast(D));
   } else {
 // Enter a new function scope
 PushFunctionScope();
Index: clang/lib/Sema/SemaConcept.cpp
===
--- clang/lib/Sema/SemaConcept.cpp
+++ clang/lib/Sema/SemaConcept.cpp
@@ -13,12 +13,14 @@
 #include "clang/Sema/SemaConcept.h"
 #include "TreeTransform.h"
 #include "clang/AST/ASTLambda.h"
+#include "clang/AST/DeclCXX.h"
 #include "clang/AST/ExprConcepts.h"
 #include