diff --git a/src/core/internal/dassert.d b/src/core/internal/dassert.d index ff14840ca3..1aa730186a 100644 --- a/src/core/internal/dassert.d +++ b/src/core/internal/dassert.d @@ -97,14 +97,21 @@ private string miniFormat(V)(const ref V v) { return "`null`"; } - else static if (__traits(compiles, { string s = v.toString(); })) - { - return v.toString(); - } - // Non-const toString(), e.g. classes inheriting from Object + // toString() isn't always const, e.g. classes inheriting from Object else static if (__traits(compiles, { string s = V.init.toString(); })) { - return (cast() v).toString(); + // Object references / struct pointers may be null + static if (is(V == class) || is(V == interface) || is(V == U*, U)) + { + if (v is null) + return "`null`"; + } + + // Prefer const overload of toString + static if (__traits(compiles, { string s = v.toString(); })) + return v.toString(); + else + return (cast() v).toString(); } // Static arrays or slices (but not aggregates with `alias this`) else static if (is(V : U[], U) && !isAggregateType!V) diff --git a/test/exceptions/src/assert_fail.d b/test/exceptions/src/assert_fail.d index 22dbf09e31..5513c08630 100644 --- a/test/exceptions/src/assert_fail.d +++ b/test/exceptions/src/assert_fail.d @@ -94,6 +94,9 @@ void testToString()() } test!"!="(Overloaded(), Overloaded(), "Const == Const"); + + Foo fnull = null; + test!"!is"(fnull, fnull, "`null` is `null`"); }