www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21195] New: Delegate to method created without a `this` in

https://issues.dlang.org/show_bug.cgi?id=21195

          Issue ID: 21195
           Summary: Delegate to method created without a `this` in certain
                    contexts
           Product: D
           Version: D2
          Hardware: All
               URL: http://dlang.org/
                OS: All
            Status: NEW
          Severity: major
          Priority: P3
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: tomer weka.io

i had code similar to this
```
import std.stdio;

void function() callback;

struct S {
    static void getter() {
        callback = &func;     <<<< this shouldn't compile, `func` needs `this`
    }

    void func() {
        writefln("func(this=%s)", &this);
    }
}

void main() {
    S.getter();
    callback();
}
```

output:
```
func(this=1)
```

which is obviously means `this` is corrupted and trying to touch any members of
it would segfault.

if instead i write
```
void main() {
    S s;
    callback = &s.func;
}
```

i get the expected
```
Error: cannot implicitly convert expression &s.func of type void delegate() to
void function()
```

but it seems that in the context of a static function, it skips checking that i
have a `this` to get a delegate

--
Aug 24 2020