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

Commit

Permalink
Fix Issue 20748 - Deprecation for assert using shared type and checka…
Browse files Browse the repository at this point in the history
…ction=context
  • Loading branch information
MoonlightSentinel committed Apr 19, 2020
1 parent ce26f60 commit 240cf2e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/core/internal/dassert.d
Expand Up @@ -75,7 +75,21 @@ private string miniFormat(V)(const ref V v)
import core.internal.traits: isAggregateType;
import core.stdc.stdio : sprintf;
import core.stdc.string : strlen;
static if (is(V : bool))

static if (is(V == shared T, T))
{
// Use atomics to avoid race conditions whenever possible
static if (__traits(compiles, atomicLoad(v)))
{
T tmp = atomicLoad(v);
return miniFormat(tmp);
}
else
{ // Fall back to a simple cast - we're violating the type system anyways
return miniFormat(*cast(T*) &v);
}
}
else static if (is(V == bool))
{
return v ? "true" : "false";
}
Expand Down Expand Up @@ -189,6 +203,11 @@ private string miniFormat(V)(const ref V v)
}
}

// This should be a local import in miniFormat but fails with a cyclic dependency error
// core.thread.osthread -> core.time -> object -> core.internal.array.capacity
// -> core.atomic -> core.thread -> core.thread.osthread
import core.atomic : atomicLoad;

// Inverts a comparison token for use in _d_assert_fail
private string invertCompToken(string comp)
{
Expand Down
4 changes: 4 additions & 0 deletions test/exceptions/src/assert_fail.d
Expand Up @@ -29,6 +29,7 @@ void testIntegers()()
test(uint.min, uint.max, "0 != 4294967295");
test(long.min, long.max, "-9223372036854775808 != 9223372036854775807");
test(ulong.min, ulong.max, "0 != 18446744073709551615");
test(shared(ulong).min, shared(ulong).max, "0 != 18446744073709551615");

int testFun() { return 1; }
test(testFun(), 2, "1 != 2");
Expand Down Expand Up @@ -135,6 +136,9 @@ void testStruct()()

NoCopy n;
test(_d_assert_fail!"!="(n, n), "NoCopy() == NoCopy()");

shared NoCopy sn;
test(_d_assert_fail!"!="(sn, sn), "NoCopy() == NoCopy()");
}

void testAA()()
Expand Down

0 comments on commit 240cf2e

Please sign in to comment.