https://gcc.gnu.org/g:de66529f2e7bb24fb2b61b82e6a953f3f6c12902

commit r15-7558-gde66529f2e7bb24fb2b61b82e6a953f3f6c12902
Author: Jason Merrill <ja...@redhat.com>
Date:   Sat Feb 15 10:48:17 2025 +0100

    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.

Diff:
---
 gcc/cp/constexpr.cc                            | 19 ++++++++++---------
 gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C | 20 ++++++++++++++++++++
 2 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 299b13456873..59dd0668af3f 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 000000000000..68152e26b899
--- /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;
+}

Reply via email to