Summary: | [4.6/4.7/4.8 Regression] Constexpr Variable Appears Uninitialized in Lambda | ||
---|---|---|---|
Product: | gcc | Reporter: | cheesear |
Component: | c++ | Assignee: | Jason Merrill <jason> |
Status: | RESOLVED FIXED | ||
Severity: | normal | CC: | jason, jpalecek |
Priority: | P3 | Keywords: | c++-lambda, wrong-code |
Version: | 4.6.1 | ||
Target Milestone: | 4.7.3 | ||
Host: | Target: | ||
Build: | Known to work: | ||
Known to fail: | Last reconfirmed: | 2012-01-28 00:00:00 | |
Bug Depends on: | 56947 | ||
Bug Blocks: | 54367 | ||
Attachments: |
The code sample exhibiting the suspected bug.
Code example to trigger the supposed bug. |
Confirmed. Created attachment 27958 [details]
Code example to trigger the supposed bug.
only the "bug" function triggers the bug, while "nobug1" and "nobug2" don't.
Sorry, to make two comments. The bug is indeed not only showing a warning, but the variable is uninitialized when executing the code. Could it be a duplicate of <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53488">53488</a>? *** Bug 53488 has been marked as a duplicate of this bug. *** BTW I have proposed a patch that fixes this problem here: http://gcc.gnu.org/ml/gcc-patches/2012-08/msg01252.html. Please take a look at it. Do you have a copyright assignment on file? (In reply to comment #7) > Do you have a copyright assignment on file? I don't know what you're talking about, so probably no. (BTW which file?) That's a big problem if you are serious about contributing to gcc. See: http://gcc.gnu.org/contribute.html In short, if you don't want to waste time, make sure to obtain, fill and return the forms before sending patches. I have a smaller reproduction case, and some extra information: References to non-explicitly captured constant values in template functions generate invalid code. An empty capture list fails to error, and generates invalid code. A catch-all capture list ([=] or [&]) correctly compiles, but generates invalid code. A capture list that explicitly captures the constant value by reference or value compiles and generates correct code. With -Wall -Wextra, the constant is flagged as "uninitialized". The code compiles and works correctly on GCC 4.5.2, but fails on GCC 4.6.3 and GCC 4.7.2. template<bool B> int func() { const int constVal = B ? 100 : -100; return [] { return constVal; }(); } int main(int, const char* []) { return func<true>(); // Returns 100 in -O0, but 0 in -O1 and above } Seems indeed a regression. Author: jason Date: Fri Feb 15 16:17:25 2013 New Revision: 196081 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196081 Log: PR c++/52026 * semantics.c (finish_id_expression): In a template, return the identifier for a constant variable. Added: trunk/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const2.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/semantics.c Author: jason Date: Fri Feb 15 18:32:12 2013 New Revision: 196087 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196087 Log: PR c++/52026 * semantics.c (finish_id_expression): In a template, return the identifier for a constant variable. Added: branches/gcc-4_7-branch/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const2.C Modified: branches/gcc-4_7-branch/gcc/cp/ChangeLog branches/gcc-4_7-branch/gcc/cp/semantics.c Fixed for 4.7.3. |
Created attachment 26487 [details] The code sample exhibiting the suspected bug. Hello everyone, In the code fragment below, g++ (4.6.1 on Linux Mint 64-bit) complains that x is uninitialized in the lambda function, and prints out an initialized value from the stack for the last three lines of the program's output. I'd except all four lines of the output to be '5', so I suspect that this may be a compiler bug. Also, even without the reference-capture in the lambda function, the code still compiles. Below follows the command used to compile the code, along with one possible output. # g++ -Wall -std=c++0x c.cpp c.cpp: In lambda function: c.cpp:25:4: warning: ‘x’ is used uninitialized in this function [-Wuninitialized] # ./a.out 5 32535 32535 32535