From patchwork Thu Sep 29 17:50:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1555 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp99165wrs; Thu, 29 Sep 2022 10:51:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5jQcWt74w0haE5Qu0FMYxfu6mOImZgqdvZv78Da1ZSFert7T1Ps2QZsaS0m+oURwBQ3zfp X-Received: by 2002:a17:907:d94:b0:782:65af:9442 with SMTP id go20-20020a1709070d9400b0078265af9442mr3630045ejc.637.1664473896633; Thu, 29 Sep 2022 10:51:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664473896; cv=none; d=google.com; s=arc-20160816; b=FkIWbzwTwk2E5OG4QyY3Udu1VwIuEpp6K1AOQh3dKJzC3nQHmbVeQrM9vKdJC3vtEg +uvwnAmbNkX0oOUqJX3CpT5CqL9zOeVlb3+80ssh+JTGTYnyKH2/Y8fpkfxGp6Yb7vM3 ocNcSO6hHodGMoGWTMIKJjjvuqH+nTKUL6LBkr2FO4u449LInvN117L5oDfEzi6g3kR9 PGWvLc77kBPC8sVRag9oTeZUEwuyVNVpF/i6dEwSvplZSr9f4la74dJmrunvH+cHxchG AyGb5NXQKkggCWQT48eUfYvBXpdIVSgRYUovg1frn2jBFCIGNbWkoFXejCxsg+5WHM5F r9gQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=/Gi1Jnp1NiNdOP0yFAgBvADGe3/o38frITy6LsOX4+Y=; b=MYC+zpSwtiDxNMlCH2JKZrY6zLljDkbpC6HSs/DUMluLKrZa1WPXOxB1S0w28QMOlZ G1MWg0fL1HkBCyRUqQWXTK/HAPO1qIIC8p2LY/X0rWk2kl1jnYqmiJDvssdBSiTQQiKr UFXd2C3Aa7dV+U0sXGe4oEBlcIYy+QcujMEKuB7z48OtvGX+Jvqq+7oESqzZ1C0ahTyE z0+tWQDFM5OjojEBo9sNpV7hcXYayrdssDNBMaZnHSNgQmY5P/+cHrlW2EpH3XWLz6zw O3DnPBDz5RPpPriWQlTdGwAjJAdLfucSp4CeBNPpMTNMc0lw8YE7Y+f7A5YUcUShICWv sK5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ipzfu006; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id sh39-20020a1709076ea700b00783150632f7si8357419ejc.11.2022.09.29.10.51.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Sep 2022 10:51:36 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ipzfu006; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9CE09385C332 for ; Thu, 29 Sep 2022 17:51:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9CE09385C332 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664473895; bh=/Gi1Jnp1NiNdOP0yFAgBvADGe3/o38frITy6LsOX4+Y=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Ipzfu006CMzsOdekp0f9RfUsFLY6wVlH0jdVO0ryWhLZMymexRnos02O8C4WWa6hL N0yQWCxt26zOt3dgx3hseF/MUEhRcMBvcJGtYIyuoUKWXnuGwn+hhg1n+mx4giYrn4 1sOi/kxlNek7sxkQtbkmpSpxOBuDRWi5GIVklGvI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 875B8385C409 for ; Thu, 29 Sep 2022 17:50:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 875B8385C409 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-283-8K7eTD3yMXW1OTayq33czw-1; Thu, 29 Sep 2022 13:50:51 -0400 X-MC-Unique: 8K7eTD3yMXW1OTayq33czw-1 Received: by mail-qk1-f197.google.com with SMTP id d18-20020a05620a241200b006ce80a4d74aso1743238qkn.6 for ; Thu, 29 Sep 2022 10:50:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date; bh=/Gi1Jnp1NiNdOP0yFAgBvADGe3/o38frITy6LsOX4+Y=; b=6HLDOq/398L318WbYKBKhkw8mPlI/jJI059JRGgLR8E7BFwghlzg+rK7RLRB3/4TJS KLSzJEcnZtKm2Y5b6UR5tjMGze9auhy9rRI/+q40ecy4f6GLpY0FBZaUiNJbkfIHlaJQ w3hgJHxKM5XD0eyzqbWEkewgXCpFoAYWXcT1D1SgtAm4vpc4h0KGX+vMvAB8DLyix5ZV INmvO/7LwNB35Vzb0kqmtc2Fil3xsx+y1swwStTCp7/3E4y6eyz/gzzhmjzfsMB2zK5w n6cBWskNzywpJRGq2EQaroohD88VD5eancQ+RfONG6ydIb1jnY9CpBsBYKjMS2Ayr6jQ jrsQ== X-Gm-Message-State: ACrzQf1F9zLWBEGMTJHFCYqQD3kSRKeEFc666uh2Y8DueFGdFGzrGEpb A9WDStbn/pVlvRrFvpM5HQawv6Hco++1jTXEf8B0nU2rzIz5RRR32HkoFRrDZj/11ZDQdzBfG9M dZbyswYOXAkuMVowsHgUmiyEG8CMggdLPIKwd6rUQWUSTTc3Y9F4sIeMxDmfgOlGJrA== X-Received: by 2002:a05:620a:24cf:b0:6ce:7d6a:9c85 with SMTP id m15-20020a05620a24cf00b006ce7d6a9c85mr3179171qkn.271.1664473850442; Thu, 29 Sep 2022 10:50:50 -0700 (PDT) X-Received: by 2002:a05:620a:24cf:b0:6ce:7d6a:9c85 with SMTP id m15-20020a05620a24cf00b006ce7d6a9c85mr3179148qkn.271.1664473849979; Thu, 29 Sep 2022 10:50:49 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id j129-20020a375587000000b006b953a7929csm7712qkb.73.2022.09.29.10.50.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Sep 2022 10:50:49 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: fix class-valued ?: extension Date: Thu, 29 Sep 2022 13:50:47 -0400 Message-Id: <20220929175047.1915926-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1745327380512616454?= X-GMAIL-MSGID: =?utf-8?q?1745327380512616454?= Tested x86_64-pc-linux-gnu, applying to trunk. -- >8 -- When the gimplifier encounters the same TARGET_EXPR twice, it evaluates TARGET_EXPR_INITIAL the first time and clears it so that the later evaluation is just the temporary. With this testcase, using the extension to treat an omitted middle operand as repeating the first operand, that led to doing a bitwise copy of the S(1) temporary on return rather than properly calling the copy constructor. We can't use S(1) to initialize the return value here anyway, because we need to materialize it into a temporary so we can convert it to bool and determine which arm we're evaluating. So let's just treat the middle operand as an xvalue. PR c++/93046 gcc/cp/ChangeLog: * call.cc (build_conditional_expr): For a?:c extension, treat a reused class prvalue as an xvalue. gcc/testsuite/ChangeLog: * g++.dg/ext/cond4.C: Add runtime test. --- gcc/cp/call.cc | 5 +++++ gcc/testsuite/g++.dg/ext/cond4.C | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) base-commit: af9034827e8f06f10767064e9fc7443b94e08184 prerequisite-patch-id: b1512f7473cce4cc8f21dbc772d07bd59c632e5e diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 9fad3cb950b..bd04a1d309a 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -5402,6 +5402,11 @@ build_conditional_expr (const op_location_t &loc, arg1 = cp_stabilize_reference (arg1); arg2 = arg1 = prevent_lifetime_extension (arg1); } + else if (TREE_CODE (arg1) == TARGET_EXPR) + /* arg1 can't be a prvalue result of the conditional + expression, since it needs to be materialized for the + conversion to bool, so treat it as an xvalue in arg2. */ + arg2 = move (TARGET_EXPR_SLOT (arg1)); else arg2 = arg1 = cp_save_expr (arg1); } diff --git a/gcc/testsuite/g++.dg/ext/cond4.C b/gcc/testsuite/g++.dg/ext/cond4.C index d2853f40387..86993306aa5 100644 --- a/gcc/testsuite/g++.dg/ext/cond4.C +++ b/gcc/testsuite/g++.dg/ext/cond4.C @@ -1,10 +1,14 @@ // PR c++/93046 -// { dg-do compile } +// { dg-do run } // { dg-options "" } +int c; + struct S { - S (int); - operator bool (); + int i; + S (int i) : i(i) { ++c; } + S (const S &s): i(s.i) { ++c; } + operator bool () { return i; } }; S @@ -12,3 +16,10 @@ foo () { return S (1) ? : S (2); } + +int main() +{ + S s = foo(); + if (s.i != 1 || c != 2) + __builtin_abort (); +}