diff mbox series

c++: Non-type-dependent variadic lambda init-capture [PR94483]

Message ID 20200416175331.2019448-1-ppalka@redhat.com
State New
Headers show
Series c++: Non-type-dependent variadic lambda init-capture [PR94483] | expand

Commit Message

Patrick Palka April 16, 2020, 5:53 p.m. UTC
In this PR (which I think is misclassified as ice-on-invalid instead of
ice-on-valid), we're ICEing on a use of an 'int... a' template parameter pack as
part of the variadic lambda init-capture [...z=a].

The unexpected thing about this variadic init-capture is that it is not
type-dependent, and so when we call do_auto_deduction from
lambda_capture_field_type it actually resolves its type to 'int' instead of
exiting early like it would do for a type-dependent variadic initializer.  This
later confuses add_capture which, according to one of its comments, assumes that
'type' is always 'auto' for a variadic init-capture.

The simplest fix, and the approach that this patch takes, seems to be to avoid
doing auto deduction in lambda_capture_field_type when the initializer uses
parameter packs, so that we always return 'auto' even in the non-type-dependent
case.

Passes 'make check-c++', does this look OK to commit after full
bootstrap/regtesting?

gcc/cp/ChangeLog:

	PR c++/94483
	* lambda.c (lambda_capture_field_type): Avoid doing auto deduction if
	the explicit initializer has parameter packs.

gcc/testsuite/ChangeLog:

	PR c++/94483
	* g++.dg/cpp2a/lambda-pack-init5.C: New test.
---
 gcc/cp/lambda.c                                |  5 ++++-
 gcc/testsuite/g++.dg/cpp2a/lambda-pack-init5.C | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-pack-init5.C

Comments

Jason Merrill April 17, 2020, 4:44 p.m. UTC | #1
On 4/16/20 1:53 PM, Patrick Palka wrote:
> In this PR (which I think is misclassified as ice-on-invalid instead of
> ice-on-valid), we're ICEing on a use of an 'int... a' template parameter pack as
> part of the variadic lambda init-capture [...z=a].
> 
> The unexpected thing about this variadic init-capture is that it is not
> type-dependent, and so when we call do_auto_deduction from
> lambda_capture_field_type it actually resolves its type to 'int' instead of
> exiting early like it would do for a type-dependent variadic initializer.  This
> later confuses add_capture which, according to one of its comments, assumes that
> 'type' is always 'auto' for a variadic init-capture.
> 
> The simplest fix, and the approach that this patch takes, seems to be to avoid
> doing auto deduction in lambda_capture_field_type when the initializer uses
> parameter packs, so that we always return 'auto' even in the non-type-dependent
> case.

Hmm, that will make 'z' more dependent than necessary, but I suppose its 
packness will tend to make uses dependent anyway, so OK.

> Passes 'make check-c++', does this look OK to commit after full
> bootstrap/regtesting?
> 
> gcc/cp/ChangeLog:
> 
> 	PR c++/94483
> 	* lambda.c (lambda_capture_field_type): Avoid doing auto deduction if
> 	the explicit initializer has parameter packs.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c++/94483
> 	* g++.dg/cpp2a/lambda-pack-init5.C: New test.
> ---
>   gcc/cp/lambda.c                                |  5 ++++-
>   gcc/testsuite/g++.dg/cpp2a/lambda-pack-init5.C | 18 ++++++++++++++++++
>   2 files changed, 22 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-pack-init5.C
> 
> diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c
> index 4f39f99756b..b55c2f85d27 100644
> --- a/gcc/cp/lambda.c
> +++ b/gcc/cp/lambda.c
> @@ -223,7 +223,10 @@ lambda_capture_field_type (tree expr, bool explicit_init_p,
>   	/* Add the reference now, so deduction doesn't lose
>   	   outermost CV qualifiers of EXPR.  */
>   	type = build_reference_type (type);
> -      type = do_auto_deduction (type, expr, auto_node);
> +      if (uses_parameter_packs (expr))
> +	/* Stick with 'auto' even if the type could be deduced.  */;
> +      else
> +	type = do_auto_deduction (type, expr, auto_node);
>       }
>     else if (!is_this && type_dependent_expression_p (expr))
>       {
> diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-pack-init5.C b/gcc/testsuite/g++.dg/cpp2a/lambda-pack-init5.C
> new file mode 100644
> index 00000000000..492fc479e94
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-pack-init5.C
> @@ -0,0 +1,18 @@
> +// PR c++/94483
> +// { dg-do compile { target c++2a } }
> +
> +template<int... a> constexpr auto x1
> +  = [...z = -a] (auto F) { return F(z...); };
> +
> +template<const int&... a> constexpr auto x2
> +  = [&...z = a] (auto F) { return F(z...); };
> +
> +template<int... a> constexpr auto x3
> +  = [z = -a] (auto F) { return F(z); }; // { dg-error "packs not expanded" }
> +
> +
> +constexpr auto sum = [] (auto... xs) { return (xs + ... + 0); };
> +const int y1 = 1, y2 = 2, y3 = 3;
> +
> +static_assert(x1<1,2,3>(sum) == -6);
> +static_assert(x2<y1,y2,y3>(sum) == 6);
>
diff mbox series

Patch

diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c
index 4f39f99756b..b55c2f85d27 100644
--- a/gcc/cp/lambda.c
+++ b/gcc/cp/lambda.c
@@ -223,7 +223,10 @@  lambda_capture_field_type (tree expr, bool explicit_init_p,
 	/* Add the reference now, so deduction doesn't lose
 	   outermost CV qualifiers of EXPR.  */
 	type = build_reference_type (type);
-      type = do_auto_deduction (type, expr, auto_node);
+      if (uses_parameter_packs (expr))
+	/* Stick with 'auto' even if the type could be deduced.  */;
+      else
+	type = do_auto_deduction (type, expr, auto_node);
     }
   else if (!is_this && type_dependent_expression_p (expr))
     {
diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-pack-init5.C b/gcc/testsuite/g++.dg/cpp2a/lambda-pack-init5.C
new file mode 100644
index 00000000000..492fc479e94
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/lambda-pack-init5.C
@@ -0,0 +1,18 @@ 
+// PR c++/94483
+// { dg-do compile { target c++2a } }
+
+template<int... a> constexpr auto x1
+  = [...z = -a] (auto F) { return F(z...); };
+
+template<const int&... a> constexpr auto x2
+  = [&...z = a] (auto F) { return F(z...); };
+
+template<int... a> constexpr auto x3
+  = [z = -a] (auto F) { return F(z); }; // { dg-error "packs not expanded" }
+
+
+constexpr auto sum = [] (auto... xs) { return (xs + ... + 0); };
+const int y1 = 1, y2 = 2, y3 = 3;
+
+static_assert(x1<1,2,3>(sum) == -6);
+static_assert(x2<y1,y2,y3>(sum) == 6);