D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7787 - Anonymous interface instantiation returned from anonymous function misbehaves
Summary: Anonymous interface instantiation returned from anonymous function misbehaves
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: 2012-03-27 10:18 UTC by Tim Shea
Modified: 2024-12-13 17:59 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 Tim Shea 2012-03-27 10:18:58 UTC
I asked about this issue on stackoverflow, thinking I had made a mistake. The url is http://stackoverflow.com/questions/9752414/why-do-i-need-opcmp-for-an-anonymous-class and includes details on how and why I discovered the problem.

Using DMD on Windows, if I return an anonymous class derived directly from an interface from an anonymous function, the results are erratic and unpredictable. I believe this is because the vtbl layout is not consistent between the anonymous function creating the object and any clients of the object. An example:

interface TestInterface {
    string helloWorld();
}
class TestClass {
    abstract string helloWorld();
}

void invokeFn(TestInterface function() f) {
    auto t = f();
    auto s = t.helloWorld();
    writeln(s);
}

unittest {
    auto f = function() {
        return new class TestInterface {
            string helloWorld() {
                return "Hello World!";
            }
        };
    };
    // Invokes (f()).toString(), printing:
    // "src.utilities.testopcmp.__unittest2.__funcliteral1.__anonclass10"
    invokeFn(f);
}

void invokeFn(TestClass function() f) {
    auto t = f();
    auto s = t.helloWorld();
    writeln(s);
}

unittest {
    auto f = function() {
        return new class TestClass {
            string helloWorld() {
                return "Goodbye World!";
            }
        };
    };
    // Invokes (f()).helloWorld(), printing:
    // "Goodbye World!" as expected
    invokeFn(f);
}

This may be a minor bug in the compiler or in object.d? If so, then a fix would be appreciated. However, as abstract classes work correctly under the same circumstances, if the issue is that interfaces *should not* be anonymously instantiated (actually seems pretty reasonable) then that could be documented and the above code could throw an error. In that case, even if the interface is fixed, the anonymous function could probably declare a static nested abstract class, and inherit that class. Thanks!
Comment 1 dlangBugzillaToGithub 2024-12-13 17:59:18 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

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

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