Here build_data_member_initialization got confused by the initialization of a field of local variable f and thought it was initializing a field of S; when we then went looking for that field in S, we didn't find it, and crashed. But if the target of an initialization is a member of a VAR_DECL, it can't be part of *this, so we should ignore it here.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 227008348567729cd124bea44227771681361c40 Author: Jason Merrill <ja...@redhat.com> Date: Mon Feb 13 15:09:37 2017 -0500 PR c++/79461 - ICE with lambda in constexpr constructor * constexpr.c (build_data_member_initialization): Ignore initialization of a local variable. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index bfdde9e..004bb45 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -379,6 +379,9 @@ build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) if (TREE_CODE (member) == COMPONENT_REF) { tree aggr = TREE_OPERAND (member, 0); + if (TREE_CODE (aggr) == VAR_DECL) + /* Initializing a local variable, don't add anything. */ + return true; if (TREE_CODE (aggr) != COMPONENT_REF) /* Normal member initialization. */ member = TREE_OPERAND (member, 1); diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda15.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda15.C new file mode 100644 index 0000000..7e05481 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda15.C @@ -0,0 +1,10 @@ +// PR c++/79461 +// { dg-options -std=c++1z } + +struct S { + constexpr S(int i) { + auto f = [i]{}; + } +}; +int main() {} +