www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10336] New: template opDispatch to function delegate undefined behavior

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10336

           Summary: template opDispatch to function delegate undefined
                    behavior
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: d+bugzilla garciat.com


--- Comment #0 from Gabriel Garcia <d+bugzilla garciat.com> 2013-06-11 10:09:55
PDT ---
DMD 2.063

Code:

struct test {
    template opDispatch(string name) {
        enum opDispatch = function(int x) {
            return x;
        };
    }
}

int main() {
    test t;

    return t.hello(12);
}

Output is consistently 0.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 11 2013
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10336



--- Comment #1 from Gabriel Garcia <d+bugzilla garciat.com> 2013-06-11 12:20:03
PDT ---
(In reply to comment #0)
 DMD 2.063
 
 Code:
 
 struct test {
     template opDispatch(string name) {
         enum opDispatch = function(int x) {
             return x;
         };
     }
 }
 
 int main() {
     test t;
 
     return t.hello(12);
 }
 
 Output is consistently 0.
It appears that the caller is treating the method as a regular/static function, while the function itself is behaving as a method. This is perhaps more evident through the following D code and the corresponding (non-optimized) generated code: struct test { int member = 32; template opDispatch(string name) { enum opDispatch = function(int x) { return x + member; }; } } int main() { test t; return t.hello(12); } (OK) Expects EAX to point to an instance of test, with parameters pushed to stack: pure nothrow safe int test.test.opDispatch!("hello").__funcliteral1(int): push EBP mov EBP,ESP sub ESP,4 mov EAX,[EAX] add EAX,8[EBP] leave ret 4 nop (ERROR) Disregards it is dealing with a method call: _Dmain: push EBP mov EBP,ESP mov EAX,_D4test4test6__initZ SYM32 ; what? mov EAX,0Ch call pure nothrow safe int test.test.opDispatch!("hello").__funcliteral1(int) PC32 pop EBP ret Expected: _Dmain: push EBP mov EBP,ESP ; <begin missing> sub ESP,4 mov EAX,_D4test4test6__initZ SYM32 mov -4[EBP],EAX push 0Ch lea EAX,-4[EBP] ; <end missing> call pure nothrow safe int test.test.opDispatch!("hello").__funcliteral1(int) PC32 leave ret -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 11 2013