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

Commit 9ade20c

Browse files
authored
Merge pull request #2178 from JinShil/fix_18828
Fix Issue 18828 - [-betterC] helpless error in object.d merged-on-behalf-of: Jacob Carlborg <jacob-carlborg@users.noreply.github.com>
2 parents 278bc80 + 0bb1660 commit 9ade20c

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
@@ -243,7 +243,7 @@ UT_MODULES:=$(patsubst src/%.d,$(ROOT)/unittest/%,$(SRCS))
243243
HAS_ADDITIONAL_TESTS:=$(shell test -d test && echo 1)
244244
ifeq ($(HAS_ADDITIONAL_TESTS),1)
245245
ADDITIONAL_TESTS:=test/init_fini test/exceptions test/coverage test/profile test/cycles test/allocations test/typeinfo \
246-
test/thread test/unittest test/imports
246+
test/thread test/unittest test/imports test/betterc
247247
ADDITIONAL_TESTS+=$(if $(SHARED),test/shared,)
248248
endif
249249

src/object.d

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,14 +3081,19 @@ unittest
30813081
/// ditto
30823082
void destroy(T)(ref T obj) if (is(T == struct))
30833083
{
3084+
// We need to re-initialize `obj`. Previously, the code
3085+
// `auto init = cast(ubyte[])typeid(T).initializer()` was used, but
3086+
// `typeid` is a runtime call and requires the `TypeInfo` object which is
3087+
// not usable when compiling with -betterC. If we do `obj = T.init` then we
3088+
// end up needlessly calling postblits and destructors. So, we create a
3089+
// static immutable lvalue that can be re-used with subsequent calls to `destroy`
3090+
shared static immutable T init = T.init;
3091+
30843092
_destructRecurse(obj);
30853093
() @trusted {
3086-
auto buf = (cast(ubyte*) &obj)[0 .. T.sizeof];
3087-
auto init = cast(ubyte[])typeid(T).initializer();
3088-
if (init.ptr is null) // null ptr means initialize to 0s
3089-
buf[] = 0;
3090-
else
3091-
buf[] = init[];
3094+
auto dest = (cast(ubyte*) &obj)[0 .. T.sizeof];
3095+
auto src = (cast(ubyte*) &init)[0 .. T.sizeof];
3096+
dest[] = src[];
30923097
} ();
30933098
}
30943099

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)