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

Commit 1456451

Browse files
Additional fixups for the initial implementation
- removed old hooks for test purposes - added template constraints to resolve ambiguitess with unary hook - added/fixed template parameters in `dassert_fail` test
1 parent c45aca0 commit 1456451

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

src/core/internal/dassert.d

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,6 @@
1919
*/
2020
module core.internal.dassert;
2121

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-
3422
/**
3523
* Generates rich assert error messages for unary expressions
3624
*
@@ -47,14 +35,6 @@ string _d_assert_fail(string comp, A, B)(auto ref const scope A a, auto ref cons
4735
* Returns:
4836
* A string such as "$a != true" or "$a == true".
4937
*/
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
5838
string _d_assert_fail(A)(const scope string op, auto ref const scope A a)
5939
{
6040
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)
7656
* Returns:
7757
* A string such as "$a $comp $b".
7858
*/
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)
8264
{
8365
string[A.length + B.length] vals;
8466
static foreach (idx; 0 .. A.length)
@@ -90,7 +72,7 @@ template _d_assert_fail2(A...) {
9072
}
9173
}
9274

93-
/// Ditto
75+
// Legacy definition
9476
string _d_assert_fail(A, B)(const scope string comp, auto ref const scope A a, auto ref const scope B b)
9577
{
9678
/*
@@ -100,10 +82,10 @@ string _d_assert_fail(A, B)(const scope string comp, auto ref const scope A a, a
10082
Hence, we can fake purity and @nogc-ness here.
10183
*/
10284

103-
string[1] valA = miniFormatFakeAttributes(a);
104-
string[1] valB = miniFormatFakeAttributes(b);
85+
string valA = miniFormatFakeAttributes(a);
86+
string valB = miniFormatFakeAttributes(b);
10587
immutable token = invertCompToken(comp);
106-
return combine(valA[], token, valB[]);
88+
return combine([valA], token, [valB]);
10789
}
10890

10991
/// 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,
11597
(valB.length - 1) * 2 + 2 + token.length;
11698
foreach (v; valA) totalLen += v.length;
11799
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+
118105
char[] buffer = cast(char[]) pureAlloc(totalLen)[0 .. totalLen];
119106
// @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)
121108
{
109+
if (printBraces) buffer[n++] = '(';
122110
foreach (idx, v; vals)
123111
{
124112
if (idx)
@@ -129,15 +117,16 @@ private string combine(const scope string[] valA, const scope string token,
129117
buffer[n .. n + v.length] = v;
130118
n += v.length;
131119
}
120+
if (printBraces) buffer[n++] = ')';
132121
}
133122

134123
size_t n;
135-
formatTuple(buffer, n, valA);
124+
formatTuple(buffer, n, valA, printBraces);
136125
buffer[n++] = ' ';
137126
buffer[n .. n + token.length] = token;
138127
n += token.length;
139128
buffer[n++] = ' ';
140-
formatTuple(buffer, n, valB);
129+
formatTuple(buffer, n, valB, printBraces);
141130
return (() @trusted => cast(string) buffer)();
142131
}
143132

@@ -206,7 +195,8 @@ private string miniFormat(V)(const scope ref V v)
206195

207196
// Format invalid enum values as their base type
208197
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 ]);
210200
}
211201
else static if (is(V == bool))
212202
{

test/exceptions/src/assert_fail.d

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import core.internal.dassert : _d_assert_fail;
33

44
void test(string comp = "==", A, B)(A a, B b, string msg, size_t line = __LINE__)
55
{
6-
test(_d_assert_fail!(A, B)(comp, a, b), msg, line);
6+
test(_d_assert_fail!(A)(comp, a, b), msg, line);
77
}
88

99
void test(const string actual, const string expected, size_t line = __LINE__)
@@ -167,10 +167,10 @@ void testStruct()()
167167
}
168168

169169
NoCopy n;
170-
test(_d_assert_fail("!=", n, n), "NoCopy() == NoCopy()");
170+
test(_d_assert_fail!(typeof(n))("!=", n, n), "NoCopy() == NoCopy()");
171171

172172
shared NoCopy sn;
173-
test(_d_assert_fail("!=", sn, sn), "NoCopy() == NoCopy()");
173+
test(_d_assert_fail!(typeof(sn))("!=", sn, sn), "NoCopy() == NoCopy()");
174174
}
175175

176176
void testAA()()
@@ -180,16 +180,18 @@ void testAA()()
180180
test!"in"("foo", ["bar": true], `"foo" !in ["bar": true]`);
181181
}
182182

183-
184183
void testAttributes() @safe pure @nogc nothrow
185184
{
186185
int a;
187-
string s = _d_assert_fail("==", a, 0);
188-
string s2 = _d_assert_fail("!", a);
186+
string s = _d_assert_fail!(int, char)("==", a, 'c', 1, 'd');
187+
assert(s == `(0, 'c') != (1, 'd')`);
188+
189+
string s2 = _d_assert_fail!int("", a);
190+
assert(s2 == `0 != true`);
189191

190192
// Test instatiation of legacy hooks
191-
s = _d_assert_fail!"=="(a, 0);
192-
s2 = _d_assert_fail!"!"(a);
193+
s2 = _d_assert_fail("==", a, 1);
194+
assert(s2 == `0 != 1`);
193195
}
194196

195197
// https://issues.dlang.org/show_bug.cgi?id=20066
@@ -227,13 +229,13 @@ void testEnum()
227229

228230
ubyte[] data;
229231
enum ctfe = UUID();
230-
test(_d_assert_fail("==", ctfe.data, data), "[1] != []");
232+
test(_d_assert_fail!(ubyte[])("==", ctfe.data, data), "[1] != []");
231233
}
232234

233235
void testUnary()
234236
{
235-
test(_d_assert_fail("", 9), "9 != true");
236-
test(_d_assert_fail("!", [1, 2, 3]), "[1, 2, 3] == true");
237+
test(_d_assert_fail!int("", 9), "9 != true");
238+
test(_d_assert_fail!(int[])("!", [1, 2, 3]), "[1, 2, 3] == true");
237239
}
238240

239241
void main()

0 commit comments

Comments
 (0)