From 899d61755e8f9b35b47986434e55af2b818fbda7 Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Wed, 26 May 2021 00:45:14 +0200 Subject: [PATCH] fix Issue 21970 - importC: Error: variable extern symbols cannot have initializers --- src/dmd/cparse.d | 6 ++++-- test/compilable/imports/cstuff1.c | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/dmd/cparse.d b/src/dmd/cparse.d index 36767c23e671..a421f10c2fa5 100644 --- a/src/dmd/cparse.d +++ b/src/dmd/cparse.d @@ -1541,8 +1541,6 @@ final class CParser(AST) : Parser!AST hasInitializer = true; initializer = cparseInitializer(); } - else - initializer = new AST.VoidInitializer(token.loc); // declare the symbol assert(id); if (dt.isTypeFunction()) @@ -1553,6 +1551,10 @@ final class CParser(AST) : Parser!AST } else { + // Give non-extern variables an implicit void initializer + // if one has not been explicitly set. + if (!hasInitializer && !(specifier.scw & SCW.xextern)) + initializer = new AST.VoidInitializer(token.loc); s = new AST.VarDeclaration(token.loc, dt, id, initializer, specifiersToSTC(level, specifier)); } } diff --git a/test/compilable/imports/cstuff1.c b/test/compilable/imports/cstuff1.c index d70f69b37eba..959fa4433c70 100644 --- a/test/compilable/imports/cstuff1.c +++ b/test/compilable/imports/cstuff1.c @@ -345,6 +345,11 @@ union U21963 const int test21967a(void); const int *test21967b(void); +/********************************/ +// https://issues.dlang.org/show_bug.cgi?id=21970 +extern int test21970a; +extern char *test21970b; + /********************************/ void test__func__()