https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123408

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <[email protected]>:

https://gcc.gnu.org/g:77411b4b0d8751ecd680a3f7fed1d0de873c41cd

commit r16-7855-g77411b4b0d8751ecd680a3f7fed1d0de873c41cd
Author: Patrick Palka <[email protected]>
Date:   Mon Mar 2 22:35:55 2026 -0500

    c++: targ generic lambda iterated substitution [PR123665]

    In the first testcase below, the targ generic lambda

      template<class T, class V = decltype([](auto) { })>
      ...

    has two levels of parameters, the outer level {T} and its own level.
    We iteratively substitute into this targ lambda three times:

      1. The first substitution is during coerce_template_parms with args={T*,
}
         and tf_partial set.  Since tf_partial is set, we defer the
substitution.

      2. The next substitution is during regeneration of f<void>()::<lambda>
         with args={void}.  Here we merge with the deferred arguments to
         obtain args={void*, } and substitute them into the lambda, returning
         a regenerated generic lambda with template depth 1 (no more outer
         template parameters).

      3. The final (non-templated) substitution is during instantiation of
         f<int>()::<lambda>'s call operator with args={int}.  But at this
         point, the targ generic lambda has only one set of template
         parameters, its own, and so this substitution causes us to substitute
         away all its template parameters (and its deduced return type).
         We end up ICEing from tsubst_template_decl due to its operator()
         having now having an empty template parameter set.

    The problem ultimately is that the targ lambda leaks into a template
    context that has more template parameters than its lexical context, and
    we end up over-substituting into the lambda.  By the third substitution
    the lambda is effectively non-dependent and we really just want to lower
    it to a non-templated lambda without actually doing any substitution.
    Unfortunately, I wasn't able to get such lowering to work adequately
    (e.g. precise dependence checks don't work, uses_template_parms (TREE_TYPE
(t))
    wrongly returns false, false, true respectively during each of the three
    substitutions.)

    This patch instead takes a different approach, and makes lambda
    deferred-ness sticky: once we decide to defer substitution into a
    lambda, we keep deferring any subsequent substitution until the
    final substitution, which must be non-templated.  So for this
    particular testcase the substitutions are now:

      1. Return a lambda with deferred args={T*, }.

      2. Merge args={void} with deferred args={T*, }, obtaining args={void*, }
         and returning a lambda with deferred args={void*, }.

      3. Merge args={int} with deferred args={void*, }, obtaining args={void*,
}.
         Since this substitution is final (processing_template_decl is
cleared),
         we substitute args={void*, } into the lambda once and for all and
         return a regenerated non-templated generic lambda with template depth
1.

    In order for a subsequent add_extra_args to properly merge arguments
    that have been iteratively deferred, it and build_extra_args needs
    to propagate TREE_STATIC appropriately (which effectively signals
    whether the arguments are a full set or not).

    While PR123655 is a regression, this patch also fixes the similar
    PR123408 which is not a regression.  Thus, I suspect that the testcase
    from the first PR only worked by accident.

            PR c++/123665
            PR c++/123408

    gcc/cp/ChangeLog:

            * pt.cc (build_extra_args): If TREE_STATIC was set on the
            arguments, keep it set.
            (add_extra_args): Set TREE_STATIC on the resulting arguments
            when substituting templated arguments into a full set of
            deferred arguments.
            (tsubst_lambda_expr): Always defer templated substitution if
            LAMBDA_EXPR_EXTRA_ARGS was set.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp2a/lambda-targ22.C: New test.
            * g++.dg/cpp2a/lambda-targ22a.C: New test.
            * g++.dg/cpp2a/lambda-targ23.C: New test.

    Reviewed-by: Jason Merrill <[email protected]>

Reply via email to