www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9822] New: cartesianProduct broken for array literals

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

           Summary: cartesianProduct broken for array literals
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: m.strashun gmail.com



Motivating code snippet:

-----
module test;    

import std.algorithm, std.traits, std.stdio;

enum r1 = [1, 2, 3];
auto product = cartesianProduct(r1, r1);

void main()
{
    writeln(product);    
}
-----
Segmentation fault
-----

What is expected: either working code, or compile-time error.

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


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



A reduction:


struct MapResult(alias fun) {
    int[] _input;

     property bool empty() {
        return _input.length == 0;
    }

    void popFront() {
        _input = _input[1 .. $];
    }

     property auto ref front() {
        return fun(1);
    }
}

auto map(alias fun)(int[] r) {
    return MapResult!(fun)(r);
}

auto foo(int[] r) {
    return map!(x => r)([1]);
}

enum r1 = [1];
auto result = foo(r1);

void main() {
    foreach (t; result) {}
}

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




Huh, this is really strange. Moving the cartesianProduct line inside main()
makes the problem go away. Why?

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




Hmph. I think this is a compiler bug. Putting the code inside a unittest block
makes the problem go away too.

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


hsteoh quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
            Summary|cartesianProduct broken for |Segfault when referencing
                   |array literals              |module-global auto variable
                   |                            |containing result of
                   |                            |std.algorithm.map.



Retitled this bug based on bearophile's reduction, and comparison of the
disassembly of buggy version (result assigned to module-global variable) vs.
non-buggy version (result assigned to local variable); this looks like a
wrong-code bug.

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


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim maxim-fomin.ru
          Component|Phobos                      |DMD
            Summary|Segfault when referencing   |Using  module variable of
                   |module-global auto variable |templated type parametrized
                   |containing range that       |by lambda
                   |updates array literal       |
           Severity|normal                      |critical



---
Simplified: 

struct MapResult(alias fun) {
     property auto ref front() {
        return fun(1);
    }
}

auto map(alias fun)() {
    return MapResult!(fun)();
}

auto foo(int[] r) {
    return map!((int x) => r)();
}

auto result = foo([1]);

void main() {
    result.front();
}

There is wrong-code on accessing or passing module object. It has nothing to do
with auto or ranges.

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