Follow up to 22674, which seems to only be fixed for for opaque types.
Repeating the example, but with the struct having a definition:
Consider the following files:
// foo_def.h
typedef struct Foo *FooRef;
struct Foo {
int x;
};
// maker.h
#include "foo_def.h"
FooRef make_foo(void);
// freer.h
#include "foo_def.h"
void free_foo(FooRef foo);
Which then preprocesses to:
// maker.i
typedef struct Foo *FooRef;
struct Foo {
int x;
};
FooRef make_foo(void);
// freer.i
typedef struct Foo *FooRef;
struct Foo { // freer.i(2): Error: struct `freer.Foo` already exists at maker.i(2). Perhaps in another function with the same name?
int x;
};
void free_foo(FooRef foo);
You then try to use it in your D program:
// use_foo.d
import maker;
import freer;
void do_foo(){
FooRef f = make_foo();
free_foo(f);
}
Comment #1 by destructionator — 2022-09-22T11:41:06Z
I'd also want to test if 1) functions defined twice work right (i think they do but would want to confirm) and 2) if structs with the same name but different layouts do error.
Same name, same layout should work in C. Same name, different layout C will allow if they are in different translation units, but for D, this gets a little bit awkward. We don't have the same compilation model (really hacking C into the import system was and remains a mistake, it just doesn't actually work) so we'd need to change the rule a bit.
I'd propose that the D rule be if the structs are defined the same, allow the multiple definition and merge them into the same namespace. If not, then issue the multiple definition error.
Comment #2 by bugzilla — 2022-09-23T08:44:30Z
It's not really necessary to rely on the preprocessor. Just the .i files are fine. The .h files and #include's are irrelevant to the problem.
@WalterBright updated dlang/dmd pull request #14509 "fix Issue 23357 - ImportC: compatible types with definitions leads to…" fixing this issue:
- fix Issue 23357 - ImportC: compatible types with definitions leads to redeclaration error when used from D
https://github.com/dlang/dmd/pull/14509
Comment #5 by dlang-bot — 2022-10-05T06:39:53Z
dlang/dmd pull request #14509 "fix Issue 23357 - ImportC: compatible types with definitions leads to…" was merged into master:
- a09b5a67352f61e369edf6af0dafddcd188b3378 by lucica28:
fix Issue 23357 - ImportC: compatible types with definitions leads to redeclaration error when used from D
https://github.com/dlang/dmd/pull/14509