d: Merge upstream dmd 3982604c5, druntime bc58b1e9, phobos 12329adb6.
[official-gcc.git] / libphobos / libdruntime / core / internal / dassert.d
blob2c51b8641ae1373c585ff2f592424a998e05bf89
1 /*
2 * Support for rich error messages generation with `assert`
4 * This module provides the `_d_assert_fail` hooks which are instantiated
5 * by the compiler whenever `-checkaction=context` is used.
6 * There are two hooks, one for unary expressions, and one for binary.
7 * When used, the compiler will rewrite `assert(a >= b)` as
8 * `assert(a >= b, _d_assert_fail!(typeof(a))(">=", a, b))`.
9 * Temporaries will be created to avoid side effects if deemed necessary
10 * by the compiler.
12 * For more information, refer to the implementation in DMD frontend
13 * for `AssertExpression`'s semantic analysis.
15 * Copyright: D Language Foundation 2018 - 2020
16 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
17 * Source: $(LINK2 https://github.com/dlang/druntime/blob/master/src/core/internal/dassert.d, _dassert.d)
18 * Documentation: https://dlang.org/phobos/core_internal_dassert.html
20 module core.internal.dassert;
22 /**
23 * Generates rich assert error messages for unary expressions
25 * The unary expression `assert(!una)` will be turned into
26 * `assert(!una, _d_assert_fail("!", una))`.
27 * This routine simply acts as if the user wrote `assert(una == false)`.
29 * Params:
30 * op = Operator that was used in the expression, currently only "!"
31 * is supported.
32 * a = Result of the expression that was used in `assert` before
33 * its implicit conversion to `bool`.
35 * Returns:
36 * A string such as "$a != true" or "$a == true".
38 string _d_assert_fail(A)(const scope string op, auto ref const scope A a)
40 // Prevent InvalidMemoryOperationError when triggered from a finalizer
41 if (inFinalizer())
42 return "Assertion failed (rich formatting is disabled in finalizers)";
44 string[2] vals = [ miniFormatFakeAttributes(a), "true" ];
45 immutable token = op == "!" ? "==" : "!=";
46 return combine(vals[0 .. 1], token, vals[1 .. $]);
49 /**
50 * Generates rich assert error messages for binary expressions
52 * The binary expression `assert(x == y)` will be turned into
53 * `assert(x == y, _d_assert_fail!(typeof(x))("==", x, y))`.
55 * Params:
56 * comp = Comparison operator that was used in the expression.
57 * a = Left hand side operand (can be a tuple).
58 * b = Right hand side operand (can be a tuple).
60 * Returns:
61 * A string such as "$a $comp $b".
63 template _d_assert_fail(A...)
65 string _d_assert_fail(B...)(
66 const scope string comp, auto ref const scope A a, auto ref const scope B b)
67 if (B.length != 0 || A.length != 1) // Resolve ambiguity with unary overload
69 // Prevent InvalidMemoryOperationError when triggered from a finalizer
70 if (inFinalizer())
71 return "Assertion failed (rich formatting is disabled in finalizers)";
73 string[A.length + B.length] vals;
74 static foreach (idx; 0 .. A.length)
75 vals[idx] = miniFormatFakeAttributes(a[idx]);
76 static foreach (idx; 0 .. B.length)
77 vals[A.length + idx] = miniFormatFakeAttributes(b[idx]);
78 immutable token = invertCompToken(comp);
79 return combine(vals[0 .. A.length], token, vals[A.length .. $]);
83 /// Combines the supplied arguments into one string `"valA token valB"`
84 private string combine(const scope string[] valA, const scope string token,
85 const scope string[] valB) pure nothrow @nogc @safe
87 // Each separator is 2 chars (", "), plus the two spaces around the token.
88 size_t totalLen = (valA.length - 1) * 2 +
89 (valB.length - 1) * 2 + 2 + token.length;
91 // Empty arrays are printed as ()
92 if (valA.length == 0) totalLen += 2;
93 if (valB.length == 0) totalLen += 2;
95 foreach (v; valA) totalLen += v.length;
96 foreach (v; valB) totalLen += v.length;
98 // Include braces when printing tuples
99 const printBraces = (valA.length + valB.length) != 2;
100 if (printBraces) totalLen += 4; // '(', ')' for both tuples
102 char[] buffer = cast(char[]) pureAlloc(totalLen)[0 .. totalLen];
103 // @nogc-concat of "<valA> <comp> <valB>"
104 static void formatTuple (scope char[] buffer, ref size_t n, in string[] vals, in bool printBraces)
106 if (printBraces) buffer[n++] = '(';
107 foreach (idx, v; vals)
109 if (idx)
111 buffer[n++] = ',';
112 buffer[n++] = ' ';
114 buffer[n .. n + v.length] = v;
115 n += v.length;
117 if (printBraces) buffer[n++] = ')';
120 size_t n;
121 formatTuple(buffer, n, valA, printBraces);
122 buffer[n++] = ' ';
123 buffer[n .. n + token.length] = token;
124 n += token.length;
125 buffer[n++] = ' ';
126 formatTuple(buffer, n, valB, printBraces);
127 return (() @trusted => cast(string) buffer)();
130 /// Yields the appropriate `printf` format token for a type `T`
131 private template getPrintfFormat(T)
133 static if (is(T == long))
135 enum getPrintfFormat = "%lld";
137 else static if (is(T == ulong))
139 enum getPrintfFormat = "%llu";
141 else static if (__traits(isIntegral, T))
143 static if (__traits(isUnsigned, T))
145 enum getPrintfFormat = "%u";
147 else
149 enum getPrintfFormat = "%d";
152 else
154 static assert(0, "Unknown format");
159 * Generates a textual representation of `v` without relying on Phobos.
160 * The value is formatted as follows:
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)`
168 * Note that unions are rejected because this method cannot determine which
169 * member is valid when calling this method.
171 * Params:
172 * v = the value to print
174 * Returns: a string respresenting `v` or `V.stringof` if `V` is not supported
176 private string miniFormat(V)(const scope ref V v)
178 import core.internal.traits: isAggregateType;
180 /// `shared` values are formatted as their base type
181 static if (is(V == shared T, T))
183 // Use atomics to avoid race conditions whenever possible
184 static if (__traits(compiles, atomicLoad(v)))
186 if (!__ctfe)
188 T tmp = cast(T) atomicLoad(v);
189 return miniFormat(tmp);
193 // Fall back to a simple cast - we're violating the type system anyways
194 return miniFormat(*cast(const T*) &v);
196 // Format enum members using their name
197 else static if (is(V BaseType == enum))
199 // Always generate repeated if's instead of switch to skip the detection
200 // of non-integral enums. This method doesn't need to be fast.
201 static foreach (mem; __traits(allMembers, V))
203 if (v == __traits(getMember, V, mem))
204 return mem;
207 // Format invalid enum values as their base type
208 enum cast_ = "cast(" ~ V.stringof ~ ")";
209 const val = miniFormat(__ctfe ? cast(const BaseType) v : *cast(const BaseType*) &v);
210 return combine([ cast_ ], "", [ val ]);
212 else static if (is(V == bool))
214 return v ? "true" : "false";
216 // Detect vectors which match isIntegral / isFloating
217 else static if (is(V == __vector(ET[N]), ET, size_t N))
219 string msg = "[";
220 foreach (i; 0 .. N)
222 if (i > 0)
223 msg ~= ", ";
225 msg ~= miniFormat(v[i]);
227 msg ~= "]";
228 return msg;
230 else static if (__traits(isIntegral, V))
232 static if (is(V == char))
234 // Avoid invalid code points
235 if (v < 0x7F)
236 return ['\'', v, '\''];
238 uint tmp = v;
239 return "cast(char) " ~ miniFormat(tmp);
241 else static if (is(V == wchar) || is(V == dchar))
243 import core.internal.utf: isValidDchar, toUTF8;
245 // Avoid invalid code points
246 if (isValidDchar(v))
247 return toUTF8(['\'', v, '\'']);
249 uint tmp = v;
250 return "cast(" ~ V.stringof ~ ") " ~ miniFormat(tmp);
252 else
254 import core.internal.string;
255 static if (__traits(isUnsigned, V))
256 const val = unsignedToTempString(v);
257 else
258 const val = signedToTempString(v);
259 return val.get().idup();
262 else static if (__traits(isFloating, V))
264 import core.stdc.stdio : sprintf;
265 import core.stdc.config : LD = c_long_double;
267 // No suitable replacement for sprintf in druntime ATM
268 if (__ctfe)
269 return '<' ~ V.stringof ~ " not supported>";
271 // Workaround for https://issues.dlang.org/show_bug.cgi?id=20759
272 static if (is(LD == real))
273 enum realFmt = "%Lg";
274 else
275 enum realFmt = "%g";
277 char[60] val;
278 int len;
279 static if (is(V == float) || is(V == double))
280 len = sprintf(&val[0], "%g", v);
281 else static if (is(V == real))
282 len = sprintf(&val[0], realFmt, cast(LD) v);
283 else static if (is(V == cfloat) || is(V == cdouble))
284 len = sprintf(&val[0], "%g + %gi", v.re, v.im);
285 else static if (is(V == creal))
286 len = sprintf(&val[0], realFmt ~ " + " ~ realFmt ~ 'i', cast(LD) v.re, cast(LD) v.im);
287 else static if (is(V == ifloat) || is(V == idouble))
288 len = sprintf(&val[0], "%gi", v);
289 else // ireal
291 static assert(is(V == ireal));
292 static if (is(LD == real))
293 alias R = ireal;
294 else
295 alias R = idouble;
296 len = sprintf(&val[0], realFmt ~ 'i', cast(R) v);
298 return val.idup[0 .. len];
300 // special-handling for void-arrays
301 else static if (is(V == typeof(null)))
303 return "`null`";
305 else static if (is(V == U*, U))
307 // Format as ulong and prepend a 0x for pointers
308 import core.internal.string;
309 return cast(immutable) ("0x" ~ unsignedToTempString!16(cast(ulong) v));
311 // toString() isn't always const, e.g. classes inheriting from Object
312 else static if (__traits(compiles, { string s = V.init.toString(); }))
314 // Object references / struct pointers may be null
315 static if (is(V == class) || is(V == interface))
317 if (v is null)
318 return "`null`";
323 // Prefer const overload of toString
324 static if (__traits(compiles, { string s = v.toString(); }))
325 return v.toString();
326 else
327 return (cast() v).toString();
329 catch (Exception e)
331 return `<toString() failed: "` ~ e.msg ~ `", called on ` ~ formatMembers(v) ~`>`;
334 // Static arrays or slices (but not aggregates with `alias this`)
335 else static if (is(V : U[], U) && !isAggregateType!V)
337 import core.internal.traits: Unqual;
338 alias E = Unqual!U;
340 // special-handling for void-arrays
341 static if (is(E == void))
343 if (__ctfe)
344 return "<void[] not supported>";
346 const bytes = cast(byte[]) v;
347 return miniFormat(bytes);
349 // anything string-like
350 else static if (is(E == char) || is(E == dchar) || is(E == wchar))
352 const s = `"` ~ v ~ `"`;
354 // v could be a char[], dchar[] or wchar[]
355 static if (is(typeof(s) : const char[]))
356 return cast(immutable) s;
357 else
359 import core.internal.utf: toUTF8;
360 return toUTF8(s);
363 else
365 string msg = "[";
366 foreach (i, ref el; v)
368 if (i > 0)
369 msg ~= ", ";
371 // don't fully print big arrays
372 if (i >= 30)
374 msg ~= "...";
375 break;
377 msg ~= miniFormat(el);
379 msg ~= "]";
380 return msg;
383 else static if (is(V : Val[K], K, Val))
385 size_t i;
386 string msg = "[";
387 foreach (ref k, ref val; v)
389 if (i > 0)
390 msg ~= ", ";
391 // don't fully print big AAs
392 if (i++ >= 30)
394 msg ~= "...";
395 break;
397 msg ~= miniFormat(k) ~ ": " ~ miniFormat(val);
399 msg ~= "]";
400 return msg;
402 else static if (is(V == struct))
404 return formatMembers(v);
406 // Extern C++ classes don't have a toString by default
407 else static if (is(V == class) || is(V == interface))
409 if (v is null)
410 return "null";
412 // Extern classes might be opaque
413 static if (is(typeof(v.tupleof)))
414 return formatMembers(v);
415 else
416 return '<' ~ V.stringof ~ '>';
418 else
420 return V.stringof;
424 /// Formats `v`'s members as `V(<member 1>, <member 2>, ...)`
425 private string formatMembers(V)(const scope ref V v)
427 enum ctxPtr = __traits(isNested, V);
428 enum isOverlapped = calcFieldOverlap([ v.tupleof.offsetof ]);
430 string msg = V.stringof ~ "(";
431 foreach (i, ref field; v.tupleof)
433 if (i > 0)
434 msg ~= ", ";
436 static if (isOverlapped[i])
438 msg ~= "<overlapped field>";
440 else
442 // Mark context pointer
443 static if (ctxPtr && i == v.tupleof.length - 1)
444 msg ~= "<context>: ";
446 msg ~= miniFormat(field);
449 msg ~= ")";
450 return msg;
454 * Calculates whether fields are overlapped based on the passed offsets.
456 * Params:
457 * offsets = offsets of all fields matching the order of `.tupleof`
459 * Returns: an array such that arr[n] = true indicates that the n'th field
460 * overlaps with an adjacent field
462 private bool[] calcFieldOverlap(const scope size_t[] offsets)
464 bool[] overlaps = new bool[](offsets.length);
466 foreach (const idx; 1 .. overlaps.length)
468 if (offsets[idx - 1] == offsets[idx])
469 overlaps[idx - 1] = overlaps[idx] = true;
472 return overlaps;
475 // This should be a local import in miniFormat but fails with a cyclic dependency error
476 // core.thread.osthread -> core.time -> object -> core.internal.array.capacity
477 // -> core.atomic -> core.thread -> core.thread.osthread
478 import core.atomic : atomicLoad;
480 /// Negates a comparison token, e.g. `==` is mapped to `!=`
481 private string invertCompToken(scope string comp) pure nothrow @nogc @safe
483 switch (comp)
485 case "==":
486 return "!=";
487 case "!=":
488 return "==";
489 case "<":
490 return ">=";
491 case "<=":
492 return ">";
493 case ">":
494 return "<=";
495 case ">=":
496 return "<";
497 case "is":
498 return "!is";
499 case "!is":
500 return "is";
501 case "in":
502 return "!in";
503 case "!in":
504 return "in";
505 default:
506 assert(0, combine(["Invalid comparison operator '"], comp, ["'"]));
510 /// Casts the function pointer to include `@safe`, `@nogc`, ...
511 private auto assumeFakeAttributes(T)(T t) @trusted
513 import core.internal.traits : Parameters, ReturnType;
514 alias RT = ReturnType!T;
515 alias P = Parameters!T;
516 alias type = RT function(P) nothrow @nogc @safe pure;
517 return cast(type) t;
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`.
522 private string miniFormatFakeAttributes(T)(const scope ref T t)
524 alias miniT = miniFormat!T;
525 return assumeFakeAttributes(&miniT)(t);
528 /// Allocates an array of `t` bytes while pretending to be `@safe`, `@nogc`, ...
529 private auto pureAlloc(size_t t)
531 static auto alloc(size_t len)
533 return new ubyte[len];
535 return assumeFakeAttributes(&alloc)(t);
538 /// Wrapper for GC.inFinalizer that fakes purity
539 private bool inFinalizer()() pure nothrow @nogc @safe
541 // CTFE doesn't trigger InvalidMemoryErrors
542 import core.memory : GC;
543 return !__ctfe && assumeFakeAttributes(&GC.inFinalizer)();
546 // https://issues.dlang.org/show_bug.cgi?id=21544
547 unittest
549 // Normal enum values
550 enum E { A, BCDE }
551 E e = E.A;
552 assert(miniFormat(e) == "A");
553 e = E.BCDE;
554 assert(miniFormat(e) == "BCDE");
556 // Invalid enum value is printed as their implicit base type (int)
557 e = cast(E) 3;
558 assert(miniFormat(e) == "cast(E) 3");
560 // Non-integral enums work as well
561 static struct S
563 int a;
564 string str;
567 enum E2 : S { a2 = S(1, "Hello") }
568 E2 es = E2.a2;
569 assert(miniFormat(es) == `a2`);
571 // Even invalid values
572 es = cast(E2) S(2, "World");
573 assert(miniFormat(es) == `cast(E2) S(2, "World")`);
576 // vectors
577 unittest
579 static if (is(__vector(float[4])))
581 __vector(float[4]) f = [-1.5f, 0.5f, 1.0f, 0.125f];
582 assert(miniFormat(f) == "[-1.5, 0.5, 1, 0.125]");
585 static if (is(__vector(int[4])))
587 __vector(int[4]) i = [-1, 0, 1, 3];
588 assert(miniFormat(i) == "[-1, 0, 1, 3]");