From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 8F2593858D32; Mon, 23 Jan 2023 00:36:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8F2593858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674434190; bh=fiig2mqZlug1ey1sKei5Jysj36Ugr6ncueNCdFhQEKc=; h=From:To:Subject:Date:From; b=PiLdV/D+Lj+9qVdSFbrw/pH8BdR0JNwppot9Ey+Yq5y1+gin/KPElXOgWeuGfOilN IwIIJIJR/SZtOU6Dtgh3cIYpN2Ons2u1aoSqm7mEbroZyXm/n5LG+QQ0gltC7t6qEy 2kDo4Z46lZbCO39+AmYEnROVIuoCYEfljyYr0It8= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5283] c++: lifetime extension with .* expression [PR53288] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 7823285a7ae8c516d2dfa96d426692bf2aef571e X-Git-Newrev: 208c6678c25bd9a11e6c5911a4c123cb6b7f3d6e Message-Id: <20230123003630.8F2593858D32@sourceware.org> Date: Mon, 23 Jan 2023 00:36:30 +0000 (GMT) List-Id: https://gcc.gnu.org/g:208c6678c25bd9a11e6c5911a4c123cb6b7f3d6e commit r13-5283-g208c6678c25bd9a11e6c5911a4c123cb6b7f3d6e Author: Jason Merrill Date: Tue Dec 20 16:27:43 2022 -0500 c++: lifetime extension with .* expression [PR53288] 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. Diff: --- gcc/cp/call.cc | 38 ++++++++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/init/lifetime4.C | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 991730713e6..a7de0e8e9a6 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -13952,6 +13952,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); @@ -13962,6 +13990,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); +}