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 @@ -97,14 +97,21 @@ private string miniFormat(V)(const ref V v)
97
97
{
98
98
return " `null`" ;
99
99
}
100
- else static if (__traits(compiles, { string s = v.toString(); }))
101
- {
102
- return v.toString();
103
- }
104
- // Non-const toString(), e.g. classes inheriting from Object
100
+ // toString() isn't always const, e.g. classes inheriting from Object
105
101
else static if (__traits(compiles, { string s = V.init.toString(); }))
106
102
{
107
- return (cast () v).toString();
103
+ // Object references / struct pointers may be null
104
+ static if (is (V == class ) || is (V == interface ) || is (V == U* , U))
105
+ {
106
+ if (v is null )
107
+ return " `null`" ;
108
+ }
109
+
110
+ // Prefer const overload of toString
111
+ static if (__traits(compiles, { string s = v.toString(); }))
112
+ return v.toString();
113
+ else
114
+ return (cast () v).toString();
108
115
}
109
116
// Static arrays or slices (but not aggregates with `alias this`)
110
117
else static if (is (V : U[], U) && ! isAggregateType! V)
Original file line number Diff line number Diff line change @@ -94,6 +94,9 @@ void testToString()()
94
94
}
95
95
96
96
test! " !=" (Overloaded(), Overloaded(), " Const == Const" );
97
+
98
+ Foo fnull = null ;
99
+ test! " !is" (fnull, fnull, " `null` is `null`" );
97
100
}
98
101
99
102
You can’t perform that action at this time.
0 commit comments