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");
}
```