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>
}
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;
--- /dev/null
+// { 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();
+}