From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 5E70C385735C; Fri, 21 Apr 2023 20:28:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E70C385735C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682108889; bh=ZpbP8jKNc0wDcJFKuN4Pr5Mvcxb34UBSDZGAsb/OV78=; h=From:To:Subject:Date:From; b=es9+okfyJoabE/UBF++t3JZrMbs+MpTcUGLxCxfzUO3uPos4KFfIwRrmQFfMNqkIZ GGJ82KRz5JR9cxboGx7vvVZ7epmRNjvjkHzTZxi4qdtr71s5rVf3txth/QR7ZIN68w c8dDINBNM9bO/1ImXpIYzRlsTueplL5p+FnNXVyo= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r10-11305] c++: constant, array, lambda, template [PR108975] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: fc03893d2fead133ed336511ac00d75c10c5a88d X-Git-Newrev: 92e2bb31ffe25271e5ae70d7533469419b883864 Message-Id: <20230421202809.5E70C385735C@sourceware.org> Date: Fri, 21 Apr 2023 20:28:09 +0000 (GMT) List-Id: https://gcc.gnu.org/g:92e2bb31ffe25271e5ae70d7533469419b883864 commit r10-11305-g92e2bb31ffe25271e5ae70d7533469419b883864 Author: Jason Merrill Date: Fri Mar 17 17:26:40 2023 -0400 c++: constant, array, lambda, template [PR108975] When a lambda refers to a constant local variable in the enclosing scope, we tentatively capture it, but if we end up pulling out its constant value, we go back at the end of the lambda and prune any unneeded captures. Here while parsing the template we decided that the dim capture was unneeded, because we folded it away, but then we brought back the use in the template trees that try to preserve the source representation with added type info. So then when we tried to instantiate that use, we couldn't find what it was trying to use, and crashed. Fixed by not trying to prune when parsing a template; we'll prune at instantiation time. PR c++/108975 gcc/cp/ChangeLog: * lambda.c (prune_lambda_captures): Don't bother in a template. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/lambda/lambda-const11.C: New test. Diff: --- gcc/cp/lambda.c | 3 +++ gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const11.C | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c index ec87f3b163c..42b9d075f9e 100644 --- a/gcc/cp/lambda.c +++ b/gcc/cp/lambda.c @@ -1537,6 +1537,9 @@ prune_lambda_captures (tree body) if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE) /* No default captures, and we don't prune explicit captures. */ return; + /* Don't bother pruning in a template, we'll prune at instantiation time. */ + if (dependent_type_p (TREE_TYPE (lam))) + return; hash_map const_vars; diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const11.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const11.C new file mode 100644 index 00000000000..26af75bf132 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const11.C @@ -0,0 +1,14 @@ +// PR c++/108975 +// { dg-do compile { target c++11 } } + +template +void f() { + constexpr int dim = 1; + auto l = [&] { + int n[dim * 1]; + }; + // In f, we shouldn't actually capture dim. + static_assert (sizeof(l) == 1, ""); +} + +template void f();