diff mbox

C++ PATCH for c++/47999 (incorrect auto deduction in template)

Message ID 4D80E409.10507@redhat.com
State New
Headers show

Commit Message

Jason Merrill March 16, 2011, 4:23 p.m. UTC
In templates, we do overload resolution and then throw away most of it, 
going back to the pre-conversion arguments.  But when the call returns a 
reference, we shouldn't elide the dereference and pretend it returns by 
value, as that confuses lvalue_p.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit 79330512f8ade4ecff47ab8cb8c050440d9648e0
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Mar 8 08:46:41 2011 -0500

    	PR c++/47999
    	* semantics.c (finish_call_expr): Preserve reference semantics
    	in templates.

Comments

H.J. Lu March 17, 2011, 5:33 a.m. UTC | #1
On Wed, Mar 16, 2011 at 9:23 AM, Jason Merrill <jason@redhat.com> wrote:
> In templates, we do overload resolution and then throw away most of it,
> going back to the pre-conversion arguments.  But when the call returns a
> reference, we shouldn't elide the dereference and pretend it returns by
> value, as that confuses lvalue_p.
>
> Tested x86_64-pc-linux-gnu, applying to trunk.
>
>
> commit 79330512f8ade4ecff47ab8cb8c050440d9648e0
> Author: Jason Merrill <jason@redhat.com>
> Date:   Tue Mar 8 08:46:41 2011 -0500
>
>        PR c++/47999
>        * semantics.c (finish_call_expr): Preserve reference semantics
>        in templates.
>

This may have caused:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48162
diff mbox

Patch

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 53497f3..ce24d46 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -2150,11 +2150,17 @@  finish_call_expr (tree fn, VEC(tree,gc) **args, bool disallow_virtual,
     /* A call where the function is unknown.  */
     result = cp_build_function_call_vec (fn, args, complain);
 
-  if (processing_template_decl)
+  if (processing_template_decl && result != error_mark_node)
     {
+      if (TREE_CODE (result) == INDIRECT_REF)
+	result = TREE_OPERAND (result, 0);
+      gcc_assert (TREE_CODE (result) == CALL_EXPR
+		  || TREE_CODE (fn) == PSEUDO_DTOR_EXPR
+		  || errorcount);
       result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
       KOENIG_LOOKUP_P (result) = koenig_p;
       release_tree_vector (orig_args);
+      result = convert_from_reference (result);
     }
 
   return result;
diff --git a/gcc/testsuite/g++.dg/cpp0x/auto22.C b/gcc/testsuite/g++.dg/cpp0x/auto22.C
new file mode 100644
index 0000000..66630e5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/auto22.C
@@ -0,0 +1,21 @@ 
+// PR c++/47999
+// { dg-options -std=c++0x }
+
+int& identity(int& i)
+{
+  return i;
+}
+
+// In a function template, auto type deduction works incorrectly.
+template <typename = void>
+void f()
+{
+  int i = 0;
+  auto&& x = identity(i); // Type of x should be `int&`, but it is `int&&`.
+}
+
+int main (int argc, char* argv[])
+{
+  f();
+  return 0;
+}