]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: NRVO, constexpr, lambda [PR118053]
authorJason Merrill <jason@redhat.com>
Sat, 15 Feb 2025 09:48:17 +0000 (10:48 +0100)
committerJason Merrill <jason@redhat.com>
Sat, 15 Feb 2025 12:27:17 +0000 (13:27 +0100)
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
gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C [new file with mode: 0644]

index 299b134568732f8f33281d375fe37f8b4e736ffb..59dd0668af3f1117a59291d750788c6fde98ae7d 100644 (file)
@@ -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 (file)
index 0000000..68152e2
--- /dev/null
@@ -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;
+}