From 383605806f92d01850cb8fd308d23cc64445f5d9 Mon Sep 17 00:00:00 2001 From: IDONTUSEGH Date: Sun, 1 Mar 2026 10:53:19 +0100 Subject: [PATCH 1/8] Skip using typedefIdent for basic types --- compiler/src/dmd/hdrgen.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dmd/hdrgen.d b/compiler/src/dmd/hdrgen.d index 7b50af1685f9..480b87fd978c 100644 --- a/compiler/src/dmd/hdrgen.d +++ b/compiler/src/dmd/hdrgen.d @@ -4547,7 +4547,7 @@ private void typeToBufferx(Type t, ref OutBuffer buf, ref HdrGenState hgs) buf.put("noreturn"); } - if (hgs.importcHdr && !hgs.inCAlias && t.mcache && t.mcache.typedefIdent) + if (hgs.importcHdr && !hgs.inCAlias && t.mcache && t.mcache.typedefIdent && !t.isTypeBasic()) { buf.put(t.mcache.typedefIdent.toString()); return; From dd58a80083eb7a8a726596513d917ddf940e9bf4 Mon Sep 17 00:00:00 2001 From: IDONTUSEGH Date: Sun, 1 Mar 2026 11:49:33 +0100 Subject: [PATCH 2/8] Skip using typedefIdent specifically for `void` type --- compiler/src/dmd/hdrgen.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dmd/hdrgen.d b/compiler/src/dmd/hdrgen.d index 480b87fd978c..6ebcfcb12df0 100644 --- a/compiler/src/dmd/hdrgen.d +++ b/compiler/src/dmd/hdrgen.d @@ -4547,7 +4547,7 @@ private void typeToBufferx(Type t, ref OutBuffer buf, ref HdrGenState hgs) buf.put("noreturn"); } - if (hgs.importcHdr && !hgs.inCAlias && t.mcache && t.mcache.typedefIdent && !t.isTypeBasic()) + if (hgs.importcHdr && !hgs.inCAlias && t.mcache && t.mcache.typedefIdent && t.ty != Tvoid) { buf.put(t.mcache.typedefIdent.toString()); return; From a730effe708bac05986042101a87e77334b6403b Mon Sep 17 00:00:00 2001 From: IDONTUSEGH Date: Sun, 1 Mar 2026 11:50:40 +0100 Subject: [PATCH 3/8] Update test --- compiler/test/compilable/ctod.i | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/test/compilable/ctod.i b/compiler/test/compilable/ctod.i index d1529de47d78..c973270f7d5a 100644 --- a/compiler/test/compilable/ctod.i +++ b/compiler/test/compilable/ctod.i @@ -53,6 +53,8 @@ extern (C) Callback cb = void; } extern __gshared int[cast(ULONG)3] arr; + alias _IO_lock_t = void; + void mylib_hello(); /+enum int __DATE__ = 1+/; /+enum int __TIME__ = 1+/; /+enum int __TIMESTAMP__ = 1+/; @@ -131,3 +133,6 @@ int arr[(ULONG) 3]; #define DEF 123 #define SQL_DRIVER_STMT_ATTR_BASE 0x00004000 // 32-bit #define ABC 64 + +typedef void _IO_lock_t; +void mylib_hello(void); From 4fffde22ac2208ae437a06f75041974a56d2bbc4 Mon Sep 17 00:00:00 2001 From: IDONTUSEGH Date: Sun, 1 Mar 2026 14:25:43 +0100 Subject: [PATCH 4/8] Added check to copy basic types and prevent typedef substitution in function return types --- compiler/src/dmd/cparse.d | 6 ++++++ compiler/src/dmd/hdrgen.d | 7 ++++++- compiler/test/compilable/ctod.i | 13 +++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/compiler/src/dmd/cparse.d b/compiler/src/dmd/cparse.d index 3ebbfe04f20f..2d6cadaeb96f 100644 --- a/compiler/src/dmd/cparse.d +++ b/compiler/src/dmd/cparse.d @@ -5080,6 +5080,12 @@ final class CParser(AST) : Parser!AST t = t.copy(); t.mcache = null; } + else if (t.isTypeBasic()) + { + // Basic types like void, int, etc. are global shared instances. + // Copy before setting typedefIdent to avoid polluting the shared type. + t = t.copy(); + } t.getMcache().typedefIdent = id; auto tab = cast(void*[void*])(typedefTab[$ - 1]); tab[cast(void*)id] = cast(void*)t; diff --git a/compiler/src/dmd/hdrgen.d b/compiler/src/dmd/hdrgen.d index 6ebcfcb12df0..71e94c2a90cd 100644 --- a/compiler/src/dmd/hdrgen.d +++ b/compiler/src/dmd/hdrgen.d @@ -58,6 +58,7 @@ struct HdrGenState bool fullDump; /// true if generating a full AST dump file bool importcHdr; /// true if generating a .di file from an ImportC file bool inCAlias; /// Set to prevent ImportC translating typedefs as `alias X = X` + bool inFuncReturn; /// Set when printing function return type to avoid typedef name bool doFuncBodies; /// include function bodies in output bool vcg_ast; /// write out codegen-ast bool skipConstraints; // skip constraints when doing templates @@ -4096,7 +4097,9 @@ private void visitFuncIdentWithPostfix(TypeFunction t, const char[] ident, ref O buf.write("static "); if (t.next) { + hgs.inFuncReturn = true; typeToBuffer(t.next, null, buf, hgs); + hgs.inFuncReturn = false; if (ident) buf.put(' '); } @@ -4165,7 +4168,9 @@ private void visitFuncIdentWithPrefix(TypeFunction t, const Identifier ident, Te } else if (t.next) { + hgs.inFuncReturn = true; typeToBuffer(t.next, null, buf, hgs); + hgs.inFuncReturn = false; if (ident) buf.put(' '); } @@ -4547,7 +4552,7 @@ private void typeToBufferx(Type t, ref OutBuffer buf, ref HdrGenState hgs) buf.put("noreturn"); } - if (hgs.importcHdr && !hgs.inCAlias && t.mcache && t.mcache.typedefIdent && t.ty != Tvoid) + if (hgs.importcHdr && !hgs.inCAlias && !hgs.inFuncReturn && t.mcache && t.mcache.typedefIdent) { buf.put(t.mcache.typedefIdent.toString()); return; diff --git a/compiler/test/compilable/ctod.i b/compiler/test/compilable/ctod.i index c973270f7d5a..db0b7d979d4a 100644 --- a/compiler/test/compilable/ctod.i +++ b/compiler/test/compilable/ctod.i @@ -53,8 +53,10 @@ extern (C) Callback cb = void; } extern __gshared int[cast(ULONG)3] arr; - alias _IO_lock_t = void; - void mylib_hello(); + alias NOPE1 = void; + void mylib_hello1(); + alias NOPE2 = int; + int mylib_hello2(); /+enum int __DATE__ = 1+/; /+enum int __TIME__ = 1+/; /+enum int __TIMESTAMP__ = 1+/; @@ -134,5 +136,8 @@ int arr[(ULONG) 3]; #define SQL_DRIVER_STMT_ATTR_BASE 0x00004000 // 32-bit #define ABC 64 -typedef void _IO_lock_t; -void mylib_hello(void); +typedef void NOPE1; +void mylib_hello1(void); + +typedef int NOPE2; +int mylib_hello2(void); From 7b0e678dc5705d841664cf1bf68fc6bc64b8a069 Mon Sep 17 00:00:00 2001 From: IDONTUSEGH Date: Tue, 3 Mar 2026 17:20:35 +0100 Subject: [PATCH 5/8] ensure mcache is cleared for basic types + update test --- compiler/src/dmd/cparse.d | 15 ++++----------- compiler/test/compilable/ctod.i | 4 ++++ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/compiler/src/dmd/cparse.d b/compiler/src/dmd/cparse.d index 2d6cadaeb96f..a02938e0ffdd 100644 --- a/compiler/src/dmd/cparse.d +++ b/compiler/src/dmd/cparse.d @@ -5075,17 +5075,10 @@ final class CParser(AST) : Parser!AST if (pt && *pt) t = *pt; } - if (t.mcache && t.mcache.typedefIdent) - { - t = t.copy(); - t.mcache = null; - } - else if (t.isTypeBasic()) - { - // Basic types like void, int, etc. are global shared instances. - // Copy before setting typedefIdent to avoid polluting the shared type. - t = t.copy(); - } +if ((t.mcache && t.mcache.typedefIdent) || t.isTypeBasic()) { + t = t.copy(); + t.mcache = null; +} t.getMcache().typedefIdent = id; auto tab = cast(void*[void*])(typedefTab[$ - 1]); tab[cast(void*)id] = cast(void*)t; diff --git a/compiler/test/compilable/ctod.i b/compiler/test/compilable/ctod.i index db0b7d979d4a..bd010edb9c4d 100644 --- a/compiler/test/compilable/ctod.i +++ b/compiler/test/compilable/ctod.i @@ -57,6 +57,8 @@ extern (C) void mylib_hello1(); alias NOPE2 = int; int mylib_hello2(); + int mylib_hello2(); + int mylib_hello3(void*); /+enum int __DATE__ = 1+/; /+enum int __TIME__ = 1+/; /+enum int __TIMESTAMP__ = 1+/; @@ -141,3 +143,5 @@ void mylib_hello1(void); typedef int NOPE2; int mylib_hello2(void); + +void mylib_hello3(void*); From bf7bdc4103361a1030de7fbd8b15ddd7f15432c2 Mon Sep 17 00:00:00 2001 From: IDONTUSEGH Date: Tue, 3 Mar 2026 17:22:35 +0100 Subject: [PATCH 6/8] Update frontend.h --- compiler/src/dmd/frontend.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/src/dmd/frontend.h b/compiler/src/dmd/frontend.h index 1ffb8a494596..beca3eacedc3 100644 --- a/compiler/src/dmd/frontend.h +++ b/compiler/src/dmd/frontend.h @@ -4082,6 +4082,7 @@ struct HdrGenState final bool fullDump; bool importcHdr; bool inCAlias; + bool inFuncReturn; bool doFuncBodies; bool vcg_ast; bool skipConstraints; @@ -4101,6 +4102,7 @@ struct HdrGenState final fullDump(), importcHdr(), inCAlias(), + inFuncReturn(), doFuncBodies(), vcg_ast(), skipConstraints(), @@ -4116,12 +4118,13 @@ struct HdrGenState final inEnumDecl() { } - HdrGenState(bool hdrgen, bool ddoc = false, bool fullDump = false, bool importcHdr = false, bool inCAlias = false, bool doFuncBodies = false, bool vcg_ast = false, bool skipConstraints = false, bool showOneMember = true, bool errorMsg = false, bool fullQual = false, int32_t tpltMember = 0, int32_t autoMember = 0, int32_t forStmtInit = 0, int32_t insideFuncBody = 0, int32_t insideAggregate = 0, bool declstring = false, EnumDeclaration* inEnumDecl = nullptr) : + HdrGenState(bool hdrgen, bool ddoc = false, bool fullDump = false, bool importcHdr = false, bool inCAlias = false, bool inFuncReturn = false, bool doFuncBodies = false, bool vcg_ast = false, bool skipConstraints = false, bool showOneMember = true, bool errorMsg = false, bool fullQual = false, int32_t tpltMember = 0, int32_t autoMember = 0, int32_t forStmtInit = 0, int32_t insideFuncBody = 0, int32_t insideAggregate = 0, bool declstring = false, EnumDeclaration* inEnumDecl = nullptr) : hdrgen(hdrgen), ddoc(ddoc), fullDump(fullDump), importcHdr(importcHdr), inCAlias(inCAlias), + inFuncReturn(inFuncReturn), doFuncBodies(doFuncBodies), vcg_ast(vcg_ast), skipConstraints(skipConstraints), From debf5dd9d2c6f4099831538b7d814234e0946e93 Mon Sep 17 00:00:00 2001 From: IDONTUSEGH Date: Tue, 3 Mar 2026 17:46:02 +0100 Subject: [PATCH 7/8] Fix test --- compiler/test/compilable/ctod.i | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/test/compilable/ctod.i b/compiler/test/compilable/ctod.i index bd010edb9c4d..b41a3b7bfd14 100644 --- a/compiler/test/compilable/ctod.i +++ b/compiler/test/compilable/ctod.i @@ -57,8 +57,7 @@ extern (C) void mylib_hello1(); alias NOPE2 = int; int mylib_hello2(); - int mylib_hello2(); - int mylib_hello3(void*); + void mylib_hello3(void*); /+enum int __DATE__ = 1+/; /+enum int __TIME__ = 1+/; /+enum int __TIMESTAMP__ = 1+/; From 10f1452eaa2714f3d4d74e1464d0ad0f374dd236 Mon Sep 17 00:00:00 2001 From: IDONTUSEGH Date: Tue, 3 Mar 2026 18:15:45 +0100 Subject: [PATCH 8/8] Fix formatting --- compiler/src/dmd/cparse.d | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/src/dmd/cparse.d b/compiler/src/dmd/cparse.d index a02938e0ffdd..c005493a970d 100644 --- a/compiler/src/dmd/cparse.d +++ b/compiler/src/dmd/cparse.d @@ -5075,10 +5075,10 @@ final class CParser(AST) : Parser!AST if (pt && *pt) t = *pt; } -if ((t.mcache && t.mcache.typedefIdent) || t.isTypeBasic()) { - t = t.copy(); - t.mcache = null; -} + if ((t.mcache && t.mcache.typedefIdent) || t.isTypeBasic()) { + t = t.copy(); + t.mcache = null; + } t.getMcache().typedefIdent = id; auto tab = cast(void*[void*])(typedefTab[$ - 1]); tab[cast(void*)id] = cast(void*)t;