|
1 | 1 | /*
|
2 |
| -Provides light-weight formatting utilities for pretty-printing |
3 |
| -on assertion failures |
4 |
| -*/ |
| 2 | + * Support for rich error messages generation with `assert` |
| 3 | + * |
| 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!">="(a, b))`. |
| 9 | + * Temporaries will be created to avoid side effects if deemed necessary |
| 10 | + * by the compiler. |
| 11 | + * |
| 12 | + * For more information, refer to the implementation in DMD frontend |
| 13 | + * for `AssertExpression`'s semantic analysis. |
| 14 | + * |
| 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 |
| 19 | + */ |
5 | 20 | module core.internal.dassert;
|
6 | 21 |
|
7 |
| -/// Allows customized assert error messages for unary expressions |
| 22 | +/** |
| 23 | + * Generates rich assert error messages for unary expressions |
| 24 | + * |
| 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)`. |
| 28 | + * |
| 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`. |
| 34 | + * |
| 35 | + * Returns: |
| 36 | + * A string such as "$a != true" or "$a == true". |
| 37 | + */ |
8 | 38 | string _d_assert_fail(string op, A)(auto ref const scope A a)
|
9 | 39 | {
|
10 | 40 | string val = miniFormatFakeAttributes(a);
|
11 | 41 | enum token = op == "!" ? "==" : "!=";
|
12 | 42 | return combine(val, token, "true");
|
13 | 43 | }
|
14 | 44 |
|
15 |
| -/// Allows customized assert error messages for binary expressions |
| 45 | +/** |
| 46 | + * Generates rich assert error messages for binary expressions |
| 47 | + * |
| 48 | + * The binary expression `assert(x == y)` will be turned into |
| 49 | + * `assert(x == y, _d_assert_fail!"=="(x, y))`. |
| 50 | + * |
| 51 | + * Params: |
| 52 | + * comp = Comparison operator that was used in the expression. |
| 53 | + * a = Left hand side operand. |
| 54 | + * b = Right hand side operand. |
| 55 | + * |
| 56 | + * Returns: |
| 57 | + * A string such as "$a $comp $b". |
| 58 | + */ |
16 | 59 | string _d_assert_fail(string comp, A, B)(auto ref const scope A a, auto ref const scope B b)
|
17 | 60 | {
|
18 | 61 | /*
|
|
0 commit comments