Bug 61362 - g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2 does not compile lambda with template
Summary: g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2 does not compile lambda with template
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.2
: P3 normal
Target Milestone: 6.0
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2014-05-29 22:00 UTC by dgront
Modified: 2022-03-11 00:32 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-06-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description dgront 2014-05-29 22:00:31 UTC
Minimal example of the problem:

#include <functional>

struct Node {
    size_t length;
};

template<typename N>
class C {
public:
  size_t longest = 0;
  std::function<void(const N )> f = [this](N node) {
    if(node->length > this->longest) this->longest = node->length;
  };
};

int main() {

  Node n;
  n.length = 5;
  C<Node*> c;
  c.f(&n);
}
Comment 1 Daniel Krügler 2014-05-30 15:07:15 UTC
The code produces an ICE on gcc 4.8.2, 4.9.0, and recent 4.10.0 trunk:

"prog.cc: In instantiation of 'struct C<Node*>::__lambda0': prog.cc:8:7: required from here prog.cc:11:38: internal compiler error: in tsubst_copy, at cp/pt.c:12116 std::function<void(const N )> f = [this](N node) { ^ 0x51c14e tsubst_copy ../../gcc-4.8.2/gcc/cp/pt.c:12116 0x51c743 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool) ../../gcc-4.8.2/gcc/cp/pt.c:14455 0x51c7e2 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool) ../../gcc-4.8.2/gcc/cp/pt.c:14236 0x52b94f instantiate_class_template_1 ../../gcc-4.8.2/gcc/cp/pt.c:9030 0x52b94f instantiate_class_template(tree_node*) ../../gcc-4.8.2/gcc/cp/pt.c:9088 0x57993d complete_type(tree_node*) ../../gcc-4.8.2/gcc/cp/typeck.c:131 0x51d448 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool) ../../gcc-4.8.2/gcc/cp/pt.c:14552 0x592618 perform_member_init ../../gcc-4.8.2/gcc/cp/init.c:538 0x592618 emit_mem_initializers(tree_node*) ../../gcc-4.8.2/gcc/cp/init.c:1109 0x596976 synthesize_method(tree_node*) ../../gcc-4.8.2/gcc/cp/method.c:796 0x54367a mark_used(tree_node*) ../../gcc-4.8.2/gcc/cp/decl2.c:4677 0x4f01ec build_over_call ../../gcc-4.8.2/gcc/cp/call.c:7055 0x4ed698 build_new_method_call_1 ../../gcc-4.8.2/gcc/cp/call.c:7715 0x4ed698 build_new_method_call(tree_node*, tree_node*, vec<tree_node*, va_gc, vl_embed>**, tree_node*, int, tree_node**, int) ../../gcc-4.8.2/gcc/cp/call.c:7785 0x4edf3e build_special_member_call(tree_node*, tree_node*, vec<tree_node*, va_gc, vl_embed>**, tree_node*, int, int) ../../gcc-4.8.2/gcc/cp/call.c:7352 0x58fec5 expand_default_init ../../gcc-4.8.2/gcc/cp/init.c:1679 0x58fec5 expand_aggr_init_1 ../../gcc-4.8.2/gcc/cp/init.c:1780 0x591a08 build_aggr_init(tree_node*, tree_node*, int, int) ../../gcc-4.8.2/gcc/cp/init.c:1531 0x4fcd9c build_aggr_init_full_exprs ../../gcc-4.8.2/gcc/cp/decl.c:5543 0x4fcd9c check_initializer ../../gcc-4.8.2/gcc/cp/decl.c:5678 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."
Comment 2 Daniel Krügler 2014-05-30 15:13:59 UTC
Reduced example (removing library dependencies) using compiler flags

 -Wall -Wextra -std=c++11 -pedantic 

for gcc HEAD 4.10.0 20140529 (experimental)

//---------------------
struct function {
  template<class F>
  function(F){}

  void operator()(...) {}
};

struct Node {
  unsigned length;
};

template<typename N>
class C {
public:
  unsigned longest = 0;
  function f = [this](N node) {
    if(node->length > this->longest) this->longest = node->length;
  };
};

int main() {
  Node n;
  n.length = 5;
  C<Node*> c;
  c.f(&n);
}
//---------------------

results in

"prog.cc: In instantiation of 'C<N>::<lambda(N)> [with N = Node*]': prog.cc:16:17: required from 'struct C<Node*>::<lambda(struct Node*)>' prog.cc:24:12: required from here prog.cc:16:29: internal compiler error: Segmentation fault function f = [this](N node) { ^ 0x96976f crash_signal /home/heads/gcc/gcc-source/gcc/toplev.c:337 0x56ddb5 instantiate_decl(tree_node*, int, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:19932 0x5802b1 instantiate_class_template_1 /home/heads/gcc/gcc-source/gcc/cp/pt.c:9367 0x5802b1 instantiate_class_template(tree_node*) /home/heads/gcc/gcc-source/gcc/cp/pt.c:9435 0x5d726d complete_type(tree_node*) /home/heads/gcc/gcc-source/gcc/cp/typeck.c:134 0x567644 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool) /home/heads/gcc/gcc-source/gcc/cp/pt.c:15327 0x5ec715 get_nsdmi(tree_node*, bool) /home/heads/gcc/gcc-source/gcc/cp/init.c:543 0x5f4bf4 walk_field_subobs /home/heads/gcc/gcc-source/gcc/cp/method.c:1098 0x5f53bc synthesized_method_walk /home/heads/gcc/gcc-source/gcc/cp/method.c:1424 0x5f64b5 get_defaulted_eh_spec(tree_node*) /home/heads/gcc/gcc-source/gcc/cp/method.c:1455 0x581707 maybe_instantiate_noexcept(tree_node*) /home/heads/gcc/gcc-source/gcc/cp/pt.c:19563 0x599ae7 mark_used(tree_node*, int) /home/heads/gcc/gcc-source/gcc/cp/decl2.c:4823 0x537459 build_over_call /home/heads/gcc/gcc-source/gcc/cp/call.c:7325 0x53de3a build_new_method_call_1 /home/heads/gcc/gcc-source/gcc/cp/call.c:8037 0x53de3a build_new_method_call(tree_node*, tree_node*, vec<tree_node*, va_gc, vl_embed>**, tree_node*, int, tree_node**, int) /home/heads/gcc/gcc-source/gcc/cp/call.c:8107 0x53e6e6 build_special_member_call(tree_node*, tree_node*, vec<tree_node*, va_gc, vl_embed>**, tree_node*, int, int) /home/heads/gcc/gcc-source/gcc/cp/call.c:7651 0x5ef036 expand_default_init /home/heads/gcc/gcc-source/gcc/cp/init.c:1713 0x5ef036 expand_aggr_init_1 /home/heads/gcc/gcc-source/gcc/cp/init.c:1814 0x5f096e build_aggr_init(tree_node*, tree_node*, int, int) /home/heads/gcc/gcc-source/gcc/cp/init.c:1566 0x549a2d build_aggr_init_full_exprs /home/heads/gcc/gcc-source/gcc/cp/decl.c:5642 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."
Comment 3 Thibaut LUTZ 2015-03-07 10:46:14 UTC
This triggers the same internal error on HEAD 5.0.0 20150306.


>>>>>>
template <typename>
struct S {
  int f{[this](){return 42;}()};
};

int main(){
  return S<int>{}.f; // should be 42
}
<<<<<<

prog.cc: In instantiation of 'struct S<int>::<lambda()>':
prog.cc:3:29:   required from here
prog.cc:3:10: internal compiler error: in tsubst_copy, at cp/pt.c:12872
  int f{[this](){return 42;}()};
Comment 4 Ville Voutilainen 2015-10-11 19:32:18 UTC
None of the examples ICE on the current trunk I have on Linux-X64. Can anyone
else verify whether the ICEs can still be triggered?
Comment 5 Ville Voutilainen 2015-10-11 19:41:49 UTC
Oh, some crash with -std=c++1z but not with the current default of -std=c++14.
Comment 6 Thibaut LUTZ 2015-10-12 10:53:28 UTC
My previous test cases still fails with 6.0.0 20151012 on linux64.
It passes with "-std=c++11".
Fails with "-std=c++14", "-std=c++17", "-std=c++1y".

Same error:
prog.cc: In instantiation of ‘struct S<int>::<lambda()>’:
prog.cc:3:29:   required from here
prog.cc:3:10: internal compiler error: in tsubst_copy, at cp/pt.c:13708
   int f{[this](){return 42;}()};
Comment 7 paolo@gcc.gnu.org 2017-09-13 18:19:19 UTC
Author: paolo
Date: Wed Sep 13 18:18:48 2017
New Revision: 252724

URL: https://gcc.gnu.org/viewcvs?rev=252724&root=gcc&view=rev
Log:
2017-09-13  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/61362
	* g++.dg/cpp0x/lambda/lambda-ice19.C: New.
	* g++.dg/cpp0x/lambda/lambda-ice20.C: Likewise.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-ice19.C
    trunk/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-ice20.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 8 Paolo Carlini 2017-09-13 18:21:47 UTC
ICE in Comment 2 fixed in 5.2.0, ICE in Comment3 fixed for c++14 and more recent standards in 6.1.0. I added both testcases.