From c7e936dbc7f59ad09c28ef57e5399a4256061747 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Mon, 11 Mar 2019 23:19:22 -0400 Subject: [PATCH] PR c++/86521 - wrong overload resolution with ref-qualifiers. Here we were wrongly treating binding a const lvalue ref to an xvalue as direct binding, which is wrong under [dcl.init.ref] and [over.match.ref]. * call.c (build_user_type_conversion_1): Don't use a conversion to a reference of the wrong rvalueness for direct binding. From-SVN: r269602 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/call.c | 8 +++++++ gcc/testsuite/g++.dg/cpp0x/overload-conv-3.C | 21 +++++++++++++++++++ .../20_util/is_constructible/value-2.cc | 6 ++++-- 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload-conv-3.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 8cad038fd1c..33402399886 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2019-03-11 Jason Merrill + + PR c++/86521 - wrong overload resolution with ref-qualifiers. + * call.c (build_user_type_conversion_1): Don't use a conversion to a + reference of the wrong rvalueness for direct binding. + 2019-03-11 Martin Liska * cvt.c (build_expr_type_conversion): Wrap apostrophes diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 98aa5ee89f7..bf48ae2c27a 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -4039,6 +4039,14 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags, rettype, totype, EXPR_LOCATION (expr)); } + else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p + && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE) + { + /* If we are called to convert to a reference type, we are trying + to find a direct binding per [over.match.ref], so rvaluedness + must match for non-functions. */ + cand->viable = 0; + } else if (DECL_NONCONVERTING_P (cand->fn) && ics->rank > cr_exact) { diff --git a/gcc/testsuite/g++.dg/cpp0x/overload-conv-3.C b/gcc/testsuite/g++.dg/cpp0x/overload-conv-3.C new file mode 100644 index 00000000000..42a135dbf44 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/overload-conv-3.C @@ -0,0 +1,21 @@ +// PR c++/86521 +// { dg-do compile { target c++11 } } + +template T&& move (T&); + +struct Dest { + Dest() = default; + Dest( Dest && ) = default; + Dest( Dest const & ) = delete; +}; + +struct Source { + Dest val; + operator Dest () && { return move( val ); } + operator Dest const & () const & { return val; } +}; + +int main() { + Source x; + Dest d(move(x)); // { dg-error "ambiguous" } +} diff --git a/libstdc++-v3/testsuite/20_util/is_constructible/value-2.cc b/libstdc++-v3/testsuite/20_util/is_constructible/value-2.cc index 0fdbab8e2d4..57487df500b 100644 --- a/libstdc++-v3/testsuite/20_util/is_constructible/value-2.cc +++ b/libstdc++-v3/testsuite/20_util/is_constructible/value-2.cc @@ -806,10 +806,12 @@ static_assert(!std::is_constructible>::value, "Error"); // Binding through reference-compatible type is required to perform // direct-initialization as described in [over.match.ref] p. 1 b. 1: static_assert(std::is_constructible>::value, "Error"); -static_assert(std::is_constructible>::value, - "Error"); static_assert(std::is_constructible>::value, "Error"); +// But an xvalue doesn't count for direct binding. +static_assert(!std::is_constructible>::value, + "Error"); + // Binding through temporary behaves like copy-initialization, // see [dcl.init.ref] p. 5, very last sub-bullet: static_assert(!std::is_constructible>::value, -- 2.30.2