Bug 12835 – std.random.uniform with open lower bound cannot support smaller integral types or character types

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2014-06-01T14:10:00Z
Last change time
2014-07-11T22:19:34Z
Assigned to
nobody
Creator
joseph.wakeling

Attachments

IDFilenameSummaryContent-TypeSize
1359uniform.dCode snippet illustrating the problemtext/x-dsrc512

Comments

Comment #0 by joseph.wakeling — 2014-06-01T14:10:10Z
Created attachment 1359 Code snippet illustrating the problem std.random.uniform will fail to compile if an open lower bound is specified (i.e. boundaries == "(]" or "()") and either: * the type requested is a character type, or * the type requested is an integral type smaller than int (i.e. ushort, short, ubyte, byte). The attached code sample will allow the problem to be seen: attempting to compile it results in errors: /opt/dmd/import/std/random.d(1336): Error: cannot implicitly convert expression (cast(int)a + 1) of type int to ushort /opt/dmd/import/std/random.d(1197): Error: template instance std.random.uniform!("()", ushort, ushort, MersenneTwisterEngine!(uint, 32LU, 624LU, 397LU, 31LU, 2567483615u, 11LU, 7LU, 2636928640u, 15LU, 4022730752u, 18LU)) error instantiating uniform.d(10): instantiated from here: uniform!("()", ushort, ushort) uniform.d(21): instantiated from here: uniformTest!ushort ... and so on for the other unsupported types. Note that this problem persists whether the uniform() is the recently-updated one provide in PR 1717 or the uniform preceding it, so we can't simply solve the problem by reverting a patch.
Comment #1 by joseph.wakeling — 2014-06-01T14:39:56Z
If we look at the problematic line in the _old_ std.random (i.e. before PR 1717 was merged), we have the following: ResultType min = cast(ResultType) a + 1; I'm presuming that this is probably simply a typo and that line should read, ResultType min = cast(ResultType) (a + 1); .... ?
Comment #2 by joseph.wakeling — 2014-06-01T15:28:25Z
Comment #3 by github-bugzilla — 2014-07-11T22:19:33Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/88d86ecda0c53568e4c7036ec85aa7a5a27438f9 Fix Issue #12835: open lower bound for integral-type uniform() Added a cast to ensure the setting of lower bound can handle character types and integral types smaller than int. The additional unittests should prevent such issues from arising again. https://github.com/D-Programming-Language/phobos/commit/9e96ee3d38b563fb70c8761a0ccee663d59810cc Merge pull request #2221 from WebDrake/uniform-lowerbound Fix Issue #12835: open lower bound for integral-type uniform()