[clang] [clang][ExprConstant] Bail out on invalid lambda capture inits (PR #138832)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed May 7 02:05:13 PDT 2025


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/138832

Fixes https://github.com/llvm/llvm-project/issues/138824

>From 8534038ad80d5de9218f8f9663bef16cbcabd19c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 7 May 2025 11:03:28 +0200
Subject: [PATCH] [clang][ExprConstant] Bail out on invalid lambda capture
 inits

Fixes https://github.com/llvm/llvm-project/issues/138824
---
 clang/lib/AST/ByteCode/Compiler.cpp              | 4 ++++
 clang/lib/AST/ExprConstant.cpp                   | 2 +-
 clang/test/SemaCXX/constant-expression-cxx11.cpp | 9 +++++++++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index ae6574cf99159..32ec917effdc7 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2932,6 +2932,10 @@ bool Compiler<Emitter>::VisitLambdaExpr(const LambdaExpr *E) {
   // record with their initializers.
   for (const Record::Field &F : R->fields()) {
     const Expr *Init = *CaptureInitIt;
+
+    if (Init->containsErrors())
+      return false;
+
     ++CaptureInitIt;
 
     if (!Init)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5950f461e4b2..500d43accb082 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -11038,7 +11038,7 @@ bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {
 
     // If there is no initializer, either this is a VLA or an error has
     // occurred.
-    if (!CurFieldInit)
+    if (!CurFieldInit || CurFieldInit->containsErrors())
       return Error(E);
 
     LValue Subobject = This;
diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp
index dc8f4bf1666ee..0a135654fab18 100644
--- a/clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -2598,3 +2598,12 @@ void foo() {
   constexpr S s[2] = { }; // expected-error {{constexpr variable 's' must be initialized by a constant expression}}
 }
 }
+
+namespace DoubleCapture {
+  int DC() {
+  int a = 1000;
+    static auto f =
+      [a, &a] { // expected-error {{'a' can appear only once in a capture list}}
+    };
+  }
+}



More information about the cfe-commits mailing list