From: Iain Buclaw Date: Tue, 23 Jun 2020 12:45:50 +0000 (+0200) Subject: d: Don't set DECL_INITIAL if initializer is 'void'. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=eacfafbc3534fb32782934d765d21855dff32e56;p=gcc.git d: Don't set DECL_INITIAL if initializer is 'void'. Declarations initialized with `= void` were being default initialized. That is not really the intent, and misses the small optimization that should have been gained from using void initializations. gcc/d/ChangeLog: * decl.cc (DeclVisitor::visit (VarDeclaration *)): Don't set DECL_INITIAL if initializer is 'void'. gcc/testsuite/ChangeLog: * gdc.dg/init1.d: New test. --- diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index ea6614fb714..77144fe11c8 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -697,13 +697,18 @@ public: return; } - if (d->_init && !d->_init->isVoidInitializer ()) + if (d->_init) { - Expression *e = initializerToExpression (d->_init, d->type); - DECL_INITIAL (decl) = build_expr (e, true); + /* Use the explicit initializer, this includes `void`. */ + if (!d->_init->isVoidInitializer ()) + { + Expression *e = initializerToExpression (d->_init, d->type); + DECL_INITIAL (decl) = build_expr (e, true); + } } else { + /* Use default initializer for the type. */ if (TypeStruct *ts = d->type->isTypeStruct ()) DECL_INITIAL (decl) = layout_struct_initializer (ts->sym); else diff --git a/gcc/testsuite/gdc.dg/init1.d b/gcc/testsuite/gdc.dg/init1.d new file mode 100644 index 00000000000..679ad15460e --- /dev/null +++ b/gcc/testsuite/gdc.dg/init1.d @@ -0,0 +1,9 @@ +// { dg-do run { target hw } } +// { dg-options "-fno-druntime" } +// 'a' should not be default initialized to -1. +static char a = void; + +extern (C) void main() +{ + assert(a == 0); +}