This repository was archived by the owner on Oct 12, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Original file line number Diff line number Diff line change @@ -111,14 +111,21 @@ private string miniFormat(V)(const ref V v)
111
111
{
112
112
return " `null`" ;
113
113
}
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
119
115
else static if (__traits(compiles, { string s = V.init.toString(); }))
120
116
{
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();
122
129
}
123
130
// Static arrays or slices (but not aggregates with `alias this`)
124
131
else static if (is (V : U[], U) && ! isAggregateType! V)
Original file line number Diff line number Diff line change @@ -98,6 +98,9 @@ void testToString()()
98
98
}
99
99
100
100
test! " !=" (Overloaded(), Overloaded(), " Const == Const" );
101
+
102
+ Foo fnull = null ;
103
+ test! " !is" (fnull, fnull, " `null` is `null`" );
101
104
}
102
105
103
106
You can’t perform that action at this time.
0 commit comments