https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97752
Bug ID: 97752 Summary: incorrect address to inherited object in constexpr context Product: gcc Version: 10.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: steven.vandersch...@nextlevel-electronics.com Target Milestone: --- Created attachment 49519 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49519&action=edit C++ code to trigger the bug When working on a project I discovered a function where gcc, when in a constexpr context, seems to incorrectly compute the value of a pointer. Attached is a reduced example that triggers the bug. I defined a class Derived that inherits from two classes Base1 and Base2. A member function in Base1 will downcast its this pointer to Derived*, which is than casted to Base2*. When dereferencing the Base2* pointer in a constexpr context gcc reports: modification of ‘*(((Derived<Base1, Base2>*)(& derived.Derived<Base1, Base2>::<anonymous>)) + 18446744073709551612)’ is not a constant expression. The code attached to this bug report has a some template magic around the described behaviour. When I tried to remove the template magic I failed to get this bug to be triggered. Note this only fails when explicitly executing in a constexpr context. When the constexpr keyword on line 178 is removed the resulting binary seems correct: 0000000000000000 <test()>: 0: b8 4a 04 00 00 mov $0x44a,%eax 5: c3 retq Also the code seems to compile fine in a constexpr context using clang: https://cpp.godbolt.org/z/n3c3ea gcc version: 10.2.1 20201016 System: Fedora 33 x86_64 Command: g++ -v -std=c++17 -O3 -Wextra -pedantic test.cpp Response when running g++: ------------------------------------------------------ Using built-in specs. COLLECT_GCC=/usr/bin/g++ 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,d,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-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 Supported LTO compression algorithms: zlib zstd gcc version 10.2.1 20201016 (Red Hat 10.2.1-6) (GCC) COLLECT_GCC_OPTIONS='-v' '-std=c++17' '-O3' '-Wextra' '-Wpedantic' '-c' '-o' 'test.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/10/cc1plus -quiet -v -D_GNU_SOURCE ./test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase-strip test.o -O3 -Wextra -Wpedantic -std=c++17 -version -o /tmp/ccIaE47Z.s GNU C++17 (GCC) version 10.2.1 20201016 (Red Hat 10.2.1-6) (x86_64-redhat-linux) compiled by GNU C version 10.2.1 20201016 (Red Hat 10.2.1-6), GMP version 6.2.0, MPFR version 4.1.0, MPC version 1.1.0, isl version isl-0.16.1-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/10/include-fixed" ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/10/../../../../x86_64-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 /usr/lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux /usr/lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward /usr/lib/gcc/x86_64-redhat-linux/10/include /usr/local/include /usr/include End of search list. GNU C++17 (GCC) version 10.2.1 20201016 (Red Hat 10.2.1-6) (x86_64-redhat-linux) compiled by GNU C version 10.2.1 20201016 (Red Hat 10.2.1-6), GMP version 6.2.0, MPFR version 4.1.0, MPC version 1.1.0, isl version isl-0.16.1-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: bc4d6419b2bc73981424827b3550eaef ./test.cpp: In function ‘int test()’: ./test.cpp:178:27: in ‘constexpr’ expansion of ‘f.test()::<lambda()>()’ ./test.cpp:175:11: in ‘constexpr’ expansion of ‘(& obj)->Base1<Derived<Base1, Base2> >::test()’ ./test.cpp:26:16: in ‘constexpr’ expansion of ‘Base1<Derived<Base1, Base2> >::callCallbacks<Base2<NS>::test<int>::<lambda(auto:2)> >(callbacks, 987)’ ./test.cpp:42:16: in ‘constexpr’ expansion of ‘Base1<Derived<Base1, Base2> >::callCallbacks<Base2<NS>::test<int>::<lambda(auto:2)>, 0>(callbacks, payload, (std::make_index_sequence<1>{}, std::make_index_sequence<1>()))’ ./test.cpp:37:26: in ‘constexpr’ expansion of ‘(& std::get<0, Base2<NS>::test<int>::<lambda(auto:2)> >(callbacks))->Base2<NS>::test<int>::<lambda(auto:2)>(payload)’ ./test.cpp:63:4: in ‘constexpr’ expansion of ‘(& other)->Base1<Derived<Base1, Base2> >::set((payload + 111))’ ./test.cpp:178:28: error: modification of ‘*(((Derived<Base1, Base2>*)(& derived.Derived<Base1, Base2>::<anonymous>)) + 18446744073709551612)’ is not a constant expression 178 | constexpr auto result = f(); | ^ ------------------------------------------------------