From patchwork Fri Nov 30 21:05:04 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 203048 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 5ED592C0040 for ; Sat, 1 Dec 2012 08:05:20 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1354914321; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Message-ID:Date:From:User-Agent:MIME-Version: To:CC:Subject:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=0NXtotVNIT+67N8xIZuhZ1AMMKk=; b=ZA0wXRX5YHpLlpD 9ncQBcRXlt+fpKpE28ExA9vwFC9Kvz4l8ULdLeY7j72z+J5eZoMjUwgrCovrsiRM 0MqQaPD/rGF2Xz+3z7rhap/mCrVV1k3+rjCyOZg3pPBUBdDzlF1kqzS9bXTYX/sq tm6bQWKntU5OjQunBI2ZtPiWJ7yM= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:Content-Type:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=FA1MFxc/Owuzqt3sYB3Z8qjOLAWDN762JZib7ddBNRGgiOzNVhcth/UseXMHcx crdqhTQSZhw77Z2UXv4HOVvRw5v9UlnpmjaY6xJrZFpxaNWa10bAEw/OluRRhurJ QWGOcGYTUeVdGoMVTXx5BHVlagf9bBuh6Z4DZybJVevaw=; Received: (qmail 27313 invoked by alias); 30 Nov 2012 21:05:15 -0000 Received: (qmail 27303 invoked by uid 22791); 30 Nov 2012 21:05:15 -0000 X-SWARE-Spam-Status: No, hits=-4.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_NO, RP_MATCHES_RCVD, TW_PM X-Spam-Check-By: sourceware.org Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 30 Nov 2012 21:05:09 +0000 Received: from ucsinet22.oracle.com (ucsinet22.oracle.com [156.151.31.94]) by aserp1040.oracle.com (Sentrion-MTA-4.2.2/Sentrion-MTA-4.2.2) with ESMTP id qAUL58h3001797 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 30 Nov 2012 21:05:08 GMT Received: from acsmt356.oracle.com (acsmt356.oracle.com [141.146.40.156]) by ucsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id qAUL57vO028216 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Fri, 30 Nov 2012 21:05:07 GMT Received: from abhmt103.oracle.com (abhmt103.oracle.com [141.146.116.55]) by acsmt356.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id qAUL56uS007903; Fri, 30 Nov 2012 15:05:07 -0600 Received: from [192.168.1.4] (/79.45.212.63) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 30 Nov 2012 13:05:06 -0800 Message-ID: <50B91F80.3080100@oracle.com> Date: Fri, 30 Nov 2012 22:05:04 +0100 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: Jason Merrill Subject: [C++ Patch] PR 54170 X-IsSubscribed: yes 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 Hi, this wrong code PR is some sort of continuation of PR 52988, where we discard side-effects of expression of nullptr_t. Here too, in cp_convert_to_pointer and in build_ptrmemfunc, we don't check for side-effects and we replace the expression with a plain int_cst or nullptr_node. The testcase in the PR exercises only plain pointers but we have also to handle correctly pointers to members, thus I extended it and the patch became slightly more complex: for the case of pointer to data member in particular I have to call by hand build2 and build a COMPOUND_EXPR to host the side-effects (similarly to what I found used in cp_build_addr_expr_1). Tested x86_64-linux. Thanks, Paolo. ///////////////////////////// /cp 2012-11-30 Paolo Carlini PR c++/54170 * cvt.c (cp_convert_to_pointer): Don't discard side-effects from expressions of nullptr_t. * typeck.c (build_ptrmemfunc): Likewise. /testsuite 2012-11-30 Paolo Carlini PR c++/54170 * g++.dg/cpp0x/lambda/lambda-nullptr.C: New. Index: cp/cvt.c =================================================================== --- cp/cvt.c (revision 193995) +++ cp/cvt.c (working copy) @@ -219,10 +219,15 @@ cp_convert_to_pointer (tree type, tree expr, tsubs { /* A NULL pointer-to-member is represented by -1, not by zero. */ - expr = build_int_cst_type (type, -1); + expr = (TREE_SIDE_EFFECTS (expr) + ? build2 (COMPOUND_EXPR, type, expr, + build_int_cst_type (type, -1)) + : build_int_cst_type (type, -1)); } else - expr = build_int_cst (type, 0); + expr = (TREE_SIDE_EFFECTS (expr) + ? build_nop (type, expr) + : build_int_cst (type, 0)); return expr; } Index: cp/typeck.c =================================================================== --- cp/typeck.c (revision 193995) +++ cp/typeck.c (working copy) @@ -7567,7 +7567,9 @@ build_ptrmemfunc (tree type, tree pfn, int force, /* Handle null pointer to member function conversions. */ if (null_ptr_cst_p (pfn)) { - pfn = build_c_cast (input_location, type, nullptr_node); + pfn = (TREE_SIDE_EFFECTS (pfn) + ? build_nop (type, pfn) + : build_c_cast (input_location, type, nullptr_node)); return build_ptrmemfunc1 (to_type, integer_zero_node, pfn); Index: testsuite/g++.dg/cpp0x/lambda/lambda-nullptr.C =================================================================== --- testsuite/g++.dg/cpp0x/lambda/lambda-nullptr.C (revision 0) +++ testsuite/g++.dg/cpp0x/lambda/lambda-nullptr.C (working copy) @@ -0,0 +1,47 @@ +// PR c++/54170 +// { dg-do run { target c++11 } } + +#include + +struct A; +typedef A* ptr; +typedef int (A::*pmf) (int); +typedef int (A::*pdm); + +int total; + +void add(int n) +{ + total += n; +} + +template +RType Call(Callable native_func, int arg) +{ + return native_func(arg); +} + +template +RType do_test(int delta) +{ + return Call([=](int delta) { add(delta); return nullptr; }, delta); +} + +template +void test() +{ + total = 0; + assert (!do_test(5)); + assert (total == 5); + assert (!do_test(20)); + assert (total == 25); + assert (!do_test(-256)); + assert (total == -231); +} + +int main() +{ + test(); + test(); + test(); +}