We were ICEing on the attached testcase, because in check_narrowing,
for = {{}}, we wanted to check recursively the CONSTRUCTOR_ELTs,
even though init in this case has 0 CONSTRUCTOR_NELTS.  So I added
the check for CONSTRUCTOR_NELTS > 0.  Moreover, since empty scalar
initializers are forbidden in C FE, I think we should error out here
too.  (Complex type is considered as an arithmetic type as a GNU
extension and arithmetic types are scalar types.)
This isn't C++11-specific as it may look from the PR.  The bug
exhibits when -Wnarrowing (that is implicitly enabled with C++11) is
on, since in check_narrowing we have

  if (!warn_narrowing || !ARITHMETIC_TYPE_P (type))
      return;

and with -Wno-narrowing we just return early.

Regtested/bootstrapped on x86_64-linux, ok for trunk?

2013-10-14  Marek Polacek  <pola...@redhat.com>

        PR c++/58705
cp/
        * typeck2.c (check_narrowing): Give an error when the scalar
        initializer is empty.
testsuite/
        * g++.dg/parse/pr58705.C: New test.

--- gcc/cp/typeck2.c.mp 2013-10-14 11:11:36.971293089 +0200
+++ gcc/cp/typeck2.c    2013-10-14 11:52:14.582061052 +0200
@@ -833,7 +833,10 @@ check_narrowing (tree type, tree init)
       && TREE_CODE (type) == COMPLEX_TYPE)
     {
       tree elttype = TREE_TYPE (type);
-      check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value);
+      if (CONSTRUCTOR_NELTS (init) > 0)
+       check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value);
+      else
+       error ("empty scalar initializer");
       if (CONSTRUCTOR_NELTS (init) > 1)
        check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value);
       return;
--- gcc/testsuite/g++.dg/parse/pr58705.C.mp     2013-10-14 11:14:46.460955343 
+0200
+++ gcc/testsuite/g++.dg/parse/pr58705.C        2013-10-14 11:51:02.698810118 
+0200
@@ -0,0 +1,5 @@
+// PR c++/58705
+// { dg-do compile }
+// { dg-options "-Wnarrowing" }
+
+_Complex float f = {{}};  // { dg-error "empty scalar initializer" }

        Marek

Reply via email to