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


--- Comment #0 from Andrea Fontana <trikkuz gmail.com> 2013-02-27 15:20:41 PST
---
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


--- Comment #1 from bearophile_hugs eml.cc 2013-02-27 15:26:58 PST ---
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


--- Comment #2 from Brad Anderson <eco gnuk.net> 2013-02-27 16:14:00 PST ---
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



--- Comment #3 from bearophile_hugs eml.cc 2013-02-27 16:29:41 PST ---
(In reply to comment #2)
 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



--- Comment #4 from Brad Anderson <eco gnuk.net> 2013-02-27 16:34:43 PST ---
(In reply to comment #3)
 (In reply to comment #2)
 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



--- Comment #5 from github-bugzilla puremagic.com 2013-02-27 20:02:38 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/bc4031066ab7d4c2221d1241800d95c85910007b
Merge pull request #1183 from eco/patch-1

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



--- Comment #6 from github-bugzilla puremagic.com 2013-03-11 03:17:27 PDT ---
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

From: #1183 :
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