diff mbox series

c++: ICE with template code in constexpr [PR104284]

Message ID 20220310220459.91301-1-polacek@redhat.com
State New
Headers show
Series c++: ICE with template code in constexpr [PR104284] | expand

Commit Message

Marek Polacek March 10, 2022, 10:04 p.m. UTC
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, 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


base-commit: b5417a0ba7e26bec2abf05cad6c6ef840a9be41c

Comments

Marek Polacek March 10, 2022, 10:27 p.m. UTC | #1
On Thu, Mar 10, 2022 at 05:04:59PM -0500, Marek Polacek via Gcc-patches 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, 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.

Forgot to mention: without -fchecking=2 there's no problem because
digest_init will subst the IMPLICIT_CONV_EXPR:

#0  tsubst_copy_and_build (t=<implicit_conv_expr 0x7fffea4a9f20>, args=<tree 0x0>, complain=3, 
    in_decl=<tree 0x0>, function_p=false, integral_constant_expression_p=true)
    at /home/mpolacek/src/gcc/gcc/cp/pt.cc:20063
#1  0x0000000000de1ae1 in instantiate_non_dependent_expr_internal (expr=<implicit_conv_expr 0x7fffea4a9f20>, 
    complain=3) at /home/mpolacek/src/gcc/gcc/cp/pt.cc:6358
#2  0x0000000000b702d4 in fold_non_dependent_expr_template (t=<implicit_conv_expr 0x7fffea4a9f20>, 
    complain=3, manifestly_const_eval=false, object=<tree 0x0>)
    at /home/mpolacek/src/gcc/gcc/cp/constexpr.cc:8050
#3  0x0000000000b706f0 in fold_non_dependent_init (t=<implicit_conv_expr 0x7fffea4a9f20>, complain=3, 
    manifestly_const_eval=false, object=<tree 0x0>) at /home/mpolacek/src/gcc/gcc/cp/constexpr.cc:8143
#4  0x0000000000f08f4f in massage_init_elt (type=<record_type 0x7fffea4b29d8 S>, 
    init=<implicit_conv_expr 0x7fffea4a9f20>, nested=0, flags=257, complain=3)
    at /home/mpolacek/src/gcc/gcc/cp/typeck2.cc:1437
#5  0x0000000000f0949c in process_init_constructor_array (type=<array_type 0x7fffea4c9348>, 
    init=<constructor 0x7fffea4bfb10>, nested=0, flags=257, complain=3)
    at /home/mpolacek/src/gcc/gcc/cp/typeck2.cc:1502
#6  0x0000000000f0aec1 in process_init_constructor (type=<array_type 0x7fffea4c9348>, 
    init=<constructor 0x7fffea4bfb10>, nested=0, flags=257, complain=3)
    at /home/mpolacek/src/gcc/gcc/cp/typeck2.cc:1917
#7  0x0000000000f0890c in digest_init_r (type=<array_type 0x7fffea4c9348>, 
    init=<constructor 0x7fffea4bfb10>, nested=0, flags=257, complain=3)
    at /home/mpolacek/src/gcc/gcc/cp/typeck2.cc:1324
#8  0x0000000000f08b1b in digest_init_flags (type=<array_type 0x7fffea4c9348>, 
    init=<constructor 0x7fffea4bfb10>, flags=257, complain=3)
    at /home/mpolacek/src/gcc/gcc/cp/typeck2.cc:1370
#9  0x0000000000f06815 in store_init_value (decl=<var_decl 0x7fffea357cf0 s>, 
    init=<constructor 0x7fffea4bfb10>, cleanups=0x7fffffffba68, flags=257)
    at /home/mpolacek/src/gcc/gcc/cp/typeck2.cc:842
#10 0x0000000000bf56cc in check_initializer (decl=<var_decl 0x7fffea357cf0 s>, 
    init=<constructor 0x7fffea4bfb10>, flags=257, cleanups=0x7fffffffba68)
    at /home/mpolacek/src/gcc/gcc/cp/decl.cc:7337
#11 0x0000000000bfa8df in cp_finish_decl (decl=<var_decl 0x7fffea357cf0 s>, 
    init=<constructor 0x7fffea4bfac8>, init_const_expr_p=true, asmspec_tree=<tree 0x0>, flags=1)
    at /home/mpolacek/src/gcc/gcc/cp/decl.cc:8174
 
> 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
> -- 
> 2.35.1
> 

Marek
Jason Merrill March 11, 2022, 11:46 p.m. UTC | #2
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
diff mbox series

Patch

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), "");