Bug 80587 - Incorrect type from outer scope inside lambda under some conditions
Summary: Incorrect type from outer scope inside lambda under some conditions
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: 7.2
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda, wrong-code
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2017-05-01 20:48 UTC by jasonr
Modified: 2022-03-11 00:32 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 7.2.0, 8.1.0
Known to fail: 5.5.0, 6.3.0, 6.4.0, 7.1.0
Last reconfirmed:


Attachments
Test case source file (317 bytes, text/x-csrc)
2017-05-01 20:48 UTC, jasonr
Details

Note You need to log in before you can comment on or make changes to this bug.
Description jasonr 2017-05-01 20:48:20 UTC
Created attachment 41292 [details]
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.
Comment 1 jasonr 2017-05-04 14:54:40 UTC
I neglected to include the preprocessed source file. It is too large to attach here, so I posted it at:

https://gist.github.com/otherjason/9f50d3f36207ea4b40bb805f6c710304
Comment 2 Andrew Pinski 2021-11-10 22:40:28 UTC
Fixed in GCC 7.2.0+ and GCC 8+.
I suspect the fix for PR 81045 which fixed this one.