Core 1148
authorJason Merrill <jason@redhat.com>
Wed, 16 Mar 2011 20:15:37 +0000 (16:15 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Wed, 16 Mar 2011 20:15:37 +0000 (16:15 -0400)
Core 1148
* typeck.c (check_return_expr): Fix conditions for setting
LOOKUP_PREFER_RVALUE.

From-SVN: r171071

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/elision2.C [new file with mode: 0644]

index 35d033758db1260a6ffbddf6d927500a9f2165bf..fe169832d2a74aeb127188a7811d972cad7e0ca2 100644 (file)
@@ -1,5 +1,9 @@
 2011-03-16  Jason Merrill  <jason@redhat.com>
 
+       Core 1148
+       * typeck.c (check_return_expr): Fix conditions for setting
+       LOOKUP_PREFER_RVALUE.
+
        * call.c (build_over_call): Remove require_complete_type_sfinae call.
 
        PR c++/48132
index 0e8a6d79b3490372d8162b4e8ab9d5d0061c3603..03aa49ee178f65eedb60f8189db9866d9358a647 100644 (file)
@@ -7760,12 +7760,19 @@ check_return_expr (tree retval, bool *no_warning)
 
       /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
         treated as an rvalue for the purposes of overload resolution to
-        favor move constructors over copy constructors.  */
-      if ((cxx_dialect != cxx98) 
-          && named_return_value_okay_p
-          /* The variable must not have the `volatile' qualifier.  */
-         && !CP_TYPE_VOLATILE_P (TREE_TYPE (retval))
-         /* The return type must be a class type.  */
+        favor move constructors over copy constructors.
+
+         Note that these conditions are similar to, but not as strict as,
+        the conditions for the named return value optimization.  */
+      if ((cxx_dialect != cxx98)
+          && (TREE_CODE (retval) == VAR_DECL
+             || TREE_CODE (retval) == PARM_DECL)
+         && DECL_CONTEXT (retval) == current_function_decl
+         && !TREE_STATIC (retval)
+         && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
+                         (TYPE_MAIN_VARIANT
+                          (TREE_TYPE (TREE_TYPE (current_function_decl)))))
+         /* This is only interesting for class type.  */
          && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
        flags = flags | LOOKUP_PREFER_RVALUE;
 
index f5e7580c39d80256d8ef8887d7102e8f26654cad..5685b45576c93eb7cbf9d0aa852fb29d796025c0 100644 (file)
@@ -1,5 +1,7 @@
 2011-03-16  Jason Merrill  <jason@redhat.com>
 
+       * g++.dg/cpp0x/elision2.C: New.
+
        * g++.dg/cpp0x/constexpr-array3.C: New.
 
 2011-03-16  Jason Merrill  <jason@redhat.com>
diff --git a/gcc/testsuite/g++.dg/cpp0x/elision2.C b/gcc/testsuite/g++.dg/cpp0x/elision2.C
new file mode 100644 (file)
index 0000000..216b1b5
--- /dev/null
@@ -0,0 +1,13 @@
+// Core 1148: should be able to move from value parameter on return
+// { dg-options -std=c++0x }
+
+struct A
+{
+  A(const A&) = delete;
+  A(A&&);
+};
+
+A f (A a)
+{
+  return a;
+}