From patchwork Wed May 23 03:51:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 918732 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-478221-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.b="WdrMgp3u"; 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 40rJVW2qygz9s15 for ; Wed, 23 May 2018 13:52:01 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=P2T9NLHZZGVmoN1I3OFpip+7hK3wZ2hEO5Phz5BdO/TfBl sl+J13FY/pXG71dxEPzW9AEkscXIGAfolhIx+dRd5/+b4EPARWFC+n+uI9AJMR3T xVfknz34NFk55uMaFTS0muBGYMu+V/NT2ggbAZkt3lExfpkbpbtChMT2y2kHk= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=2zw1kN74Gq99UONlHMgbqVIc2Us=; b=WdrMgp3ufShHYbVDljbm grmrHZ5fVd9+bU79MBVVlFSBy+VqbdF0EWONCL3c9DW5nbuA9Dn3hu3t0Eoictdc 57A+sxRrVPcVt+Vztzx0GvuoRInxanBJLCxjbYW+554f0n1TP3M1+WadRM7WXiQM heWLtd4z+YiAm4CRSN1DqoQ= Received: (qmail 102660 invoked by alias); 23 May 2018 03:51:53 -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 102601 invoked by uid 89); 23 May 2018 03:51:52 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.7 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.2 spammy=1299 X-HELO: mail-oi0-f53.google.com Received: from mail-oi0-f53.google.com (HELO mail-oi0-f53.google.com) (209.85.218.53) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 23 May 2018 03:51:51 +0000 Received: by mail-oi0-f53.google.com with SMTP id p62-v6so18243492oie.10 for ; Tue, 22 May 2018 20:51:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=3RK25/ijF3llatgXCX4QkGhNceDPqgjtpRxt9mCy5KE=; b=aQYCsDzJWgrTP9j5NQXbZWaYcRf+GG1eR+1TvwvNlL4pU3vjD9Jd8mYdKvyCb2rNem QRdwwhs89P031AHioBJGmGVemWsG470STjzmy5DTSrc35W72R3fEjt0LvoFFE7At9YSI 8DdWAesSCyZHHTVNyH/gq1Bf1dthtsqR3u0qcXRV990PoaSdDjQUOrYPntQ/+YDTvSWF F7xiNPdm9fpn3PuagSFxbHzCoMbd5FsE6LKs+WpxoeEbbaY0BVXMPvVLSJj67pZsqwcK sB/J1vNU2eH+lJokRnQtIk3M2Xk3nRyqHKG1wJTokQdjswLrFes4gamZQ8uc8M3XKfbp 1bHw== X-Gm-Message-State: ALKqPweofhaWuFA9LjNbTeJLX44H89pBYCRbOWoCe0VTQN7eFNxyi63A kyR1IquWb+oekYzuYZ20Lco/h66kfoSbqu/qcdtb5LLU X-Google-Smtp-Source: AB8JxZrpchX0bQsZHeM/7Yr/TzQCjtVFZQdaaKrnu5e54m7iQbXqFfMj5wKGnuapLDGhrOMXEAMC+dwF41lgNNvHHNQ= X-Received: by 2002:aca:5b89:: with SMTP id p131-v6mr616380oib.8.1527047509057; Tue, 22 May 2018 20:51:49 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ac9:de5:0:0:0:0:0 with HTTP; Tue, 22 May 2018 20:51:28 -0700 (PDT) From: Jason Merrill Date: Tue, 22 May 2018 23:51:28 -0400 Message-ID: Subject: C++ PATCH for c++/81420, lifetime extension and array subscripting To: gcc-patches List X-IsSubscribed: yes The first hunk fixes looking through the array reference; the second fixes looking through the base class conversion. There's more to do to implement DR 1299, but this is a solid improvement from a fairly trivial patch. Tested x86_64-pc-linux-gnu, applying to trunk. commit a065c041556f30a685e2d6b02c0486f7e7e77443 Author: Jason Merrill Date: Tue May 1 20:23:18 2018 -0400 PR c++/81420 - not extending temporary lifetime. * call.c (extend_ref_init_temps_1): Handle ARRAY_REF. * class.c (build_base_path): Avoid redundant move of an rvalue. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 1df4d14dfe6..c100a92f2fb 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -11061,7 +11061,9 @@ extend_ref_init_temps_1 (tree decl, tree init, vec **cleanups) if (TREE_CODE (sub) != ADDR_EXPR) return init; /* Deal with binding to a subobject. */ - for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; ) + for (p = &TREE_OPERAND (sub, 0); + (TREE_CODE (*p) == COMPONENT_REF + || TREE_CODE (*p) == ARRAY_REF); ) p = &TREE_OPERAND (*p, 0); if (TREE_CODE (*p) == TARGET_EXPR) { diff --git a/gcc/cp/class.c b/gcc/cp/class.c index a9a0fa92727..25753d4c45f 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -426,7 +426,7 @@ build_base_path (enum tree_code code, { expr = cp_build_fold_indirect_ref (expr); expr = build_simple_base_path (expr, binfo); - if (rvalue) + if (rvalue && lvalue_p (expr)) expr = move (expr); if (want_pointer) expr = build_address (expr); diff --git a/gcc/testsuite/g++.dg/cpp0x/temp-extend1.C b/gcc/testsuite/g++.dg/cpp0x/temp-extend1.C new file mode 100644 index 00000000000..639f9456573 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/temp-extend1.C @@ -0,0 +1,19 @@ +// PR c++/81420 +// { dg-do run { target c++11 } } + +int d; + +struct A +{ + int i[2]; + ~A() { ++d; }; +}; + +struct B: A {}; + +int main() +{ + const int &r = B().i[0]; + if (d != 0) + __builtin_abort(); +}