Hi,

yesterday I figured out that this issue is in fact orthogonal to possible improvements to maybe_deduce_size_from_array_init, indeed it happens also when we are not deducing the size. The testcase is very similar to that recently submitted for c++/78345, which led Jason to add the bit of additional checking around the middle of build_aggr_init. Since we did already have a similar check a bit earlier - not requiring the computation of from_array - I added one for VAR_DECLs too in the same place and tweaked/improved a bit the error message in the process (a few greps revealed that we don't seem to have any other generic error message using the form "bad ..."), consistently with the diagnostic that we already provide for, eg, a simple

    int a[2] = a;

FYI, I must also add that entire testsuite doesn't have a test triggering the existing TREE_CODE (init) == TREE_LIST check, and failed so far to construct one... (many such issues are normally catched by digest_init). Tested x86_64-linux.

Thanks, Paolo.

/////////////////////


/cp
2018-03-22  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/84632
        * init.c (build_aggr_init): Reject VAR_DECLs too as initializer.

/testsuite
2018-03-22  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/84632
        * g++.dg/init/array49.C: New.
Index: cp/init.c
===================================================================
--- cp/init.c   (revision 258745)
+++ cp/init.c   (working copy)
@@ -1688,12 +1688,14 @@ build_aggr_init (tree exp, tree init, int flags, t
        }
       else
        {
-         /* An array may not be initialized use the parenthesized
-            initialization form -- unless the initializer is "()".  */
-         if (init && TREE_CODE (init) == TREE_LIST)
+         /* An array may not be initialized usinge the parenthesized
+            initialization form -- unless the initializer is "()".  
+            Also reject VAR_DECLs (c++/84632).  */
+         if (init && (TREE_CODE (init) == TREE_LIST || VAR_P (init)))
            {
              if (complain & tf_error)
-               error ("bad array initializer");
+               error_at (init_loc, "array must be initialized "
+                         "with a brace-enclosed initializer");
              return error_mark_node;
            }
          /* Must arrange to initialize each element of EXP
Index: testsuite/g++.dg/init/array49.C
===================================================================
--- testsuite/g++.dg/init/array49.C     (nonexistent)
+++ testsuite/g++.dg/init/array49.C     (working copy)
@@ -0,0 +1,6 @@
+// PR c++/84632
+// { dg-additional-options "-w" }
+
+class {
+  &a;  // { dg-error "forbids declaration" }
+} b[2] = b;  // { dg-error "initialized" }

Reply via email to