re PR c++/85847 (unexpected expression of kind template_id_expr)
authorMarek Polacek <polacek@redhat.com>
Thu, 24 May 2018 13:36:21 +0000 (13:36 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Thu, 24 May 2018 13:36:21 +0000 (13:36 +0000)
PR c++/85847
* init.c (build_new_1): Use fold_non_dependent_expr.  Use a dedicated
variable for its result.  Fix a condition.
(build_new): Use fold_non_dependent_expr.  Tweak a condition.

* g++.dg/cpp0x/new3.C: New test.

From-SVN: r260671

gcc/cp/ChangeLog
gcc/cp/init.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/new3.C [new file with mode: 0644]

index f38d385..4f4c9e7 100644 (file)
@@ -1,3 +1,10 @@
+2018-05-24  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/85847
+       * init.c (build_new_1): Use fold_non_dependent_expr.  Use a dedicated
+       variable for its result.  Fix a condition.
+       (build_new): Use fold_non_dependent_expr.  Tweak a condition.
+
 2018-05-23  Jason Merrill  <jason@redhat.com>
 
        Fix cast to rvalue reference from prvalue.
index b558742..5bfd084 100644 (file)
@@ -2857,10 +2857,9 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
       outer_nelts_from_type = true;
     }
 
-  /* Lots of logic below. depends on whether we have a constant number of
+  /* Lots of logic below depends on whether we have a constant number of
      elements, so go ahead and fold it now.  */
-  if (outer_nelts)
-    outer_nelts = maybe_constant_value (outer_nelts);
+  const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts);
 
   /* If our base type is an array, then make sure we know how many elements
      it has.  */
@@ -2912,7 +2911,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
   /* Warn if we performed the (T[N]) to T[N] transformation and N is
      variable.  */
   if (outer_nelts_from_type
-      && !TREE_CONSTANT (outer_nelts))
+      && !TREE_CONSTANT (cst_outer_nelts))
     {
       if (complain & tf_warning_or_error)
        {
@@ -3011,9 +3010,9 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
 
       size = size_binop (MULT_EXPR, size, fold_convert (sizetype, nelts));
 
-      if (INTEGER_CST == TREE_CODE (outer_nelts))
+      if (TREE_CODE (cst_outer_nelts) == INTEGER_CST)
        {
-         if (tree_int_cst_lt (max_outer_nelts_tree, outer_nelts))
+         if (tree_int_cst_lt (max_outer_nelts_tree, cst_outer_nelts))
            {
              /* When the array size is constant, check it at compile time
                 to make sure it doesn't exceed the implementation-defined
@@ -3639,13 +3638,13 @@ build_new (vec<tree, va_gc> **placement, tree type, tree nelts,
       /* Try to determine the constant value only for the purposes
         of the diagnostic below but continue to use the original
         value and handle const folding later.  */
-      const_tree cst_nelts = maybe_constant_value (nelts);
+      const_tree cst_nelts = fold_non_dependent_expr (nelts);
 
       /* The expression in a noptr-new-declarator is erroneous if it's of
         non-class type and its value before converting to std::size_t is
         less than zero. ... If the expression is a constant expression,
         the program is ill-fomed.  */
-      if (INTEGER_CST == TREE_CODE (cst_nelts)
+      if (TREE_CODE (cst_nelts) == INTEGER_CST
          && tree_int_cst_sgn (cst_nelts) == -1)
        {
          if (complain & tf_error)
index d71ee6c..8a6fe20 100644 (file)
@@ -1,3 +1,8 @@
+2018-05-24  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/85847
+       * g++.dg/cpp0x/new3.C: New test.
+
 2018-05-24  Javier Miranda  <miranda@adacore.com>
 
        * gnat.dg/discr52.adb: New testcase.
diff --git a/gcc/testsuite/g++.dg/cpp0x/new3.C b/gcc/testsuite/g++.dg/cpp0x/new3.C
new file mode 100644 (file)
index 0000000..c388acf
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/85847
+// { dg-do compile { target c++11 } }
+
+template <class>
+int f(int b) { return b; }
+
+template <class>
+void g()
+{
+  auto a = new int[f<int>(2), 2];
+}