From patchwork Thu Feb 15 15:19:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1899416 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Zh8i6J/j; dkim-atps=neutral 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=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TbJfb0C5Bz23gM for ; Fri, 16 Feb 2024 02:20:37 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A2C7F3865490 for ; Thu, 15 Feb 2024 15:20:35 +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 642B538618BE for ; Thu, 15 Feb 2024 15:19:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 642B538618BE 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 642B538618BE 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=1708010409; cv=none; b=XCejrnnc406ZWKuuJB0RQjo/oj7ynmOB/Q+B1rdhj2BGvG80Ly+T0Oq4RIPQ2ZeTJPZ/gzEVsUbOpAD6D0PwM43KjCLgd6j40w5w9n/uYUStYx+uUJYJNZM7erC/CvM0WDci5/1MnzVQIckL3Po1IO0bX6ZoVqJUfpvMdClUHrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708010409; c=relaxed/simple; bh=+VZcVgAdnq0cgPA1ztRuMj9zU79PvCnTulIoSlxWJcw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=x/xDShJzgRXVl4NYH7PtM9dleZGesjxZej3ie3mnNldUvR26ar6zOag6Sd7e/hBleKpmj5P6f7ULoHHvF0Th6FSdMh0vcYodI1aCzJpDK4afHetvHmdAOZKYbnAtZT8YCAcc+hQJkXHryQBuC4zBMe5eZ2+3prfXFcHoSk5zVTA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708010399; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mVIClqxWV7yWkZ0TSpiIGFzV2xJo2a8lsw110gBvlNo=; b=Zh8i6J/jo91JDHLTEUaLGuThLOvJBiegk+w2/oQIeXg8XLT441uT17drKXaFt6pTJ37yCa ukJARbtqL27XPqqGPIbG7oBZNr8diyJ7hTJcgNoK5TLFeGj8J5JZnDAMnuWNQrQxymXFAr narauXko7HGriGABaqW1zpQZn1Y1BZc= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-94-t8K3oRMWPxaatGWeo0aNOQ-1; Thu, 15 Feb 2024 10:19:57 -0500 X-MC-Unique: t8K3oRMWPxaatGWeo0aNOQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 40D5E38116E7 for ; Thu, 15 Feb 2024 15:19:57 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.33.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1ED4A1C060B1; Thu, 15 Feb 2024 15:19:57 +0000 (UTC) From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH] c++: implicit move with throw [PR113853] Date: Thu, 15 Feb 2024 10:19:47 -0500 Message-ID: <20240215151947.38390-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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 base-commit: 5459a9074afabf700f055fc8164f88dadb1c39b0 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. */ /* 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; - } + } + + 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..af4f62e5c7f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/implicit-move4.C @@ -0,0 +1,59 @@ +// 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 +f3 () +try { + X x; + throw x; // { dg-error "use of deleted function" } +} catch (...) { +} + +void +f4 (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; }; +}