19
19
*/
20
20
module core.internal.dassert ;
21
21
22
- // Legacy hooks currently used by dmd, remove once dmd uses
23
- // the new runtime versions defined below
24
- string _d_assert_fail (string op, A)(auto ref const scope A a)
25
- {
26
- return _d_assert_fail! (A)(op, a);
27
- }
28
-
29
- string _d_assert_fail (string comp, A, B)(auto ref const scope A a, auto ref const scope B b)
30
- {
31
- return _d_assert_fail! (A, B)(comp, a, b);
32
- }
33
-
34
22
/**
35
23
* Generates rich assert error messages for unary expressions
36
24
*
@@ -47,14 +35,6 @@ string _d_assert_fail(string comp, A, B)(auto ref const scope A a, auto ref cons
47
35
* Returns:
48
36
* A string such as "$a != true" or "$a == true".
49
37
*/
50
- string _d_assert_fail2 (A)(string op, auto ref const scope A a)
51
- {
52
- string [2 ] vals = [ miniFormatFakeAttributes(a), " true" ];
53
- immutable token = op == " !" ? " ==" : " !=" ;
54
- return combine (vals[0 .. 1 ], token, vals[1 .. $]);
55
- }
56
-
57
- // / Ditto
58
38
string _d_assert_fail (A)(const scope string op, auto ref const scope A a)
59
39
{
60
40
string [2 ] vals = [ miniFormatFakeAttributes(a), " true" ];
@@ -76,9 +56,11 @@ string _d_assert_fail(A)(const scope string op, auto ref const scope A a)
76
56
* Returns:
77
57
* A string such as "$a $comp $b".
78
58
*/
79
- template _d_assert_fail2 (A... ) {
80
- string _d_assert_fail2 (B... )(
81
- string comp, auto ref const scope A a, auto ref const scope B b)
59
+ template _d_assert_fail (A... )
60
+ {
61
+ string _d_assert_fail (B... )(
62
+ const scope string comp, auto ref const scope A a, auto ref const scope B b)
63
+ if (B.length > 0 )
82
64
{
83
65
string [A.length + B.length] vals;
84
66
static foreach (idx; 0 .. A.length)
@@ -90,7 +72,7 @@ template _d_assert_fail2(A...) {
90
72
}
91
73
}
92
74
93
- // / Ditto
75
+ // Legacy definition
94
76
string _d_assert_fail (A, B)(const scope string comp, auto ref const scope A a, auto ref const scope B b)
95
77
{
96
78
/*
@@ -100,10 +82,10 @@ string _d_assert_fail(A, B)(const scope string comp, auto ref const scope A a, a
100
82
Hence, we can fake purity and @nogc-ness here.
101
83
*/
102
84
103
- string [ 1 ] valA = miniFormatFakeAttributes(a);
104
- string [ 1 ] valB = miniFormatFakeAttributes(b);
85
+ string valA = miniFormatFakeAttributes(a);
86
+ string valB = miniFormatFakeAttributes(b);
105
87
immutable token = invertCompToken(comp);
106
- return combine (valA[ ], token, valB[ ]);
88
+ return combine ([valA ], token, [valB ]);
107
89
}
108
90
109
91
// / Combines the supplied arguments into one string "valA token valB"
@@ -115,10 +97,16 @@ private string combine(const scope string[] valA, const scope string token,
115
97
(valB.length - 1 ) * 2 + 2 + token.length;
116
98
foreach (v; valA) totalLen += v.length;
117
99
foreach (v; valB) totalLen += v.length;
100
+
101
+ // Include braces when printing tuples
102
+ const printBraces = (valA.length + valB.length) > 2 ;
103
+ if (printBraces) totalLen += 4 ; // '(', ')' for both tuples
104
+
118
105
char [] buffer = cast (char []) pureAlloc(totalLen)[0 .. totalLen];
119
106
// @nogc-concat of "<valA> <comp> <valB>"
120
- static void formatTuple (scope char [] buffer, ref size_t n, in string [] vals)
107
+ static void formatTuple (scope char [] buffer, ref size_t n, in string [] vals, in bool printBraces )
121
108
{
109
+ if (printBraces) buffer[n++ ] = ' (' ;
122
110
foreach (idx, v; vals)
123
111
{
124
112
if (idx)
@@ -129,15 +117,16 @@ private string combine(const scope string[] valA, const scope string token,
129
117
buffer[n .. n + v.length] = v;
130
118
n += v.length;
131
119
}
120
+ if (printBraces) buffer[n++ ] = ' )' ;
132
121
}
133
122
134
123
size_t n;
135
- formatTuple(buffer, n, valA);
124
+ formatTuple(buffer, n, valA, printBraces );
136
125
buffer[n++ ] = ' ' ;
137
126
buffer[n .. n + token.length] = token;
138
127
n += token.length;
139
128
buffer[n++ ] = ' ' ;
140
- formatTuple(buffer, n, valB);
129
+ formatTuple(buffer, n, valB, printBraces );
141
130
return (() @trusted => cast (string ) buffer)();
142
131
}
143
132
@@ -206,7 +195,8 @@ private string miniFormat(V)(const scope ref V v)
206
195
207
196
// Format invalid enum values as their base type
208
197
enum cast_ = " cast(" ~ V.stringof ~ " )" ;
209
- return combine (cast_, " " , miniFormat(* (cast (BaseType* ) &v)));
198
+ const val = miniFormat(* (cast (BaseType* ) &v));
199
+ return combine ([ cast_ ], " " , [ val ]);
210
200
}
211
201
else static if (is (V == bool ))
212
202
{
0 commit comments