D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3154 - foreach corrupting data returned by ref functions.
Summary: foreach corrupting data returned by ref functions.
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 major
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2009-07-08 13:43 UTC by Serg Kovrov
Modified: 2015-06-09 01:28 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Serg Kovrov 2009-07-08 13:43:11 UTC
void main()
{
    auto test = Foo(3);
    foreach (i; test.items) { }
}

struct Foo
{
    struct Generator
    {
        int opApply(int delegate(ref long) dg)
        {
            const bak = this.foo.data;  // control copy
            int res;
            for (long i=0; i < bak; i++)
            {
                assert (bak == this.foo.data, "this.foo changed?!");
                res = dg(i);
                if (res)
                    break;
            }
            return res;
        }
        Foo foo;
    }
    ref Generator items() // ref is important
    {
        return Generator(this); // Generator.foo = this
    }
    int data;
}
Comment 1 Don 2010-05-06 23:53:17 UTC
Fixed DMD 2.034.