From eacfafbc3534fb32782934d765d21855dff32e56 Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Tue, 23 Jun 2020 14:45:50 +0200 Subject: [PATCH] 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. --- gcc/d/decl.cc | 11 ++++++++--- gcc/testsuite/gdc.dg/init1.d | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/init1.d 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); +} -- 2.30.2