]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Ensure mark_lvalue_use returns an lvalue [PR122163]
authorNathaniel Shead <nathanieloshead@gmail.com>
Sun, 5 Oct 2025 10:30:56 +0000 (21:30 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Sun, 12 Oct 2025 11:22:30 +0000 (22:22 +1100)
When processing a tentative capture of a rvalue reference, mark_use
folds it away to the referred-to entity.  But this is an rvalue, and
when called from an lvalue context an rvalue reference should still be
an lvalue.

PR c++/122163

gcc/cp/ChangeLog:

* expr.cc (mark_use): When processing a reference, always return
an lvalue reference when !rvalue_p.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/lambda/lambda-ref3.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/expr.cc
gcc/testsuite/g++.dg/cpp0x/lambda/lambda-ref3.C [new file with mode: 0644]

index f2e99397acac68d91be42e99a5b20018f34ff74d..a29cb2461a5b1440d0eff06cafe9a91b2a8e6783 100644 (file)
@@ -183,7 +183,16 @@ mark_use (tree expr, bool rvalue_p, bool read_p,
            }
          tree r = mark_rvalue_use (ref, loc, reject_builtin);
          if (r != ref)
-           expr = convert_from_reference (r);
+           {
+             if (!rvalue_p)
+               {
+                 /* Make sure we still return an lvalue.  */
+                 gcc_assert (TREE_CODE (r) == NOP_EXPR);
+                 TREE_TYPE (r) = cp_build_reference_type (TREE_TYPE (r),
+                                                          false);
+               }
+             expr = convert_from_reference (r);
+           }
        }
       break;
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-ref3.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-ref3.C
new file mode 100644 (file)
index 0000000..1e085b2
--- /dev/null
@@ -0,0 +1,18 @@
+// { dg-do run { target c++11 } }
+
+int x;
+int main() {
+  constexpr int&& r = static_cast<int&&>(x);
+  r = 1;
+
+  auto a = [] { r = 2; };  // Not an ODR-use of 'r' so no capture needed.
+  auto b = [&] { r = 3; };
+
+  a();
+  if (r != 2)
+    __builtin_abort();
+
+  b();
+  if (r != 3)
+    __builtin_abort();
+}