Bug 5305 – Cannot take pointer to intrinsic function

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-12-01T16:50:30Z
Last change time
2021-08-25T19:27:23Z
Keywords
pull, safe
Assigned to
No Owner
Creator
Bernard Helyer

Comments

Comment #0 by b.helyer — 2010-12-01T16:50:30Z
https://gist.github.com/724508 The code snippet compiles with -release, but not without. In -release mode, the maths intrinsics have the mangling for @safe removed ('Nf'), and so the linker chokes on the mangled signature (because libphobos2.a was compiled with -release.)
Comment #1 by bugzilla — 2012-01-20T12:17:17Z
For convenience, here's the example: import std.math; T[] map(T, V)(T function(T) f, V[] list) { T[] result = new T[](list.length); foreach(k, v; list) { result[k] = f(v); } return result; } void main() { assert (map!(real, float) (&sqrt, [4.0f, 9.0f]) == [2.0f, 3.0f]); }
Comment #2 by bugzilla — 2012-01-20T12:33:25Z
A reduced test case: import std.math; void map(real function(real) f) { } void main() { map(&sqrt); } What is happening here is that sqrt() is an intrinsic, it doesn't actually exist in the libphobos2.a. When compiled with -release, the assert() goes away, and sqrt is never referenced, hence no error. Without -release, the linker looks for std.math.sqrt, and can't find it because it's an intrinsic. The solution is one of: 1. have the compiler complain about attempts to take the address of an intrinsic 2. add a library version of the intrinsic
Comment #3 by bearophile_hugs — 2012-01-21T18:50:09Z
(In reply to comment #2) > The solution is one of: > > 1. have the compiler complain about attempts to take the address of an > intrinsic > > 2. add a library version of the intrinsic Is it possible for the D compiler to use the intrinsic in most cases when sqrt is used directly, and give the pointer to a library sqrt when the programmer uses a &sqrt?
Comment #4 by bugzilla — 2016-06-07T06:39:59Z
Works with the current version of DMD. Adding a test case: https://github.com/dlang/dmd/pull/5845
Comment #5 by mathias.lang — 2016-06-17T10:47:49Z
As mentioned by #c2, the issue is taking a pointer to an intrinsic function. Closing as duplicate and renaming for clarity. *** This issue has been marked as a duplicate of issue 4541 ***
Comment #6 by bugzilla — 2016-06-17T10:50:28Z
The test case I added is to verify that taking the address of sqrt from std.math works.
Comment #7 by github-bugzilla — 2016-06-17T11:56:14Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/fad9e24cff5aff88c5635b010076bf6d39e659e7 fix Issue 5305 - intrinsic functions have @safe stripped of them in release mode https://github.com/dlang/dmd/commit/5f8f6f569eb9d5076c8e2ad68237e18c86052eb0 Merge pull request #5845 from WalterBright/test5305 fix Issue 5305 - intrinsic functions have @safe stripped of them in r…
Comment #8 by github-bugzilla — 2016-10-01T11:47:29Z
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/fad9e24cff5aff88c5635b010076bf6d39e659e7 fix Issue 5305 - intrinsic functions have @safe stripped of them in release mode https://github.com/dlang/dmd/commit/5f8f6f569eb9d5076c8e2ad68237e18c86052eb0 Merge pull request #5845 from WalterBright/test5305
Comment #9 by dlang-bot — 2021-08-25T15:17:32Z
dlang/dmd pull request #13016 "test: Remove test case for issue 5305" was merged into master: - 499d0aa53ac2b4d6a3c39dd7976432de4f926538 by Iain Buclaw: test: Remove test case for issue 5305 This test is already covered in Phobos std.math https://github.com/dlang/dmd/pull/13016
Comment #10 by dlang-bot — 2021-08-25T19:27:23Z
dlang/phobos pull request #8210 "std.math: Reference Issue 5305 in unittest taking pointer to function" was merged into master: - 5a39765c4c5e4c34cd5ee84bae0901af6869d66f by Iain Buclaw: std.math: Reference Issue 5305 in unittest taking pointer to function https://github.com/dlang/phobos/pull/8210