D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20040 - dip1000 implicit delegate attribute stripping fails
Summary: dip1000 implicit delegate attribute stripping fails
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 regression
Assignee: No Owner
URL:
Keywords: safe
Depends on:
Blocks:
 
Reported: 2019-07-10 19:31 UTC by thomas.bockman
Modified: 2022-08-11 07:32 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description thomas.bockman 2019-07-10 19:31:43 UTC
struct A {
	void method() pure @safe nothrow @nogc { }
}

void test(scope void delegate() del) @safe { }

void main() @safe {
    A a;

    // OK:
    void delegate() del = &a.method;

/*
onlineapp.d(19): Error: function onlineapp.test(scope void delegate() del) is
	not callable using argument types (void delegate() pure nothrow @nogc @safe)
onlineapp.d(19):        cannot pass argument &a.method of type void delegate()
	pure nothrow @nogc @safe to parameter scope void delegate() del
*/
    test(&a.method); 
}

The above code works fine - as, I believe, it should - unless I compile with -preview=dip1000 . This issue breaks some of my toString() implementations, so a fix would be appreciated...
Comment 1 Walter Bright 2019-12-19 08:07:44 UTC
The following will work:

  struct A {
    int* p;
    void method() scope pure @safe nothrow @nogc { }
  }

  void test(scope void delegate() del) @safe { }

  void main() @safe {
    A a;
    test(&a.method); 
  }

Note the two additions:

1. `scope` to method(), which says that the method won't escape `this.p`
2. `int* p;` because otherwise the `scope` added will be ignored

A possible fix to the compiler would be to have the address of a method be implicitly marked `scope` if none of the fields have any indirections.
Comment 2 Walter Bright 2022-08-11 07:32:41 UTC
This is working in master.