From patchwork Fri Feb 16 15:58:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 85888 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 502083857C49 for ; Fri, 16 Feb 2024 15:59:08 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 852CD385800C for ; Fri, 16 Feb 2024 15:58:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 852CD385800C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 852CD385800C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708099117; cv=none; b=cGO8jRbiXujESPHsEYeBcqTTxH0cSVOMj2GSD+XRrx8ddQJ0dEYZeMZUKMoaXMn/fdyXpZyqNEpXgaPzKC+bZ8Q2JiD3ZXiLxrt6qtFGyo2Bavnqp0RFJs1gXD8XKFyj2L5+Y/h9lPYpvw13HFcqmvXRSVuYkRdUYtiAYbsM7jA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708099117; c=relaxed/simple; bh=QvaJoN4vWDIZVs5AyvWZIytRlYykQHjcqRjR3fkVM0o=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Zmmnxh5GPLVPQihX3OIZYdVnue2eNnWAPToL4bsskYwKspVfn1JSfIcHpALW89WM4XFRzy8TBFdGNOkRBl4ZR/xrdVtYIR/98QutJz0+PTSunKCyR6LoTWJ4JBOONIqOvIEL6YHOmbtAR73DfjT+L0yj9n37mleryghR/lLNMFY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708099113; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Hoht9fAk6gd5wzHkazX+zKcRu9nF4KNNxQTJwxockMU=; b=LLUzhNQMHkrVD1VDXWihHWwf2Z8rUVjwzkGGa6AOaUvGEcZNFUQDktDyE2jXmVxfuYc8zs P3CXFpCxS0PC0Zn/X24pAR4KH5FkK3syEsMb9Q5yy9xfZSTmD/NJdm2lSYyvoqsKr4KNII EGxvJMr3WQVTF577HVknYr2cosUh+3g= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-136-RwPmK0mxOn24d858tfDkMQ-1; Fri, 16 Feb 2024 10:58:31 -0500 X-MC-Unique: RwPmK0mxOn24d858tfDkMQ-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-68c51abe499so34913586d6.2 for ; Fri, 16 Feb 2024 07:58:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708099110; x=1708703910; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Hoht9fAk6gd5wzHkazX+zKcRu9nF4KNNxQTJwxockMU=; b=VgXS0SnS1vqN3DsqYk3i0PkqsIhUK6728HrrzzVsWLVGK1rRDgpje9PcEVLAiLqCOq oFogGx0FPPcGJfmDt+++xRiNyJufjyInPPDOiWtObePnXiYWivVoloJ4HXUxuAN39kGd e8J7hAZK4piqI56O6wH1PF0RuRzXvjBvPh1VaGcX7IM1y/bHkPiI8w5/E+CYo2rf8Bdv xYyotR5Gmh8+84TXoPZIbLJU1IQhkucQf+Jut0VbbJioGV4PkzeM9stBohEWWC2Buh7D w3MWq0s0SXJnSIF9sM2oljI0IoUv6slkri71WvkmZbkhdlGZ8f7cHq9HEzLhHBXfjW9s 4gig== X-Gm-Message-State: AOJu0YyYRKqq+rvOLrx0OkcB1SfDLopgwlE7x/vMCXWq8hDCSUx1E9tT woHUsYdc/5knO8g1dKouhkOumzIHGraAWRpcCmhNCxlW2/MNbKtnHC9X2ztvOwYD1Q7vL95JNHc 3xfvpn/zJjQ6hJEZFOU2vHRsoD6JiJVoylY4lFy5qA/X5faY5nP78j2LR6AQr304= X-Received: by 2002:a0c:f5d3:0:b0:68c:46e0:78d with SMTP id q19-20020a0cf5d3000000b0068c46e0078dmr5183375qvm.45.1708099110166; Fri, 16 Feb 2024 07:58:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IGHvxlbZs+72XAHGjuLXsDZ6ggBC3arGVglFpvUPjnyKrcPI4FVb+ZZq7t+1pTsJ2NrrjNY4Q== X-Received: by 2002:a0c:f5d3:0:b0:68c:46e0:78d with SMTP id q19-20020a0cf5d3000000b0068c46e0078dmr5183357qvm.45.1708099109744; Fri, 16 Feb 2024 07:58:29 -0800 (PST) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id ql4-20020a056214540400b0068c510634d1sm16948qvb.108.2024.02.16.07.58.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 07:58:29 -0800 (PST) Date: Fri, 16 Feb 2024 10:58:27 -0500 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: implicit move with throw [PR113853] Message-ID: References: <20240215151947.38390-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org On Thu, Feb 15, 2024 at 04:36:40PM -0500, Jason Merrill wrote: > On 2/15/24 10:19, Marek Polacek wrote: > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > -- >8 -- > > Here we have > > > > template > > auto is_throwable(T t) -> decltype(throw t, true) { ... } > > > > where we didn't properly mark 't' as IMPLICIT_RVALUE_P, which caused > > the wrong overload to have been chosen. Jason figured out it's because > > we don't correctly implement [expr.prim.id.unqual]#4.2, which post-P2266 > > says that an id-expression is move-eligible if > > > > "the id-expression (possibly parenthesized) is the operand of > > a throw-expression, and names an implicitly movable entity that belongs > > to a scope that does not contain the compound-statement of the innermost > > lambda-expression, try-block, or function-try-block (if any) whose > > compound-statement or ctor-initializer contains the throw-expression." > > > > I worked out that it's trying to say that given > > > > struct X { > > X(); > > X(const X&); > > X(X&&) = delete; > > }; > > > > the following should fail: the scope of the throw is an sk_try, and it's > > also x's scope S, and S "does not contain the compound-statement of the > > *try-block" so x is move-eligible, so we move, so we fail. > > > > void f () > > try { > > X x; > > throw x; // use of deleted function > > } catch (...) { > > } > > > > Whereas here: > > > > void g (X x) > > try { > > throw x; > > } catch (...) { > > } > > > > the throw is again in an sk_try, but x's scope is an sk_function_parms > > which *does* contain the {} of the *try-block, so x is not move-eligible, > > so we don't move, so we use X(const X&), and the code is fine. > > > > The current code also doesn't seem to handle > > > > void h (X x) { > > void z (decltype(throw x, true)); > > } > > > > where there's no enclosing lambda or sk_try so we should move. > > > > I'm not doing anything about lambdas because we shouldn't reach the > > code at the end of the function: the DECL_HAS_VALUE_EXPR_P check > > shouldn't let us go further. > > > > PR c++/113789 > > PR c++/113853 > > > > gcc/cp/ChangeLog: > > > > * typeck.cc (treat_lvalue_as_rvalue_p): Update code to better > > reflect [expr.prim.id.unqual]#4.2. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp0x/sfinae69.C: Remove dg-bogus. > > * g++.dg/cpp0x/sfinae70.C: New test. > > * g++.dg/cpp0x/sfinae71.C: New test. > > * g++.dg/cpp0x/sfinae72.C: New test. > > * g++.dg/cpp2a/implicit-move4.C: New test. > > --- > > gcc/cp/typeck.cc | 32 +++++------ > > gcc/testsuite/g++.dg/cpp0x/sfinae69.C | 2 +- > > gcc/testsuite/g++.dg/cpp0x/sfinae70.C | 16 ++++++ > > gcc/testsuite/g++.dg/cpp0x/sfinae71.C | 17 ++++++ > > gcc/testsuite/g++.dg/cpp0x/sfinae72.C | 17 ++++++ > > gcc/testsuite/g++.dg/cpp2a/implicit-move4.C | 59 +++++++++++++++++++++ > > 6 files changed, 126 insertions(+), 17 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/sfinae70.C > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/sfinae71.C > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/sfinae72.C > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/implicit-move4.C > > > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > > index 132c55cfc6d..0dc44cd87ca 100644 > > --- a/gcc/cp/typeck.cc > > +++ b/gcc/cp/typeck.cc > > @@ -10837,37 +10837,37 @@ treat_lvalue_as_rvalue_p (tree expr, bool return_p) > > parenthesized) id-expression that names an implicitly movable entity > > declared in the body or parameter-declaration-clause of the innermost > > enclosing function or lambda-expression, */ > > - if (DECL_CONTEXT (retval) != current_function_decl) > > - return NULL_TREE; > > if (return_p) > > { > > + if (DECL_CONTEXT (retval) != current_function_decl) > > + return NULL_TREE; > > expr = move (expr); > > if (expr == error_mark_node) > > return NULL_TREE; > > return set_implicit_rvalue_p (expr); > > } > > - /* if the operand of a throw-expression is a (possibly parenthesized) > > - id-expression that names an implicitly movable entity whose scope does not > > - extend beyond the compound-statement of the innermost try-block or > > - function-try-block (if any) whose compound-statement or ctor-initializer > > - encloses the throw-expression, */ > > + /* if the id-expression (possibly parenthesized) is the operand of > > + a throw-expression, and names an implicitly movable entity that belongs > > + to a scope that does not contain the compound-statement of the innermost > > + lambda-expression, try-block, or function-try-block (if any) whose > > + compound-statement or ctor-initializer contains the throw-expression. */ > > Please also mention here why you aren't checking for lambdas. Done. > > /* C++20 added move on throw of parms. */ > > if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20) > > return NULL_TREE; > > for (cp_binding_level *b = current_binding_level; > > - ; b = b->level_chain) > > - { > > - for (tree decl = b->names; decl; decl = TREE_CHAIN (decl)) > > - if (decl == retval) > > - return set_implicit_rvalue_p (move (expr)); > > - if (b->kind == sk_function_parms > > - || b->kind == sk_try > > - || b->kind == sk_namespace) > > + b->kind != sk_namespace; b = b->level_chain) > > + if (b->kind == sk_try) > > + { > > + for (tree decl = b->names; decl; decl = TREE_CHAIN (decl)) > > + if (decl == retval) > > + return set_implicit_rvalue_p (move (expr)); > > return NULL_TREE; > > - } > > What if the variable is in a block nested within the try-block? I think we > still want to walk b->names for every inner binding level. Oy, that's a bug. Fixed by moving the == sk_try check below. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Here we have template auto is_throwable(T t) -> decltype(throw t, true) { ... } where we didn't properly mark 't' as IMPLICIT_RVALUE_P, which caused the wrong overload to have been chosen. Jason figured out it's because we don't correctly implement [expr.prim.id.unqual]#4.2, which post-P2266 says that an id-expression is move-eligible if "the id-expression (possibly parenthesized) is the operand of a throw-expression, and names an implicitly movable entity that belongs to a scope that does not contain the compound-statement of the innermost lambda-expression, try-block, or function-try-block (if any) whose compound-statement or ctor-initializer contains the throw-expression." I worked out that it's trying to say that given struct X { X(); X(const X&); X(X&&) = delete; }; the following should fail: the scope of the throw is an sk_try, and it's also x's scope S, and S "does not contain the compound-statement of the *try-block" so x is move-eligible, so we move, so we fail. void f () try { X x; throw x; // use of deleted function } catch (...) { } Whereas here: void g (X x) try { throw x; } catch (...) { } the throw is again in an sk_try, but x's scope is an sk_function_parms which *does* contain the {} of the *try-block, so x is not move-eligible, so we don't move, so we use X(const X&), and the code is fine. The current code also doesn't seem to handle void h (X x) { void z (decltype(throw x, true)); } where there's no enclosing lambda or sk_try so we should move. I'm not doing anything about lambdas because we shouldn't reach the code at the end of the function: the DECL_HAS_VALUE_EXPR_P check shouldn't let us go further. PR c++/113789 PR c++/113853 gcc/cp/ChangeLog: * typeck.cc (treat_lvalue_as_rvalue_p): Update code to better reflect [expr.prim.id.unqual]#4.2. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/sfinae69.C: Remove dg-bogus. * g++.dg/cpp0x/sfinae70.C: New test. * g++.dg/cpp0x/sfinae71.C: New test. * g++.dg/cpp0x/sfinae72.C: New test. * g++.dg/cpp2a/implicit-move4.C: New test. --- gcc/cp/typeck.cc | 24 +++--- gcc/testsuite/g++.dg/cpp0x/sfinae69.C | 2 +- gcc/testsuite/g++.dg/cpp0x/sfinae70.C | 16 ++++ gcc/testsuite/g++.dg/cpp0x/sfinae71.C | 17 ++++ gcc/testsuite/g++.dg/cpp0x/sfinae72.C | 17 ++++ gcc/testsuite/g++.dg/cpp2a/implicit-move4.C | 88 +++++++++++++++++++++ 6 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/sfinae70.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/sfinae71.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/sfinae72.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/implicit-move4.C base-commit: 7c6071a66f32f43cea7aa4aa32d89b338e768307 diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 132c55cfc6d..f5a0a2273be 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -10837,37 +10837,39 @@ treat_lvalue_as_rvalue_p (tree expr, bool return_p) parenthesized) id-expression that names an implicitly movable entity declared in the body or parameter-declaration-clause of the innermost enclosing function or lambda-expression, */ - if (DECL_CONTEXT (retval) != current_function_decl) - return NULL_TREE; if (return_p) { + if (DECL_CONTEXT (retval) != current_function_decl) + return NULL_TREE; expr = move (expr); if (expr == error_mark_node) return NULL_TREE; return set_implicit_rvalue_p (expr); } - /* if the operand of a throw-expression is a (possibly parenthesized) - id-expression that names an implicitly movable entity whose scope does not - extend beyond the compound-statement of the innermost try-block or - function-try-block (if any) whose compound-statement or ctor-initializer - encloses the throw-expression, */ + /* if the id-expression (possibly parenthesized) is the operand of + a throw-expression, and names an implicitly movable entity that belongs + to a scope that does not contain the compound-statement of the innermost + lambda-expression, try-block, or function-try-block (if any) whose + compound-statement or ctor-initializer contains the throw-expression. */ /* C++20 added move on throw of parms. */ if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20) return NULL_TREE; + /* We don't check for lambda-expression here, because we should not get past + the DECL_HAS_VALUE_EXPR_P check above. */ for (cp_binding_level *b = current_binding_level; - ; b = b->level_chain) + b->kind != sk_namespace; b = b->level_chain) { for (tree decl = b->names; decl; decl = TREE_CHAIN (decl)) if (decl == retval) return set_implicit_rvalue_p (move (expr)); - if (b->kind == sk_function_parms - || b->kind == sk_try - || b->kind == sk_namespace) + if (b->kind == sk_try) return NULL_TREE; } + + return set_implicit_rvalue_p (move (expr)); } /* Warn about dubious usage of std::move (in a return statement, if RETURN_P diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae69.C b/gcc/testsuite/g++.dg/cpp0x/sfinae69.C index 361e0b79ba2..60eba61165d 100644 --- a/gcc/testsuite/g++.dg/cpp0x/sfinae69.C +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae69.C @@ -15,7 +15,7 @@ constexpr bool is_throwable(...) { return false; } constexpr bool b = is_throwable(moveonly{}); #if __cplusplus >= 202002L -static_assert (b, "move from the function parameter"); // { dg-bogus "" "PR113853" { xfail c++20 } } +static_assert (b, "move from the function parameter"); #else static_assert (!b, "no move from the function parameter"); #endif diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae70.C b/gcc/testsuite/g++.dg/cpp0x/sfinae70.C new file mode 100644 index 00000000000..48ea70a61a4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae70.C @@ -0,0 +1,16 @@ +// PR c++/113789 +// { dg-do compile { target c++11 } } + +struct AutoPtr { + AutoPtr() = default; + AutoPtr(AutoPtr&) {} +}; + +template auto f(T p, int) -> decltype(throw p, 1) = delete; +template void f(T p, long); + +void +g () +{ + f (AutoPtr (), 42); // { dg-error "use of deleted function" "" { target c++20_down } } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae71.C b/gcc/testsuite/g++.dg/cpp0x/sfinae71.C new file mode 100644 index 00000000000..2fefe5f70e0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae71.C @@ -0,0 +1,17 @@ +// PR c++/113789 +// { dg-do compile { target c++11 } } +// Like sfinae70.C but T&&. + +struct AutoPtr { + AutoPtr() = default; + AutoPtr(AutoPtr&) {} +}; + +template auto f(T&& p, int) -> decltype(throw p, 1) = delete; +template void f(T p, long); + +void +g () +{ + f (AutoPtr (), 42); // { dg-error "use of deleted function" "" { target c++20_down } } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae72.C b/gcc/testsuite/g++.dg/cpp0x/sfinae72.C new file mode 100644 index 00000000000..397cdbd8c23 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae72.C @@ -0,0 +1,17 @@ +// PR c++/113789 +// { dg-do compile { target c++11 } } +// Like sfinae70.C but (). + +struct AutoPtr { + AutoPtr() = default; + AutoPtr(AutoPtr&) {} +}; + +template auto f(T p, int) -> decltype(throw (p), 1) = delete; +template void f(T p, long); + +void +g () +{ + f (AutoPtr (), 42); // { dg-error "use of deleted function" "" { target c++20_down } } +} diff --git a/gcc/testsuite/g++.dg/cpp2a/implicit-move4.C b/gcc/testsuite/g++.dg/cpp2a/implicit-move4.C new file mode 100644 index 00000000000..09ac5c93ab9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/implicit-move4.C @@ -0,0 +1,88 @@ +// PR c++/113853 +// { dg-do compile { target c++20 } } + +struct X { + X(); + X(const X&); + X(X&&) = delete; +}; + +void +f1 () +{ + try { + ; + } catch (X x) { + throw x; // { dg-error "use of deleted function" } + } +} + +void +f2 (X x) +try { + ; +} catch (...) { + throw x; // { dg-error "use of deleted function" } +} + +void +f2b (X x) +try { + ; +} catch (...) { + { + throw x; // { dg-error "use of deleted function" } + } +} + +void +f3 () +try { + X x; + throw x; // { dg-error "use of deleted function" } +} catch (...) { +} + +void +f3b () +try { + { + X x; + throw x; // { dg-error "use of deleted function" } + } +} catch (...) { +} + +void +f4 (X x) +try { + throw x; +} catch (...) { +} + +void +f4b (X x) +try { + { + throw x; + } +} catch (...) { +} + +void +f5 (X x) +{ + void g (decltype(throw x, true)); // { dg-error "use of deleted function|expected" } +} + +// The "expected" shouldn't be here, c++/113924. +void +f6 (X x, int = decltype(throw x, true){}) // { dg-error "use of deleted function|expected" } +{ +} + +void +f7 (X x) +{ + [&] { throw x; }; +}