public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda
@ 2011-12-10  8:32 ulidtko at gmail dot com
  2011-12-22  1:39 ` [Bug c++/51494] " paolo.carlini at oracle dot com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: ulidtko at gmail dot com @ 2011-12-10  8:32 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

             Bug #: 51494
           Summary: Legal program rejection - capturing "this" when using
                    static method inside lambda
    Classification: Unclassified
           Product: gcc
           Version: 4.6.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: ulidtko@gmail.com


Consider the following source code:

struct A
{
    static void f() {}

    void foo()
    {
        [] () { f(); };
    }
};


GCC 4.6.1 produces the following error message when compiling it:
test.cpp: In lambda function:
test.cpp:7:27: error: ‘this’ was not captured for this lambda function

However, the member function f is static and it's call doesn't use 'this';
therefore the lambda need not to capture 'this'. The code shall compile.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
@ 2011-12-22  1:39 ` paolo.carlini at oracle dot com
  2012-05-03 21:53 ` luto at mit dot edu
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-12-22  1:39 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2011-12-22
                 CC|ulidtko at gmail dot com    |
         AssignedTo|unassigned at gcc dot       |paolo.carlini at oracle dot
                   |gnu.org                     |com
     Ever Confirmed|0                           |1

--- Comment #1 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-12-22 01:38:46 UTC ---
On it.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
  2011-12-22  1:39 ` [Bug c++/51494] " paolo.carlini at oracle dot com
@ 2012-05-03 21:53 ` luto at mit dot edu
  2012-05-03 23:05 ` paolo.carlini at oracle dot com
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: luto at mit dot edu @ 2012-05-03 21:53 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

Andy Lutomirski <luto at mit dot edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |luto at mit dot edu

--- Comment #2 from Andy Lutomirski <luto at mit dot edu> 2012-05-03 21:53:18 UTC ---
This seems to have gotten worse in gcc 4.7.  This code is now rejected as well:

This code:

struct A
{
  static void func1() {}
};

template<typename T>
struct B : public A
{
  void func2() const
  {
    func3([&]{ func1(); });
  }

  template<typename Func>
  void func3(Func f) const
  {
  }
};

template class B<int>;

The error is:

template_bug.cc: In instantiation of ‘B<T>::func2() const [with T =
int]::<lambda()>’:
template_bug.cc:11:22:   required from ‘struct B<T>::func2() const [with T =
int]::<lambda()>’
template_bug.cc:11:5:   required from ‘void B<T>::func2() const [with T = int]’
template_bug.cc:20:16:   required from here
template_bug.cc:11:16: error: redeclaration of ‘const B<int>* const
B<T>::func2() const [with T = int]::<lambda()>::__this’
template_bug.cc:11:22: note: previous declaration ‘const B<int>* const
B<T>::func2() const [with T = int]::<lambda()>::__this’
template_bug.cc:11:16: error: redeclaration of ‘const B<int>* const this’
template_bug.cc:11:22: error: ‘const B<int>* const this’ previously declared
here
template_bug.cc:15:8: error: ‘void B<T>::func3(Func) const [with Func =
B<T>::func2() const [with T = int]::<lambda()>; T = int]’, declared using local
type ‘B<T>::func2() const [with T = int]::<lambda()>’, is used but never
defined [-fpermissive]

Your patch fixes this test case, too.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
  2011-12-22  1:39 ` [Bug c++/51494] " paolo.carlini at oracle dot com
  2012-05-03 21:53 ` luto at mit dot edu
@ 2012-05-03 23:05 ` paolo.carlini at oracle dot com
  2012-06-04  8:22 ` gcc-bugx at mailinator dot com
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-05-03 23:05 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

--- Comment #3 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-05-03 23:04:35 UTC ---
A slightly improved patch is available here:

  http://gcc.gnu.org/ml/gcc-patches/2012-02/msg00429.html

but see the comments. I'm not sure I will be able soon enough to address the
non-trivial comments and post a new version.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
                   ` (2 preceding siblings ...)
  2012-05-03 23:05 ` paolo.carlini at oracle dot com
@ 2012-06-04  8:22 ` gcc-bugx at mailinator dot com
  2012-06-04 19:35 ` jason at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: gcc-bugx at mailinator dot com @ 2012-06-04  8:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

gcc-bugx at mailinator dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gcc-bugx at mailinator dot
                   |                            |com

--- Comment #4 from gcc-bugx at mailinator dot com 2012-06-04 08:21:54 UTC ---
Probably related error: this capture ignored on explicit instantiation.

--------------------8<---------------
struct A
{
    static void f() {}

    template <typename T>
    void foo()
    {
        [this] () { f(); };
    }
};

template void A::foo<int>();
-------------------->8---------------


Compiler version & output:

$ g++ -std=c++11 -c -v y.cc
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-unknown-linux-gnu
Configured with: ./configure --enable-languages=c,c++ --prefix=/home/x
Thread model: posix
gcc version 4.7.0 (GCC) 
COLLECT_GCC_OPTIONS='-std=c++11' '-c' '-v' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
 /home/x/libexec/gcc/x86_64-unknown-linux-gnu/4.7.0/cc1plus -quiet -v
-D_GNU_SOURCE y.cc -quiet -dumpbase y.cc -mtune=generic -march=x86-64 -auxbase
y -std=c++11 -version -o /tmp/ccdFvfri.s
GNU C++ (GCC) version 4.7.0 (x86_64-unknown-linux-gnu)
        compiled by GNU C version 4.7.0, GMP version 4.3.2, MPFR version
3.0.0-p8, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory
"/home/x/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../x86_64-unknown-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/x/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0

/home/x/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/x86_64-unknown-linux-gnu

/home/x/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/backward
 /home/x/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/include
 /usr/local/include
 /home/x/include
 /home/x/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/include-fixed
 /usr/include
End of search list.
GNU C++ (GCC) version 4.7.0 (x86_64-unknown-linux-gnu)
        compiled by GNU C version 4.7.0, GMP version 4.3.2, MPFR version
3.0.0-p8, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: ...
y.cc: In instantiation of ‘A::foo() [with T = int]::<lambda()>’:
y.cc:8:4:   required from ‘struct A::foo() [with T = int]::<lambda()>’
y.cc:8:3:   required from ‘void A::foo() [with T = int]’
y.cc:11:27:   required from here
y.cc:8:17: error: ‘this’ was not captured for this lambda function


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
                   ` (3 preceding siblings ...)
  2012-06-04  8:22 ` gcc-bugx at mailinator dot com
@ 2012-06-04 19:35 ` jason at gcc dot gnu.org
  2012-08-16  9:54 ` paolo.carlini at oracle dot com
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2012-06-04 19:35 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

--- Comment #5 from Jason Merrill <jason at gcc dot gnu.org> 2012-06-04 19:35:40 UTC ---
The testcases in comments 2 and 4 are PR 53137, which is fixed for 4.7.1.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
                   ` (4 preceding siblings ...)
  2012-06-04 19:35 ` jason at gcc dot gnu.org
@ 2012-08-16  9:54 ` paolo.carlini at oracle dot com
  2012-12-07 15:18 ` kpx1894 at gmail dot com
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-08-16  9:54 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |NEW
         AssignedTo|paolo.carlini at oracle dot |unassigned at gcc dot
                   |com                         |gnu.org

--- Comment #6 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-08-16 09:53:36 UTC ---
Not actively working on this.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
                   ` (5 preceding siblings ...)
  2012-08-16  9:54 ` paolo.carlini at oracle dot com
@ 2012-12-07 15:18 ` kpx1894 at gmail dot com
  2013-03-04 18:16 ` jason at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: kpx1894 at gmail dot com @ 2012-12-07 15:18 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

kpx1894 <kpx1894 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kpx1894 at gmail dot com

--- Comment #7 from kpx1894 <kpx1894 at gmail dot com> 2012-12-07 15:18:03 UTC ---
This bug is still present in GCC 4.7.2. There are cases where capturing "this"
(as a workaround) is not possible:
----------
#include <iostream>

struct Test
{
    void (*get())()
    {
        return [](){ Test::sf(); };
    }

    static void sf()
    {
        std::cout << "Test::sf" << std::endl;
    }

};

int main()
{
    Test test;

    auto f = test.get();
    (*f)();
}
----------


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
                   ` (6 preceding siblings ...)
  2012-12-07 15:18 ` kpx1894 at gmail dot com
@ 2013-03-04 18:16 ` jason at gcc dot gnu.org
  2013-03-05 17:47 ` jason at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2013-03-04 18:16 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |vladimir.bayda at gmail dot
                   |                            |com

--- Comment #8 from Jason Merrill <jason at gcc dot gnu.org> 2013-03-04 18:15:08 UTC ---
*** Bug 55828 has been marked as a duplicate of this bug. ***


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
                   ` (7 preceding siblings ...)
  2013-03-04 18:16 ` jason at gcc dot gnu.org
@ 2013-03-05 17:47 ` jason at gcc dot gnu.org
  2013-03-08 16:04 ` jason at gcc dot gnu.org
  2013-03-08 16:12 ` jason at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2013-03-05 17:47 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|unassigned at gcc dot       |jason at gcc dot gnu.org
                   |gnu.org                     |


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
                   ` (8 preceding siblings ...)
  2013-03-05 17:47 ` jason at gcc dot gnu.org
@ 2013-03-08 16:04 ` jason at gcc dot gnu.org
  2013-03-08 16:12 ` jason at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2013-03-08 16:04 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

--- Comment #9 from Jason Merrill <jason at gcc dot gnu.org> 2013-03-08 16:03:57 UTC ---
Author: jason
Date: Fri Mar  8 16:03:48 2013
New Revision: 196549

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196549
Log:
    PR c++/51494
    PR c++/51884
    PR c++/56222
    * tree.c (maybe_dummy_object): Don't capture 'this'.
    * semantics.c (maybe_resolve_dummy): New.
    (finish_non_static_data_member): Use it.
    (finish_qualified_id_expr): Don't test is_dummy_object.
    * cp-tree.h: Declare maybe_resolve_dummy.
    * call.c (build_new_method_call_1): Use it.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this11.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/semantics.c
    trunk/gcc/cp/tree.c


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/51494] Legal program rejection - capturing "this" when using static method inside lambda
  2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
                   ` (9 preceding siblings ...)
  2013-03-08 16:04 ` jason at gcc dot gnu.org
@ 2013-03-08 16:12 ` jason at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2013-03-08 16:12 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.8.0

--- Comment #10 from Jason Merrill <jason at gcc dot gnu.org> 2013-03-08 16:10:48 UTC ---
Fixed for 4.8.


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2013-03-08 16:12 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-10  8:32 [Bug c++/51494] New: Legal program rejection - capturing "this" when using static method inside lambda ulidtko at gmail dot com
2011-12-22  1:39 ` [Bug c++/51494] " paolo.carlini at oracle dot com
2012-05-03 21:53 ` luto at mit dot edu
2012-05-03 23:05 ` paolo.carlini at oracle dot com
2012-06-04  8:22 ` gcc-bugx at mailinator dot com
2012-06-04 19:35 ` jason at gcc dot gnu.org
2012-08-16  9:54 ` paolo.carlini at oracle dot com
2012-12-07 15:18 ` kpx1894 at gmail dot com
2013-03-04 18:16 ` jason at gcc dot gnu.org
2013-03-05 17:47 ` jason at gcc dot gnu.org
2013-03-08 16:04 ` jason at gcc dot gnu.org
2013-03-08 16:12 ` jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).