Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return in the body of a foreach in a constructor backed by opApply corrupts the object #19184

Open
dlangBugzillaToGithub opened this issue Aug 31, 2016 · 2 comments

Comments

@dlangBugzillaToGithub
Copy link

André Puel reported this on 2016-08-31T17:45:38Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=16454

CC List

  • ag0aep6g

Description

import std.stdio;

struct OpApply {
    int opApply(int delegate(int) cb) {
        return cb(42);
    }
}

struct Bolinha {
    int a;
    this(ref OpApply moviadao) {
        foreach(int b; moviadao) {
            this.a = b;
            return;
        }
    }
};

void main() {
    import std.stdio;

    OpApply range;
    writeln(Bolinha(range).a);
}

The expected print result was 42. But I get a random memory garbage instead. The following minor modification in main will 'fix' the issue:

void main() {
    import std.stdio;

    OpApply range;
    Bolinha littleBall = Bolinha(range);
    writeln(littleBall.a);
}
@dlangBugzillaToGithub
Copy link
Author

ag0aep6g commented on 2016-08-31T18:14:51Z

Slightly reduced:

----
struct OpApply {
    int opApply(int delegate(int) cb) { return 0; }
}

struct Bolinha {
    int a = 0;
    this(int dummy) {
        OpApply moviadao;
        foreach(int b; moviadao) return;
    }
}

void main() {
    import std.stdio;
    writeln(Bolinha(0).a);
}
----

Also fails on Linux and Windows (wine).

@dlangBugzillaToGithub
Copy link
Author

moonlightsentinel commented on 2021-10-26T13:19:53Z

Works reliably since 2.094.1 according to run.dlang.io / local tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant