On 3/10/22 18:04, Marek Polacek wrote:
Since r9-6073 cxx_eval_store_expression preevaluates the value to
be stored, and that revealed a crash where a template code (here,
code=IMPLICIT_CONV_EXPR) leaks into cxx_eval*.
It happens because we're performing build_vec_init while processing
a template
Hmm, that seems like the bug. Where's that call coming from?
which calls get_temp_regvar which creates an INIT_EXPR.
This INIT_EXPR's RHS contains an rvalue conversion so we create an
IMPLICIT_CONV_EXPR. Its operand is not type-dependent and the whole
INIT_EXPR is not type-dependent. So we call build_non_dependent_expr
which, with -fchecking=2, calls fold_non_dependent_expr. At this
point the expression still has an IMPLICIT_CONV_EXPR, which ought to
be handled in instantiate_non_dependent_expr_internal. However,
tsubst_copy_and_build doesn't handle INIT_EXPR; it will just call
tsubst_copy which does nothing when args is null. So we fail to
replace the IMPLICIT_CONV_EXPR and ICE.
Eliding the IMPLICIT_CONV_EXPR in this particular case would be too
risky, so we could do
if (TREE_CODE (t) == INIT_EXPR)
t = TREE_OPERAND (t, 1);
in fold_non_dependent_expr, but that feels too ad hoc. So it might
make sense to actually take care of INIT_EXPR in tsubst_c_and_b.
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/11?
PR c++/104284
gcc/cp/ChangeLog:
* pt.cc (tsubst_copy_and_build): Handle INIT_EXPR.
gcc/testsuite/ChangeLog:
* g++.dg/cpp1y/constexpr-104284.C: New test.
---
gcc/cp/pt.cc | 8 ++++++++
gcc/testsuite/g++.dg/cpp1y/constexpr-104284.C | 17 +++++++++++++++++
2 files changed, 25 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-104284.C
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index f7ee33a6dfd..e8920f98e4d 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -21289,6 +21289,14 @@ tsubst_copy_and_build (tree t,
with constant operands. */
RETURN (t);
+ case INIT_EXPR:
+ {
+ tree op0 = RECUR (TREE_OPERAND (t, 0));
+ tree op1 = RECUR (TREE_OPERAND (t, 1));
+ RETURN (build2_loc (input_location, INIT_EXPR, TREE_TYPE (op0),
+ op0, op1));
+ }
+
case NON_LVALUE_EXPR:
case VIEW_CONVERT_EXPR:
if (location_wrapper_p (t))
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-104284.C
b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284.C
new file mode 100644
index 00000000000..f60033069e4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284.C
@@ -0,0 +1,17 @@
+// PR c++/104284
+// { dg-do compile { target c++14 } }
+// { dg-additional-options "-fchecking=2" }
+
+struct S {
+ char c{};
+};
+
+auto x = [](auto) { constexpr S s[]{{}}; };
+
+template<class>
+constexpr void gn ()
+{
+ constexpr S s[]{{}};
+}
+
+static_assert ((gn<int>(), true), "");
base-commit: b5417a0ba7e26bec2abf05cad6c6ef840a9be41c