1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
commit 158f4f30adb4bfd390057742a32934e4344e8fd3
Author: Corentin Jabot <corentinjabot@gmail.com>
Date: Mon Aug 21 18:07:43 2023 +0200
[Clang] Do not change the type of captured vars when checking lambda constraints
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
Reviewed By: aaron.ballman, #clang-language-wg
Differential Revision: https://reviews.llvm.org/D158433
diff --git clang/include/clang/Sema/Sema.h clang/include/clang/Sema/Sema.h
index c992e8763057..807a52886ccb 100644
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -7343,6 +7343,8 @@ public:
CXXConversionDecl *Conv,
Expr *Src);
+ sema::LambdaScopeInfo *RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator);
+
/// Check whether the given expression is a valid constraint expression.
/// A diagnostic is emitted if it is not, false is returned, and
/// PossibleNonPrimary will be set to true if the failure might be due to a
diff --git clang/lib/Sema/SemaConcept.cpp clang/lib/Sema/SemaConcept.cpp
index f24b549dd2ef..fa3dadf68229 100644
--- 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 @@ bool Sema::addInstantiatedCapturesToScope(
auto AddSingleCapture = [&](const ValueDecl *CapturedPattern,
unsigned Index) {
ValueDecl *CapturedVar = LambdaClass->getCapture(Index)->getCapturedVar();
- if (cast<CXXMethodDecl>(Function)->isConst()) {
- QualType T = CapturedVar->getType();
- T.addConst();
- CapturedVar->setType(T);
- }
if (CapturedVar->isInitCapture())
Scope.InstantiatedLocal(CapturedPattern, CapturedVar);
};
@@ -714,6 +711,22 @@ bool Sema::CheckFunctionConstraints(const FunctionDecl *FD,
Record = const_cast<CXXRecordDecl *>(Method->getParent());
}
CXXThisScopeRAII ThisScope(*this, Record, ThisQuals, Record != nullptr);
+
+ // When checking the constraints of a lambda, we need to restore a
+ // LambdaScopeInfo populated with correct capture information so that the type
+ // of a variable referring to a capture is correctly const-adjusted.
+ FunctionScopeRAII FuncScope(*this);
+ if (isLambdaCallOperator(FD)) {
+ LambdaScopeInfo *LSI = RebuildLambdaScopeInfo(
+ const_cast<CXXMethodDecl *>(cast<CXXMethodDecl>(FD)));
+ // Constraints are checked from the parent context of the lambda, so we set
+ // AfterParameterList to false, so that `tryCaptureVariable` finds
+ // explicit captures in the appropriate context.
+ LSI->AfterParameterList = false;
+ } else {
+ FuncScope.disable();
+ }
+
return CheckConstraintSatisfaction(
FD, {FD->getTrailingRequiresClause()}, *MLTAL,
SourceRange(UsageLoc.isValid() ? UsageLoc : FD->getLocation()),
@@ -902,10 +915,13 @@ bool Sema::CheckInstantiatedFunctionTemplateConstraints(
}
CXXThisScopeRAII ThisScope(*this, Record, ThisQuals, Record != nullptr);
FunctionScopeRAII FuncScope(*this);
- if (isLambdaCallOperator(Decl))
- PushLambdaScope();
- else
+
+ if (isLambdaCallOperator(Decl)) {
+ LambdaScopeInfo *LSI = RebuildLambdaScopeInfo(cast<CXXMethodDecl>(Decl));
+ LSI->AfterParameterList = false;
+ } else {
FuncScope.disable();
+ }
llvm::SmallVector<Expr *, 1> Converted;
return CheckConstraintSatisfaction(Template, TemplateAC, Converted, *MLTAL,
diff --git clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaDecl.cpp
index 3925e2a7f338..0d5f696bf040 100644
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -15289,11 +15289,10 @@ Sema::CheckForFunctionRedefinition(FunctionDecl *FD,
FD->setInvalidDecl();
}
-static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator,
- Sema &S) {
- 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 @@ static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator,
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 @@ static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator,
}
++I;
}
+ return LSI;
}
Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
@@ -15437,7 +15437,7 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
assert(inTemplateInstantiation() &&
"There should be an active template instantiation on the stack "
"when instantiating a generic lambda!");
- RebuildLambdaScopeInfo(cast<CXXMethodDecl>(D), *this);
+ RebuildLambdaScopeInfo(cast<CXXMethodDecl>(D));
} else {
// Enter a new function scope
PushFunctionScope();
diff --git clang/lib/Sema/SemaExpr.cpp clang/lib/Sema/SemaExpr.cpp
index 34284a8d9381..ac6c3ba6b357 100644
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -19722,13 +19722,6 @@ bool Sema::tryCaptureVariable(
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<ParmVarDecl>(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 @@ bool Sema::tryCaptureVariable(
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<ParmVarDecl>(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
diff --git clang/test/SemaCXX/lambda-capture-type-deduction.cpp clang/test/SemaCXX/lambda-capture-type-deduction.cpp
index e524d3bc20ab..9855122c9627 100644
--- clang/test/SemaCXX/lambda-capture-type-deduction.cpp
+++ clang/test/SemaCXX/lambda-capture-type-deduction.cpp
@@ -246,3 +246,17 @@ void check_params_tpl() {
static_assert(is_same<int&, decltype((ap))>);
};
}
+
+namespace GH61267 {
+template <typename> concept C = true;
+
+template<typename>
+void f(int) {
+ int i;
+ [i]<C P>(P) {}(0);
+ i = 4;
+}
+
+void test() { f<int>(0); }
+
+}
|