Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mak/DOCS
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ DOCS=\
$(DOCDIR)\core_sys_darwin_mach_thread_act.html \
$(DOCDIR)\core_sys_darwin_netinet_in_.html \
\
$(DOCDIR)\core_internal_dassert.html \
$(DOCDIR)\core_internal_switch_.html \
\
$(DOCDIR)\core_internal_array_appending.html \
Expand Down
3 changes: 3 additions & 0 deletions posix.mak
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ $(DOCDIR)/core_sys_darwin_mach_%.html : src/core/sys/darwin/mach/%.d $(DMD)
$(DOCDIR)/core_sys_darwin_netinet_%.html : src/core/sys/darwin/netinet/%.d $(DMD)
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<

$(DOCDIR)/core_internal_dassert.html : src/core/internal/dassert.d $(DMD)
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<

$(DOCDIR)/core_internal_switch_.html : src/core/internal/switch_.d $(DMD)
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<

Expand Down
35 changes: 31 additions & 4 deletions src/core/internal/dassert.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@ on assertion failures
*/
module core.internal.dassert;

/// Allows customized assert error messages
string _d_assert_fail(string comp, A, B)(A a, B b) @nogc @safe nothrow pure
{
/*
The program will be terminated after the assertion error message has
been printed and its not considered part of the "main" program.
Also, catching an AssertError is Undefined Behavior
Hence, we can fake purity and @nogc-ness here.
*/

auto valA = miniFormatFakeAttributes(a);
auto valB = miniFormatFakeAttributes(b);
enum token = invertCompToken(comp);

const totalLen = valA.length + token.length + valB.length + 2;
char[] buffer = cast(char[]) pureAlloc(totalLen)[0 .. totalLen];
// @nogc-concat of "<valA> <comp> <valB>"
auto n = valA.length;
buffer[0 .. n] = valA;
buffer[n++] = ' ';
buffer[n .. n + token.length] = token;
n += token.length;
buffer[n++] = ' ';
buffer[n .. n + valB.length] = valB;
return (() @trusted => cast(string) buffer)();
}

// Yields the appropriate printf format token for a type T
// Indended to be used by miniFormat
private template getPrintfFormat(T)
Expand Down Expand Up @@ -37,7 +64,7 @@ private template getPrintfFormat(T)
Minimalistic formatting for use in _d_assert_fail to keep the compilation
overhead small and avoid the use of Phobos.
*/
auto miniFormat(V)(V v)
private auto miniFormat(V)(V v)
{
import core.stdc.stdio : sprintf;
import core.stdc.string : strlen;
Expand Down Expand Up @@ -134,7 +161,7 @@ auto miniFormat(V)(V v)
}

// Inverts a comparison token for use in _d_assert_fail
string invertCompToken(string comp)
private string invertCompToken(string comp)
{
switch (comp)
{
Expand Down Expand Up @@ -172,13 +199,13 @@ private auto assumeFakeAttributes(T)(T t) @trusted
return cast(type) t;
}

auto miniFormatFakeAttributes(T)(T t)
private auto miniFormatFakeAttributes(T)(T t)
{
alias miniT = miniFormat!T;
return assumeFakeAttributes(&miniT)(t);
}

auto pureAlloc(size_t t)
private auto pureAlloc(size_t t)
{
static auto alloc(size_t len)
{
Expand Down
31 changes: 3 additions & 28 deletions src/object.d
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public import core.internal.array.capacity: assumeSafeAppend;
/// See $(REF _d_arraysetlengthTImpl, core,internal,array,capacity)
public import core.internal.array.capacity: _d_arraysetlengthTImpl;

/// See $(REF _d_assert_fail, core,internal,dassert)
public import core.internal.dassert: _d_assert_fail;

/// See $(REF __switch, core,internal,switch_)
public import core.internal.switch_: __switch;
/// See $(REF __switch_error, core,internal,switch_)
Expand Down Expand Up @@ -4175,31 +4178,3 @@ void __ArrayDtor(T)(T[] a)
foreach_reverse (ref T e; a)
e.__xdtor();
}

// Allows customized assert error messages
string _d_assert_fail(string comp, A, B)(A a, B b) @nogc @safe nothrow pure
{
import core.internal.dassert : invertCompToken, miniFormatFakeAttributes, pureAlloc;
/*
The program will be terminated after the assertion error message has
been printed and its not considered part of the "main" program.
Also, catching an AssertError is Undefined Behavior
Hence, we can fake purity and @nogc-ness here.
*/

auto valA = miniFormatFakeAttributes(a);
auto valB = miniFormatFakeAttributes(b);
enum token = invertCompToken(comp);

const totalLen = valA.length + token.length + valB.length + 2;
char[] buffer = cast(char[]) pureAlloc(totalLen)[0 .. totalLen];
// @nogc-concat of "<valA> <comp> <valB>"
auto n = valA.length;
buffer[0 .. n] = valA;
buffer[n++] = ' ';
buffer[n .. n + token.length] = token;
n += token.length;
buffer[n++] = ' ';
buffer[n .. n + valB.length] = valB;
return (() @trusted => cast(string) buffer)();
}