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

Wrong instantiation of opDispatch when used in WithStatement #19572

Open
dlangBugzillaToGithub opened this issue May 31, 2019 · 0 comments
Open

Comments

@dlangBugzillaToGithub
Copy link

Simen Kjaeraas reported this on 2019-05-31T08:53:09Z

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

Description

DMD is of two minds when invoking opDispatch inside WithStatements, and requires that the template be possible to instantiate with the arguments passed, followed by instatiating it with an empty parameter tuple:

unittest {
    with (S1.init) {
        a(1); // Compiles
    }
    with (S2.init) {
        a(1); // is not callable using argument types (int)
    }
}

struct S1 {
    auto opDispatch(string name, Args...)(Args args) {
        static assert(Args.length == 0);
        return (int i){};
    }
}

struct S2 {
    auto opDispatch(string name)() {
        return (int i){};
    }
}

As the example with S2 shows, the compiler complains when opDispatch takes no parameters (this is sensible). However, as S1 shows, Args is actually empty when opDispatch is instantiated, and instead opDispatch needs to return a lambda that actually handles the run-time arguments (this is not sensible, on more than one level).

The correct behavior would of course be for this to compile and run:

unittest {
    with (S3.init) {
        a(1);
    }
}

struct S3 {
    auto opDispatch(string name, Args...)(Args args) {
        static assert(Args.length == 1);
        static assert(is(Args[0] == int));
        assert(args[0] == 1);
    }
}
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