Skip to content

Commit 53925e3

Browse files
authored
[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.
1 parent 36c5753 commit 53925e3

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

clang/lib/AST/Interp/Descriptor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ static void dtorTy(Block *, std::byte *Ptr, const Descriptor *) {
3333
template <typename T>
3434
static void moveTy(Block *, const std::byte *Src, std::byte *Dst,
3535
const Descriptor *) {
36-
const auto *SrcPtr = reinterpret_cast<const T *>(Src);
36+
// FIXME: Get rid of the const_cast.
37+
auto *SrcPtr = reinterpret_cast<T *>(const_cast<std::byte *>(Src));
3738
auto *DstPtr = reinterpret_cast<T *>(Dst);
3839
new (DstPtr) T(std::move(*SrcPtr));
3940
}

clang/test/AST/Interp/lifetimes.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,30 @@ struct S {
3333
constexpr int k1 = S().t; // both-error {{must be initialized by a constant expression}} \
3434
// ref-note {{in call to}} \
3535
// expected-note {{in call to}}
36+
37+
38+
namespace MoveFnWorks {
39+
template<typename T> constexpr T &&ref(T &&t) { return (T&&)t; }
40+
41+
struct Buf {};
42+
43+
struct A {
44+
constexpr A(Buf &buf) : buf(buf) { }
45+
Buf &buf;
46+
};
47+
48+
constexpr bool dtor_calls_dtor() {
49+
struct B {
50+
A &&d;
51+
constexpr B(Buf &buf) : d(ref(A(buf))) {}
52+
};
53+
54+
Buf buf;
55+
{
56+
B b(buf);
57+
}
58+
59+
return true;
60+
}
61+
static_assert(dtor_calls_dtor(), "");
62+
}

0 commit comments

Comments
 (0)