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

Lambda with default initializer gets called with random values instead #19440

Open
dlangBugzillaToGithub opened this issue May 26, 2018 · 2 comments
Labels

Comments

@dlangBugzillaToGithub
Copy link

Malte reported this on 2018-05-26T16:12:25Z

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

CC List

  • ag0aep6g
  • Hiroki Noda

Description

The following code should print 2 but writes random numbers instead:

void main() @safe
{
    import std.stdio : writeln;

    writeln(identity(2));
}

int identity(immutable int q) pure nothrow @safe @nogc
{
    import std.algorithm : map;

    static immutable auto arr = [42];
    int getSecondArgument(int a, int b)
    {
        return b;
    }

    return arr.map!((int a, int b = q) => getSecondArgument(a, b))[0];
}
@dlangBugzillaToGithub
Copy link
Author

ag0aep6g commented on 2018-05-26T16:37:31Z

Reduced Phobos away:

----
void main() @safe
{
    assert(identity(2) == 2); /* fails; should pass */
}

struct Map(alias fun)
{
    int front() @safe { return fun(); }
}

int identity(immutable int q) pure nothrow @safe @nogc
{
    int getArg(int b) @safe
    {
        return b;
    }
    return Map!((int b = q) => getArg(b))().front();
}
----

@dlangBugzillaToGithub
Copy link
Author

kubo39 commented on 2019-04-27T07:53:53Z

This should fail to compile, and in this case you can use `lazy int b = q` and will work.

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

No branches or pull requests

1 participant