Issue 23402 - importc function definitions from includes can cause D name conflicts
Summary: importc function definitions from includes can cause D name conflicts
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: ImportC, pull
Depends on:
Blocks:
 
Reported: 2022-10-10 13:07 UTC by Adam D. Ruppe
Modified: 2023-04-13 19:17 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Adam D. Ruppe 2022-10-10 13:07:17 UTC
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 Lance Bachmeier 2022-11-14 02:12:28 UTC
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 Walter Bright 2023-01-12 06:29:06 UTC
Can you boil it down to a compilable example, please?
Comment 3 Lance Bachmeier 2023-01-12 14:33:03 UTC
(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 Walter Bright 2023-04-09 06:25:57 UTC
---- foo.c ------
#include <math.h>

int main() {}
---------

Now compiles without error. Though this has nothing to do with Adam's report.
Comment 5 Lance Bachmeier 2023-04-10 01:51:15 UTC
(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 Dlang Bot 2023-04-13 02:41:32 UTC
@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 Dlang Bot 2023-04-13 19:17:23 UTC
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