Skip to content

Commit

Permalink
Merge pull request #4905 from MartinNowak/fix16667
Browse files Browse the repository at this point in the history
fix Issue 16667 - wrong @safe unittest compilation error
  • Loading branch information
Михаил Страшун committed Nov 20, 2016
2 parents de688d9 + ea55c96 commit 2a6b436
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions std/conv.d
Original file line number Diff line number Diff line change
Expand Up @@ -5552,42 +5552,49 @@ template castFrom(From)

return cast(To) value;
}
}

///
@safe unittest
///
@system unittest
{
// Regular cast, which has been verified to be legal by the programmer:
{
// Regular cast, which has been verified to be legal by the programmer:
{
long x;
auto y = cast(int) x;
}
long x;
auto y = cast(int) x;
}

// However this will still compile if 'x' is changed to be a pointer:
{
long* x;
auto y = cast(int) x;
}
// However this will still compile if 'x' is changed to be a pointer:
{
long* x;
auto y = cast(int) x;
}

// castFrom provides a more reliable alternative to casting:
{
long x;
auto y = castFrom!long.to!int(x);
}
// castFrom provides a more reliable alternative to casting:
{
long x;
auto y = castFrom!long.to!int(x);
}

// Changing the type of 'x' will now issue a compiler error,
// allowing bad casts to be caught before it's too late:
{
long* x;
static assert (
!__traits(compiles, castFrom!long.to!int(x))
);
// Changing the type of 'x' will now issue a compiler error,
// allowing bad casts to be caught before it's too late:
{
long* x;
static assert (
!__traits(compiles, castFrom!long.to!int(x))
);

// if cast is still needed, must be changed to:
auto y = castFrom!(long*).to!int(x);
}
// if cast is still needed, must be changed to:
auto y = castFrom!(long*).to!int(x);
}
}

// https://issues.dlang.org/show_bug.cgi?id=16667
unittest
{
ubyte[] a = ['a', 'b', 'c'];
assert(castFrom!(ubyte[]).to!(string)(a) == "abc");
}

/**
Check the correctness of a string for $(D hexString).
The result is true if and only if the input string is composed of whitespace
Expand Down

0 comments on commit 2a6b436

Please sign in to comment.