Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< --
Here during constant evaluation we encounter a VAR_DECL with DECL_VALUE_EXPR of the RESULT_DECL, where the latter has been adjusted for pass-by-invisible-reference. We already had the code to deal with this, we just need to use it in the non-capture case of DECL_VALUE_EXPR as well. PR c++/118053 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_constant_expression): Generalize DECL_VALUE_EXPR invisiref handling. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-lambda1.C: New test. --- gcc/cp/constexpr.cc | 19 +++++++++--------- .../g++.dg/cpp1y/constexpr-lambda1.C | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 299b1345687..59dd0668af3 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -7683,18 +7683,19 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, definition, so don't try to look at the closure. But if the captured variable is constant, try to evaluate it directly. */ r = DECL_CAPTURED_VARIABLE (t); - tree type = TREE_TYPE (t); - if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) - { - /* Adjust r to match the reference-ness of t. */ - if (TYPE_REF_P (type)) - r = build_address (r); - else - r = convert_from_reference (r); - } } else r = DECL_VALUE_EXPR (t); + + tree type = TREE_TYPE (t); + if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) + { + /* Adjust r to match the reference-ness of t. */ + if (TYPE_REF_P (type)) + r = build_address (r); + else + r = convert_from_reference (r); + } return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, overflow_p); } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C new file mode 100644 index 00000000000..68152e26b89 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C @@ -0,0 +1,20 @@ +// PR c++/118053 +// { dg-do compile { target c++14 } } + +template <typename _Tp> struct vector { + _Tp * _M_finish; + vector(_Tp); + vector(const vector &); + constexpr auto back() { return *_M_finish; } +}; +template <typename Funct> void +run(Funct funct) { funct(1); } + +vector<int> +runner() try { + vector<int> vec{1}; + run([&](auto) { vec.back(); }); + return vec; +} catch (...) { + return 1; +} base-commit: bf84e5e64662f8f0fdebfc0212e32bfca678f9eb -- 2.48.1