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

            Bug ID: 121777
           Summary: Possible UB in g++.
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ba...@hudson-trading.com
  Target Milestone: ---

When building this code:

class A {
    template <class T> static auto f() {
        return [](auto&& x) -> T { return (T) x; };
    }

    class B {};

    static void test() {
        f<const B&>();
    }

};

in Godbolt, using gcc-trunk, I get an ICE:

Using built-in specs.
COLLECT_GCC=/opt/compiler-explorer/gcc-snapshot/bin/g++
Target: x86_64-linux-gnu
Configured with: ../gcc-trunk-20250903/configure
--prefix=/opt/compiler-explorer/gcc-build/staging
--enable-libstdcxx-backtrace=yes --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu --disable-bootstrap
--enable-multiarch --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --enable-clocale=gnu
--enable-languages=c,c++,fortran,ada,objc,obj-c++,go,d,rust,m2,cobol
--enable-ld=yes --enable-gold=yes --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-linker-build-id --enable-lto
--enable-plugins --enable-threads=posix
--with-pkgversion=Compiler-Explorer-Build-gcc-23d5056310bda8da6a277f42f57226301809ca89-binutils-2.44
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 16.0.0 20250903 (experimental)
(Compiler-Explorer-Build-gcc-23d5056310bda8da6a277f42f57226301809ca89-binutils-2.44)
 
COLLECT_GCC_OPTIONS='-fdiagnostics-color=always' '-g' '-o' '/app/output.s'
'-masm=intel' '-fno-verbose-asm' '-S' '-v' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-dumpdir' '/app/'

/opt/compiler-explorer/gcc-trunk-20250903/bin/../libexec/gcc/x86_64-linux-gnu/16.0.0/cc1plus
-quiet -v -imultiarch x86_64-linux-gnu -iprefix
/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/x86_64-linux-gnu/16.0.0/
-D_GNU_SOURCE <source> -quiet -dumpdir /app/ -dumpbase output.cpp -dumpbase-ext
.cpp -masm=intel -mtune=generic -march=x86-64 -g -version
-fdiagnostics-color=always -fno-verbose-asm -o /app/output.s
GNU C++17
(Compiler-Explorer-Build-gcc-23d5056310bda8da6a277f42f57226301809ca89-binutils-2.44)
version 16.0.0 20250903 (experimental) (x86_64-linux-gnu)
        compiled by GNU C version 11.4.0, GMP version 6.3.0, MPFR version
4.2.2, MPC version 1.3.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
ignoring nonexistent directory
"/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/x86_64-linux-gnu/16.0.0/../../../../x86_64-linux-gnu/include"
ignoring duplicate directory
"/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0"
ignoring duplicate directory
"/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/x86_64-linux-gnu"
ignoring duplicate directory
"/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/backward"
ignoring duplicate directory
"/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/16.0.0/include"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring duplicate directory
"/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/16.0.0/include-fixed/x86_64-linux-gnu"
ignoring duplicate directory
"/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/16.0.0/include-fixed"
ignoring nonexistent directory
"/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/16.0.0/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:

/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0

/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/x86_64-linux-gnu

/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/backward

/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/x86_64-linux-gnu/16.0.0/include

/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/x86_64-linux-gnu/16.0.0/include-fixed/x86_64-linux-gnu

/opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/x86_64-linux-gnu/16.0.0/include-fixed
 /usr/local/include
 /opt/compiler-explorer/gcc-trunk-20250903/bin/../lib/gcc/../../include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
Compiler executable checksum: e4a3cea63434782b38ea6cee2e619ca6
<source>: In instantiation of 'static auto A::f() [with T = const B&]':
required from here
<source>:9:20:   
    9 |         f<const B&>();
      |         ~~~~~~~~~~~^~
<source>:3:47: internal compiler error: in convert_like_internal, at
cp/call.cc:8697
    3 |         return [](auto&& x) -> T { return (T) x; };
      |                                               ^
0x28b0218 diagnostics::context::diagnostic_impl(rich_location*,
diagnostics::metadata const*, diagnostics::option_id, char const*,
__va_list_tag (*) [1], diagnostics::kind)
        ???:0
0x28a51eb internal_error(char const*, ...)
        ???:0
0xafdb54 fancy_abort(char const*, int, char const*)
        ???:0
0xb35a8e initialize_reference(tree_node*, tree_node*, int, int)
        ???:0
0xe13a00 convert_for_initialization(tree_node*, tree_node*, tree_node*, int,
impl_conv_rhs, tree_node*, int, int)
        ???:0
0xe152ad check_return_expr(tree_node*, bool*, bool*)
        ???:0
0xdb2008 finish_return_stmt(tree_node*)
        ???:0
0xd5b1de tsubst_lambda_expr(tree_node*, tree_node*, int, tree_node*)
        ???:0
0xd927fd instantiate_decl(tree_node*, bool, bool)
        ???:0
0xc0e703 maybe_instantiate_decl(tree_node*)
        ???:0
0xc104d7 mark_used(tree_node*, int)
        ???:0
0xb1f273 build_new_method_call(tree_node*, tree_node*, vec<tree_node*, va_gc,
vl_embed>**, tree_node*, int, tree_node**, int)
        ???:0
0xdb704c finish_call_expr(tree_node*, vec<tree_node*, va_gc, vl_embed>**, bool,
bool, int)
        ???:0
0xd313b3 c_parse_file()
        ???:0
0xea0ec9 c_common_parse_file()
        ???:0
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Compiler returned: 1

I also get a similar ICE with gcc-15.2 I've built from the source:

Using built-in specs.
COLLECT_GCC=/usr/bin/g++-15
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian
15.2.0-0+hrt1+hrtdeb11' --with-bugurl=file:///usr/share/doc/gcc-15/README.Bugs
--enable-languages=c,c++,go,fortran,objc,obj-c++,m2,rust,cobol,algol68
--prefix=/usr --with-gcc-major-version-only --program-suffix=-15
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --enable-objc-gc=auto
--enable-multiarch --disable-werror --disable-cet --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic --enable-checking=yes,extra,rtl --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-serialization=16
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 15.2.0 (Debian 15.2.0-0+hrt1+hrtdeb11)
COLLECT_GCC_OPTIONS='-v' '-c' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-linux-gnu/15/cc1plus -quiet -v -imultiarch
x86_64-linux-gnu -D_GNU_SOURCE /home/basil/ice_test.cc -quiet -dumpbase
ice_test.cc -dumpbase-ext .cc -mtune=generic -march=x86-64 -version
-fasynchronous-unwind-tables -o /usr/tmp/ccArctLi.s
GNU C++17 (Debian 15.2.0-0+hrt1+hrtdeb11) version 15.2.0 (x86_64-linux-gnu)
        compiled by GNU C version 15.2.0, GMP version 6.2.1, MPFR version
4.1.0, MPC version 1.2.0, isl version isl-0.23-GMP

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/15"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/15/include-fixed/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/15/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/15
 /usr/include/x86_64-linux-gnu/c++/15
 /usr/include/c++/15/backward
 /usr/lib/gcc/x86_64-linux-gnu/15/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
Compiler executable checksum: 010c233156329c3a73387be20e36d804
/home/basil/ice_test.cc: In instantiation of ‘static auto A::f() [with T =
const B&]’:
/home/basil/ice_test.cc:9:20:   required from here
    9 |         f<const B&>();
      |         ~~~~~~~~~~~^~
/home/basil/ice_test.cc:3:47: internal compiler error: in
convert_like_internal, at cp/call.cc:8652
    3 |         return [](auto&& x) -> T { return (T) x; };
      |                                               ^
0x1ed105f internal_error(char const*, ...)
        ../../src/gcc/diagnostic-global-context.cc:517
0x1ed11d1 fancy_abort(char const*, int, char const*)
        ../../src/gcc/diagnostic.cc:1749
0x8315ee convert_like_internal
        ../../src/gcc/cp/call.cc:8652
0x26aa415 convert_like
        ../../src/gcc/cp/call.cc:9429
0x22f8397 initialize_reference(tree_node*, tree_node*, int, int)
        ../../src/gcc/cp/call.cc:14423
0x208bb02 convert_for_initialization(tree_node*, tree_node*, tree_node*, int,
impl_conv_rhs, tree_node*, int, int)
        ../../src/gcc/cp/typeck.cc:10761
0x208e8c9 check_return_expr(tree_node*, bool*, bool*)
        ../../src/gcc/cp/typeck.cc:11598
0x208b663 finish_return_stmt(tree_node*)
        ../../src/gcc/cp/semantics.cc:1585
0x2426def tsubst_stmt
        ../../src/gcc/cp/pt.cc:18912
0x2426d85 tsubst_stmt
        ../../src/gcc/cp/pt.cc:19267
0x27e7123 tsubst_lambda_expr(tree_node*, tree_node*, int, tree_node*)
        ../../src/gcc/cp/pt.cc:20416
0x22839cd tsubst_expr(tree_node*, tree_node*, int, tree_node*)
        ../../src/gcc/cp/pt.cc:22357
0x2426d2f tsubst_stmt
        ../../src/gcc/cp/pt.cc:20049
0x2426de7 tsubst_stmt
        ../../src/gcc/cp/pt.cc:18912
0x2426dcb tsubst_stmt
        ../../src/gcc/cp/pt.cc:18902
0x2426d85 tsubst_stmt
        ../../src/gcc/cp/pt.cc:19267
0x238a5b7 instantiate_body
        ../../src/gcc/cp/pt.cc:27767
0x2387b14 instantiate_decl(tree_node*, bool, bool)
        ../../src/gcc/cp/pt.cc:28056
0x2386845 maybe_instantiate_decl(tree_node*)
        ../../src/gcc/cp/decl2.cc:6105
0x2386845 maybe_instantiate_decl(tree_node*)
        ../../src/gcc/cp/decl2.cc:6093
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <file:///usr/share/doc/gcc-15/README.Bugs> for instructions.

However, the gcc-15.2 ICE seemingly depends on the environment, where gcc-15
had been built. I have a build from the same source that does not express the
ICE. In one case, gcc-15 built with gcc-10 does not ICE but built with gcc-14
on the same machine ICEs, on another machine, both gcc-10 and gcc-14 built
gcc-15 ICE so does not seem to depend on the host compiler but rather the whole
environment.

Reply via email to