c++: Fix paren init of aggregates in unevaluated context [PR92947]

Message ID 20200207202945.123657-1-polacek@redhat.com
State New
Headers show
Series
  • c++: Fix paren init of aggregates in unevaluated context [PR92947]
Related show

Commit Message

Marek Polacek Feb. 7, 2020, 8:29 p.m.
When I implemented C++20 parenthesized initialization of aggregates
I introduced this bogus cp_unevaluated_operand check, thus disabling
this feature in unevaluated context.  Oop.

Removing the check turned up another bug: I wasn't checking the
return value of digest_init.  So when constructible_expr called
build_new_method_call_1 to see if we can construct one type from
another, it got back a bogus INIT_EXPR that looked something like
*(struct T &) 1 = <<< error >>>.  But that isn't the error_mark_node,
so constructible_expr thought we had been successful in creating the
ctor call, and it gave the wrong answer.  Covered by paren-init17.C.

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

	PR c++/92947 - Paren init of aggregates in unevaluated context.
	* call.c (build_new_method_call_1): Don't check
	cp_unevaluated_operand.  Check the return value of digest_init.

	* g++.dg/cpp2a/paren-init21.C: New test.
---
 gcc/cp/call.c                             |  3 ++-
 gcc/testsuite/g++.dg/cpp2a/paren-init21.C | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/paren-init21.C


base-commit: 572992c8920d5339a3ac28d442c436d6daa0bfae
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

Comments

Jason Merrill Feb. 7, 2020, 9:41 p.m. | #1
On 2/7/20 3:29 PM, Marek Polacek wrote:
> When I implemented C++20 parenthesized initialization of aggregates

> I introduced this bogus cp_unevaluated_operand check, thus disabling

> this feature in unevaluated context.  Oop.

> 

> Removing the check turned up another bug: I wasn't checking the

> return value of digest_init.  So when constructible_expr called

> build_new_method_call_1 to see if we can construct one type from

> another, it got back a bogus INIT_EXPR that looked something like

> *(struct T &) 1 = <<< error >>>.  But that isn't the error_mark_node,

> so constructible_expr thought we had been successful in creating the

> ctor call, and it gave the wrong answer.  Covered by paren-init17.C.

> 

> Bootstrapped/regtested on x86_64-linux, ok for trunk?


OK.

> 	PR c++/92947 - Paren init of aggregates in unevaluated context.

> 	* call.c (build_new_method_call_1): Don't check

> 	cp_unevaluated_operand.  Check the return value of digest_init.

> 

> 	* g++.dg/cpp2a/paren-init21.C: New test.

> ---

>   gcc/cp/call.c                             |  3 ++-

>   gcc/testsuite/g++.dg/cpp2a/paren-init21.C | 14 ++++++++++++++

>   2 files changed, 16 insertions(+), 1 deletion(-)

>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/paren-init21.C

> 

> diff --git a/gcc/cp/call.c b/gcc/cp/call.c

> index fde29f8f631..51621b7dd87 100644

> --- a/gcc/cp/call.c

> +++ b/gcc/cp/call.c

> @@ -10179,7 +10179,6 @@ build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,

>   	 the two.  */

>         if (DECL_CONSTRUCTOR_P (fn)

>   	  && !(flags & LOOKUP_ONLYCONVERTING)

> -	  && !cp_unevaluated_operand

>   	  && cxx_dialect >= cxx2a

>   	  && CP_AGGREGATE_TYPE_P (basetype)

>   	  && !user_args->is_empty ())

> @@ -10194,6 +10193,8 @@ build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,

>   	  else

>   	    {

>   	      ctor = digest_init (basetype, ctor, complain);

> +	      if (ctor == error_mark_node)

> +		return error_mark_node;

>   	      ctor = build2 (INIT_EXPR, TREE_TYPE (instance), instance, ctor);

>   	      TREE_SIDE_EFFECTS (ctor) = true;

>   	      return ctor;

> diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init21.C b/gcc/testsuite/g++.dg/cpp2a/paren-init21.C

> new file mode 100644

> index 00000000000..cfd37193f66

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/cpp2a/paren-init21.C

> @@ -0,0 +1,14 @@

> +// PR c++/92947 - Paren init of aggregates in unevaluated context.

> +// { dg-do compile { target c++2a } }

> +

> +struct A {

> +  int a;

> +  int b;

> +};

> +

> +int main()

> +{

> +  static_assert(__is_constructible(A, int, int));

> +  decltype(A(1,2)) foo;

> +  bool b = noexcept(A(1,2));

> +}

> 

> base-commit: 572992c8920d5339a3ac28d442c436d6daa0bfae

>

Patch

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index fde29f8f631..51621b7dd87 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -10179,7 +10179,6 @@  build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
 	 the two.  */
       if (DECL_CONSTRUCTOR_P (fn)
 	  && !(flags & LOOKUP_ONLYCONVERTING)
-	  && !cp_unevaluated_operand
 	  && cxx_dialect >= cxx2a
 	  && CP_AGGREGATE_TYPE_P (basetype)
 	  && !user_args->is_empty ())
@@ -10194,6 +10193,8 @@  build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
 	  else
 	    {
 	      ctor = digest_init (basetype, ctor, complain);
+	      if (ctor == error_mark_node)
+		return error_mark_node;
 	      ctor = build2 (INIT_EXPR, TREE_TYPE (instance), instance, ctor);
 	      TREE_SIDE_EFFECTS (ctor) = true;
 	      return ctor;
diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init21.C b/gcc/testsuite/g++.dg/cpp2a/paren-init21.C
new file mode 100644
index 00000000000..cfd37193f66
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/paren-init21.C
@@ -0,0 +1,14 @@ 
+// PR c++/92947 - Paren init of aggregates in unevaluated context.
+// { dg-do compile { target c++2a } }
+
+struct A {
+  int a;
+  int b;
+};
+
+int main()
+{
+  static_assert(__is_constructible(A, int, int));
+  decltype(A(1,2)) foo;
+  bool b = noexcept(A(1,2));
+}