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

Commit 1165b5a

Browse files
authored
Merge pull request #2766 from JinShil/move_assert
Move _d_assert_fail from object.d to core.internal.dassert.d merged-on-behalf-of: Petar Kirov <ZombineDev@users.noreply.github.com>
2 parents ed87259 + 776523c commit 1165b5a

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

mak/DOCS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ DOCS=\
7575
$(DOCDIR)\core_sys_darwin_mach_thread_act.html \
7676
$(DOCDIR)\core_sys_darwin_netinet_in_.html \
7777
\
78+
$(DOCDIR)\core_internal_dassert.html \
7879
$(DOCDIR)\core_internal_switch_.html \
7980
\
8081
$(DOCDIR)\core_internal_array_appending.html \

posix.mak

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ $(DOCDIR)/core_sys_darwin_mach_%.html : src/core/sys/darwin/mach/%.d $(DMD)
174174
$(DOCDIR)/core_sys_darwin_netinet_%.html : src/core/sys/darwin/netinet/%.d $(DMD)
175175
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<
176176

177+
$(DOCDIR)/core_internal_dassert.html : src/core/internal/dassert.d $(DMD)
178+
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<
179+
177180
$(DOCDIR)/core_internal_switch_.html : src/core/internal/switch_.d $(DMD)
178181
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<
179182

src/core/internal/dassert.d

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@ on assertion failures
44
*/
55
module core.internal.dassert;
66

7+
/// Allows customized assert error messages
8+
string _d_assert_fail(string comp, A, B)(A a, B b) @nogc @safe nothrow pure
9+
{
10+
/*
11+
The program will be terminated after the assertion error message has
12+
been printed and its not considered part of the "main" program.
13+
Also, catching an AssertError is Undefined Behavior
14+
Hence, we can fake purity and @nogc-ness here.
15+
*/
16+
17+
auto valA = miniFormatFakeAttributes(a);
18+
auto valB = miniFormatFakeAttributes(b);
19+
enum token = invertCompToken(comp);
20+
21+
const totalLen = valA.length + token.length + valB.length + 2;
22+
char[] buffer = cast(char[]) pureAlloc(totalLen)[0 .. totalLen];
23+
// @nogc-concat of "<valA> <comp> <valB>"
24+
auto n = valA.length;
25+
buffer[0 .. n] = valA;
26+
buffer[n++] = ' ';
27+
buffer[n .. n + token.length] = token;
28+
n += token.length;
29+
buffer[n++] = ' ';
30+
buffer[n .. n + valB.length] = valB;
31+
return (() @trusted => cast(string) buffer)();
32+
}
33+
734
// Yields the appropriate printf format token for a type T
835
// Indended to be used by miniFormat
936
private template getPrintfFormat(T)
@@ -37,7 +64,7 @@ private template getPrintfFormat(T)
3764
Minimalistic formatting for use in _d_assert_fail to keep the compilation
3865
overhead small and avoid the use of Phobos.
3966
*/
40-
auto miniFormat(V)(V v)
67+
private auto miniFormat(V)(V v)
4168
{
4269
import core.stdc.stdio : sprintf;
4370
import core.stdc.string : strlen;
@@ -134,7 +161,7 @@ auto miniFormat(V)(V v)
134161
}
135162

136163
// Inverts a comparison token for use in _d_assert_fail
137-
string invertCompToken(string comp)
164+
private string invertCompToken(string comp)
138165
{
139166
switch (comp)
140167
{
@@ -172,13 +199,13 @@ private auto assumeFakeAttributes(T)(T t) @trusted
172199
return cast(type) t;
173200
}
174201

175-
auto miniFormatFakeAttributes(T)(T t)
202+
private auto miniFormatFakeAttributes(T)(T t)
176203
{
177204
alias miniT = miniFormat!T;
178205
return assumeFakeAttributes(&miniT)(t);
179206
}
180207

181-
auto pureAlloc(size_t t)
208+
private auto pureAlloc(size_t t)
182209
{
183210
static auto alloc(size_t len)
184211
{

src/object.d

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public import core.internal.array.capacity: assumeSafeAppend;
6969
/// See $(REF _d_arraysetlengthTImpl, core,internal,array,capacity)
7070
public import core.internal.array.capacity: _d_arraysetlengthTImpl;
7171

72+
/// See $(REF _d_assert_fail, core,internal,dassert)
73+
public import core.internal.dassert: _d_assert_fail;
74+
7275
/// See $(REF __switch, core,internal,switch_)
7376
public import core.internal.switch_: __switch;
7477
/// See $(REF __switch_error, core,internal,switch_)
@@ -4175,31 +4178,3 @@ void __ArrayDtor(T)(T[] a)
41754178
foreach_reverse (ref T e; a)
41764179
e.__xdtor();
41774180
}
4178-
4179-
// Allows customized assert error messages
4180-
string _d_assert_fail(string comp, A, B)(A a, B b) @nogc @safe nothrow pure
4181-
{
4182-
import core.internal.dassert : invertCompToken, miniFormatFakeAttributes, pureAlloc;
4183-
/*
4184-
The program will be terminated after the assertion error message has
4185-
been printed and its not considered part of the "main" program.
4186-
Also, catching an AssertError is Undefined Behavior
4187-
Hence, we can fake purity and @nogc-ness here.
4188-
*/
4189-
4190-
auto valA = miniFormatFakeAttributes(a);
4191-
auto valB = miniFormatFakeAttributes(b);
4192-
enum token = invertCompToken(comp);
4193-
4194-
const totalLen = valA.length + token.length + valB.length + 2;
4195-
char[] buffer = cast(char[]) pureAlloc(totalLen)[0 .. totalLen];
4196-
// @nogc-concat of "<valA> <comp> <valB>"
4197-
auto n = valA.length;
4198-
buffer[0 .. n] = valA;
4199-
buffer[n++] = ' ';
4200-
buffer[n .. n + token.length] = token;
4201-
n += token.length;
4202-
buffer[n++] = ' ';
4203-
buffer[n .. n + valB.length] = valB;
4204-
return (() @trusted => cast(string) buffer)();
4205-
}

0 commit comments

Comments
 (0)