]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: bound ttp in lambda function type [PR109651]
authorPatrick Palka <ppalka@redhat.com>
Sun, 7 May 2023 15:54:21 +0000 (11:54 -0400)
committerPatrick Palka <ppalka@redhat.com>
Sun, 7 May 2023 15:54:21 +0000 (11:54 -0400)
commit7bfb1550ccea7c426d50244e980f01f30db8ba0c
treec13d2022eaeee556e86844162bcd10bc6e9a88f7
parenta1a9ce2441df0675540faee8476523164e12578b
c++: bound ttp in lambda function type [PR109651]

After r14-11-g2245459c85a3f4 we now coerce the template arguments of a
bound ttp again after level-lowering it.  Notably a level-lowered ttp
doesn't have DECL_CONTEXT set, so during this coercion we fall back to
using current_template_parms to obtain the relevant set of in-scope
parameters.

But it turns out current_template_parms isn't properly set when
substituting the function type of a generic lambda, and so if the type
contains bound ttps that need to be lowered we'll crash during their
attempted coercion.  Specifically in the first testcase below,
current_template_parms during the lambda type substitution (with T=int)
is "1 U" instead of the expected "2 TT, 1 U", and we crash when level
lowering TT<int>.

Ultimately the problem is that tsubst_lambda_expr does things in the
wrong order: we ought to substitute (and install) the in-scope template
parameters _before_ substituting anything that may use those template
parameters (such as the function type of a generic lambda).  This patch
corrects this substitution order.

PR c++/109651

gcc/cp/ChangeLog:

* pt.cc (coerce_template_args_for_ttp): Mention we can hit the
current_template_parms fallback when level-lowering a bound ttp.
(tsubst_template_decl): Add lambda_tparms parameter.  Prefer to
use lambda_tparms instead of substituting DECL_TEMPLATE_PARMS.
(tsubst_decl) <case TEMPLATE_DECL>: Pass NULL_TREE as lambda_tparms
to tsubst_template_decl.
(tsubst_lambda_expr): For a generic lambda, substitute
DECL_TEMPLATE_PARMS and set current_template_parms to it
before substituting the function type.  Pass the substituted
DECL_TEMPLATE_PARMS as lambda_tparms to tsubst_template_decl.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/lambda-generic-ttp1.C: New test.
* g++.dg/cpp2a/lambda-generic-ttp2.C: New test.
gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp2a/lambda-generic-ttp1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/lambda-generic-ttp2.C [new file with mode: 0644]