[clang] [Clang] correct error message when assigning to const reference captured in lambda (PR #105647)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 22 05:14:20 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: None (nfrmtk)
<details>
<summary>Changes</summary>
Fixes:#<!-- -->98772
---
Full diff: https://github.com/llvm/llvm-project/pull/105647.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaExpr.cpp (+2)
- (modified) clang/test/SemaCXX/lambda-expressions.cpp (+5)
``````````diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index c67183df335dd5..9c256fd1ed1a41 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -13140,6 +13140,8 @@ static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) {
VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl());
if (!var) return NCCK_None;
if (var->getType().isConstQualified()) return NCCK_None;
+ if (var->getType()->isReferenceType())
+ return NCCK_None;
assert(var->hasLocalStorage() && "capture added 'const' to non-local?");
// Decide whether the first capture was for a block or a lambda.
diff --git a/clang/test/SemaCXX/lambda-expressions.cpp b/clang/test/SemaCXX/lambda-expressions.cpp
index acf8d014a9896b..612c6db950f273 100644
--- a/clang/test/SemaCXX/lambda-expressions.cpp
+++ b/clang/test/SemaCXX/lambda-expressions.cpp
@@ -189,6 +189,11 @@ namespace ModifyingCapture {
[=] {
n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
};
+ const int cn = 0;
+ // cxx03-cxx11-warning at +1 {{initialized lambda captures are a C++14 extension}}
+ [&cnr = cn]{ // expected-note {{variable 'cnr' declared const here}}
+ cnr = 1; // expected-error {{cannot assign to variable 'cnr' with const-qualified type 'const int &'}}
+ };
}
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/105647
More information about the cfe-commits
mailing list