From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id DFFE3385800C; Fri, 16 Feb 2024 17:09:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DFFE3385800C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1708103368; bh=98hXmGxF4i16m50ezBXOdCfG2W/Y4J41nXXa36toqwo=; h=From:To:Subject:Date:From; b=W6bpuXPK8/6vbddHHaiKgajX7AuMWapH+RjJiHWQ5mXCxbtSSqX19o78zezx940IY pspjqU8e8k8fbi3K3WR1fQ0+SfqwPPjRhqwyEG1c9yDKW4QNuAaBlvEIS1r06d8as6 SoP4KajC/W8Zt9nKlQlNnQzsj7yHZ1IZF+ZCgxUE= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-9034] c++: implicit move with throw [PR113853] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 5286b0761b5dfac4348d1c5bfdcc162a66f338ee X-Git-Newrev: 254ff3d0e34835b4de93d5e5763a7366dc7d989d Message-Id: <20240216170928.DFFE3385800C@sourceware.org> Date: Fri, 16 Feb 2024 17:09:28 +0000 (GMT) List-Id: https://gcc.gnu.org/g:254ff3d0e34835b4de93d5e5763a7366dc7d989d commit r14-9034-g254ff3d0e34835b4de93d5e5763a7366dc7d989d Author: Marek Polacek Date: Wed Feb 14 17:53:52 2024 -0500 c++: implicit move with throw [PR113853] 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. Diff: --- 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(-) diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 132c55cfc6d5..f5a0a2273be2 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 361e0b79ba24..60eba61165de 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 000000000000..48ea70a61a42 --- /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 000000000000..2fefe5f70e00 --- /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 000000000000..397cdbd8c234 --- /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 000000000000..09ac5c93ab92 --- /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; }; +}