From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1664473852; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/Gi1Jnp1NiNdOP0yFAgBvADGe3/o38frITy6LsOX4+Y=; b=bFJGJhXIrrHHK3Y4qvgsMFZZahFAqfuL2+LkMPRXyFbPpo329GL2RYqDbu5txN8l2pC73/ KUV8UzSO9YoJW1HrXbThxOpp353qIm583rzjbCGF0zONqTNvO1ANzcu0cz80hwx0h3Jkkb PjJ7T4HvuODxWBLOsYNWCYeGt2IhqiI= 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-Google-Smtp-Source: AMsMyM72jYpJNkOAASHWcLJHFU7PcdwWBp/p0ZeigCkHkp7fzhLfhA9ycjcAx8/JfxxETygCTZxWfA== 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) From: Jason Merrill 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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(-) 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 (); +} base-commit: af9034827e8f06f10767064e9fc7443b94e08184 prerequisite-patch-id: b1512f7473cce4cc8f21dbc772d07bd59c632e5e -- 2.31.1