Skip to content

Commit 78e3a58

Browse files
authored
[Clang] correct error message when assigning to const reference captured in lambda (#105647)
Fixes #98772
1 parent 4c91627 commit 78e3a58

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ Improvements to Clang's diagnostics
246246
potential misaligned members get processed before they can get discarded.
247247
(#GH144729)
248248

249+
- Clang now emits dignostic with correct message in case of assigning to const reference captured in lambda. (#GH105647)
250+
249251
- Fixed false positive in ``-Wmissing-noreturn`` diagnostic when it was requiring the usage of
250252
``[[noreturn]]`` on lambdas before C++23 (#GH154493).
251253

clang/lib/Sema/SemaExpr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13620,6 +13620,8 @@ static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) {
1362013620
VarDecl *Var = dyn_cast<VarDecl>(Value);
1362113621
if (!Var)
1362213622
return NCCK_None;
13623+
if (Var->getType()->isReferenceType())
13624+
return NCCK_None;
1362313625

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

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)