c++: ICE with VEC_INIT_EXPR and defarg [PR106925]

Message ID 20221011200003.695682-1-polacek@redhat.com
State Accepted, archived
Headers
Series c++: ICE with VEC_INIT_EXPR and defarg [PR106925] |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Marek Polacek Oct. 11, 2022, 8 p.m. UTC
  Since r12-8066, in cxx_eval_vec_init we perform expand_vec_init_expr
while processing the default argument in this test.  At this point
start_preparsed_function hasn't yet set current_function_decl.
expand_vec_init_expr then leads to maybe_splice_retval_cleanup which
checks DECL_CONSTRUCTOR_P (current_function_decl) without checking that
c_f_d is non-null first.  It seems correct that c_f_d is null here, so
it seems to me that maybe_splice_retval_cleanup should check c_f_d as
in the following patch.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/12?

	PR c++/106925

gcc/cp/ChangeLog:

	* except.cc (maybe_splice_retval_cleanup): Check current_function_decl.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/initlist-defarg3.C: New test.
---
 gcc/cp/except.cc                              |  3 +++
 gcc/testsuite/g++.dg/cpp0x/initlist-defarg3.C | 13 +++++++++++++
 2 files changed, 16 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-defarg3.C


base-commit: 23c3cbaed36f6d2f3a7a64f6ebda69329723514b
  

Comments

Jason Merrill Oct. 11, 2022, 8:28 p.m. UTC | #1
On 10/11/22 16:00, Marek Polacek wrote:
> Since r12-8066, in cxx_eval_vec_init we perform expand_vec_init_expr
> while processing the default argument in this test.

Hmm, why are we calling cxx_eval_vec_init during parsing of the default 
argument?  In particular, any expansion that depends on the enclosing 
function context should be deferred until the default arg is used by a call.

But it's certainly true that the "function_body" test is wrong in this 
situation; you might move the c_f_d test into the calculation of that 
variable.  The patch is OK with that change, but please also answer my 
question above.

> At this point
> start_preparsed_function hasn't yet set current_function_decl.
> expand_vec_init_expr then leads to maybe_splice_retval_cleanup which
> checks DECL_CONSTRUCTOR_P (current_function_decl) without checking that
> c_f_d is non-null first.  It seems correct that c_f_d is null here, so
> it seems to me that maybe_splice_retval_cleanup should check c_f_d as
> in the following patch.

> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/12?
> 
> 	PR c++/106925
> 
> gcc/cp/ChangeLog:
> 
> 	* except.cc (maybe_splice_retval_cleanup): Check current_function_decl.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/initlist-defarg3.C: New test.
> ---
>   gcc/cp/except.cc                              |  3 +++
>   gcc/testsuite/g++.dg/cpp0x/initlist-defarg3.C | 13 +++++++++++++
>   2 files changed, 16 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-defarg3.C
> 
> diff --git a/gcc/cp/except.cc b/gcc/cp/except.cc
> index b8a85ed0572..9f77289b9ca 100644
> --- a/gcc/cp/except.cc
> +++ b/gcc/cp/except.cc
> @@ -1327,6 +1327,9 @@ maybe_splice_retval_cleanup (tree compound_stmt)
>          && current_binding_level->level_chain->kind == sk_function_parms);
>   
>     if ((function_body || current_binding_level->kind == sk_try)
> +      /* When we're processing a default argument, c_f_d may not have been
> +	 set.  */
> +      && current_function_decl
>         && !DECL_CONSTRUCTOR_P (current_function_decl)
>         && !DECL_DESTRUCTOR_P (current_function_decl)
>         && current_retval_sentinel)
> diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-defarg3.C b/gcc/testsuite/g++.dg/cpp0x/initlist-defarg3.C
> new file mode 100644
> index 00000000000..5c3e886b306
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-defarg3.C
> @@ -0,0 +1,13 @@
> +// PR c++/106925
> +// { dg-do compile { target c++11 } }
> +
> +struct Foo;
> +template <int _Nm> struct __array_traits { typedef Foo _Type[_Nm]; };
> +template <int _Nm> struct array {
> +  typename __array_traits<_Nm>::_Type _M_elems;
> +};
> +template <int size> struct MyVector { array<size> data{}; };
> +struct Foo {
> +  float a{0};
> +};
> +void foo(MyVector<1> = MyVector<1>());
> 
> base-commit: 23c3cbaed36f6d2f3a7a64f6ebda69329723514b
  

Patch

diff --git a/gcc/cp/except.cc b/gcc/cp/except.cc
index b8a85ed0572..9f77289b9ca 100644
--- a/gcc/cp/except.cc
+++ b/gcc/cp/except.cc
@@ -1327,6 +1327,9 @@  maybe_splice_retval_cleanup (tree compound_stmt)
        && current_binding_level->level_chain->kind == sk_function_parms);
 
   if ((function_body || current_binding_level->kind == sk_try)
+      /* When we're processing a default argument, c_f_d may not have been
+	 set.  */
+      && current_function_decl
       && !DECL_CONSTRUCTOR_P (current_function_decl)
       && !DECL_DESTRUCTOR_P (current_function_decl)
       && current_retval_sentinel)
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-defarg3.C b/gcc/testsuite/g++.dg/cpp0x/initlist-defarg3.C
new file mode 100644
index 00000000000..5c3e886b306
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist-defarg3.C
@@ -0,0 +1,13 @@ 
+// PR c++/106925
+// { dg-do compile { target c++11 } }
+
+struct Foo;
+template <int _Nm> struct __array_traits { typedef Foo _Type[_Nm]; };
+template <int _Nm> struct array {
+  typename __array_traits<_Nm>::_Type _M_elems;
+};
+template <int size> struct MyVector { array<size> data{}; };
+struct Foo {
+  float a{0};
+};
+void foo(MyVector<1> = MyVector<1>());