D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4288 - Error on passing delegate to C linkage function.
Summary: Error on passing delegate to C linkage function.
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-06 09:24 UTC by Adam Chrapkowski
Modified: 2014-02-15 02:19 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Adam Chrapkowski 2010-06-06 09:24:38 UTC
Error occurs, when you try to use a delegate as a parameter to a C linked function.

ex:
extern (C)
void c_func(void delegate() dg);
void d_func(void delegate() dg) { c_func(dg); }

Error: function test.c_func (void delegate()) does not match parameter types (void delegate())
Error: cannot implicitly convert expression (dg) of type void delegate() to void delegate()
Comment 1 Robert Clipsham 2010-06-06 09:59:33 UTC
I don't think this bug is valid, something does need to be done about it though. The following works:
----
alias extern(C) void delegate() cdg;
extern (C)
void c_func(void delegate() dg);
void d_func(cdg dg) { c_func(dg); }
----
So the problem is the linkage of the delegates doesn't match, there's no way to get around it without an alias though, you can't have extern(C) in the function parameters. Either the error needs improving, or there needs to be a way to specify the linkage in the function args.
Comment 2 Adam Chrapkowski 2010-06-06 11:11:41 UTC
So two questions:

1. Why the following is allowed:
extern (C) void c_func(void delegate());
?

2. What does mean extern (C) void delegate() ?

Delegate is just a 'data structure which consists of two pointers' and anything more. Something like C delegate does not exists. Linkage is related to functions not data structures delegate is a data structure.

So if D data structures are disallowed in C functions why the following is allowed?
extern (C) void c_func(string, Foo);
class Foo{...}
void d_func() {
  scope  foo = new Foo();
  string str = "Hello World!";
  c_func(str, foo);
}

string is a data structure as delegate, foo is a pointer to a data structure.
Comment 3 David Nadlinger 2012-05-12 07:40:25 UTC
Error message fix:

https://github.com/D-Programming-Language/dmd/pull/943
Comment 4 github-bugzilla 2012-05-12 22:23:23 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/6fec4c76494534bdbcd958eafea65c73b418deed
Fix issue 4288 – Error on passing delegate to C linkage function.

This commit makes TypeDelegate::toCBuffer2 reuse the same code as for function pointers, which solves the problem that the linkage type is not included in error messages.

Previously, nonsensical messages like »cannot implicitly convert expression (dg) of type void delegate() to void delegate()« would be emitted.

https://github.com/D-Programming-Language/dmd/commit/7b1ed6349f405e3fed46d7c7b1a7cc641d7e9a8a
Merge pull request #943 from klickverbot/argument-calling-conv-error

Issue 4288 – Error on passing delegate to C linkage function.