https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80587
Bug ID: 80587 Summary: Incorrect type from outer scope inside lambda under some conditions Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jas...@3db-labs.com Target Milestone: --- Created attachment 41292 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=41292&action=edit Test case source file I observed this error on an Ubuntu 17.04 system; here is the output of `g++ -v`: ----- Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 6.3.0-12ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 6.3.0 20170406 (Ubuntu 6.3.0-12ubuntu2) ----- The nature of the bug is tough to explain, so see the attached test case. I minimized it as much as possible, but I ran into it when using Boost.Hana from Boost v1.64. Based on the test case, this appears to be a compiler bug, not an issue with the library. In the test, I create a `boost::hana::tuple<>` object, then use `boost::hana::for_each()` to invoke a callable with each element of the tuple. In my test case, I provide a lambda as the callable. I've noticed that, if I try to use the enclosing tuple type from within the lambda, g++ uses the incorrect type in some cases. I would expect the program to print: ----- working example: tuple type before for_each: boost::hana::tuple<int, int> tuple type inside for_each: boost::hana::tuple<int, int> tuple type inside for_each: boost::hana::tuple<int, int> non-working example: tuple type before for_each: boost::hana::tuple<int, int> tuple type inside for_each: boost::hana::tuple<int, int> tuple type inside for_each: boost::hana::tuple<int, int> ----- However, when I compile and run the program using g++ as follows: g++ tuple_test.cc -o tuple_test -std=c++14 I get the following output: ----- working example: tuple type before for_each: boost::hana::tuple<int, int> tuple type inside for_each: boost::hana::tuple<int, int> tuple type inside for_each: boost::hana::tuple<int, int> non-working example: tuple type before for_each: boost::hana::tuple<int, int> tuple type inside for_each: int tuple type inside for_each: int ----- I see this error on every version of gcc that I've tried: 6.1, 6.2, 6.3, 7.0, and 8.0 trunk (https://wandbox.org/permlink/4zKtIIZ9k7sI7Ckv). All versions of clang that I've tried that are supported by Hana seem to work fine. I've minimized the test case as much as I can, but I haven't been able to identify a workaround.