Bug 3909 – toDelegate handles only a tiny subset of function pointer types

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-03-08T23:33:42Z
Last change time
2021-01-09T19:05:58Z
Assigned to
No Owner
Creator
Max Samukha
Depends on
1818

Comments

Comment #0 by samukha — 2010-03-08T23:33:42Z
Current implementation of toDelegate requires parameter and return types of the argument to be accessible from std.functional, meaning that it won't accept most of function pointers taking or returning instances of UDTs: ---- import std.functional; struct S { } void foo(S s) { } void main() { auto dg = &toDelegate(&foo); } Error: identifier 'S' is not defined ---- The cause is the mixed-in delegate type generated from the function pointer type's stringof, which is invalid in the context of the template declaration. A workaround is to avoid the stringof like this: alias ParameterTypeTuple!(F) Params; // mixed-in string: alias <storage classes> ReturnType!(F) delegate(<storage classes> Params[0], ... <storage classes> Params[$ - 1]) Dg; where <storage classes> are extracted from F's stringof (isRef, isOut, isLazy may work too)
Comment #1 by samukha — 2010-03-09T00:01:42Z
In the test case, auto dg = &toDelegate(&foo); should be auto dg = toDelegate(&foo);
Comment #2 by dsimcha — 2010-05-25T06:29:49Z
I just noticed this report now. Really, the reason this code is so poorly designed (I wrote it, and I'll admit in hindsight that it does suck) is bug 1818. The stupid mixin hack was a last-minute workaround for this bug and wasn't thought through properly. This is noted in the comments: functional.d around line 550: // Workaround for DMD Bug 1818. mixin("alias " ~ ReturnType!(F).stringof ~ " delegate" ~ ParameterTypeTuple!(F).stringof ~ " DelType;"); version(none) { // What the code would be if it weren't for bug 1818: alias ReturnType!(F) delegate(ParameterTypeTuple!(F)) DelType; }
Comment #3 by samukha — 2010-05-25T08:12:12Z
(In reply to comment #2) > I just noticed this report now. Really, the reason this code is so poorly > designed (I wrote it, and I'll admit in hindsight that it does suck) is bug > 1818. The stupid mixin hack was a last-minute workaround for this bug and > wasn't thought through properly. This is noted in the comments: > > functional.d around line 550: > > // Workaround for DMD Bug 1818. > mixin("alias " ~ ReturnType!(F).stringof ~ > " delegate" ~ ParameterTypeTuple!(F).stringof ~ " DelType;"); > > version(none) { > // What the code would be if it weren't for bug 1818: > alias ReturnType!(F) delegate(ParameterTypeTuple!(F)) DelType; > } Phobos has recently acquired functionality for extracting storage classes from functions and function parameters. I haven't given it a close look but it seems to do that by parsing mangled names. Hacky but should make it possible to correctly generate function types based on other function types.
Comment #4 by rsinfu — 2010-05-27T22:12:09Z
I commited a hacky workaround in svn r1561. Now it works with user-defined types and function attributes (such as pure). But I leave this bug opened becuase the bug is not completely fixed.
Comment #5 by qs.il.paperinik — 2021-01-09T19:05:58Z
This code snippet: import std.functional; struct S { } void foo(S s) { } void main() { auto dg = toDelegate(&foo); } compiled on all dmd versions since 2.060; I won't go into the hurdle and find out which version it got fixed. Closing this.