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

Commit b6f45c8

Browse files
authored
Merge pull request #3045 from MoonlightSentinel/nullToString
Fix Issue 20750 - checkaction=context segfaults for null references merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents 7bfd504 + a045839 commit b6f45c8

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/core/internal/dassert.d

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,21 @@ private string miniFormat(V)(const ref V v)
111111
{
112112
return "`null`";
113113
}
114-
else static if (__traits(compiles, { string s = v.toString(); }))
115-
{
116-
return v.toString();
117-
}
118-
// Non-const toString(), e.g. classes inheriting from Object
114+
// toString() isn't always const, e.g. classes inheriting from Object
119115
else static if (__traits(compiles, { string s = V.init.toString(); }))
120116
{
121-
return (cast() v).toString();
117+
// Object references / struct pointers may be null
118+
static if (is(V == class) || is(V == interface) || is(V == U*, U))
119+
{
120+
if (v is null)
121+
return "`null`";
122+
}
123+
124+
// Prefer const overload of toString
125+
static if (__traits(compiles, { string s = v.toString(); }))
126+
return v.toString();
127+
else
128+
return (cast() v).toString();
122129
}
123130
// Static arrays or slices (but not aggregates with `alias this`)
124131
else static if (is(V : U[], U) && !isAggregateType!V)

test/exceptions/src/assert_fail.d

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ void testToString()()
9898
}
9999

100100
test!"!="(Overloaded(), Overloaded(), "Const == Const");
101+
102+
Foo fnull = null;
103+
test!"!is"(fnull, fnull, "`null` is `null`");
101104
}
102105

103106

0 commit comments

Comments
 (0)