https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95388
Bug ID: 95388 Summary: C++20 & 17 Lambda capture of *this by Value causes ICE "in tsubst_decl at cp/pt.c" Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: eng.amehra at gmail dot com Target Milestone: --- 1. While using lambda capture by value, cause ICE with gcc 11.0.0. I tried with both -std=c++17 & then with -std=c++20. Both give same error. Short Error: internal compiler error: in tsubst_decl, at cp/pt.c:14637 What works: 1. On this online gcc comiler, it works for gcc10.1, but fails for trunk. Code Code fails to compile on this link when compiler is set to gcc trunk - https://godbolt.org/z/PNxfQc Same code compiles on this link when compiler is set to gcc10.1 - https://godbolt.org/z/izPfY_ 2. It works fine with GCC 7.5.0 with compiler flag -std=c++17 on my machine. (See machine details below) My local machine details: Linux DESKTOP-SHA8RMO 4.4.0-18362-Microsoft #836-Microsoft x86_64 x86_64 x86_64 GNU/Linux OS: "Ubuntu 18.04.4 LTS (Bionic Beaver)". Code sample which causes error: #include <iostream> #include <vector> #include <algorithm> class FeatureLambdaCaptureThis { private: int addend; public: FeatureLambdaCaptureThis(int value) : addend{value} { } void DemoLambdaCapture_ThisByValue() { std::vector<int> values{1, 3, 5, 7, 9, 11, 13, 15}; std::for_each(values.begin(), values.end(), [=, *this](auto &value) mutable { value += addend; addend++; }); std::cout << "\n" ; for (auto &value : values) { std::cout << " " << value; } std::cout << std::endl; } }; int main(int argc, char *argv[]) { std::cout << "__cplusplus:" << __cplusplus << std::endl; FeatureLambdaCaptureThis obj{2}; obj.DemoLambdaCapture_ThisByValue(); } Detailed error log from my local machine: 51 | std::for_each(values.begin(), values.end(), [=, *this](auto value) { [build] | ^ [build] 0x69059f tsubst_decl [build] ../../gcc/cp/pt.c:14637 [build] 0xa2c8eb tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) [build] ../../gcc/cp/pt.c:17934 [build] 0xa2b96a tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) [build] ../../gcc/cp/pt.c:17832 [build] 0xa29d14 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) [build] ../../gcc/cp/pt.c:18151 [build] 0xa27c91 instantiate_decl(tree_node*, bool, bool) [build] ../../gcc/cp/pt.c:25622 [build] 0x95ce6a maybe_instantiate_decl [build] ../../gcc/cp/decl2.c:5377 [build] 0x95f520 maybe_instantiate_decl [build] ../../gcc/tree.h:3411 [build] 0x95f520 mark_used(tree_node*, int) [build] ../../gcc/cp/decl2.c:5573 [build] 0x8b3385 build_over_call [build] ../../gcc/cp/call.c:9082 [build] 0x8b8141 build_op_call_1 [build] ../../gcc/cp/call.c:4889 [build] 0x8b8141 build_op_call(tree_node*, vec<tree_node*, va_gc, vl_embed>**, int) [build] ../../gcc/cp/call.c:4918 [build] 0xa6f3ee finish_call_expr(tree_node*, vec<tree_node*, va_gc, vl_embed>**, bool, bool, int) [build] ../../gcc/cp/semantics.c:2692 [build] 0xa39bdd tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool) [build] ../../gcc/cp/pt.c:20017 [build] 0xa28439 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) [build] ../../gcc/cp/pt.c:18750 [build] 0xa2a963 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) [build] ../../gcc/cp/pt.c:17862 [build] 0xa2a3c3 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) [build] ../../gcc/cp/pt.c:18061 [build] 0xa2b96a tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) [build] ../../gcc/cp/pt.c:17832 [build] 0xa29d14 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool) [build] ../../gcc/cp/pt.c:18151 [build] 0xa27c91 instantiate_decl(tree_node*, bool, bool) [build] ../../gcc/cp/pt.c:25622 [build] 0x8dab1e instantiate_cx_fn_r [build] ../../gcc/cp/constexpr.c:6549 [build] Please submit a full bug report, [build] with preprocessed source if appropriate. [build] Please include the complete backtrace with any bug report. [build] See <https://gcc.gnu.org/bugs/> for instructions.