From: Jason Merrill Date: Fri, 17 Mar 2023 21:26:40 +0000 (-0400) Subject: c++: constant, array, lambda, template [PR108975] X-Git-Tag: upstream/13.1.0~501 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6f90de97634d6f1617a054429f28b85fbfbd8b6f;p=platform%2Fupstream%2Fgcc.git 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.cc (prune_lambda_captures): Don't bother in a template. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/lambda/lambda-const11.C: New test. --- diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc index 212990a..9925209 100644 --- a/gcc/cp/lambda.cc +++ b/gcc/cp/lambda.cc @@ -1760,6 +1760,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 0000000..26af75b --- /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();