From patchwork Wed Jan 15 20:04:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1223847 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-517463-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha1 header.s=default header.b=IM4iu9NM; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Tp4XoX4j; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47ydZx2Vb4z9sR4 for ; Thu, 16 Jan 2020 07:04:39 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:content-type :content-transfer-encoding; q=dns; s=default; b=HxpIpCjBmm53SzzP cPigsVusb3OZoGhgM/68Yh1IhHpOeXjrZK9yGgISkJAOPB1+8TIEWclCiw+G8Smh JJD0e/AT/R+J/v73mdGRv4B1SZufcKD1/LwcUCedbwucHQRr/oPlKi+Cda/qLE+z xf8xCKUX8wSGf5iEEF666G2KWlI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:content-type :content-transfer-encoding; s=default; bh=cMYS724kxwSu/X0i4TzQtM bWvc4=; b=IM4iu9NMLRQtHqCKkP0hAwu1981GpDE3TrTtvr7MZwA0i1z7wrkJLe Hvp856DXug9v15lH8JmwO/Gpnn3DdUeLZ4LWyX8fWF5uua088XZzuVrPvhhpxh9l UUiQ/MPGHk31faeaMj1cUBAhCqvQpEGh0cmutZ/N30B85/lSvYOP4= Received: (qmail 7357 invoked by alias); 15 Jan 2020 20:04:31 -0000 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 Received: (qmail 7345 invoked by uid 89); 15 Jan 2020 20:04:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-19.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=sk:prevent X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (207.211.31.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 15 Jan 2020 20:04:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579118659; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ekwZQFU+uTFQ5+jZ32ktlMU3j2KrqHlWHtcuyna4p9s=; b=Tp4XoX4jCBNuSdrR8qOLsJgAssyV9+B08z2hBvdTHiK3Au/RibyuEW/ru1z4/Szv+KY0sJ IWGzuuR1frS96bnOoKCiBq4w1jncqdlMSW1Uj9W/b73TASZhKidfN8mWxAUHnLcbAFgQW7 0M95pvCBikcCVXmguEcFyBkVZfiEBA8= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-321-H9VQyT_ANm-9hE3zbBaZ_g-1; Wed, 15 Jan 2020 15:04:15 -0500 Received: by mail-qt1-f197.google.com with SMTP id k27so12000170qtu.12 for ; Wed, 15 Jan 2020 12:04:15 -0800 (PST) Received: from barrymore.redhat.com (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id g16sm8955496qkk.61.2020.01.15.12.04.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2020 12:04:13 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] PR c++/92871 - bad code with xvalue and GNU ?: extension. Date: Wed, 15 Jan 2020 15:04:12 -0500 Message-Id: <20200115200412.15684-1-jason@redhat.com> X-Mimecast-Spam-Score: 0 X-IsSubscribed: yes I steered Jakub wrong on the desired behavior for temp-extend1.C in the context of bug 92831; it doesn't make sense to try to extend the lifetime of a temporary that we've already materialized to evaluate the test. So this patch munges the stabilized expression so that it won't be subject to lifetime extension. Tested x86_64-pc-linux-gnu, applying to trunk. * call.c (prevent_lifetime_extension): New. (build_conditional_expr_1): Use it. --- gcc/cp/call.c | 24 +++++++++++++++++++++++- gcc/testsuite/g++.dg/ext/temp-extend1.C | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) base-commit: bc071d3a951a98284a3f46043afd44c03c123376 diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 9a22398e794..32ccfc973e4 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -217,6 +217,7 @@ static conversion *merge_conversion_sequences (conversion *, conversion *); static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t); static conversion *build_identity_conv (tree, tree); static inline bool conv_binds_to_array_of_unknown_bound (conversion *); +static tree prevent_lifetime_extension (tree); /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE. NAME can take many forms... */ @@ -5078,7 +5079,10 @@ build_conditional_expr_1 (const op_location_t &loc, /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */ if (glvalue_p (arg1)) - arg2 = arg1 = cp_stabilize_reference (arg1); + { + arg2 = arg1 = cp_stabilize_reference (arg1); + arg2 = arg1 = prevent_lifetime_extension (arg1); + } else arg2 = arg1 = cp_save_expr (arg1); } @@ -12168,6 +12172,24 @@ initialize_reference (tree type, tree expr, return expr; } +/* If *P is an xvalue expression, prevent temporary lifetime extension if it + gets used to initialize a reference. */ + +static tree +prevent_lifetime_extension (tree t) +{ + tree *p = &t; + while (TREE_CODE (*p) == COMPOUND_EXPR) + p = &TREE_OPERAND (*p, 1); + while (handled_component_p (*p)) + p = &TREE_OPERAND (*p, 0); + /* Change a TARGET_EXPR from prvalue to xvalue. */ + if (TREE_CODE (*p) == TARGET_EXPR) + *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p, + move (TARGET_EXPR_SLOT (*p))); + return t; +} + /* Subroutine of extend_ref_init_temps. Possibly extend one initializer, which is bound either to a reference or a std::initializer_list. */ diff --git a/gcc/testsuite/g++.dg/ext/temp-extend1.C b/gcc/testsuite/g++.dg/ext/temp-extend1.C index 7df9ca51681..aaef115494c 100644 --- a/gcc/testsuite/g++.dg/ext/temp-extend1.C +++ b/gcc/testsuite/g++.dg/ext/temp-extend1.C @@ -21,7 +21,7 @@ baz (int i) { const bool&& a = id{false, true, false}[i].s ? : id{true, false, true, false}[i].s; - if (S::c != (i ? 3 : 4)) + if (S::c != (i ? 0 : 4)) __builtin_abort (); }