The diagnostic code in build_new{,_1} was using maybe_constant_value to fold the array length, but that breaks while parsing a template, because we might then leak template codes to the constexpr machinery.
Bootstrapped/regtested on x86_64-linux, ok for trunk/8? 2018-05-23 Marek Polacek <pola...@redhat.com> PR c++/85847 * init.c (build_new_1): Use fold_non_dependent_expr. (build_new): Likewise. * g++.dg/cpp0x/new3.C: New test. diff --git gcc/cp/init.c gcc/cp/init.c index b558742abf6..d96fec46f65 100644 --- gcc/cp/init.c +++ gcc/cp/init.c @@ -2860,7 +2860,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, /* 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); + 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. */ @@ -3639,7 +3639,7 @@ 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 diff --git gcc/testsuite/g++.dg/cpp0x/new3.C gcc/testsuite/g++.dg/cpp0x/new3.C index e69de29bb2d..c388acf552e 100644 --- gcc/testsuite/g++.dg/cpp0x/new3.C +++ gcc/testsuite/g++.dg/cpp0x/new3.C @@ -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]; +}