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

Commit 7bfd504

Browse files
authored
Merge pull request #3044 from MoonlightSentinel/shared-assert
Fix Issue 20748 - Deprecation for assert using shared type and checka… merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents ce26f60 + 62ae7ad commit 7bfd504

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/core/internal/dassert.d

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,21 @@ private string miniFormat(V)(const ref V v)
7575
import core.internal.traits: isAggregateType;
7676
import core.stdc.stdio : sprintf;
7777
import core.stdc.string : strlen;
78-
static if (is(V : bool))
78+
79+
static if (is(V == shared T, T))
80+
{
81+
// Use atomics to avoid race conditions whenever possible
82+
static if (__traits(compiles, atomicLoad(v)))
83+
{
84+
T tmp = cast(T) atomicLoad(v);
85+
return miniFormat(tmp);
86+
}
87+
else
88+
{ // Fall back to a simple cast - we're violating the type system anyways
89+
return miniFormat(*cast(T*) &v);
90+
}
91+
}
92+
else static if (is(V == bool))
7993
{
8094
return v ? "true" : "false";
8195
}
@@ -189,6 +203,11 @@ private string miniFormat(V)(const ref V v)
189203
}
190204
}
191205

206+
// This should be a local import in miniFormat but fails with a cyclic dependency error
207+
// core.thread.osthread -> core.time -> object -> core.internal.array.capacity
208+
// -> core.atomic -> core.thread -> core.thread.osthread
209+
import core.atomic : atomicLoad;
210+
192211
// Inverts a comparison token for use in _d_assert_fail
193212
private string invertCompToken(string comp)
194213
{

test/exceptions/src/assert_fail.d

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void testIntegers()()
2929
test(uint.min, uint.max, "0 != 4294967295");
3030
test(long.min, long.max, "-9223372036854775808 != 9223372036854775807");
3131
test(ulong.min, ulong.max, "0 != 18446744073709551615");
32+
test(shared(ulong).min, shared(ulong).max, "0 != 18446744073709551615");
3233

3334
int testFun() { return 1; }
3435
test(testFun(), 2, "1 != 2");
@@ -79,6 +80,9 @@ void testToString()()
7980
}
8081
test(new Foo("a"), new Foo("b"), "Foo(a) != Foo(b)");
8182

83+
scope f = cast(shared) new Foo("a");
84+
test!"!="(f, f, "Foo(a) == Foo(a)");
85+
8286
// Verifiy that the const toString is selected if present
8387
static struct Overloaded
8488
{
@@ -135,6 +139,9 @@ void testStruct()()
135139

136140
NoCopy n;
137141
test(_d_assert_fail!"!="(n, n), "NoCopy() == NoCopy()");
142+
143+
shared NoCopy sn;
144+
test(_d_assert_fail!"!="(sn, sn), "NoCopy() == NoCopy()");
138145
}
139146

140147
void testAA()()

0 commit comments

Comments
 (0)