[clang] [Clang] Prevent assignment to captured structured bindings inside immutable lambda (PR #120849)

via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 23 08:49:45 PST 2024


================
@@ -13299,7 +13293,18 @@ static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) {
 
   // The declaration must be a variable which is not declared 'const'.
   VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl());
-  if (!var) return NCCK_None;
+  if (!var) {
+    // Bindings also can be captured by lambda in C++
+    BindingDecl *binding = dyn_cast<BindingDecl>(DRE->getDecl());
+    if (!binding || binding->getType().isConstQualified())
+      return NCCK_None;
+
+    assert(S.getLangOpts().CPlusPlus && "BindingDecl outside of C++?");
+    assert(!isa<BlockDecl>(binding->getDeclContext()));
----------------
cor3ntin wrote:

I would prefer you move the whole code _before_ the cast to Var (L13293)
Alternatively, first cast to ValueDecl, deal with the common const-qualified case, then cast to Var

https://github.com/llvm/llvm-project/pull/120849


More information about the cfe-commits mailing list