diff mbox series

c++: templated substitution into lambda-expr, cont [PR114393]

Message ID 20240412134711.3308389-1-ppalka@redhat.com
State New
Headers show
Series c++: templated substitution into lambda-expr, cont [PR114393] | expand

Commit Message

Patrick Palka April 12, 2024, 1:47 p.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

-- >8 --

The original PR114393 testcase is unfortunately still not accepted after
r14-9938-g081c1e93d56d35 due to return type deduction confusion when a
lambda-expr is used as a default template argument.

The below reduced testcase demonstrates the bug.  Here, when forming the
dependent specialization b_v<U> we substitute the default argument of F,
a lambda-expr, with _Descriptor=U.  (In this case in_template_context is
true since we're in the context of the template c_v so we don't defer.)
This substitution in turn lowers the level of its auto return type from
2 to 1.  So later, when instantiating c_v<int, char> we incorrectly
replace the auto with the template argument at level=0, index=0, i.e.
int, instead of going through do_auto_deduction which would yield char.

One way to fix this would be to use a level-less auto to represent a
deduced return type of a lambda, but that might be too invasive of a
change at this stage.

Another way would be to pass tf_partial during dependent substitution
into a default template argument from coerce_template_parms so that the
levels of deeper parameters don't get lowered, but that wouldn't do
the right thing currently due to the tf_partial early exit in the
LAMBDA_EXPR case of tsubst_expr.

Another way, and the approach that this patch takes, is to just defer
all dependent substitution into a lambda-expr, building upon the logic
added in r14-9938-g081c1e93d56d35.  This seems like the right thing to
do in light of the way we build up LAMBDA_EXPR_REGEN_INFO which should
consist only of the concrete template arguments used to regenerate the
lambda.

	PR c++/114393

gcc/cp/ChangeLog:

	* pt.cc (tsubst_lambda_expr): Also defer all dependent
	substitution into a lambda-expr.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/lambda-targ2a.C: New test.
---
 gcc/cp/pt.cc                               |  9 +++++++--
 gcc/testsuite/g++.dg/cpp2a/lambda-targ2a.C | 14 ++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-targ2a.C

Comments

Jason Merrill April 12, 2024, 5:43 p.m. UTC | #1
On 4/12/24 09:47, Patrick Palka wrote:
> 
> The original PR114393 testcase is unfortunately still not accepted after
> r14-9938-g081c1e93d56d35 due to return type deduction confusion when a
> lambda-expr is used as a default template argument.
> 
> The below reduced testcase demonstrates the bug.  Here, when forming the
> dependent specialization b_v<U> we substitute the default argument of F,
> a lambda-expr, with _Descriptor=U.  (In this case in_template_context is
> true since we're in the context of the template c_v so we don't defer.)
> This substitution in turn lowers the level of its auto return type from
> 2 to 1.  So later, when instantiating c_v<int, char> we incorrectly
> replace the auto with the template argument at level=0, index=0, i.e.
> int, instead of going through do_auto_deduction which would yield char.
> 
> One way to fix this would be to use a level-less auto to represent a
> deduced return type of a lambda, but that might be too invasive of a
> change at this stage.

I suspect we want to move to all level-less autos, apart from those that 
imply an actual template parameter.  But agreed, not in stage 4.

The patch is OK.

Jason
diff mbox series

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index ec259ee0fbf..bbd35417f8d 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -19622,11 +19622,16 @@  tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   in_decl = oldfn;
 
   args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl);
-  if (processing_template_decl && !in_template_context)
+  if (processing_template_decl
+      && (!in_template_context || any_dependent_template_arguments_p (args)))
     {
       /* Defer templated substitution into a lambda-expr if we lost the
 	 necessary template context.  This may happen for a lambda-expr
-	 used as a default template argument.  */
+	 used as a default template argument.
+
+	 Defer dependent substitution as well so that we don't lower the
+	 level of a deduced return type or any other auto or template
+	 parameter.  */
       t = copy_node (t);
       LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE;
       LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, complain);
diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-targ2a.C b/gcc/testsuite/g++.dg/cpp2a/lambda-targ2a.C
new file mode 100644
index 00000000000..7136ce79872
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/lambda-targ2a.C
@@ -0,0 +1,14 @@ 
+// PR c++/114393
+// { dg-do compile { target c++20 } }
+
+template <auto _DescriptorFn> struct c1 {};
+
+template <class _Descriptor, auto F = [] { return _Descriptor(); }>
+inline constexpr auto b_v = F;
+
+template <class T, class U>
+inline constexpr auto c_v = b_v<U>;
+
+auto f = c_v<int, char>;
+using type = decltype(f());
+using type = char;