Skip to content

Commit 254ff3d

Browse files
committed
c++: implicit move with throw [PR113853]
Here we have template<class T> 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.
1 parent 5286b07 commit 254ff3d

File tree

6 files changed

+152
-12
lines changed

6 files changed

+152
-12
lines changed

gcc/cp/typeck.cc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10837,37 +10837,39 @@ treat_lvalue_as_rvalue_p (tree expr, bool return_p)
1083710837
parenthesized) id-expression that names an implicitly movable entity
1083810838
declared in the body or parameter-declaration-clause of the innermost
1083910839
enclosing function or lambda-expression, */
10840-
if (DECL_CONTEXT (retval) != current_function_decl)
10841-
return NULL_TREE;
1084210840
if (return_p)
1084310841
{
10842+
if (DECL_CONTEXT (retval) != current_function_decl)
10843+
return NULL_TREE;
1084410844
expr = move (expr);
1084510845
if (expr == error_mark_node)
1084610846
return NULL_TREE;
1084710847
return set_implicit_rvalue_p (expr);
1084810848
}
1084910849

10850-
/* if the operand of a throw-expression is a (possibly parenthesized)
10851-
id-expression that names an implicitly movable entity whose scope does not
10852-
extend beyond the compound-statement of the innermost try-block or
10853-
function-try-block (if any) whose compound-statement or ctor-initializer
10854-
encloses the throw-expression, */
10850+
/* if the id-expression (possibly parenthesized) is the operand of
10851+
a throw-expression, and names an implicitly movable entity that belongs
10852+
to a scope that does not contain the compound-statement of the innermost
10853+
lambda-expression, try-block, or function-try-block (if any) whose
10854+
compound-statement or ctor-initializer contains the throw-expression. */
1085510855

1085610856
/* C++20 added move on throw of parms. */
1085710857
if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
1085810858
return NULL_TREE;
1085910859

10860+
/* We don't check for lambda-expression here, because we should not get past
10861+
the DECL_HAS_VALUE_EXPR_P check above. */
1086010862
for (cp_binding_level *b = current_binding_level;
10861-
; b = b->level_chain)
10863+
b->kind != sk_namespace; b = b->level_chain)
1086210864
{
1086310865
for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
1086410866
if (decl == retval)
1086510867
return set_implicit_rvalue_p (move (expr));
10866-
if (b->kind == sk_function_parms
10867-
|| b->kind == sk_try
10868-
|| b->kind == sk_namespace)
10868+
if (b->kind == sk_try)
1086910869
return NULL_TREE;
1087010870
}
10871+
10872+
return set_implicit_rvalue_p (move (expr));
1087110873
}
1087210874

1087310875
/* Warn about dubious usage of std::move (in a return statement, if RETURN_P

gcc/testsuite/g++.dg/cpp0x/sfinae69.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ constexpr bool is_throwable(...) { return false; }
1515

1616
constexpr bool b = is_throwable<moveonly>(moveonly{});
1717
#if __cplusplus >= 202002L
18-
static_assert (b, "move from the function parameter"); // { dg-bogus "" "PR113853" { xfail c++20 } }
18+
static_assert (b, "move from the function parameter");
1919
#else
2020
static_assert (!b, "no move from the function parameter");
2121
#endif

gcc/testsuite/g++.dg/cpp0x/sfinae70.C

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// PR c++/113789
2+
// { dg-do compile { target c++11 } }
3+
4+
struct AutoPtr {
5+
AutoPtr() = default;
6+
AutoPtr(AutoPtr&) {}
7+
};
8+
9+
template<class T> auto f(T p, int) -> decltype(throw p, 1) = delete;
10+
template<class T> void f(T p, long);
11+
12+
void
13+
g ()
14+
{
15+
f (AutoPtr (), 42); // { dg-error "use of deleted function" "" { target c++20_down } }
16+
}

gcc/testsuite/g++.dg/cpp0x/sfinae71.C

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// PR c++/113789
2+
// { dg-do compile { target c++11 } }
3+
// Like sfinae70.C but T&&.
4+
5+
struct AutoPtr {
6+
AutoPtr() = default;
7+
AutoPtr(AutoPtr&) {}
8+
};
9+
10+
template<class T> auto f(T&& p, int) -> decltype(throw p, 1) = delete;
11+
template<class T> void f(T p, long);
12+
13+
void
14+
g ()
15+
{
16+
f (AutoPtr (), 42); // { dg-error "use of deleted function" "" { target c++20_down } }
17+
}

gcc/testsuite/g++.dg/cpp0x/sfinae72.C

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// PR c++/113789
2+
// { dg-do compile { target c++11 } }
3+
// Like sfinae70.C but ().
4+
5+
struct AutoPtr {
6+
AutoPtr() = default;
7+
AutoPtr(AutoPtr&) {}
8+
};
9+
10+
template<class T> auto f(T p, int) -> decltype(throw (p), 1) = delete;
11+
template<class T> void f(T p, long);
12+
13+
void
14+
g ()
15+
{
16+
f (AutoPtr (), 42); // { dg-error "use of deleted function" "" { target c++20_down } }
17+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// PR c++/113853
2+
// { dg-do compile { target c++20 } }
3+
4+
struct X {
5+
X();
6+
X(const X&);
7+
X(X&&) = delete;
8+
};
9+
10+
void
11+
f1 ()
12+
{
13+
try {
14+
;
15+
} catch (X x) {
16+
throw x; // { dg-error "use of deleted function" }
17+
}
18+
}
19+
20+
void
21+
f2 (X x)
22+
try {
23+
;
24+
} catch (...) {
25+
throw x; // { dg-error "use of deleted function" }
26+
}
27+
28+
void
29+
f2b (X x)
30+
try {
31+
;
32+
} catch (...) {
33+
{
34+
throw x; // { dg-error "use of deleted function" }
35+
}
36+
}
37+
38+
void
39+
f3 ()
40+
try {
41+
X x;
42+
throw x; // { dg-error "use of deleted function" }
43+
} catch (...) {
44+
}
45+
46+
void
47+
f3b ()
48+
try {
49+
{
50+
X x;
51+
throw x; // { dg-error "use of deleted function" }
52+
}
53+
} catch (...) {
54+
}
55+
56+
void
57+
f4 (X x)
58+
try {
59+
throw x;
60+
} catch (...) {
61+
}
62+
63+
void
64+
f4b (X x)
65+
try {
66+
{
67+
throw x;
68+
}
69+
} catch (...) {
70+
}
71+
72+
void
73+
f5 (X x)
74+
{
75+
void g (decltype(throw x, true)); // { dg-error "use of deleted function|expected" }
76+
}
77+
78+
// The "expected" shouldn't be here, c++/113924.
79+
void
80+
f6 (X x, int = decltype(throw x, true){}) // { dg-error "use of deleted function|expected" }
81+
{
82+
}
83+
84+
void
85+
f7 (X x)
86+
{
87+
[&] { throw x; };
88+
}

0 commit comments

Comments
 (0)