From patchwork Fri Feb 15 04:09:32 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: C++ PATCH for c++/52026 (use of enclosing constant var in lambda) Date: Thu, 14 Feb 2013 18:09:32 -0000 From: Jason Merrill X-Patchwork-Id: 220621 Message-Id: <511DB4FC.9050600@redhat.com> To: gcc-patches List In this testcase, we see a use of a constant variable in a lambda (though the same thing can happen with a local class member function). We try to pull out the constant value in finish_id_expression, but because the initializer isn't a reduced constant yet, that fails and we end up just returning the variable. At instantiation time tsubst instantiates the use of the variable by creating a new one in the lambda context, which is obviously broken. A better solution is to defer deciding how to handle the variable until instantiation time, at which point we will know. Since the logic for this is in finish_id_expression, it's easy to just return the name and look it up again at that point. Tested x86_64-pc-linux-gnu, applying to trunk and 4.7. commit 38e1d69672c4028124637e3b78430f4c45dff6a8 Author: Jason Merrill Date: Thu Feb 14 22:11:56 2013 -0500 PR c++/52026 * semantics.c (finish_id_expression): In a template, return the identifier for a constant variable. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 28b4b79..0e09d04 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -3015,7 +3015,14 @@ finish_id_expression (tree id_expression, FIXME update for final resolution of core issue 696. */ if (decl_constant_var_p (decl)) - return integral_constant_value (decl); + { + if (processing_template_decl) + /* In a template, the constant value may not be in a usable + form, so look it up again at instantiation time. */ + return id_expression; + else + return integral_constant_value (decl); + } /* If we are in a lambda function, we can move out until we hit 1. the context, diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const2.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const2.C new file mode 100644 index 0000000..d2457d6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const2.C @@ -0,0 +1,15 @@ +// PR c++/52026 +// { dg-options "-std=c++11 -O" } +// { dg-do run } + +template +int func() { + const int constVal1 = B ? 100 : -100; + const int constVal = constVal1; + return [] { return constVal; }(); +} + +int main() { + if (func() != 100) + __builtin_abort (); +}