From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 58E043858D1E; Tue, 25 Apr 2023 19:59:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 58E043858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682452783; bh=b70NYh0V1HLgfvAx20w4y6Va1uO2+vgiEgAZiYG7aOs=; h=From:To:Subject:Date:From; b=SVU2th0byXKmdkucTBHhkAkeG8WkqmY7WR+3acOeebk+nSyWvre8tlId+j4+siVFD PGMF9xAiMD4j/pGdyT9CShyyY00qyYEnER69JFue4kyu9ZyVvec2xig5EYZFK6VvBu j8/f+R9NUJkcOwh4xMQHUq5tTXGbEbzheM7Uvflk= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-228] c++: value dependence of by-ref lambda capture [PR108975] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: 0530254413f8d356805442f659c6d2921739284d X-Git-Newrev: 3d674e29d7f89bf93fcfcc963ff0248c6347586d Message-Id: <20230425195943.58E043858D1E@sourceware.org> Date: Tue, 25 Apr 2023 19:59:43 +0000 (GMT) List-Id: https://gcc.gnu.org/g:3d674e29d7f89bf93fcfcc963ff0248c6347586d commit r14-228-g3d674e29d7f89bf93fcfcc963ff0248c6347586d Author: Patrick Palka Date: Tue Apr 25 15:59:22 2023 -0400 c++: value dependence of by-ref lambda capture [PR108975] We are still ICEing on the generic lambda version of the testcase from this PR, even after r13-6743-g6f90de97634d6f, due to the by-ref capture of the constant local variable 'dim' being considered value-dependent when regenerating the lambda (at which point processing_template_decl is set since the lambda is generic), which prevents us from constant folding its uses. Later during prune_lambda_captures we end up not thoroughly walking the body of the lambda and overlook the (non-folded) uses of 'dim' within the array bound and using-decls. We could fix this by making prune_lambda_captures walk the body of the lambda more thoroughly so that it finds these uses of 'dim', but ideally we should be able to constant fold all uses of 'dim' ahead of time and prune the implicit capture after all. To that end this patch makes value_dependent_expression_p return false for such by-ref captures of constant local variables, allowing their uses to get constant folded ahead of time. It seems we just need to disable the predicate's conservative early exit for reference variables (added by r5-5022-g51d72abe5ea04e) when DECL_HAS_VALUE_EXPR_P. This effectively makes us treat by-value and by-ref captures more consistently when it comes to value dependence. PR c++/108975 gcc/cp/ChangeLog: * pt.cc (value_dependent_expression_p) : Suppress conservative early exit for reference variables when DECL_HAS_VALUE_EXPR_P. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/lambda/lambda-const11a.C: New test. Diff: --- gcc/cp/pt.cc | 7 ++++--- gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const11a.C | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 3e5f0104056..a668825b5f9 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -27959,9 +27959,7 @@ value_dependent_expression_p (tree expression) case VAR_DECL: /* A constant with literal type and is initialized with an expression that is value-dependent. */ - if (DECL_DEPENDENT_INIT_P (expression) - /* FIXME cp_finish_decl doesn't fold reference initializers. */ - || TYPE_REF_P (TREE_TYPE (expression))) + if (DECL_DEPENDENT_INIT_P (expression)) return true; if (DECL_HAS_VALUE_EXPR_P (expression)) { @@ -27976,6 +27974,9 @@ value_dependent_expression_p (tree expression) && value_expr == error_mark_node)) return true; } + else if (TYPE_REF_P (TREE_TYPE (expression))) + /* FIXME cp_finish_decl doesn't fold reference initializers. */ + return true; return false; case DYNAMIC_CAST_EXPR: diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const11a.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const11a.C new file mode 100644 index 00000000000..7fc3d48a8dd --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const11a.C @@ -0,0 +1,21 @@ +// PR c++/108975 +// A version of lambda-const11.C using a generic lambda. +// { dg-do compile { target c++14 } } + +template void g(); +template struct A { }; + +template +void f() { + constexpr int dim = 1; + auto l = [&](auto) { + int n[dim * 1]; + using ty1 = decltype(g()); + using ty2 = A; + }; + l(0); + // In f, we shouldn't actually capture dim. + static_assert (sizeof(l) == 1, ""); +} + +template void f();