@@ -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,
0 commit comments