https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87685

            Bug ID: 87685
           Summary: [Regression] Calling a static method from inside a
                    generic lambda requires to capture 'this'
           Product: gcc
           Version: 8.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: abun.lp at gmail dot com
  Target Milestone: ---

Created attachment 44877
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44877&action=edit
preprocessed file

Hello,

We've stumbled upon what seems to be a regression in GCC 8 compared to 7.3. It
involves calling a static class method from inside a generic lambda. If the
static method is overloaded by a another non-static method, GCC complains that
'this' is not captured.

It looks like a slightly more complex version of bug 51494.

Here's an example that fails to compile on GCC 8.1.1 but compiles on 7.3 and
previous versions:

struct A
{
  template <typename T> static void f(T) {}
  void f() {}

  void foo()
  {
    [] (auto&& v) { A::f(v); }; // OK if parameter type is specified
  }
};

Compiler output:

$ gcc -std=c++14 -v -save-temps -Wall -Wextra test.cpp
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array --with-isl --enable-libmpx
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --enable-cet --with-tune=generic
--with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.1.1 20180712 (Red Hat 8.1.1-5) (GCC) 
COLLECT_GCC_OPTIONS='-std=c++14' '-v' '-save-temps' '-Wall' '-Wextra'
'-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/8/cc1plus -E -quiet -v -D_GNU_SOURCE
test.cpp -mtune=generic -march=x86-64 -std=c++14 -Wall -Wextra -fpch-preprocess
-o test.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/8/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8

/usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward
 /usr/lib/gcc/x86_64-redhat-linux/8/include
 /usr/local/include
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-std=c++14' '-v' '-save-temps' '-Wall' '-Wextra'
'-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/8/cc1plus -fpreprocessed test.ii -quiet
-dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -Wall -Wextra
-std=c++14 -version -o test.s
GNU C++14 (GCC) version 8.1.1 20180712 (Red Hat 8.1.1-5) (x86_64-redhat-linux)
        compiled by GNU C version 8.1.1 20180712 (Red Hat 8.1.1-5), GMP version
6.1.2, MPFR version 3.1.6-p2, MPC version 1.0.2, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++14 (GCC) version 8.1.1 20180712 (Red Hat 8.1.1-5) (x86_64-redhat-linux)
        compiled by GNU C version 8.1.1 20180712 (Red Hat 8.1.1-5), GMP version
6.1.2, MPFR version 3.1.6-p2, MPC version 1.0.2, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 29634e2139b669a6563cdc7cebcf60bd
test.cpp: In lambda function:
test.cpp:8:27: error: ‘this’ was not captured for this lambda function
     [] (auto&& v) { A::f(v); }; // OK if parameter type is specified



Remarks: 
- It is important that the static method is overloaded by another non-static
method of the class.
- Specifying the type of v (i.e. making the lambda non-generic) solves the
problem.
- If A:: is omitted when calling f, then GCC 7 also fails to compile with the
same error, but GCC 6 works just fine according to godbolt.org.

Thanks,
Guillaume

Reply via email to