On 2/26/20 1:44 PM, Marek Polacek wrote:
r7-2111 introduced maybe_constant_value in cp_fully_fold.
maybe_constant_value uses cxx_eval_outermost_constant_expr, which
can clear TREE_CONSTANT:
6510   else if (non_constant_p && TREE_CONSTANT (r))
[...]
6529       TREE_CONSTANT (r) = false;

In this test the array size is '(long int) "h"'.  This used to be
TREE_CONSTANT but given the change above, the flag will be cleared
when we cp_fully_fold the array size in compute_array_index_type_loc.
That means we don't emit an error in the
10391   else if (TREE_CONSTANT (size)
block in the same function, and we go on.  Then we compute ITYPE
using cp_build_binary_op and use maybe_constant_value on it and
suddenly we have something that's TREE_CONSTANT again.  And then we
crash in reshape_init_array_1 in tree_to_uhwi, because what we have
doesn't fit in an unsigned HWI.

icc accepts this code, but since we used to reject it, I see no desire
to make this work, so I've added a check that triggers when we end up
with a constant that is not an INTEGER_CST.

G++ accepts this equivalent:

  void f () {
    long n = ( long ) "h";
    const int tbl [n] = { 12 };
    ...
  }

and it doesn't look like the patch changes that.

I think all these cases of initialized VLAs ought to be handled
the same way: either they should all be rejected or they should
all be accepted.  The rather dated pr58646, also a regression,
is another example of a few ICEs in this area.  See also this
recently submitted patch:
  https://gcc.gnu.org/ml/gcc-patches/2020-02/msg01148.html

FWIW, at one point I got VLA initialization to work again (r234966),
after it had been removed due to the last minute removal of VLAs
from the standard, but it caused trouble for Java and I was forced
to revert the changes.  I've been hoping to revisit it but other
things have been getting in the way.

Martin



Bootstrapped/regtested on x86_64-linux, ok for trunk?  Probably no need
to change released versions.

2020-02-26  Marek Polacek  <pola...@redhat.com>

        PR c++/93789 - ICE with invalid array bounds.
        * decl.c (compute_array_index_type_loc): Give an error
        when ITYPE ends up being a constant that is not an INTEGER_CST.

        * g++.dg/ext/vla22.C: New test.
---
  gcc/cp/decl.c                    |  7 +++++++
  gcc/testsuite/g++.dg/ext/vla22.C | 10 ++++++++++
  2 files changed, 17 insertions(+)
  create mode 100644 gcc/testsuite/g++.dg/ext/vla22.C

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 1947c4ddb7f..4ac29d99d04 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -10473,6 +10473,13 @@ compute_array_index_type_loc (location_t name_loc, 
tree name, tree size,
          error ("overflow in array dimension");
          TREE_OVERFLOW (itype) = 0;
        }
+      else if (TREE_CODE (itype) != INTEGER_CST)
+       {
+         if (!(complain & tf_error))
+           return error_mark_node;
+         invalid_array_size_error (loc, cst_size_not_constant, itype, name);
+         itype = integer_one_node;
+       }
      }
/* Create and return the appropriate index type. */
diff --git a/gcc/testsuite/g++.dg/ext/vla22.C b/gcc/testsuite/g++.dg/ext/vla22.C
new file mode 100644
index 00000000000..22e200b49cf
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/vla22.C
@@ -0,0 +1,10 @@
+// PR c++/93789 - ICE with invalid array bounds.
+// { dg-do compile }
+// { dg-options "" }
+
+void
+f ()
+{
+  const int tbl[(long) "h"] = { 12 }; // { dg-error "size of array .tbl. is not a constant 
expression" "" { target c++11 } }
+// { dg-error "size of array .tbl. is not an integral constant-expression" "" 
{ target c++98_only } .-1 }
+}

base-commit: 051b9873e78fe1acb1a3fecd0c6e5685b6c12fb3


Reply via email to