[Issue 2023] New: Returning from foreach body doesn't work as expected.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Apr 21 02:59:29 PDT 2008


http://d.puremagic.com/issues/show_bug.cgi?id=2023

           Summary: Returning from foreach body doesn't work as expected.
           Product: D
           Version: 2.012
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: samukha at voliacable.com


The bug is reproduced by the following code. Instead of making foo return, the
'return' statement in foo's foreach body breaks the loop:

----
struct S
{
    int a[];

    int opApply(int delegate(ref int) dg)
    {        
        foreach (x; a)
        {
            if(dg(x))
                return 1;
        }

        return 0;
    }
}

void foo()
{
    auto s = S([1, 2, 3]);

    foreach (x; s)
    {
        if (x == 1)
            return; // breaks the loop instead of returning from foo.
    }

    assert(false); // should never reach here.
}

void main()
{
    foo();   
}
----


-- 



More information about the Digitalmars-d-bugs mailing list