Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Fix issue 22336 - betterC move of non zero structs
Browse files Browse the repository at this point in the history
Co-authored-by: Razvan Nitu <razvan.nitu1305@gmail.com>
  • Loading branch information
2 people authored and dlang-bot committed Sep 27, 2021
1 parent 9012401 commit c7d5a22
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/core/lifetime.d
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,9 @@ private void moveEmplaceImpl(T)(scope ref T target, return scope ref T source)
() @trusted { memset(&source, 0, sz); }();
else
{
auto init = typeid(T).initializer();
import core.internal.lifetime : emplaceInitializer;
ubyte[T.sizeof] init = void;
emplaceInitializer(*(() @trusted { return cast(T*)init.ptr; }()));
() @trusted { memcpy(&source, init.ptr, sz); }();
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/betterc/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include ../common.mak

TESTS:=test18828 test19416 test19421 test19561 test20088 test20613 test19924
TESTS:=test18828 test19416 test19421 test19561 test20088 test20613 test19924 test22336

.PHONY: all clean
all: $(addprefix $(ROOT)/,$(addsuffix ,$(TESTS))) $(addprefix $(ROOT)/,test19924.done)
Expand Down
19 changes: 19 additions & 0 deletions test/betterc/src/test22336.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************/
// https://issues.dlang.org/show_bug.cgi?id=22336

import core.lifetime;

struct Foo {
int f = -1;
@disable this(this);
this(int x) { f = x; }
@disable this();
}

extern(C) int main() {
Foo a = Foo(42);
Foo b = move(a);
assert(a.f == -1);
assert(b.f == 42);
return 0;
}

0 comments on commit c7d5a22

Please sign in to comment.