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

Commit 3a9ede0

Browse files
MoonlightSentinelthewilsonator
authored andcommitted
Improve documentation in dassert.d
Add missing DDoc comments and several notes to the implementation.
1 parent 5f16f16 commit 3a9ede0

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/core/internal/dassert.d

+28-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* by the compiler whenever `-checkaction=context` is used.
66
* There are two hooks, one for unary expressions, and one for binary.
77
* When used, the compiler will rewrite `assert(a >= b)` as
8-
* `assert(a >= b, _d_assert_fail!">="(a, b))`.
8+
* `assert(a >= b, _d_assert_fail!(typeof(a))(">=", a, b))`.
99
* Temporaries will be created to avoid side effects if deemed necessary
1010
* by the compiler.
1111
*
@@ -80,7 +80,7 @@ template _d_assert_fail(A...)
8080
}
8181
}
8282

83-
/// Combines the supplied arguments into one string "valA token valB"
83+
/// Combines the supplied arguments into one string `"valA token valB"`
8484
private string combine(const scope string[] valA, const scope string token,
8585
const scope string[] valB) pure nothrow @nogc @safe
8686
{
@@ -127,8 +127,7 @@ private string combine(const scope string[] valA, const scope string token,
127127
return (() @trusted => cast(string) buffer)();
128128
}
129129

130-
// Yields the appropriate printf format token for a type T
131-
// Indended to be used by miniFormat
130+
/// Yields the appropriate `printf` format token for a type `T`
132131
private template getPrintfFormat(T)
133132
{
134133
static if (is(T == long))
@@ -157,13 +156,28 @@ private template getPrintfFormat(T)
157156
}
158157

159158
/**
160-
Minimalistic formatting for use in _d_assert_fail to keep the compilation
161-
overhead small and avoid the use of Phobos.
162-
*/
159+
* Generates a textual representation of `v` without relying on Phobos.
160+
* The value is formatted as follows:
161+
*
162+
* - primitive types and arrays yield their respective literals
163+
* - pointers are printed as hexadecimal numbers
164+
* - enum members are represented by their name
165+
* - user-defined types are formatted by either calling `toString`
166+
* if defined or printing all members, e.g. `S(1, 2)`
167+
*
168+
* Note that unions are rejected because this method cannot determine which
169+
* member is valid when calling this method.
170+
*
171+
* Params:
172+
* v = the value to print
173+
*
174+
* Returns: a string respresenting `v` or `V.stringof` if `V` is not supported
175+
*/
163176
private string miniFormat(V)(const scope ref V v)
164177
{
165178
import core.internal.traits: isAggregateType;
166179

180+
/// `shared` values are formatted as their base type
167181
static if (is(V == shared T, T))
168182
{
169183
// Use atomics to avoid race conditions whenever possible
@@ -199,6 +213,7 @@ private string miniFormat(V)(const scope ref V v)
199213
{
200214
return v ? "true" : "false";
201215
}
216+
// Detect vectors which match isIntegral / isFloating
202217
else static if (is(V == __vector(ET[N]), ET, size_t N))
203218
{
204219
string msg = "[";
@@ -249,6 +264,7 @@ private string miniFormat(V)(const scope ref V v)
249264
import core.stdc.stdio : sprintf;
250265
import core.stdc.config : LD = c_long_double;
251266

267+
// No suitable replacement for sprintf in druntime ATM
252268
if (__ctfe)
253269
return '<' ~ V.stringof ~ " not supported>";
254270

@@ -461,7 +477,7 @@ private bool[] calcFieldOverlap(const scope size_t[] offsets)
461477
// -> core.atomic -> core.thread -> core.thread.osthread
462478
import core.atomic : atomicLoad;
463479

464-
// Inverts a comparison token for use in _d_assert_fail
480+
/// Negates a comparison token, e.g. `==` is mapped to `!=`
465481
private string invertCompToken(scope string comp) pure nothrow @nogc @safe
466482
{
467483
switch (comp)
@@ -491,6 +507,7 @@ private string invertCompToken(scope string comp) pure nothrow @nogc @safe
491507
}
492508
}
493509

510+
/// Casts the function pointer to include `@safe`, `@nogc`, ...
494511
private auto assumeFakeAttributes(T)(T t) @trusted
495512
{
496513
import core.internal.traits : Parameters, ReturnType;
@@ -500,12 +517,15 @@ private auto assumeFakeAttributes(T)(T t) @trusted
500517
return cast(type) t;
501518
}
502519

520+
/// Wrapper for `miniFormat` which assumes that the implementation is `@safe`, `@nogc`, ...
521+
/// s.t. it does not violate the constraints of the the function containing the `assert`.
503522
private string miniFormatFakeAttributes(T)(const scope ref T t)
504523
{
505524
alias miniT = miniFormat!T;
506525
return assumeFakeAttributes(&miniT)(t);
507526
}
508527

528+
/// Allocates an array of `t` bytes while pretending to be `@safe`, `@nogc`, ...
509529
private auto pureAlloc(size_t t)
510530
{
511531
static auto alloc(size_t len)

0 commit comments

Comments
 (0)