[Issue 10336] template opDispatch to function delegate undefined behavior

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jun 11 12:20:04 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=10336



--- Comment #1 from Gabriel Garcia <d+bugzilla at 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 at 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 at 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: -------


More information about the Digitalmars-d-bugs mailing list