[clang] 53925e3 - [clang][Interp] Fix primitive MoveFn (#101165)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 30 04:27:01 PDT 2024


Author: Timm Baeder
Date: 2024-07-30T13:26:57+02:00
New Revision: 53925e33c5943bee7cf2fe29ca1274a42a5d5687

URL: https://github.com/llvm/llvm-project/commit/53925e33c5943bee7cf2fe29ca1274a42a5d5687
DIFF: https://github.com/llvm/llvm-project/commit/53925e33c5943bee7cf2fe29ca1274a42a5d5687.diff

LOG: [clang][Interp] Fix primitive MoveFn (#101165)

Declaring the SrcPtr as const will cause us later to call the copy ctor
instead of the move ctor.

Added: 
    

Modified: 
    clang/lib/AST/Interp/Descriptor.cpp
    clang/test/AST/Interp/lifetimes.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Descriptor.cpp b/clang/lib/AST/Interp/Descriptor.cpp
index 4f7e9eac76a32..671f2c03d7e5c 100644
--- a/clang/lib/AST/Interp/Descriptor.cpp
+++ b/clang/lib/AST/Interp/Descriptor.cpp
@@ -33,7 +33,8 @@ static void dtorTy(Block *, std::byte *Ptr, const Descriptor *) {
 template <typename T>
 static void moveTy(Block *, const std::byte *Src, std::byte *Dst,
                    const Descriptor *) {
-  const auto *SrcPtr = reinterpret_cast<const T *>(Src);
+  // FIXME: Get rid of the const_cast.
+  auto *SrcPtr = reinterpret_cast<T *>(const_cast<std::byte *>(Src));
   auto *DstPtr = reinterpret_cast<T *>(Dst);
   new (DstPtr) T(std::move(*SrcPtr));
 }

diff  --git a/clang/test/AST/Interp/lifetimes.cpp b/clang/test/AST/Interp/lifetimes.cpp
index d47533ab547b3..9fca54fe11120 100644
--- a/clang/test/AST/Interp/lifetimes.cpp
+++ b/clang/test/AST/Interp/lifetimes.cpp
@@ -33,3 +33,30 @@ struct S {
 constexpr int k1 = S().t; // both-error {{must be initialized by a constant expression}} \
                           // ref-note {{in call to}} \
                           // expected-note {{in call to}}
+
+
+namespace MoveFnWorks {
+  template<typename T> constexpr T &&ref(T &&t) { return (T&&)t; }
+
+  struct Buf {};
+
+  struct A {
+    constexpr A(Buf &buf) : buf(buf) { }
+    Buf &buf;
+  };
+
+  constexpr bool dtor_calls_dtor() {
+    struct B {
+      A &&d;
+      constexpr B(Buf &buf) : d(ref(A(buf))) {}
+    };
+
+    Buf buf;
+    {
+      B b(buf);
+    }
+
+    return true;
+  }
+  static_assert(dtor_calls_dtor(), "");
+}


        


More information about the cfe-commits mailing list