https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78840
Bug ID: 78840 Summary: ICE with const variables in templates implicitly captured by nested lambda expressions Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: myoga.murase at gmail dot com Target Milestone: --- The following valid C++ code causes an internal compiler error in recent versions of GCC from 5.1.0 to 7.0.0 20161216. Earlier versions from 4.5.4 to 4.9.4, and clang successfully compiles the code. http://melpon.org/wandbox/permlink/hJM9b4zWQzTJeBLp int gvar; template<typename F> void tfunc2(int, F) {} template<int I> void tfunc() { // ICE is caused when, in function templates, const int a = gvar; // a const variable initialized by a non-const value tfunc2(a, [&]() { tfunc2(a, [&]() { (void) a; // is used in nested lambda expressions. }); }); } int main() { tfunc<1>(); return 0; } This is the diagnostic messages from GCC 7.0.0 20161216: prog.cc: In instantiation of 'tfunc()::<lambda()>::<lambda()> [with int I = 1]': prog.cc:9:17: required from 'struct tfunc()::<lambda()> [with int I = 1]::<lambda()>' prog.cc:9:11: required from 'tfunc()::<lambda()> [with int I = 1]' prog.cc:8:15: required from 'struct tfunc() [with int I = 1]::<lambda()>' prog.cc:8:9: required from 'void tfunc() [with int I = 1]' prog.cc:16:12: required from here prog.cc:10:7: internal compiler error: in tsubst_copy, at cp/pt.c:14294 (void) a; ^~~~~~~~ 0x61e089 tsubst_copy /home/heads/gcc/gcc-source/gcc/cp/pt.c:14292 0x6254b3 tsubst_copy /home/heads/gcc/gcc-source/gcc/cp/pt.c:14119 0x6254b3 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:17397 0x6271e8 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:17159 0x6269bc tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:16421 0x61aeb7 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:16146 0x619925 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:15414 0x61ace3 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:15624 0x61ace3 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:15624 0x619372 instantiate_decl(tree_node*, int, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:22560 0x62ce2d instantiate_class_template_1 /home/heads/gcc/gcc-source/gcc/cp/pt.c:10449 0x62ce2d instantiate_class_template(tree_node*) /home/heads/gcc/gcc-source/gcc/cp/pt.c:10519 0x68f5e5 complete_type(tree_node*) /home/heads/gcc/gcc-source/gcc/cp/typeck.c:133 0x624e39 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:17522 0x6266de tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:16836 0x61aeb7 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:16146 0x619925 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:15414 0x61aa95 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:15400 0x61ace3 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:15624 0x61ace3 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:15624 Please submit a full bug report, with preprocessed source if appropriate. Please include the complete backtrace with any bug report. See <http://gcc.gnu.org/bugs.html> for instructions.