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

Commit 0bb1660

Browse files
committed
Fix Issue 18828 - [-betterC] helpless error in object.d
1 parent bd2ea42 commit 0bb1660

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

posix.mak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ UT_MODULES:=$(patsubst src/%.d,$(ROOT)/unittest/%,$(SRCS))
241241
HAS_ADDITIONAL_TESTS:=$(shell test -d test && echo 1)
242242
ifeq ($(HAS_ADDITIONAL_TESTS),1)
243243
ADDITIONAL_TESTS:=test/init_fini test/exceptions test/coverage test/profile test/cycles test/allocations test/typeinfo \
244-
test/thread test/unittest test/imports
244+
test/thread test/unittest test/imports test/betterc
245245
ADDITIONAL_TESTS+=$(if $(SHARED),test/shared,)
246246
endif
247247

src/object.d

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,14 +3027,19 @@ unittest
30273027
/// ditto
30283028
void destroy(T)(ref T obj) if (is(T == struct))
30293029
{
3030+
// We need to re-initialize `obj`. Previously, the code
3031+
// `auto init = cast(ubyte[])typeid(T).initializer()` was used, but
3032+
// `typeid` is a runtime call and requires the `TypeInfo` object which is
3033+
// not usable when compiling with -betterC. If we do `obj = T.init` then we
3034+
// end up needlessly calling postblits and destructors. So, we create a
3035+
// static immutable lvalue that can be re-used with subsequent calls to `destroy`
3036+
shared static immutable T init = T.init;
3037+
30303038
_destructRecurse(obj);
30313039
() @trusted {
3032-
auto buf = (cast(ubyte*) &obj)[0 .. T.sizeof];
3033-
auto init = cast(ubyte[])typeid(T).initializer();
3034-
if (init.ptr is null) // null ptr means initialize to 0s
3035-
buf[] = 0;
3036-
else
3037-
buf[] = init[];
3040+
auto dest = (cast(ubyte*) &obj)[0 .. T.sizeof];
3041+
auto src = (cast(ubyte*) &init)[0 .. T.sizeof];
3042+
dest[] = src[];
30383043
} ();
30393044
}
30403045

test/betterc/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
include ../common.mak
2+
3+
TESTS:=test18828
4+
5+
.PHONY: all clean
6+
all: $(addprefix $(ROOT)/,$(addsuffix ,$(TESTS)))
7+
8+
$(ROOT)/%: $(SRC)/%.d
9+
$(QUIET)$(DMD) -betterC -c -of$@ $<
10+
11+
clean:
12+
rm -rf $(ROOT)

test/betterc/src/test18828.d

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*******************************************/
2+
// https://issues.dlang.org/show_bug.cgi?id=18828
3+
4+
struct S18828 { }
5+
6+
void test18828()
7+
{
8+
S18828 s;
9+
destroy(s);
10+
}

0 commit comments

Comments
 (0)