D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19926 - Wrong instantiation of opDispatch when used in WithStatement
Summary: Wrong instantiation of opDispatch when used in WithStatement
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-05-31 08:53 UTC by Simen Kjaeraas
Modified: 2024-12-13 19:03 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simen Kjaeraas 2019-05-31 08:53:09 UTC
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);
    }
}
Comment 1 dlangBugzillaToGithub 2024-12-13 19:03:36 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19572

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB