From patchwork Wed Mar 16 16:23:37 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 87279 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 6D7C3B6FDE for ; Thu, 17 Mar 2011 03:24:00 +1100 (EST) Received: (qmail 12187 invoked by alias); 16 Mar 2011 16:23:58 -0000 Received: (qmail 12136 invoked by uid 22791); 16 Mar 2011 16:23:56 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 16 Mar 2011 16:23:40 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p2GGNd0o009885 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 16 Mar 2011 12:23:39 -0400 Received: from [127.0.0.1] (ovpn-113-145.phx2.redhat.com [10.3.113.145]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p2GGNc37007783 for ; Wed, 16 Mar 2011 12:23:39 -0400 Message-ID: <4D80E409.10507@redhat.com> Date: Wed, 16 Mar 2011 12:23:37 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110307 Fedora/3.1.9-0.39.b3pre.fc14 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/47999 (incorrect auto deduction in template) Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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 Date: Tue Mar 8 08:46:41 2011 -0500 PR c++/47999 * semantics.c (finish_call_expr): Preserve reference semantics in templates. 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 +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; +}