diff --git a/compiler/src/dmd/tocsym.d b/compiler/src/dmd/tocsym.d index d5d59833ec..d9ff3db223 100644 --- a/compiler/src/dmd/tocsym.d +++ b/compiler/src/dmd/tocsym.d @@ -657,7 +657,24 @@ Symbol *toInitializer(AggregateDeclaration ad) else { auto stag = fake_classsym(Id.ClassInfo); - auto s = toSymbolX(ad, "__init", SC.extern_, stag.Stype, "Z"); + + Symbol* s; + + Module m = ad.getModule(); + if (m.filetype == FileType.c) + { + /* For ImportC structs, the module names are stripped from the mangled name. + * This leads to name collisions. Add the module name back in. + */ + import dmd.common.outbuffer : OutBuffer; + OutBuffer buf; + buf.writestring("__init"); + buf.writestring(m.toChars()); + s = toSymbolX(ad, buf.peekChars(), SC.extern_, stag.Stype, "Z"); + } + else + s = toSymbolX(ad, "__init", SC.extern_, stag.Stype, "Z"); + s.Sfl = FLextern; s.Sflags |= SFLnodebug; if (sd) diff --git a/compiler/test/runnable/imports/freer.i b/compiler/test/runnable/imports/freer.i new file mode 100644 index 0000000000..57e8792bd8 --- /dev/null +++ b/compiler/test/runnable/imports/freer.i @@ -0,0 +1,5 @@ +typedef struct Foo *FooRef; +struct Foo { + int x; +}; +void free_foo(FooRef foo) { } diff --git a/compiler/test/runnable/imports/maker.i b/compiler/test/runnable/imports/maker.i new file mode 100644 index 0000000000..d3a7d94383 --- /dev/null +++ b/compiler/test/runnable/imports/maker.i @@ -0,0 +1,5 @@ +typedef struct Foo *FooRef; +struct Foo { + int x; +}; +FooRef make_foo(void) { return 0; } diff --git a/compiler/test/runnable/test23387.d b/compiler/test/runnable/test23387.d new file mode 100644 index 0000000000..4419cf1200 --- /dev/null +++ b/compiler/test/runnable/test23387.d @@ -0,0 +1,30 @@ +/* COMPILE_SEPARATELY: + * EXTRA_SOURCES: imports/maker.i imports/freer.i + */ + +// https://issues.dlang.org/show_bug.cgi?id=23387 + +/+ maker.i +typedef struct Foo *FooRef; +struct Foo { + int x; +}; +FooRef make_foo(void); ++/ +import imports.maker; + + +/+ freer.i +typedef struct Foo *FooRef; +struct Foo { + int x; +}; +void free_foo(FooRef foo); ++/ +import imports.freer; + +int main(){ + FooRef f = make_foo(); + free_foo(f); + return 0; +}