On 2/26/20 3: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.

I wonder about giving an error at that point; if size is TREE_CONSTANT and folded is not, we're in a strange situation already.

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.

Why does maybe_constant_value consider (long)"h" - 1 to be a constant-expression?

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.

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