I have two single-file c libraries, and dmd can compile either one just fine, but not at the same time. Reduced example:
a.c:
```C
struct timespec
{
int s;
};
```
b.c:
```C
struct S;
struct timespec
{
int s;
};
typedef struct timespec Clock;
Clock now()
{
Clock result;
return result;
}
struct S
{
Clock clock;
};
```
```
dmd a.c // works
dmd b.c // works
dmd b.c a.c // works
dmd a.c b.c // fails:
b.c(13): Error: forward reference to type `timespec`
```
It's very fragile, sometimes the error goes away when removing a random unused macro `#define _POSIX_C_SOURCE 200112L` or changing an identifier name (I can't rename `timespec`), but it does seem deterministic: I get the error every time as long as the input stays the same.
Comment #1 by dkorpel — 2023-12-12T23:04:37Z
Another example: (not sure if it's the same bug)
a.c
```C
typedef struct {} Slice;
struct Lang
{
Slice *slices;
};
```
b.c
```C
typedef struct {} Slice;
struct Lang
{
Slice *slices;
};
void langmap(struct Lang *self)
{
Slice slice = *self->slices;
}
```
```
dmd a.c // works
dmd b.c // works
dmd b.c a.c // works
dmd a.c b.c // fails:
b.c(11): Error: cannot implicitly convert expression `*(*self).slices` of type `__tag2` to `__tag3`
```
Comment #2 by bugzilla — 2023-12-26T18:40:32Z
The source of the trouble is dmd assigns each type a unique "deco" string. C structs defined in different .c files are considered to be the same if they have the same "deco" name. But since they are sometimes assumed to be different types because they are in different modules, confusion appears.
Not sure how to fix it yet.
Comment #3 by bugzilla — 2023-12-27T02:55:58Z
(In reply to Dennis from comment #1)
> Another example: (not sure if it's the same bug)
This will require additional work, so refiled it as a separate issue:
https://issues.dlang.org/show_bug.cgi?id=24303
Comment #4 by dlang-bot — 2023-12-27T07:32:29Z
@WalterBright created dlang/dmd pull request #15954 "fix Issue 24280 - ImportC: forward reference error when compiling mul…" fixing this issue:
- fix Issue 24280 - ImportC: forward reference error when compiling multiple files
https://github.com/dlang/dmd/pull/15954
Comment #5 by dlang-bot — 2023-12-27T08:34:18Z
dlang/dmd pull request #15954 "fix Issue 24280 - ImportC: forward reference error when compiling mul…" was merged into master:
- e590fba9d81c6060f806a52c11c030161780484d by Walter Bright:
fix Issue 24280 - ImportC: forward reference error when compiling multiple files
https://github.com/dlang/dmd/pull/15954