Bug 52739 - [c++0x] Segfault because of nested lambda member capture inside member template
Summary: [c++0x] Segfault because of nested lambda member capture inside member template
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2012-03-27 14:49 UTC by Bruno De Fraine
Modified: 2012-03-27 15:54 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.7.0
Known to fail:
Last reconfirmed: 2012-03-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bruno De Fraine 2012-03-27 14:49:13 UTC
In the following specific combination of a class template, a member template, and two nested lambdas, where the outer lambda captures `this' and the inner lambda captures a member of `this', I can trigger a gcc segmentation fault.

test.cpp contains:

template<int n>
class Test {
    int i;
public:
    template<typename Func>
    void foo(const Func& f);
};

template<int n> template<typename Func>
void Test<n>::foo(const Func& f) {
    auto lam1 = [this,&f]() {
        auto lam2 = [&i,&f]() {
            f(i);
        };
    };
}

void bar(Test<1>& t) {
    t.foo([](int i) { });
}

The compilation:

$ g++ -Wall -O2 -std=c++0x -c test.cpp 
test.cpp: In lambda function:
test.cpp:12:17: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.

The segmentation fault is always reproducible with gcc 4.6.2 on two platforms:

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/gcc-4.6.2/libexec/gcc/x86_64-unknown-linux-gnu/4.6.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/opt/gcc-4.6.2 --enable-languages=c,c++,fortran --disable-multilib
Thread model: posix
gcc version 4.6.2 (GCC) 

$ g++-fsf-4.6 -v
Using built-in specs.
COLLECT_GCC=g++-fsf-4.6
COLLECT_LTO_WRAPPER=/sw/lib/gcc4.6/libexec/gcc/x86_64-apple-darwin11.2.0/4.6.2/lto-wrapper
Target: x86_64-apple-darwin11.2.0
Configured with: ../gcc-4.6.2/configure --prefix=/sw --prefix=/sw/lib/gcc4.6 --mandir=/sw/share/man --infodir=/sw/lib/gcc4.6/info --enable-languages=c,c++,fortran,lto,objc,obj-c++,java --with-gmp=/sw --with-libiconv-prefix=/sw --with-ppl=/sw --with-cloog=/sw --with-mpc=/sw --with-system-zlib --x-includes=/usr/X11R6/include --x-libraries=/usr/X11R6/lib --program-suffix=-fsf-4.6 --enable-cloog-backend=isl
Thread model: posix
gcc version 4.6.2 (GCC) 

I haven't tested this yet using gcc 4.6.3 nor gcc 4.7. My apologies if this has been already fixed.
Comment 1 Jonathan Wakely 2012-03-27 15:06:16 UTC
$ g++-4.6.3 -std=c++0x t.cc
t.cc: In lambda function:
t.cc:12:23: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
$
$ g++-4.7.0 -std=c++0x t.cc
t.cc: In lambda function:
t.cc:12:23: error: capture of non-variable 'Test<n>::i'
t.cc:3:9: note: 'int Test<n>::i' declared here
t.cc: In lambda function:
t.cc:13:15: error: 'this' was not captured for this lambda function
Comment 2 Paolo Carlini 2012-03-27 15:54:04 UTC
Then we can resolve it.