Bug 52026 - [4.6/4.7/4.8 Regression] Constexpr Variable Appears Uninitialized in Lambda
Summary: [4.6/4.7/4.8 Regression] Constexpr Variable Appears Uninitialized in Lambda
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.1
: P3 normal
Target Milestone: 4.7.3
Assignee: Jason Merrill
URL:
Keywords: wrong-code
: 53488 (view as bug list)
Depends on: 56947
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2012-01-28 03:25 UTC by cheesear
Modified: 2014-05-27 08:50 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-01-28 00:00:00


Attachments
The code sample exhibiting the suspected bug. (279 bytes, text/plain)
2012-01-28 03:25 UTC, cheesear
Details
Code example to trigger the supposed bug. (324 bytes, text/plain)
2012-08-08 08:57 UTC, TbR
Details

Note You need to log in before you can comment on or make changes to this bug.
Description cheesear 2012-01-28 03:25:27 UTC
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
Comment 1 Andrew Pinski 2012-01-28 04:34:34 UTC
Confirmed.
Comment 2 TbR 2012-08-08 08:57:49 UTC
Created attachment 27958 [details]
Code example to trigger the supposed bug.

only the "bug" function triggers the bug, while "nobug1" and "nobug2" don't.
Comment 3 TbR 2012-08-08 09:01:39 UTC
Sorry, to make two comments. The bug is indeed not only showing a warning, but the variable is uninitialized when executing the code.
Comment 4 Jiří Paleček 2012-08-17 15:35:22 UTC
Could it be a duplicate of <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53488">53488</a>?
Comment 5 Jiří Paleček 2012-08-19 14:04:34 UTC
*** Bug 53488 has been marked as a duplicate of this bug. ***
Comment 6 Jiří Paleček 2012-08-19 14:13:44 UTC
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.
Comment 7 Paolo Carlini 2012-08-19 14:38:36 UTC
Do you have a copyright assignment on file?
Comment 8 Jiří Paleček 2012-08-19 15:38:33 UTC
(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?)
Comment 9 Paolo Carlini 2012-08-20 09:07:26 UTC
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.
Comment 10 Matt Godbolt 2013-01-29 13:31:01 UTC
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
}
Comment 11 Paolo Carlini 2013-02-14 23:53:43 UTC
Seems indeed a regression.
Comment 12 Jason Merrill 2013-02-15 16:17:31 UTC
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
Comment 13 Jason Merrill 2013-02-15 18:32:25 UTC
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
Comment 14 Jason Merrill 2013-02-15 18:39:10 UTC
Fixed for 4.7.3.