Skip to content

Commit

Permalink
Issue 18024 - checkedint.Warn should be @safe
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed Dec 15, 2017
1 parent 5964600 commit 44a4d9a
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions std/experimental/checkedint.d
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ module std.experimental.checkedint;
import std.traits : isFloatingPoint, isIntegral, isNumeric, isUnsigned, Unqual;

///
@system unittest
@safe unittest
{
int[] concatAndAdd(int[] a, int[] b, int offset)
{
Expand Down Expand Up @@ -994,7 +994,7 @@ if (is(typeof(Checked!(T, Hook)(value))))
}

///
@system unittest
@safe unittest
{
static assert(is(typeof(checked(42)) == Checked!int));
assert(checked(42) == Checked!int(42));
Expand Down Expand Up @@ -1157,7 +1157,7 @@ static:
}
}

@system unittest
@safe unittest
{
void test(T)()
{
Expand Down Expand Up @@ -1366,8 +1366,13 @@ default behavior.
*/
struct Warn
{
import std.stdio : stderr;
import std.stdio : File;
static:
private @property File trustedStderr() @trusted
{
import std.stdio : stderr;
return stderr;
}
/**
Called automatically upon a bad cast from `src` to type `Dst` (one that
Expand All @@ -1383,7 +1388,7 @@ static:
*/
Dst onBadCast(Dst, Src)(Src src)
{
stderr.writefln("Erroneous cast: cast(%s) %s(%s)",
trustedStderr.writefln("Erroneous cast: cast(%s) %s(%s)",
Dst.stringof, Src.stringof, src);
return cast(Dst) src;
}
Expand All @@ -1402,14 +1407,14 @@ static:
*/
Lhs onLowerBound(Rhs, T)(Rhs rhs, T bound)
{
stderr.writefln("Lower bound error: %s(%s) < %s(%s)",
trustedStderr.writefln("Lower bound error: %s(%s) < %s(%s)",
Rhs.stringof, rhs, T.stringof, bound);
return cast(T) rhs;
}
/// ditto
T onUpperBound(Rhs, T)(Rhs rhs, T bound)
{
stderr.writefln("Upper bound error: %s(%s) > %s(%s)",
trustedStderr.writefln("Upper bound error: %s(%s) > %s(%s)",
Rhs.stringof, rhs, T.stringof, bound);
return cast(T) rhs;
}
Expand All @@ -1418,7 +1423,7 @@ static:
Called automatically upon a comparison for equality. In case of an Erroneous
comparison (one that would make a signed negative value appear equal to an
unsigned positive value), writes a warning message to `stderr` as a side
unsigned positive value), writes a warning message to `trustedStderr` as a side
effect.
Params:
Expand All @@ -1436,7 +1441,7 @@ static:
auto result = opChecked!"=="(lhs, rhs, error);
if (error)
{
stderr.writefln("Erroneous comparison: %s(%s) == %s(%s)",
trustedStderr.writefln("Erroneous comparison: %s(%s) == %s(%s)",
Lhs.stringof, lhs, Rhs.stringof, rhs);
return lhs == rhs;
}
Expand All @@ -1458,7 +1463,7 @@ static:
Called automatically upon a comparison for ordering using one of the
operators `<`, `<=`, `>`, or `>=`. In case the comparison is erroneous (i.e.
it would make a signed negative value appear greater than or equal to an
unsigned positive value), then a warning message is printed to `stderr`.
unsigned positive value), then a warning message is printed to `trustedStderr`.
Params:
lhs = The first argument of `Checked`, e.g. `int` if the left-hand side of
Expand All @@ -1475,7 +1480,7 @@ static:
auto result = opChecked!"cmp"(lhs, rhs, error);
if (error)
{
stderr.writefln("Erroneous ordering comparison: %s(%s) and %s(%s)",
trustedStderr.writefln("Erroneous ordering comparison: %s(%s) and %s(%s)",
Lhs.stringof, lhs, Rhs.stringof, rhs);
return lhs < rhs ? -1 : lhs > rhs;
}
Expand Down Expand Up @@ -1508,21 +1513,21 @@ static:
*/
typeof(~Lhs()) onOverflow(string x, Lhs)(ref Lhs lhs)
{
stderr.writefln("Overflow on unary operator: %s%s(%s)",
trustedStderr.writefln("Overflow on unary operator: %s%s(%s)",
x, Lhs.stringof, lhs);
return mixin(x ~ "lhs");
}
/// ditto
typeof(Lhs() + Rhs()) onOverflow(string x, Lhs, Rhs)(Lhs lhs, Rhs rhs)
{
stderr.writefln("Overflow on binary operator: %s(%s) %s %s(%s)",
trustedStderr.writefln("Overflow on binary operator: %s(%s) %s %s(%s)",
Lhs.stringof, lhs, x, Rhs.stringof, rhs);
return mixin("lhs" ~ x ~ "rhs");
}
}

///
@system unittest
@safe unittest
{
auto x = checked!Warn(42);
short x1 = cast(short) x;
Expand Down

0 comments on commit 44a4d9a

Please sign in to comment.