www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9612] New: cycle() and opSlice

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9612

           Summary: cycle() and opSlice
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: trikkuz gmail.com



---
This line:

writeln(iota(10).cycle()[5..2].take(4));

prints:

[5, 6, 7, 8]

It seems some preconditions/tests are missing from cycle()

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9612


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid
                 CC|                            |bearophile_hugs eml.cc
            Summary|cycle() and opSlice         |std.range.Cycle.opSlice
                   |                            |tests on the bounds are
                   |                            |missing



I was about to file this bug myself, this is the text I was going to use:

This code:

void main() {
    auto a = [0,1,2,3,4,5,6,7,8,9][5 .. 2];
}


Gives the correct compile-time error:

temp3.d(2): Error: array slice [5 .. 2] is out of bounds
temp3.d(2): Error: array slice [5 .. 2] is out of bounds



While this gives no compile-time nor run-time errors:

import std.stdio: writeln;
import std.range: iota, cycle, take;
void main() {
    iota(10).cycle()[5 .. 2].take(4).writeln();
}

And prints (DMD 2.063alpha):

[5, 6, 7, 8]


The opSlice of cycle() lacks pre-conditions or tests, and there are not enough
unittests to catch this bug:

        auto opSlice(size_t i, size_t j)
        {
            auto retval = this.save;
            retval._index += i;
            return takeExactly(retval, j - i);
        }


j - i is positive because those numbers are unsigned, and because D lacks
run-time errors for integral overflows:


import std.stdio;
struct Foo {
    auto opSlice(size_t i, size_t j) {
        writeln(j - i);
    }
}
void main() {
    Foo f;
    f[5 .. 2];
}


Output:
4294967293


Maybe there is a need to run something similar to QuickCheck on Phobos:
http://en.wikipedia.org/wiki/QuickCheck

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9612


Brad Anderson <eco gnuk.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |eco gnuk.net



https://github.com/D-Programming-Language/phobos/pull/1183

should take care of this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9612





 https://github.com/D-Programming-Language/phobos/pull/1183
 
 should take care of this.
Maybe that RangeError() should have a message that tells what's the error. Something like: RangeError(text(i, " > ", j)) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 27 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9612






 https://github.com/D-Programming-Language/phobos/pull/1183
 
 should take care of this.
Maybe that RangeError() should have a message that tells what's the error. Something like: RangeError(text(i, " > ", j))
Good idea. Done. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 27 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9612




Commit pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/bc4031066ab7d4c2221d1241800d95c85910007b


Issue 9612: Cycle opSlice should throw when finish > start

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9612


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |jmdavisProg gmx.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9612




Commit pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/dd1a80c56fb848003fb8c3028bc4555ce8388e44
Fixup for cycle RangeError invocation


Issue 9612: Cycle opSlice should throw when finish > start

Because a RangeError is not actually customizable, and the first argument is
actually the file name. The error was producing:
```
core.exception.RangeError 2 > 1(3836): Range violation
```

Now it produces:
```
core.exception.RangeError std\range.d(3835): Range violation
```

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 11 2013