From 7e7a1b612654479e821214f7ef7658de8a132714 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Sat, 15 Feb 2025 10:48:17 +0100 Subject: [PATCH] c++: NRVO, constexpr, lambda [PR118053] 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. (cherry picked from commit de66529f2e7bb24fb2b61b82e6a953f3f6c12902) --- gcc/cp/constexpr.cc | 19 +++++++++-------- .../g++.dg/cpp1y/constexpr-lambda1.C | 21 +++++++++++++++++++ 2 files changed, 31 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 4b10763950d..a5c6227b8dd 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -7460,18 +7460,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..dca3920ddc0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C @@ -0,0 +1,21 @@ +// PR c++/118053 +// { dg-do compile { target c++14 } } +// { dg-additional-options -O } + +template struct vector { + _Tp * _M_finish; + vector(_Tp); + vector(const vector &); + constexpr auto back() { return *_M_finish; } +}; +template void +run(Funct funct) { funct(1); } + +vector +runner() try { + vector vec{1}; + run([&](auto) { vec.back(); }); + return vec; +} catch (...) { + return 1; +} -- 2.47.2