d: Don't set DECL_INITIAL if initializer is 'void'.
authorIain Buclaw <ibuclaw@gdcproject.org>
Tue, 23 Jun 2020 12:45:50 +0000 (14:45 +0200)
committerIain Buclaw <ibuclaw@gdcproject.org>
Thu, 25 Jun 2020 15:02:45 +0000 (17:02 +0200)
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
gcc/testsuite/gdc.dg/init1.d [new file with mode: 0644]

index ea6614fb7144917c0308d9f42d34bba0e2600261..77144fe11c88ec88c1b7e7a92dbad01e68428ad0 100644 (file)
@@ -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 (file)
index 0000000..679ad15
--- /dev/null
@@ -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);
+}