[clang] 9a521e2 - [clang][Interp] Fix primitive lambda capture defaults

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Mon May 6 01:37:44 PDT 2024


Author: Timm Bäder
Date: 2024-05-06T10:37:30+02:00
New Revision: 9a521e274d0ad4a4a461952d23809320e080ffb4

URL: https://github.com/llvm/llvm-project/commit/9a521e274d0ad4a4a461952d23809320e080ffb4
DIFF: https://github.com/llvm/llvm-project/commit/9a521e274d0ad4a4a461952d23809320e080ffb4.diff

LOG: [clang][Interp] Fix primitive lambda capture defaults

We need to use InitField here, not SetField.

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/lambda.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index b9318fbd9ae9e6..630fdb60c35182 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -2025,7 +2025,7 @@ bool ByteCodeExprGen<Emitter>::VisitLambdaExpr(const LambdaExpr *E) {
       if (!this->visit(Init))
         return false;
 
-      if (!this->emitSetField(*T, F.Offset, E))
+      if (!this->emitInitField(*T, F.Offset, E))
         return false;
     } else {
       if (!this->emitDupPtr(E))

diff  --git a/clang/test/AST/Interp/lambda.cpp b/clang/test/AST/Interp/lambda.cpp
index d056bb304eeb30..77e035ce254703 100644
--- a/clang/test/AST/Interp/lambda.cpp
+++ b/clang/test/AST/Interp/lambda.cpp
@@ -248,3 +248,19 @@ namespace ns2_capture_this_byval {
   constexpr auto L = S{5}.f(S{10});
   static_assert(L(S{100}) == 115, "");
 } // end test_captures_1::ns2_capture_this_byval
+
+namespace CaptureDefaults {
+  struct S {
+    int x;
+  };
+
+  constexpr auto f = [x = S{10}]() {
+      return x.x;
+  };
+  static_assert(f() == 10, "");
+
+  constexpr auto f2 = [x = 3]() {
+      return x;
+  };
+  static_assert(f2() == 3, "");
+}


        


More information about the cfe-commits mailing list