www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4277] New: delegate reference wrong scope var value

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

           Summary: delegate reference wrong scope var value
           Product: D
           Version: 1.057
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: changlon gmail.com



-------------------------------------------------------------------------------
extern(C) void printf(char*, ... );

interface MyCall{
    void call(int);
}

class MyObject{
    MyCall[] list;

    void add(MyCall _mycall){
        list    ~= _mycall ;
    }

    void call(int i){
        foreach( c ; list) {
            c.call(i);
        }
    }
}


class MyTest {
    public void run(){
        MyObject obj    = new MyObject ;

        test3(obj);
        test2(obj);
        test1(obj);

        obj.call(3);
    }

    static void test1(char[] name = "test1\0" )(MyObject obj){
        printf("out: `%s` = `%x` \n\0".ptr ,  name.ptr, cast(void*) obj );
        obj.add( new class( delegate(int i){
            printf("in: `%s` = `%x` \n\0".ptr ,  name.ptr, cast(void*) obj );
        } ) MyCall{
            void delegate(int) dg;
            this(void delegate(int) _dg){
                this.dg    = _dg;
            }
            void call(int i) {
                dg(i);
            }
        } ) ;
    }

    void test2(MyObject obj){
        test1!("test2\0")(obj);
    }

    void test3(ref MyObject obj){
        test1!("test3\0")(obj);
    }
}

void main(){
    auto t    = new MyTest;
    t.run;
}
-------------------------------------------------------------------------------

out: `test3` = `a42fe0` 
out: `test2` = `a42fe0` 
out: `test1` = `a42fe0` 
in: `test3` = `414204` 
in: `test2` = `414204` 
in: `test1` = `12fe88`

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 05 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4277




this block dwt, and dmd 1.062 beta still failure. but dmd2 is work.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 12 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4277


changlon <changlon gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|1.057                       |D1
         OS/Version|Windows                     |All



I test this bug in linux and windows, dmd1.062 print wrong address in linux
too.

dmd 2.047 is work for windows and linux.


I use obj2asm to compare the d1 and d2 object file, but i have no ideal where
is wrong .


http://gool.googlecode.com/files/bug4277.tar
this package include the bug2177.d (for both dmd1 and dmd2), 1.asm (from dmd1
generate bug2177.o file) and 2.asm (from dmd2 generate bug2177.o file).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 27 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4277





 I test this bug in linux and windows, dmd1.062 print wrong address in linux
 too.
 
 dmd 2.047 is work for windows and linux.
 
 
 I use obj2asm to compare the d1 and d2 object file, but i have no ideal where
 is wrong .
 
 
 http://gool.googlecode.com/files/bug4277.tar
 this package include the bug2177.d (for both dmd1 and dmd2), 1.asm (from dmd1
 generate bug2177.o file) and 2.asm (from dmd2 generate bug2177.o file).
sorry , i mean bug4277.d -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 27 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4277




the ldc output:
----------------------------
out: `test3` = `b761bfe0`
out: `test2` = `b761bfe0`
out: `test1` = `b761bfe0`
in: `test3` = `b761cfe0`
in: `test2` = `b761cfc0`
in: `test1` = `b761cfb4`
----------------------------


the dmd2 output:
----------------------------
out: `test3` = `b7617fe0`
out: `test2` = `b7617fe0`
out: `test1` = `b7617fe0`
in: `test3` = `b7617fe0`
in: `test2` = `b7617fe0`
in: `test1` = `b7617fe0`
---------------------------


the dmd1 output:
----------------------------
out: `test3` = `a42fe0` 
out: `test2` = `a42fe0` 
out: `test1` = `a42fe0` 
in: `test3` = `414204` 
in: `test2` = `414204` 
in: `test1` = `12fe88`
---------------------------

there should be a error in dmd1-front and  dmd-backend.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 28 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4277


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg gmail.com



Duh, how about a simpler testcase?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 28 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4277




this is simpler testcase:
-----------------------------------------------------
version( D_Version2 ){
    mixin("extern(C) void printf(const(char)*, ... ) ;");
}else{
    extern(C) void printf(char*, ... );
}


class MyTest {
    void delegate(int)[]    list ;

    public void run(){
        test3(this);
        test2(this);
        test1(this);
        foreach( dg; list) {
            dg(3);
        }
    }

    static void test1(string name = "test1\0" )(MyTest obj){
        printf("out: `%s` = `%x` \n\0".ptr ,  name.ptr, cast(void*) obj );
        void     add(int i){
            printf("in: `%s` = `%x` \n\0".ptr ,  name.ptr, cast(void*) obj );
        }
            obj.list    ~= &add;
    }

    void test2(MyTest obj){
        test1!("test2\0")(obj);
    }

    void test3(ref MyTest obj){
        test1!("test3\0")(obj);
    }
}

void main(){
    auto t    = new MyTest;
    t.run;
}
---------------------------------------------------------------------------

my os is ubuntu 10.4 i386,

dmd1:
out: `test3` = `b750eff0`
out: `test2` = `b750eff0`
out: `test1` = `b750eff0`
in: `test3` = `8050844`
in: `test2` = `8050860`
in: `test1` = `bf89037c`


dmd2:
out: `test3` = `b7494fe0`
out: `test2` = `b7494fe0`
out: `test1` = `b7494fe0`
in: `test3` = `b7494fe0`
in: `test2` = `b7494fe0`
in: `test1` = `b7494fe0`

ldc:
out: `test3` = `b7549ff0`
out: `test2` = `b7549ff0`
out: `test1` = `b7549ff0`
in: `test3` = `3`
in: `test2` = `3`
in: `test1` = `bfaae088`

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 28 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4277




You're creating a delegate of a nested function, and then return it. This is
not allowed in D1. It's like writing "int* foo() { int x; return x; }" and then
expecting the returned value is valid.

Returning delegates to nested functions is allowed in D2, though.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 28 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4277


changlon <changlon gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID



IF I understood correct, the dwt.widgets.Listener design by Frank Benoit  is
danger in d1.  the source is:

------------------------------------------
module dwt.widgets.Listener;

import dwt.widgets.Event;
import tango.core.Traits;
import tango.core.Tuple;

public interface Listener {
void handleEvent (Event event);
}


/// Helper class for the dgListener template function
private class _DgListenerT(Dg,T...) : Listener {

    alias ParameterTupleOf!(Dg) DgArgs;
    static assert( is(DgArgs == Tuple!(Event,T)),
                "Delegate args not correct: delegate args:
("~DgArgs.stringof~") vs. passed args: ("~Tuple!(Event,T).stringof~")" );

    Dg dg;
    T  t;

    private this( Dg dg, T t ){
        this.dg = dg;
        static if( T.length > 0 ){
            this.t = t;
        }
    }

    void handleEvent( Event e ){
        dg(e,t);
    }
}

Listener dgListener( Dg, T... )( Dg dg, T args ){
    return new _DgListenerT!( Dg, T )( dg, args );
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 28 2010