Skip to content

Commit 0925798

Browse files
committed
[Clang] correct error message when assigning to const reference captured in lambda
1 parent ffddf33 commit 0925798

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13521,6 +13521,8 @@ static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) {
1352113521
VarDecl *Var = dyn_cast<VarDecl>(Value);
1352213522
if (!Var)
1352313523
return NCCK_None;
13524+
if (Var->getType()->isReferenceType())
13525+
return NCCK_None;
1352413526

1352513527
assert(Var->hasLocalStorage() && "capture added 'const' to non-local?");
1352613528

clang/test/SemaCXX/lambda-expressions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ namespace ModifyingCapture {
194194
[=] {
195195
n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
196196
};
197+
const int cn = 0;
198+
// cxx03-cxx11-warning@+1 {{initialized lambda captures are a C++14 extension}}
199+
[&cnr = cn]{ // expected-note {{variable 'cnr' declared const here}}
200+
cnr = 1; // expected-error {{cannot assign to variable 'cnr' with const-qualified type 'const int &'}}
201+
};
197202
}
198203
}
199204

0 commit comments

Comments
 (0)