Bug 15843 – D-type mangling used for extern(C) (extern) function declaration inside function body, on LDC, GDC, and DMD.

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-03-28T16:32:00Z
Last change time
2024-10-03T09:41:22Z
Keywords
mangling
Assigned to
No Owner
Creator
Lass Safin
See also
https://issues.dlang.org/show_bug.cgi?id=6132, https://issues.dlang.org/show_bug.cgi?id=22682

Comments

Comment #0 by lasssafin — 2016-03-28T16:32:00Z
Code: void main() { extern(C) extern int func(int); static assert(func.mangleof == "_D1x4mainFZ4funcUiZi"); } It is mangled as such on all 3 major compilers. Using "pragma(mangle, "func")" as an attribute for func gives: x.d(4): Error: unrecognized pragma(mangle) x.d(6): Error: undefined identifier 'func' x.d(7): Error: undefined identifier 'func' Seemingly, the only user-side fix is moving the declaration out of the function body.
Comment #1 by ibuclaw — 2022-01-16T21:53:03Z
I would err on the side of this is working as intended. Nested functions are not compatible with C, and so should not have a C name (by declaring the function inside main, it implicitly gets a "this" context parameter)
Comment #2 by pro.mathias.lang — 2022-01-16T23:44:28Z
Perhaps we should error out on `extern` and `extern(XXX)` functions then?
Comment #3 by dfj1esp02 — 2022-01-17T11:03:52Z
Static nested functions with C calling convention are useful as system callbacks. They just don't need to be visible to other linked modules.
Comment #4 by schveiguy — 2022-01-17T15:09:45Z
extern(C) specifies not only the mangling, but also the calling convention. So having extern(C) inner functions do serve a purpose. I think the mangling is really implementation defined, since C doesn't have any notion of inner functions. Note that `extern` on its own isn't doing anything here. If anything, I'd say pragma(mangle) should work in this context, that would at least give a more straightforward path (really issue 22682). Note that defining the prototype without `static` indeed suggests it should have a `this` pointer, but it doesn't work even if you add `static`.
Comment #5 by b2.temp — 2024-10-03T09:41:22Z
Prgma mangle works locally since a year or so: ``` void main() { pragma(mangle, "func") extern(C) int func(int); static assert(func.mangleof == "func"); } ```