From patchwork Mon Jan 23 00:34:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1730224 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Z4ROWWey; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P0WN539Dcz23gY for ; Mon, 23 Jan 2023 11:35:15 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3FEB4385840D for ; Mon, 23 Jan 2023 00:35:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3FEB4385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674434113; bh=SFMSvHklJS9zrxTF3ksF48OasamN9ilKPAyIRyhWzyY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Z4ROWWey+wZmCXoop/RX2KafhlZBbVtJEcKWipUpTDDi6Z4kJcXMOA4kqH0mz+brZ 610GwHQQtLCCY2erSkvdbQ+cx6RXJqVu7uZrsAYBNXuzm99w6mZ0TZkrqnkjUqoZER LY+JDY6lNCKcak8G93mKrNgyqZAxVajZBZ0WjH8s= 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 D1B343858D32 for ; Mon, 23 Jan 2023 00:34:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D1B343858D32 Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-241-705nXBcWO4CJ-xEf90ppLQ-1; Sun, 22 Jan 2023 19:34:52 -0500 X-MC-Unique: 705nXBcWO4CJ-xEf90ppLQ-1 Received: by mail-qt1-f197.google.com with SMTP id w2-20020ac84d02000000b003b63cd590dfso4050217qtv.14 for ; Sun, 22 Jan 2023 16:34:52 -0800 (PST) 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:message-id:reply-to; bh=SFMSvHklJS9zrxTF3ksF48OasamN9ilKPAyIRyhWzyY=; b=PkDwX2hh6Hz8TnIfXvnsYuLf4GYzj5BELNAwQzXDcVe/06SnVofzRGia7IyyYD8+Rv 3HGbrBmWdk+kX1N3uWCApOsbeCuTjOuIhnO7vrUBduzbV5BAf559oJ1vI/HKI4qvM5wz 4NK5uC9DK//eerbCLsV8Yri4RL4wsoZ2/QPnxxUCORil3Dx+cmHBtJOA7HxxKYHPBgbj pd1e6dQngrUknRXzf/hDYjWZlj5XEpjUciXGc/BBuEj/1awELfFF7b2O9UR5cTzLRyII vmvHqJkZOqYxaSbelYHXUdJol/0OzZfHVVZA6uGduYl5ilO895mVqJYGOWOdUOdDO3Mz DJbQ== X-Gm-Message-State: AFqh2kr++Mx0uWPfz74S7u2xC2MuWik4MHB9r/2zUXdxHGG3JMwEwjC9 gGs1KRcyN1EGZDlXhUscFTKeSGmT1+CSp+OGEWEy8Z0KGuJ4rn5Dts7VLw6O4V1vRxAZom3jsd1 T++HwmyM+Y4kVY9hQk989ZZt1ducR3aZiyNK8I+MRkjag/GxkHVRCbfJimPCEfzUxRw== X-Received: by 2002:ac8:5a95:0:b0:3a8:faf:296a with SMTP id c21-20020ac85a95000000b003a80faf296amr45247969qtc.54.1674434091383; Sun, 22 Jan 2023 16:34:51 -0800 (PST) X-Google-Smtp-Source: AMrXdXtMYs7zWVdEka5W7c/c2C9lXrjIufC6XQGUqdSOdg91NQRKeAWWuqM88mqTOYB4uEUBYz/pBA== X-Received: by 2002:ac8:5a95:0:b0:3a8:faf:296a with SMTP id c21-20020ac85a95000000b003a80faf296amr45247922qtc.54.1674434090856; Sun, 22 Jan 2023 16:34:50 -0800 (PST) Received: from jason.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 u24-20020ac87518000000b003b2365c9aa6sm15305633qtq.14.2023.01.22.16.34.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Jan 2023 16:34:50 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: lifetime extension with .* expression [PR53288] Date: Sun, 22 Jan 2023 19:34:47 -0500 Message-Id: <20230123003447.3975772-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=-12.5 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_NONE, RCVD_IN_MSPIKE_H2, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- This PR points out a case where we are not extending the lifetime of a temporary when the subobject is denoted by a pointer-to-member operation. These rules were clarified in C++20 by CWG1299. There are other cases that also need to be handled under CWG1299, but are not fixed by this patch. PR c++/53288 DR 1299 gcc/cp/ChangeLog: * call.cc (extend_ref_init_temps_1): Handle ptrmem expression. gcc/testsuite/ChangeLog: * g++.dg/init/lifetime4.C: New test. --- gcc/cp/call.cc | 38 ++++++++++++++++++++++++++ gcc/testsuite/g++.dg/init/lifetime4.C | 39 +++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 gcc/testsuite/g++.dg/init/lifetime4.C base-commit: 1dcc9779d6196db6dd881fffc91d731cbea7c8bc diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index bd174b8d655..37297738d0f 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -13950,6 +13950,34 @@ static tree extend_ref_init_temps_1 (tree decl, tree init, vec **cleanups, tree *cond_guard) { + /* CWG1299 (C++20): The temporary object to which the reference is bound or + the temporary object that is the complete object of a subobject to which + the reference is bound persists for the lifetime of the reference if the + glvalue to which the reference is bound was obtained through one of the + following: + - a temporary materialization conversion ([conv.rval]), + - ( expression ), where expression is one of these expressions, + - subscripting ([expr.sub]) of an array operand, where that operand is one + of these expressions, + - a class member access ([expr.ref]) using the . operator where the left + operand is one of these expressions and the right operand designates a + non-static data member of non-reference type, + - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator + where the left operand is one of these expressions and the right operand + is a pointer to data member of non-reference type, + - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]), + dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast + ([expr.reinterpret.cast]) converting, without a user-defined conversion, + a glvalue operand that is one of these expressions to a glvalue that + refers to the object designated by the operand, or to its complete + object or a subobject thereof, + - a conditional expression ([expr.cond]) that is a glvalue where the + second or third operand is one of these expressions, or + - a comma expression ([expr.comma]) that is a glvalue where the right + operand is one of these expressions. */ + + /* FIXME several cases are still handled wrong (101572, 81420). */ + tree sub = init; tree *p; STRIP_NOPS (sub); @@ -13960,6 +13988,16 @@ extend_ref_init_temps_1 (tree decl, tree init, vec **cleanups, cond_guard); return init; } + if (TREE_CODE (sub) == POINTER_PLUS_EXPR + && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions + (TREE_OPERAND (sub, 1))))) + { + /* A pointer-to-member operation. */ + TREE_OPERAND (sub, 0) + = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups, + cond_guard); + return init; + } if (TREE_CODE (sub) == COND_EXPR) { tree cur_cond_guard = NULL_TREE; diff --git a/gcc/testsuite/g++.dg/init/lifetime4.C b/gcc/testsuite/g++.dg/init/lifetime4.C new file mode 100644 index 00000000000..4106af7070c --- /dev/null +++ b/gcc/testsuite/g++.dg/init/lifetime4.C @@ -0,0 +1,39 @@ +// PR c++/53288 +// { dg-do compile { target c++11 } } + +struct B { + B(int data) : _data(data) { } + ~B() { } + + int _data; + +private: + B() = delete; + B(const B &) = delete; + B(B &&) = delete; +}; + +int c,d; +struct A { + B b; + A(int data) : b(data) { ++c; } + ~A() { ++d; } + +private: + A() = delete; + A(const A &) = delete; + A(A &&) = delete; +}; + +template +void f(T t) { + const B &b = A(1).*t; + if (d) __builtin_abort (); +} + +int main() { + const B &b = A(1).*(&A::b); + if (d) __builtin_abort (); + + f(&A::b); +}