Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions std/experimental/checkedint.d
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,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 @@ -1085,7 +1085,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 @@ -1248,7 +1248,7 @@ static:
}
}

@system unittest
@safe unittest
{
void test(T)()
{
Expand Down Expand Up @@ -1457,8 +1457,24 @@ default behavior.
*/
struct Warn
{
import std.stdio : stderr;
static:

private void _writefln(Args...)(Args args)
{
import std.format : format;
import std.functional : forward;
import std.stdio : stderr;
auto msg = format(args);
() @trusted {
import core.thread : thread_resumeAll, thread_suspendAll;
thread_suspendAll;
scope(exit) thread_resumeAll;
scope(failure) assert(0, "Thread suspension while printing'" ~ msg ~ "' from CheckedInt failed.");
// this can be done @safe-ly, because it happens just before the program terminates
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warn just logs right? Whats this about program termination?

// and all other threads have been suspended
stderr.lockingTextWriter.put(msg);
}();
}
/**

Called automatically upon a bad cast from `src` to type `Dst` (one that
Expand All @@ -1474,7 +1490,7 @@ static:
*/
Dst onBadCast(Dst, Src)(Src src)
{
stderr.writefln("Erroneous cast: cast(%s) %s(%s)",
_writefln("Erroneous cast: cast(%s) %s(%s)",
Dst.stringof, Src.stringof, src);
return cast(Dst) src;
}
Expand All @@ -1493,14 +1509,14 @@ static:
*/
Lhs onLowerBound(Rhs, T)(Rhs rhs, T bound)
{
stderr.writefln("Lower bound error: %s(%s) < %s(%s)",
_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)",
_writefln("Upper bound error: %s(%s) > %s(%s)",
Rhs.stringof, rhs, T.stringof, bound);
return cast(T) rhs;
}
Expand All @@ -1527,7 +1543,7 @@ static:
auto result = opChecked!"=="(lhs, rhs, error);
if (error)
{
stderr.writefln("Erroneous comparison: %s(%s) == %s(%s)",
_writefln("Erroneous comparison: %s(%s) == %s(%s)",
Lhs.stringof, lhs, Rhs.stringof, rhs);
return lhs == rhs;
}
Expand Down Expand Up @@ -1566,7 +1582,7 @@ static:
auto result = opChecked!"cmp"(lhs, rhs, error);
if (error)
{
stderr.writefln("Erroneous ordering comparison: %s(%s) and %s(%s)",
_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 @@ -1599,21 +1615,21 @@ static:
*/
typeof(~Lhs()) onOverflow(string x, Lhs)(ref Lhs lhs)
{
stderr.writefln("Overflow on unary operator: %s%s(%s)",
_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)",
_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