Bug 22884 – ImportC: function does not decay to pointer when being cast
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-03-17T07:32:31Z
Last change time
2022-03-19T09:24:01Z
Keywords
ImportC, pull, rejects-valid
Assigned to
No Owner
Creator
dave287091
Comments
Comment #0 by dave287091 — 2022-03-17T07:32:31Z
The following C code fails to compile. This kind of pointer cast can be seen in sqlite and also in some C library APIs like python.
int printf(const char*, ...);
typedef void (*funcptr)(void);
funcptr a = printf; // ok
funcptr b = (funcptr)printf; // Error: non-constant expression `printf`
funcptr foo(void){
funcptr f = (funcptr)printf; // ok
return f;
}
Comment #1 by dave287091 — 2022-03-17T07:40:43Z
(In reply to dave287091 from comment #0)
> The following C code fails to compile. This kind of pointer cast can be seen
> [...]
I have discovered that changing
funcptr b = (funcptr)printf;
to
funcptr b = (funcptr)&printf;
will compile. So it might be that the function expression is not decaying to a function pointer before the cast.
Comment #2 by dave287091 — 2022-03-17T07:43:54Z
(In reply to dave287091 from comment #1)
> (In reply to dave287091 from comment #0)
> > The following C code fails to compile. This kind of pointer cast can be seen
> > [...]
Also, it looks like this function does error, compilation was just not reaching that point due to the previous error.
typedef void (*funcptr)(void);
funcptr foo(void){
funcptr f = (funcptr)printf; // Error: e2ir: cannot cast `printf` of type `extern (C) int(const(char)*, ...)` to type `extern (C) void function()
return f;
}
Probably same problem, it’s not decaying to a function pointer when it should.
Comment #3 by dlang-bot — 2022-03-19T06:54:30Z
@WalterBright created dlang/dmd pull request #13842 "fix Issue 22884 - ImportC: function does not decay to pointer when be…" fixing this issue:
- fix Issue 22884 - ImportC: function does not decay to pointer when being cast
https://github.com/dlang/dmd/pull/13842
Comment #4 by bugzilla — 2022-03-19T07:37:33Z
*** Issue 22893 has been marked as a duplicate of this issue. ***
Comment #5 by dlang-bot — 2022-03-19T09:24:01Z
dlang/dmd pull request #13842 "fix Issue 22884 - ImportC: function does not decay to pointer when be…" was merged into master:
- 383d9c376918fc0e0fc216e27946a58156c3eaa5 by Walter Bright:
fix Issue 22884 - ImportC: function does not decay to pointer when being cast
https://github.com/dlang/dmd/pull/13842