Bug 23402 – importc function definitions from includes can cause D name conflicts
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-10-10T13:07:17Z
Last change time
2023-04-13T19:17:23Z
Keywords
ImportC, pull
Assigned to
No Owner
Creator
Adam D. Ruppe
Comments
Comment #0 by destructionator — 2022-10-10T13:07:17Z
Suppose you have a.c that #include<stdio.h> and b.c that also includes it.
Then the D file imports both a and b. Now functions like printf get a name conflict.
This is expected in D, since different modules have their own namespaces. But in C, it is common for different headers to include different things. An include guard keeps it out there, but in D this doesn't work so well.
I suggest putting the importC symbols in a magic module, then public aliasing them/selectively importing them into the D module being imported with the include. Then they refer to the same C namespace while keeping the D namespace hygienic and avoids spurious conflicts.
Comment #1 by lance — 2022-11-14T02:12:28Z
This causes a similar problem with enums. I'm getting error messages like this:
#defines(1378): Error: variable `arma_example.FP_NAN` conflicts with enum member `__anonymous.FP_NAN` at /usr/include/math.h(936)
The error message is confusing, because (a) I don't define FP_NAN in my program, and (b) the error message itself doesn't make sense, as arma_example.FP_NAN and __anonymous.FP_NAN are not the same thing.
Comment #2 by bugzilla — 2023-01-12T06:29:06Z
Can you boil it down to a compilable example, please?
Comment #3 by lance — 2023-01-12T14:33:03Z
(In reply to Walter Bright from comment #2)
> Can you boil it down to a compilable example, please?
This new bug covers mine (not Adam's). It might be specific to including math.h.
https://issues.dlang.org/show_bug.cgi?id=23622
A complete test case:
foo.c:
#include <math.h>
int main() {}
gcc foo.c -> compiles
dmd foo.c ->
#defines(797): Error: variable `foo.FP_NAN` conflicts with enum member `__anonymous.FP_NAN` at /usr/include/math.h(855)
#defines(798): Error: variable `foo.FP_INFINITE` conflicts with enum member `__anonymous.FP_INFINITE` at /usr/include/math.h(858)
#defines(799): Error: variable `foo.FP_ZERO` conflicts with enum member `__anonymous.FP_ZERO` at /usr/include/math.h(861)
#defines(800): Error: variable `foo.FP_SUBNORMAL` conflicts with enum member `__anonymous.FP_SUBNORMAL` at /usr/include/math.h(864)
#defines(801): Error: variable `foo.FP_NORMAL` conflicts with enum member `__anonymous.FP_NORMAL` at /usr/include/math.h(867)
Comment #4 by bugzilla — 2023-04-09T06:25:57Z
---- foo.c ------
#include <math.h>
int main() {}
---------
Now compiles without error. Though this has nothing to do with Adam's report.
Comment #5 by lance — 2023-04-10T01:51:15Z
(In reply to Walter Bright from comment #4)
> ---- foo.c ------
> #include <math.h>
>
> int main() {}
> ---------
>
> Now compiles without error. Though this has nothing to do with Adam's report.
Yes. My problem was fixed when you fixed this bug:
https://issues.dlang.org/show_bug.cgi?id=23622
I originally thought it was related to Adam's report, but that turned out to be wrong.
Comment #6 by dlang-bot — 2023-04-13T02:41:32Z
@WalterBright created dlang/dmd pull request #15101 "fix Issue 23402 - importc function definitions from includes can caus…" fixing this issue:
- fix Issue 23402 - importc function definitions from includes can cause D name conflicts
https://github.com/dlang/dmd/pull/15101
Comment #7 by dlang-bot — 2023-04-13T19:17:23Z
dlang/dmd pull request #15101 "fix Issue 23402 - importc function definitions from includes can caus…" was merged into master:
- e028af1fd9a9cf1d123e37447dfb91203b93ae40 by Walter Bright:
fix Issue 23402 - importc function definitions from includes can cause D name conflicts
https://github.com/dlang/dmd/pull/15101