www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18471] New: std.experimental.checkedint.Checked doesn't check

https://issues.dlang.org/show_bug.cgi?id=18471

          Issue ID: 18471
           Summary: std.experimental.checkedint.Checked doesn't check on
                    assignment or construction
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: simen.kjaras gmail.com

When constructing or assigning to a Checked!int with a custom hook that define
min and max, these min and max values are ignored.

import std.experimental.checkedint;

struct MyHook {
    enum min(T) = 3;
    enum max(T) = 15;

    static B onLowerBound(T, B)(T value, B bound)
    {
        assert(0);
    }

    static B onUpperBound(T, B)(T value, B bound)
    {
        assert(0);
    }
}

unittest
{
    // This should trigger one of the above asserts:
    Checked!(int, MyHook) a = 500;
    // As should this:
    a = 22;
    // Instead, Checked blithely ignores the limits, and this assert triggers:
    assert(a != 22);
}

--
Feb 19 2018